package com.ruoyi.postCheck.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ruoyi.common.constant.PostTeamEnum;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.postCheck.domain.PostTeam;
import com.ruoyi.postCheck.domain.PostTeamUser;
import com.ruoyi.postCheck.service.IPostTeamService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.postCheck.mapper.PostTeamDeptMapper;
import com.ruoyi.postCheck.domain.PostTeamDept;
import com.ruoyi.postCheck.service.IPostTeamDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

/**
 * 岗检工作组关联部门Service业务层处理
 *
 * @author csg
 * @date 2023-12-19
 */
@Service
public class PostTeamDeptServiceImpl extends ServiceImpl<PostTeamDeptMapper, PostTeamDept> implements IPostTeamDeptService {
    @Autowired
    private PostTeamDeptMapper postTeamDeptMapper;
    @Autowired
    private IPostTeamService postTeamService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询岗检工作组关联部门
     *
     * @param id 岗检工作组关联部门主键
     * @return 岗检工作组关联部门
     */
    @Override
    public PostTeamDept selectPostTeamDeptById(Long id) {
        return postTeamDeptMapper.selectPostTeamDeptById(id);
    }

    /**
     * 查询岗检工作组关联部门列表
     *
     * @param postTeamDept 岗检工作组关联部门
     * @return 岗检工作组关联部门
     */
    @Override
    public List<PostTeamDept> selectPostTeamDeptList(PostTeamDept postTeamDept) {
        return postTeamDeptMapper.selectPostTeamDeptList(postTeamDept);
    }

    /**
     * 新增岗检工作组关联部门
     *
     * @param postTeamDeptList 岗检工作组关联部门数组
     * @return 结果
     */
    @Override
    public int insertPostTeamDept(List<PostTeamDept> postTeamDeptList) {

        if (CollectionUtils.isEmpty(postTeamDeptList)) {
            throw new BaseException("参数为空，请检查！");
        }

        List<Long> postTeamIdList = postTeamDeptList.stream().map(t -> t.getPostTeamId()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(postTeamIdList)) {
            throw new BaseException("postTeamId为空，请检查！");
        }

        HashSet<Long> postTeamIdSet = new HashSet<>(postTeamIdList);
        if (postTeamIdSet.size() != 1) {
            throw new BaseException("一次只允许为一个组织架构绑定部门，请检查！");
        }

        PostTeam postTeam = postTeamService.selectPostTeamById(postTeamIdList.get(0));
        if (postTeam == null) {
            throw new BaseException("组织架构不存在请检查，请检查！");
        }
        Integer type = postTeam.getType();


        if (type == PostTeamEnum.TYPE_L.getValue()) {
            throw new BaseException("岗检领导小组不可添加部门，请检查！");
        }
        if (type == PostTeamEnum.TYPE_X.getValue()) {
            throw new BaseException("工作小组不可添加部门，请检查！");
        }

//        if (type == PostTeamEnum.TYPE_B.getValue() && postTeamDeptList.size() > 2) {
//            throw new BaseException("联合办公室不能超过2人，请检查！");
//        }
//        if (type == PostTeamEnum.TYPE_G.getValue()) {
//            throw new BaseException("岗检工作组不可添加人员，请检查！");
//        }

        //删除原来的用户
        LambdaQueryWrapper<PostTeamDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PostTeamDept::getPostTeamId, postTeamIdList);
        postTeamDeptMapper.delete(wrapper);


        //新增
        int count = 0;
        int index = 0;
        Long postTeamIdLast = null;
        for (PostTeamDept postTeamDept : postTeamDeptList) {
            Integer deptType = postTeamDept.getType();
            if (deptType == null || (deptType != 1 && deptType != 2)) {
                throw new BaseException("部门类型设置错误【type=" + deptType + "】，请检查！");
            }
            //类型（1-牵头；2-成员）：2联合办公室（多部门）；3综合/生产工作组（1牵头部门/多成员部门）
//            if (index++ == 0) {
//                postTeamDept.setType(1);
//            } else {
//                postTeamDept.setType(2);
//            }
            Long postTeamId = postTeamDept.getPostTeamId();
            if (postTeamId == null) {
                throw new BaseException("postTeamId为空，请检查！");
            }

            if (postTeamIdLast != null && !postTeamIdLast.equals(postTeamId)) {
                throw new BaseException("postTeamId出现多个值，不允许多个小组混合添加成员，请检查！");
            }

            postTeamIdLast = postTeamId;

            Long deptId = postTeamDept.getDeptId();
            if (deptId == null) {
                throw new BaseException("deptId为空，请检查！");
            }

            SysDept sysDept = sysDeptService.selectDeptById(deptId);
            if (sysDept == null) {
                throw new BaseException("deptId错误，部门表无匹配该ID，请检查！");
            }

            postTeamDept.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
            postTeamDept.setDeptName(sysDept.getDeptName());
            postTeamDept.setCreateDefault();
            count += postTeamDeptMapper.insertPostTeamDept(postTeamDept);
        }

        return count;
    }

    /**
     * 修改岗检工作组关联部门
     *
     * @param postTeamDept 岗检工作组关联部门
     * @return 结果
     */
//    @Override
//    public int updatePostTeamDept(PostTeamDept postTeamDept) {
//        postTeamDept.setUpdateDefault();
//        return postTeamDeptMapper.updatePostTeamDept(postTeamDept);
//    }

    /**
     * 批量删除岗检工作组关联部门
     *
     * @param ids 需要删除的岗检工作组关联部门主键
     * @return 结果
     */
    @Override
    public int deletePostTeamDeptByIds(Long[] ids) {
        return postTeamDeptMapper.deletePostTeamDeptByIds(ids);
    }

    /**
     * 删除岗检工作组关联部门信息
     *
     * @param id 岗检工作组关联部门主键
     * @return 结果
     */
//    @Override
//    public int deletePostTeamDeptById(Long id) {
//        return postTeamDeptMapper.deletePostTeamDeptById(id);
//    }
}
