package com.zehong.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.DateUtils;
import com.zehong.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.mapper.TRectifyGroupMemberInfoMapper;
import com.zehong.system.domain.TRectifyGroupMemberInfo;
import com.zehong.system.service.ITRectifyGroupMemberInfoService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 整改组内成员信息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(){
        //登录人信息
        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.组成员只能看到自己班的班长
             */

            if("1".equals(memberInfo.getPost())){
                //组员信息
                TRectifyGroupMemberInfo team = new TRectifyGroupMemberInfo();
                team.setGroupId(memberInfo.getGroupId());
                team.setPost("2");
                List<TRectifyGroupMemberInfo> teamInfo = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(team);
                result.addAll(teamInfo);
                //判断是否存在组长信息
                Optional<TRectifyGroupMemberInfo> isHasLeaderInfo = result.stream().filter(item -> "1".equals(item.getPost())).findAny();
                if(isHasLeaderInfo.isPresent()){
                    //组长信息
                    TRectifyGroupMemberInfo leader = new TRectifyGroupMemberInfo();
                    leader.setPost("1");
                    List<TRectifyGroupMemberInfo> leaderInfo = tRectifyGroupMemberInfoMapper.selectTRectifyGroupMemberInfoList(team);
                    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;
    }
}
