package com.zehong.system.service.impl;

import com.zehong.common.utils.DateUtils;
import com.zehong.system.domain.TUserRelation;
import com.zehong.system.mapper.TUserRelationMapper;
import com.zehong.system.service.ITUserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户关联Service业务层处理
 * 
 * @author zehong
 * @date 2023-07-11
 */
@Service
public class TUserRelationServiceImpl implements ITUserRelationService 
{
    @Autowired
    private TUserRelationMapper tUserRelationMapper;

    /**
     * 查询用户关联
     * 
     * @param relationId 用户关联ID
     * @return 用户关联
     */
    @Override
    public TUserRelation selectTUserRelationById(Long relationId)
    {
        return tUserRelationMapper.selectTUserRelationById(relationId);
    }

    /**
     * 查询用户关联列表
     * 
     * @param tUserRelation 用户关联
     * @return 用户关联
     */
    @Override
    public List<TUserRelation> selectTUserRelationList(TUserRelation tUserRelation)
    {
        return tUserRelationMapper.selectTUserRelationList(tUserRelation);
    }

    /**
     * 新增用户关联
     * 
     * @param tUserRelation 用户关联
     * @return 结果
     */
    @Override
    public int insertTUserRelation(TUserRelation tUserRelation)
    {
        tUserRelation.setCreateTime(DateUtils.getNowDate());
        return tUserRelationMapper.insertTUserRelation(tUserRelation);
    }

    /**
     * 修改用户关联
     * 
     * @param tUserRelation 用户关联
     * @return 结果
     */
    @Override
    public int updateTUserRelation(TUserRelation tUserRelation)
    {
        tUserRelation.setUpdateTime(DateUtils.getNowDate());
        return tUserRelationMapper.updateTUserRelation(tUserRelation);
    }

    /**
     * 批量删除用户关联
     * 
     * @param relationIds 需要删除的用户关联ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTUserRelationByIds(Long[] relationIds)
    {
        for(Long relationId : relationIds){
            TUserRelation relation = tUserRelationMapper.selectTUserRelationById(relationId);
            Map<String,Long> map = new HashMap<>();
            map.put("userId",relation.getRelationUserId());
            map.put("relationUserId",relation.getUserId());
            tUserRelationMapper.deleteUserRelationByUserIdAndRelationUserId(map);
        }
        return tUserRelationMapper.deleteTUserRelationByIds(relationIds);
    }

    /**
     * 删除用户关联信息
     * 
     * @param relationId 用户关联ID
     * @return 结果
     */
    @Override
    public int deleteTUserRelationById(Long relationId)
    {
        return tUserRelationMapper.deleteTUserRelationById(relationId);
    }
}
