package com.zehong.system.service.impl;

import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.DateUtils;
import com.zehong.common.utils.SecurityUtils;
import com.zehong.system.domain.TRectifyGroupMemberInfo;
import com.zehong.system.domain.TRectifyPersonGroupInfo;
import com.zehong.system.mapper.TRectifyGroupMemberInfoMapper;
import com.zehong.system.mapper.TRectifyPersonGroupInfoMapper;
import com.zehong.system.service.ITRectifyPersonGroupInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 整改人员分组信息Service业务层处理
 * 
 * @author zehong
 * @date 2026-07-16
 */
@Service
public class TRectifyPersonGroupInfoServiceImpl implements ITRectifyPersonGroupInfoService 
{
    @Resource
    private TRectifyPersonGroupInfoMapper tRectifyPersonGroupInfoMapper;
    @Resource
    private TRectifyGroupMemberInfoMapper tRectifyGroupMemberInfoMapper;

    /**
     * 查询整改人员分组信息
     * 
     * @param groupId 整改人员分组信息ID
     * @return 整改人员分组信息
     */
    @Override
    public TRectifyPersonGroupInfo selectTRectifyPersonGroupInfoById(Long groupId)
    {
        TRectifyPersonGroupInfo personGroupInfo = tRectifyPersonGroupInfoMapper.selectTRectifyPersonGroupInfoById(groupId);
        TRectifyGroupMemberInfo tRectifyGroupMemberInfo = new TRectifyGroupMemberInfo();
        tRectifyGroupMemberInfo.setGroupId(groupId);
        List<TRectifyGroupMemberInfo> memberInfoList = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(tRectifyGroupMemberInfo);
        personGroupInfo.setMemberInfoList(memberInfoList);
        return personGroupInfo;
    }

    /**
     * 查询整改人员分组信息列表
     * 
     * @param tRectifyPersonGroupInfo 整改人员分组信息
     * @return 整改人员分组信息
     */
    @Override
    public List<TRectifyPersonGroupInfo> selectTRectifyPersonGroupInfoList(TRectifyPersonGroupInfo tRectifyPersonGroupInfo) {
        return tRectifyPersonGroupInfoMapper.selectTRectifyPersonGroupInfoList(tRectifyPersonGroupInfo);
    }

    /**
     * 新增整改人员分组信息
     * 
     * @param tRectifyPersonGroupInfo 整改人员分组信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTRectifyPersonGroupInfo(TRectifyPersonGroupInfo tRectifyPersonGroupInfo) {
        tRectifyPersonGroupInfo.setCreateTime(DateUtils.getNowDate());
        tRectifyPersonGroupInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        int result = tRectifyPersonGroupInfoMapper.insertTRectifyPersonGroupInfo(tRectifyPersonGroupInfo);
        addMemberInfo(tRectifyPersonGroupInfo);
        return result;
    }

    /**
     * 新增组成员
     * @param tRectifyPersonGroupInfo 组信息
     */
    private void addMemberInfo(TRectifyPersonGroupInfo tRectifyPersonGroupInfo){
        for(TRectifyGroupMemberInfo memberInfo : tRectifyPersonGroupInfo.getMemberInfoList()){
            //校验人员是否已在其他组
            TRectifyGroupMemberInfo tRectifyGroupMemberInfo = new TRectifyGroupMemberInfo();
            tRectifyGroupMemberInfo.setUserId(memberInfo.getUserId());
            List<TRectifyGroupMemberInfo> memberInfoList = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(tRectifyGroupMemberInfo);
            if(!CollectionUtils.isEmpty(memberInfoList)) throw new CustomException(memberInfo.getNickName() + "已加入其他组");
            memberInfo.setGroupId(tRectifyPersonGroupInfo.getGroupId());
            memberInfo.setCreateTime(new Date());
            memberInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
            tRectifyGroupMemberInfoMapper.insertTRectifyGroupMemberInfo(memberInfo);
        }
    }

    /**
     * 修改整改人员分组信息
     * 
     * @param tRectifyPersonGroupInfo 整改人员分组信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTRectifyPersonGroupInfo(TRectifyPersonGroupInfo tRectifyPersonGroupInfo){
        tRectifyPersonGroupInfo.setUpdateTime(DateUtils.getNowDate());
        tRectifyPersonGroupInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        tRectifyGroupMemberInfoMapper.deleteTRectifyGroupMemberInfoByGroupId(tRectifyPersonGroupInfo.getGroupId());
        addMemberInfo(tRectifyPersonGroupInfo);
        return tRectifyPersonGroupInfoMapper.updateTRectifyPersonGroupInfo(tRectifyPersonGroupInfo);
    }

    /**
     * 批量删除整改人员分组信息
     * 
     * @param groupIds 需要删除的整改人员分组信息ID
     * @return 结果
     */
    @Override
    public int deleteTRectifyPersonGroupInfoByIds(Long[] groupIds)
    {
        return tRectifyPersonGroupInfoMapper.deleteTRectifyPersonGroupInfoByIds(groupIds);
    }

    /**
     * 删除整改人员分组信息信息
     * 
     * @param groupId 整改人员分组信息ID
     * @return 结果
     */
    @Override
    public int deleteTRectifyPersonGroupInfoById(Long groupId)
    {
        return tRectifyPersonGroupInfoMapper.deleteTRectifyPersonGroupInfoById(groupId);
    }
}
