package com.ruoyi.postCheck.service.impl;

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

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ruoyi.common.constant.PostTeamEnum;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.postCheck.domain.PostTeamDept;
import com.ruoyi.postCheck.domain.PostTeamUser;
import com.ruoyi.postCheck.service.IPostTeamDeptService;
import com.ruoyi.postCheck.service.IPostTeamUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.postCheck.mapper.PostTeamMapper;
import com.ruoyi.postCheck.domain.PostTeam;
import com.ruoyi.postCheck.service.IPostTeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

/**
 * 岗检小组Service业务层处理
 *
 * @author csg
 * @date 2023-12-19
 */
@Service
public class PostTeamServiceImpl extends ServiceImpl<PostTeamMapper, PostTeam> implements IPostTeamService {
    @Autowired
    private PostTeamMapper postTeamMapper;

    @Autowired
    private IPostTeamDeptService postTeamDeptService;
    @Autowired
    private IPostTeamUserService postTeamUserService;

    /**
     * 查询岗检小组
     *
     * @param id 岗检小组主键
     * @return 岗检小组
     */
    @Override
    public PostTeam selectPostTeamById(Long id) {
        PostTeam postTeam = postTeamMapper.selectPostTeamById(id);
        if (postTeam == null)
            return null;

        Integer type = postTeam.getType();
        Long postTeamId = postTeam.getId();
        if (type != null && postTeamId == null)
            return postTeam;

        //查询部门
        if (type == PostTeamEnum.TYPE_B.getValue()
                || type == PostTeamEnum.TYPE_G.getValue()) {
            PostTeamDept param = new PostTeamDept();
            param.setPostTeamId(postTeamId);
            List<PostTeamDept> postTeamDeptList = postTeamDeptService.selectPostTeamDeptList(param);
            if (!CollectionUtils.isEmpty(postTeamDeptList))
                postTeam.setChildrenDept(postTeamDeptList);
        }

        //查询人员
        if (type == PostTeamEnum.TYPE_L.getValue()
                || type == PostTeamEnum.TYPE_B.getValue()
                || type == PostTeamEnum.TYPE_X.getValue()) {
            PostTeamUser param = new PostTeamUser();
            param.setPostTeamId(postTeamId);
            List<PostTeamUser> postTeamUserList = postTeamUserService.selectPostTeamUserList(param);
            if (!CollectionUtils.isEmpty(postTeamUserList))
                postTeam.setChildrenUser(postTeamUserList);

        }
        return postTeam;
    }

    /**
     * 查询岗检小组列表
     *
     * @param postTeam 岗检小组
     * @return 岗检小组
     */
    @Override
    public List<PostTeam> selectPostTeamList(PostTeam postTeam) {
        List<PostTeam> postTeams = postTeamMapper.selectPostTeamList(postTeam);

        if (CollectionUtils.isEmpty(postTeams))
            return postTeams;

        for (PostTeam team : postTeams) {
            Integer type = team.getType();
            Long postTeamId = team.getId();
            if (type != null && postTeamId == null)
                continue;

            //查询部门
            if (type == PostTeamEnum.TYPE_B.getValue()
                    || type == PostTeamEnum.TYPE_G.getValue()) {
                PostTeamDept param = new PostTeamDept();
                param.setPostTeamId(postTeamId);
                List<PostTeamDept> postTeamDeptList = postTeamDeptService.selectPostTeamDeptList(param);
                if (!CollectionUtils.isEmpty(postTeamDeptList))
                    team.setChildrenDept(postTeamDeptList);
            }

            //查询人员
            if (type == PostTeamEnum.TYPE_L.getValue()
                    || type == PostTeamEnum.TYPE_B.getValue()
                    || type == PostTeamEnum.TYPE_X.getValue()) {
                PostTeamUser param = new PostTeamUser();
                param.setPostTeamId(postTeamId);
                List<PostTeamUser> postTeamUserList = postTeamUserService.selectPostTeamUserList(param);
                if (!CollectionUtils.isEmpty(postTeamUserList))
                    team.setChildrenUser(postTeamUserList);

            }
        }

        return postTeams;
    }

