package com.yanzhu.jh.project.service.impl;

import java.util.*;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DeptTypeEnum;
import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.enums.SysRoleEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.yanzhu.jh.project.domain.SurProject;
import com.yanzhu.jh.project.domain.SurProjectUnitInfo;
import com.yanzhu.jh.project.mapper.SurProjectMapper;
import com.yanzhu.jh.project.mapper.SurProjectUnitInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.project.mapper.SurProjectUserinfoMapper;
import com.yanzhu.jh.project.domain.SurProjectUserinfo;
import com.yanzhu.jh.project.service.ISurProjectUserinfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目用户Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-07-29
 */
@Service
public class SurProjectUserinfoServiceImpl implements ISurProjectUserinfoService 
{
    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SurProjectMapper surProjectMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SurProjectUnitInfoMapper surProjectUnitInfoMapper;

    @Autowired
    private SurProjectUserinfoMapper surProjectUserinfoMapper;

    /**
     * 查询项目用户
     * 
     * @param id 项目用户主键
     * @return 项目用户
     */
    @Override
    public SurProjectUserinfo selectSurProjectUserinfoById(Long id)
    {
        return surProjectUserinfoMapper.selectSurProjectUserinfoById(id);
    }

    /**
     * 查询项目用户列表
     * 
     * @param surProjectUserinfo 项目用户
     * @return 项目用户
     */
    @Override
    public List<SurProjectUserinfo> selectSurProjectUserinfoList(SurProjectUserinfo surProjectUserinfo)
    {
        return surProjectUserinfoMapper.selectSurProjectUserinfoList(surProjectUserinfo);
    }

    /**
     * 查询项目部门人员
     *
     * @param surProjectUserinfo 项目用户
     * @return 项目用户集合
     */
    public List<SurProjectUnitInfo> selectProjectUnitUser(SurProjectUserinfo surProjectUserinfo) {
        SurProjectUnitInfo surProjectUnitInfo = new SurProjectUnitInfo();
        surProjectUnitInfo.setProjectId(surProjectUserinfo.getProjectId());
        List<SurProjectUnitInfo> projectUnitUserInfoList = surProjectUnitInfoMapper.selectSurProjectUnitInfoList(surProjectUnitInfo);
        List<SurProjectUserinfo> projectUserinfoList = surProjectUserinfoMapper.selectSurProjectUserinfoList(surProjectUserinfo);
        for(SurProjectUnitInfo projectUnitInfo:projectUnitUserInfoList){
            List<SurProjectUserinfo> unitUserList = new ArrayList<>();
            for(SurProjectUserinfo projectUserinfo:projectUserinfoList){
                //同一部门组装数据
                if(projectUnitInfo.getUnitId().equals(projectUserinfo.getDeptId())){
                    unitUserList.add(projectUserinfo);
                }else if(DeptTypeEnum.ZGS.getCode().equals(projectUnitInfo.getUnitType().toString()) && PublicStateEnum.USERTYPE_JFJL.getCode().equals(projectUserinfo.getJobType())) {
                    unitUserList.add(projectUserinfo);
                }
            }
            projectUnitInfo.setUserinfoList(unitUserList);
        }
        return projectUnitUserInfoList;
    }


