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.mapper.TRectifyGroupMemberInfoMapper;
import com.zehong.system.service.ITRectifyGroupMemberInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 整改组内成员信息Service业务层处理
 * 
 * @author zehong
 * @date 2026-07-16
 */
@Service
public class TRectifyGroupMemberInfoServiceImpl implements ITRectifyGroupMemberInfoService 
{
    @Resource
    private TRectifyGroupMemberInfoMapper tRectifyGroupMemberInfoMapper;

    /**
     * 查询整改组内成员信息
     * 
     * @param memberId 整改组内成员信息ID
     * @return 整改组内成员信息
     */
    @Override
    public TRectifyGroupMemberInfo selectTRectifyGroupMemberInfoById(Long memberId)
    {
        return tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoById(memberId);
    }

    /**
     * 查询整改组内成员信息列表
     * 
     * @param tRectifyGroupMemberInfo 整改组内成员信息
     * @return 整改组内成员信息
     */
    @Override
    public List<TRectifyGroupMemberInfo> selectTRectifyGroupMemberInfoList(TRectifyGroupMemberInfo tRectifyGroupMemberInfo)
    {
        return tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(tRectifyGroupMemberInfo);
    }

    /**
     * 新增整改组内成员信息
     * 
     * @param tRectifyGroupMemberInfo 整改组内成员信息
     * @return 结果
     */
    @Override
    public int insertTRectifyGroupMemberInfo(TRectifyGroupMemberInfo tRectifyGroupMemberInfo)
    {
        tRectifyGroupMemberInfo.setCreateTime(DateUtils.getNowDate());
        return tRectifyGroupMemberInfoMapper.insertTRectifyGroupMemberInfo(tRectifyGroupMemberInfo);
    }

    /**
     * 修改整改组内成员信息
     * 
     * @param tRectifyGroupMemberInfo 整改组内成员信息
     * @return 结果
     */
    @Override
    public int updateTRectifyGroupMemberInfo(TRectifyGroupMemberInfo tRectifyGroupMemberInfo)
    {
        tRectifyGroupMemberInfo.setUpdateTime(DateUtils.getNowDate());
        return tRectifyGroupMemberInfoMapper.updateTRectifyGroupMemberInfo(tRectifyGroupMemberInfo);
    }

    /**
     * 批量删除整改组内成员信息
     * 
     * @param memberIds 需要删除的整改组内成员信息ID
     * @return 结果
     */
    @Override
    public int deleteTRectifyGroupMemberInfoByIds(Long[] memberIds)
    {
        return tRectifyGroupMemberInfoMapper.deleteTRectifyGroupMemberInfoByIds(memberIds);
    }

    /**
     * 删除整改组内成员信息信息
     * 
     * @param memberId 整改组内成员信息ID
     * @return 结果
     */
    @Override
    public int deleteTRectifyGroupMemberInfoById(Long memberId)
    {
        return tRectifyGroupMemberInfoMapper.deleteTRectifyGroupMemberInfoById(memberId);
    }


    /**
     * 获取整改人员人员信息
     * @return
     */
    @Override
    public List<Map<String,Object>> selectRectifyUserByPostCode(Map<String,Object> map){
        return tRectifyGroupMemberInfoMapper.selectRectifyUserByPostCode(map);
    }


    /**
     * 根据登录人职位获取可选成员
     * @return
     */
    @Override
    public List<TRectifyGroupMemberInfo> selectMemberByUserPost(String dealMethod){
        //登录人信息
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        //获取登录人职务
        TRectifyGroupMemberInfo tRectifyGroupMemberInfo = new TRectifyGroupMemberInfo();
        tRectifyGroupMemberInfo.setUserId(userId);
        List<TRectifyGroupMemberInfo> memberInfoList = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(tRectifyGroupMemberInfo);
        if(CollectionUtils.isEmpty(memberInfoList)) throw new CustomException("未获取到登录人分组信息");
        List<TRectifyGroupMemberInfo> result = new ArrayList<>();
        for(TRectifyGroupMemberInfo memberInfo : memberInfoList){
            /**
             * 1.不是维修班的组员只能看到自己的组长
             * 2.不是维修班的组长只能看到维修班的班长
             * 3.维修班的班长只能看到自己的组员和其他维修班的班长
             * 4.维修班的组员只能看到自己的组长
             */

            if("1".equals(memberInfo.getPost())){
                //班长登录
                if("2".equals(memberInfo.getType())){
                    //维修班班长登录
                    //维修班组员信息
                    TRectifyGroupMemberInfo team = new TRectifyGroupMemberInfo();
                    team.setGroupId(memberInfo.getGroupId());
                    team.setPost("2");
                    List<TRectifyGroupMemberInfo> teamInfo = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(team);
                    result.addAll(teamInfo);
                    //维修班组长信息
                    TRectifyGroupMemberInfo leader = new TRectifyGroupMemberInfo();
                    leader.setPost("1");
                    leader.setType("2");
                    List<TRectifyGroupMemberInfo> leaderInfo = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(leader);
                    /*if(!CollectionUtils.isEmpty(leaderInfo)){
                        //其他组的组长
                        List<TRectifyGroupMemberInfo> otherLeaderInfo = leaderInfo.stream().filter(item -> !memberInfo.getGroupId().equals(item.getGroupId())).collect(Collectors.toList());
                        result.addAll(otherLeaderInfo);
                    }*/
                    result.addAll(leaderInfo);
                }else{
                    //其他班班长
                    if("1".equals(dealMethod)){
                        //自行整改只能看到自己的组员
                        TRectifyGroupMemberInfo InspectTeam = new TRectifyGroupMemberInfo();
                        InspectTeam.setGroupId(memberInfo.getGroupId());
                        InspectTeam.setPost("2");
                        List<TRectifyGroupMemberInfo> teamInfo = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(InspectTeam);
                        result.addAll(teamInfo);
                    }else{
                        //非自行整改只能看到维修班组长信息
                        TRectifyGroupMemberInfo leader = new TRectifyGroupMemberInfo();
                        leader.setPost("1");
                        leader.setType("2");
                        List<TRectifyGroupMemberInfo> leaderInfo = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(leader);
                        result.addAll(leaderInfo);
                    }
                }

            }else{
                //组员登录
                //本组组长信息
                TRectifyGroupMemberInfo team = new TRectifyGroupMemberInfo();
                team.setGroupId(memberInfo.getGroupId());
                team.setPost("1");
                List<TRectifyGroupMemberInfo> leaderInfo = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(team);
                result.addAll(leaderInfo);
            }
        }
        return result;
    }
}