    /**
     * 新增岗检小组
     *
     * @param postTeam 岗检小组
     * @return 结果
     */
    @Override
    public int insertPostTeam(PostTeam postTeam) {

        Integer type = postTeam.getType();
        Long parentId = postTeam.getParentId();
        String name = postTeam.getName();


        if (type == null) {
            throw new BaseException("未选择类型，请检查！");
        }

        if (parentId == null) {
            throw new BaseException("未选择上级机构，请检查！");
        }
        if (StringUtils.isBlank(name)) {
            throw new BaseException("名称为空，请检查！");
        }

        int countL = 0;
        int countB = 0;
        int countG = 0;
        PostTeam postTeamParam = new PostTeam();
        Map<String, Object> param = new HashMap<>();
        param.put("beginTime", DateUtils.dateTimeNow(DateUtils.YYYY) + "-01-01");
        param.put("endTime", DateUtils.dateTimeNow(DateUtils.YYYY) + "-12-31");
        postTeamParam.setParams(param);
        List<PostTeam> postTeamList = postTeamMapper.selectPostTeamList(postTeamParam);

        for (PostTeam team : postTeamList) {
            if (team.getType() == PostTeamEnum.TYPE_L.getValue()) {
                countL++;
            }
            if (team.getType() == PostTeamEnum.TYPE_B.getValue()) {
                countB++;
            }
            if (team.getType() == PostTeamEnum.TYPE_G.getValue()) {
                countG++;
            }
        }

        //1、领导小组：只能存在1个
        //2、联合办公室：只能存在1个
        //3、岗检工作组：只能存在2个
        if (type == PostTeamEnum.TYPE_L.getValue() && countL >= 1) {
            throw new BaseException("领导小组已存在1个，不可重复添加，请检查！");
        }
        if (type == PostTeamEnum.TYPE_B.getValue() && countB >= 1) {
            throw new BaseException("联合办公室已存在1个，不可重复添加，请检查！");
        }
        if (type == PostTeamEnum.TYPE_G.getValue() && countG >= 2) {
            throw new BaseException("岗检工作组已存在2个，不可重复添加，请检查！");
        }

        //添加数据
        postTeam.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
        postTeam.setCreateDefault();
        postTeam.setStatus(PostTeamEnum.VALID.getValue());
        postTeam.setCreateUserId(SecurityUtils.getUserId());
        return postTeamMapper.insertPostTeam(postTeam);
    }

    /**
     * 修改岗检小组
     *
     * @param postTeam 岗检小组
     * @return 结果
     */
    @Override
    public int updatePostTeam(PostTeam postTeam) {
        postTeam.setUpdateDefault();
        return postTeamMapper.updatePostTeam(postTeam);
    }

    /**
     * 批量删除岗检小组
     *
     * @param ids 需要删除的岗检小组主键
     * @return 结果
     */
    @Override
    public int deletePostTeamByIds(Long[] ids) {
        for (Long id : ids) {
            if (id == null) {
                throw new BaseException("缺少参数请检查，请检查！");
            }
            PostTeam postTeamParam = new PostTeam();
            postTeamParam.setParentId(id);
            List<PostTeam> postTeamList = postTeamMapper.selectPostTeamList(postTeamParam);
            if (!CollectionUtils.isEmpty(postTeamList)) {
                throw new BaseException("无法删除，请先删除下级组织，请检查！");
            }


            PostTeam postTeam = postTeamMapper.selectPostTeamById(id);
            Integer type = postTeam.getType();
            //查询部门
            if (type == PostTeamEnum.TYPE_B.getValue()
                    || type == PostTeamEnum.TYPE_G.getValue()) {
                PostTeamDept param = new PostTeamDept();
                param.setPostTeamId(id);
                List<PostTeamDept> postTeamDeptList = postTeamDeptService.selectPostTeamDeptList(param);
                if (!CollectionUtils.isEmpty(postTeamDeptList))
                    throw new BaseException("无法删除，请先删除绑定的部门，请检查！");
            }

            //查询人员
            if (type == PostTeamEnum.TYPE_L.getValue()
                    || type == PostTeamEnum.TYPE_B.getValue()
                    || type == PostTeamEnum.TYPE_X.getValue()) {
                PostTeamUser param = new PostTeamUser();
                param.setPostTeamId(id);
                List<PostTeamUser> postTeamUserList = postTeamUserService.selectPostTeamUserList(param);
                if (!CollectionUtils.isEmpty(postTeamUserList))
                    throw new BaseException("无法删除，请先删除绑定的成员（人员），请检查！");
            }
        }
        return postTeamMapper.deletePostTeamByIds(ids);
    }

    /**
     * 删除岗检小组信息
     *
     * @param id 岗检小组主键
     * @return 结果
     */
    @Override
    public int deletePostTeamById(Long id) {
        if (id == null) {
            throw new BaseException("缺少参数请检查，请检查！");
        }
        PostTeam postTeamParam = new PostTeam();
        postTeamParam.setParentId(id);
        List<PostTeam> postTeamList = postTeamMapper.selectPostTeamList(postTeamParam);
        if (!CollectionUtils.isEmpty(postTeamList)) {
            throw new BaseException("无法删除，请先删除下级组织，请检查！");
        }


        PostTeam postTeam = postTeamMapper.selectPostTeamById(id);
        Integer type = postTeam.getType();
        //查询部门
        if (type == PostTeamEnum.TYPE_B.getValue()
                || type == PostTeamEnum.TYPE_G.getValue()) {
            PostTeamDept param = new PostTeamDept();
            param.setPostTeamId(id);
            List<PostTeamDept> postTeamDeptList = postTeamDeptService.selectPostTeamDeptList(param);
            if (!CollectionUtils.isEmpty(postTeamDeptList))
                throw new BaseException("无法删除，请先删除绑定的部门，请检查！");
        }

        //查询人员
        if (type == PostTeamEnum.TYPE_L.getValue()
                || type == PostTeamEnum.TYPE_B.getValue()
                || type == PostTeamEnum.TYPE_X.getValue()) {
            PostTeamUser param = new PostTeamUser();
            param.setPostTeamId(id);
            List<PostTeamUser> postTeamUserList = postTeamUserService.selectPostTeamUserList(param);
            if (!CollectionUtils.isEmpty(postTeamUserList))
                throw new BaseException("无法删除，请先删除绑定的成员（人员），请检查！");
        }


        return postTeamMapper.deletePostTeamById(id);
    }
}