    /**
     * 根据不同角色查询项目人员信息
     *
     * @param projectId 项目主键
     * @param loginName 用户名称
     * @return 项目用户集合
     */
    @Override
    public List<SurProjectUnitInfo> queryAllProjectUserInfo(Long projectId, String loginName) {
        String nowRole = Convert.toStr(sysRoleMapper.getUserMinRoleIdByLoginName(loginName));
        //查询出公司信息
        SurProjectUnitInfo surProjectUnitInfo = new SurProjectUnitInfo();
        surProjectUnitInfo.setProjectId(projectId);
        List<SurProjectUnitInfo> surProjectUnitInfoList = surProjectUnitInfoMapper.selectSurProjectUnitInfoList(surProjectUnitInfo);
        List<SurProjectUserinfo> projectUserList = new ArrayList<>();
        SurProjectUserinfo surProjectUserinfo = new SurProjectUserinfo();
        surProjectUserinfo.setProjectId(projectId);
        surProjectUserinfo.setCreateBy(loginName);
        //根据项目主键查询所有项目人员
        projectUserList = surProjectUserinfoMapper.selectSurProjectUserinfoList(surProjectUserinfo);
        //根据角色查询项目单位人员信息
        if(SysRoleEnum.ADMIN.getCode().equals(nowRole) || SysRoleEnum.SUPR.getCode().equals(nowRole) || SysRoleEnum.JTGS.getCode().equals(nowRole) || SysRoleEnum.ZGS.getCode().equals(nowRole)){
            surProjectUserinfo.setRoleId(SysRoleEnum.ZGS.getCode());
            //查询所属项目部门信息
            SysDept sysDept = deptMapper.selectDeptByProjectId(projectId);
            surProjectUserinfo.setDeptId(sysDept.getDeptId());
        }else if(SysRoleEnum.JLDW.getCode().equals(nowRole)){
            //监理单位只能选择监理单位
            surProjectUserinfo.setRoleId(SysRoleEnum.JLDW.getCode());
            //查询部门信息
            SysDept sysDept = deptMapper.selectDeptByUserName(loginName);
            surProjectUserinfo.setDeptId(sysDept.getDeptId());
        }else if(SysRoleEnum.ZBDW.getCode().equals(nowRole)){
            //总包单位只能选择总包单位
            surProjectUserinfo.setRoleId(SysRoleEnum.ZBDW.getCode());
            //查询部门信息
            SysDept sysDept = deptMapper.selectDeptByUserName(loginName);
            surProjectUserinfo.setDeptId(sysDept.getDeptId());
        }else if(SysRoleEnum.FBDW.getCode().equals(nowRole)){
            //分包单位只能选择分包单位
            surProjectUserinfo.setRoleId(SysRoleEnum.FBDW.getCode());
            //查询部门信息
            SysDept sysDept = deptMapper.selectDeptByUserName(loginName);
            surProjectUserinfo.setDeptId(sysDept.getDeptId());
        }
        //查询相关人员
        List<SurProjectUserinfo> deptUserList = new ArrayList<>();

        if(!SysRoleEnum.COMMON.getCode().equals(nowRole)){
            deptUserList = surProjectUserinfoMapper.selectProjectDeptUserinfoList(surProjectUserinfo);
            projectUserList.addAll(deptUserList);
        }
        for(SurProjectUnitInfo unitInfo:surProjectUnitInfoList){
            List<SurProjectUserinfo> userInfoList = new ArrayList<>();
            for(SurProjectUserinfo userinfo:projectUserList){
                //同一部门组装数据
                if(unitInfo.getUnitId().equals(userinfo.getDeptId())){
                    userInfoList.add(userinfo);
                }else if(userinfo.getDeptAncestors()!=null && userinfo.getDeptAncestors().indexOf(Convert.toStr(unitInfo.getUnitId()))>-1){
                    userInfoList.add(userinfo);
                }
            }
            unitInfo.setUserinfoList(userInfoList);
        }
        return surProjectUnitInfoList;
    }

    /**
     * 新增项目用户
     * 
     * @param surProjectUserinfo 项目用户
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSurProjectUserinfo(SurProjectUserinfo surProjectUserinfo)
    {
        int res = 0;
        /****
         * 遗留BUg
         * 选人时直接选到主菜单，选出来的单位有问题
         */
        surProjectUserinfo.setCreateTime(DateUtils.getNowDate());
        SysDept sysDept = deptMapper.selectDeptById(surProjectUserinfo.getDeptId());
        //如果时城建公司的人&&当前选的单位非子公司，这里自己查询项目的所属单位
        if(DeptTypeEnum.ZGS.getCode().equals(sysDept.getTypeFlag()) || DeptTypeEnum.JTGS.getCode().equals(sysDept.getTypeFlag())){
            SurProject surProject = surProjectMapper.selectSurProjectById(surProjectUserinfo.getProjectId());
            //选择人员职务是甲方经理时，同步到项目负责人
            if(PublicStateEnum.USERTYPE_JFJL.getCode().equals(surProjectUserinfo.getJobType())){
                SysUser sysUser = sysUserMapper.selectUserById(surProjectUserinfo.getUserIds()[0]);
                surProject.setProjectPerson(sysUser.getNickName());
                surProject.setProjectPhone(sysUser.getPhonenumber());
                surProjectMapper.updateSurProject(surProject);
            }
            sysDept = deptMapper.selectDeptById(surProject.getDeptId());
            //集团人进入项目也是责任主体
            surProjectUserinfo.setDeptType(DeptTypeEnum.ZGS.getCode());
        }else{
            surProjectUserinfo.setDeptType(sysDept.getTypeFlag());
        }
        surProjectUserinfo.setDeptName(sysDept.getDeptName());
        surProjectUserinfo.setIsDel(Convert.toLong(PublicStateEnum.OK.getCode()));
        for(Long userId:surProjectUserinfo.getUserIds()){
            surProjectUserinfo.setUserId(userId);
            surProjectUserinfoMapper.insertSurProjectUserinfo(surProjectUserinfo);
            res++;
        }

        //保存项目和部门关系表
        SurProjectUnitInfo surProjectUnitInfo = new SurProjectUnitInfo();
        surProjectUnitInfo.setProjectId(surProjectUserinfo.getProjectId());
        surProjectUnitInfo.setUnitId(surProjectUserinfo.getDeptId());
        List<SurProjectUnitInfo> surProjectUnitInfosList = surProjectUnitInfoMapper.selectSurProjectUnitInfoList(surProjectUnitInfo);
        if(surProjectUnitInfosList!=null && surProjectUnitInfosList.size()>0){
            //已有关系不再保存
        }else{
            surProjectUnitInfo.setUnitType(Long.parseLong(DeptTypeEnum.ZGS.getCode().equals(sysDept.getTypeFlag())?DeptTypeEnum.JTGS.getCode():sysDept.getTypeFlag()));
            surProjectUnitInfo.setUnitName(sysDept.getDeptName());
            surProjectUnitInfo.setCreateTime(new Date());
            surProjectUnitInfo.setDelFlag(Convert.toLong(PublicStateEnum.OK.getCode()));
            surProjectUnitInfoMapper.insertSurProjectUnitInfo(surProjectUnitInfo);
        }
        return res;
    }

    /**
     * 修改项目用户
     * 
     * @param surProjectUserinfo 项目用户
     * @return 结果
     */
    @Override
    public int updateSurProjectUserinfo(SurProjectUserinfo surProjectUserinfo)
    {
        surProjectUserinfo.setUpdateTime(DateUtils.getNowDate());
        return surProjectUserinfoMapper.updateSurProjectUserinfo(surProjectUserinfo);
    }

    /**
     * 批量删除项目用户
     * 
     * @param ids 需要删除的项目用户主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSurProjectUserinfoByIds(Long[] ids)
    {
        Long projectId = surProjectUserinfoMapper.selectSurProjectUserinfoById(ids[0]).getProjectId();
        int res = surProjectUserinfoMapper.deleteSurProjectUserinfoByIds(ids);
        if(res>0){
            //删除人员后核对单位表数据
            surProjectUnitInfoMapper.deleteSurProjectUnitInfoByProjectId(projectId);
        }
        return res;
    }

    /**
     * 删除项目用户信息
     * 
     * @param id 项目用户主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectUserinfoById(Long id)
    {
        Long projectId = surProjectUserinfoMapper.selectSurProjectUserinfoById(id).getProjectId();
        int res = surProjectUserinfoMapper.deleteSurProjectUserinfoById(id);
        if(res>0){
            //删除人员后核对单位表数据
            surProjectUnitInfoMapper.deleteSurProjectUnitInfoByProjectId(projectId);
        }
        return res;
    }

    /**
     * 根据条件分页查询用户列表
     *
     * @param surProject 项目信息
     * @return 用户信息集合信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SysUser> selectProjectUserList(SurProject surProject)
    {
        return surProjectUserinfoMapper.selectProjectUserList(surProject);
    }
}
