package com.ruoyi.framework.web.service.projectV2.common;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.jPush.JPushToolUtil;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.domain.projectV2.*;
import com.ruoyi.system.domain.projectV2.Vo.ZpVo;
import com.ruoyi.system.domain.projectV2.common.ChinaArea;
import com.ruoyi.system.domain.projectV2.common.SendSmsConfig;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.projectV2.*;
import com.ruoyi.system.mapper.projectV2.common.ChinaAreaMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.projectV2.IZsyzProjectDeptService;
import com.ruoyi.system.service.projectV2.IZsyzSbbzbService;
import com.ruoyi.system.service.projectV2.IZsyzShyjService;
import com.ruoyi.system.service.projectV2.common.IZsyzCommonService;
import org.jacoco.agent.rt.internal_035b120.core.internal.flow.IFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.common.constant.CommonConstants.*;

/**
 * 公共
 *
 * @Author: tjf
 * @Date: 2023/2/24 10:53
 * @Describe:
 */
@Service
public class ZsyzCommonServiceImpL implements IZsyzCommonService {
    @Autowired
    private ZsyzShyjMapper zsyzShyjMapper;

    @Autowired
    private ZsyzSbbzbMapper zsyzSbbzbMapper;
    @Autowired
    private IZsyzSbbzbService zsyzSbbzbService;
    @Autowired
    private ZsyzSkxmMapper zsyzSkxmMapper;
    @Autowired
    private IZsyzProjectDeptService projectDeptService;
    @Autowired
    private ZsyzLcjlMapper zsyzLcjlMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ZsyzQktjMapper zsyzQktjMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private ZsyzWczsMapper zsyzWczsMapper;
    @Autowired
    private ZsyzDjksMapper zsyzDjksMapper;
    @Autowired
    private ChinaAreaMapper chinaAreaMapper;
    @Autowired
    private ZsyzSkDzjeMapper zsyzSkDzjeMapper;


    //主要功能, 对线索进度进行控制
    @Override
    public AjaxResult xssh(ZsyzShyj zsyzShyj) {

        //获取审核结果
        String shjg = zsyzShyj.getShjg();
        //查询该项目
        ZsyzSbbzb zsyzSbbzb = zsyzSbbzbService.selectZsyzSbbzbById(zsyzShyj.getXmId());
        if (zsyzSbbzb != null && TWO.equals(zsyzSbbzb.getProgress())) {
            //如果项目进度是2,项目承接,招商中心不能审核
            return AjaxResult.error("当前项目已被指派,等待相关部门审核");
        }
        if (zsyzSbbzb != null && TWO.equals(zsyzSbbzb.getXmStatus())) {
            //如果项目状态是2已通过
            return AjaxResult.error("当前项目已审核");
        }
        if (zsyzSbbzb != null) {
            if (TWO.equals(shjg)) {
                //通过更改项目状态
                zsyzSbbzb.setXmStatus(TWO);
                zsyzSbbzb.setShjg(ONE);
                String sfwlhxxxm = zsyzSbbzb.getSfwlhxxxm();
                if ("S".equals(sfwlhxxxm)){
                    zsyzSbbzb.setSfwlhxxxm("Y");
                }
                zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);
            } else if (THR.equals(shjg)) {
                //驳回
                zsyzSbbzb.setXmStatus(THR);
                zsyzSbbzb.setShjg(TWO);
                zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);
            }
            //往审核意见表插入数据
            LoginUser loginUser = SecurityUtils.getLoginUser();
            SysDept dept = loginUser.getUser().getDept();
            zsyzShyj.setDeptId(dept.getDeptId());
            zsyzShyj.setShrxm(dept.getDeptName());
            zsyzShyj.setXmjd(xmjd(zsyzSbbzb.getProgress()));
            zsyzShyj.setShsj(DateUtils.getNowDate());
            zsyzShyjMapper.insertZsyzShyj(zsyzShyj);
            //插入流程记录表
            ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
            zsyzLcjl.setXmId(zsyzSbbzb.getId());
            zsyzLcjl.setXmmc(zsyzShyj.getXmmc());
            zsyzLcjl.setXmbh(zsyzShyj.getXmbh());
            zsyzLcjl.setDeptId(dept.getDeptId());
            String deptName = dept.getDeptName();
            zsyzLcjl.setDeptName(deptName);
            zsyzLcjl.setProgress(zsyzSbbzb.getProgress());
            zsyzLcjl.setXmStatus(zsyzSbbzb.getXmStatus());
            zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);
            return AjaxResult.success();
        }
        return AjaxResult.error("未查询到当前项目信息");
    }

    /**
     * 省库项目审核
     * @param zsyzSkxm
     * @return
     */
    @Override
    public AjaxResult sksh(ZsyzSkxm zsyzSkxm) {
        ZsyzSkxm zsyzSkxmOld = zsyzSkxmMapper.selectZsyzSkxmById(zsyzSkxm.getId());
        if (zsyzSkxmOld != null){
            String isSh = zsyzSkxmOld.getIsSh();
            if (!ONE.equals(isSh)){
                return AjaxResult.error("当前项目已审核");
            }
        }
        zsyzSkxm.setShTime(DateUtils.getNowDate());
        zsyzSkxm.setShName(SecurityUtils.getUsername());
        zsyzSkxmMapper.updateZsyzSkxm(zsyzSkxm);
        ZsyzSkDzje zsyzSkDzje = new ZsyzSkDzje();
        zsyzSkDzje.setSkId(zsyzSkxm.getId());
        zsyzSkDzje.setIsSh(zsyzSkxm.getIsSh());
        zsyzSkDzjeMapper.updateZsyzSkDzjeSkId(zsyzSkDzje);
        return AjaxResult.success();
    }

    /**
     * 获取行政规划
     * @param pid
     * @return
     */
    @Override
    public AjaxResult area(String pid) {
        List<ChinaArea> chinaAreas = chinaAreaMapper.selectChinaAreaByPId(pid);
        return AjaxResult.success(chinaAreas);
    }

    /**
     * Cjd的项目审核/驳回接口
     * 参数是审核建议
     *
     * @return
     */
    @Override
    public AjaxResult xsshCjd(ZsyzShyj zsyzShyj) {
        //先获取角色
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        String roleName = "";
        if (roles != null && roles.size() > 0) {
            roleName = roles.get(0).getRoleKey();
        }
        if (!CJD.equals(roleName)) {
            return AjaxResult.error("当前用户不是承接地角色");
        }

        //判断项目阶段
        String progress = zsyzShyj.getXmjd();
        if (!TWO.equals(progress)) {
            return AjaxResult.error("当前项目进度异常,承接地无法审核");
        }
        //判断该项目的承接地是否是该用户
        ZsyzSbbzb zsyzSbbzb1 = zsyzSbbzbMapper.selectZsyzSbbzbById(zsyzShyj.getXmId());
        if (zsyzSbbzb1 != null) {
            Long cjdId = zsyzSbbzb1.getCjdId();
            if (!SecurityUtils.getDeptId().equals(cjdId)) {
                return AjaxResult.error("您不是当前项目得指定承接地,无法审核");
            }
        }
        //获取审核结果
        String shjg = zsyzShyj.getShjg();
        //需要判断是否已经回复过该条指派
        ZsyzProjectDept zsyzProjectDept = new ZsyzProjectDept();
        zsyzProjectDept.setXmId(zsyzShyj.getXmId());
        zsyzProjectDept.setDeptId(SecurityUtils.getDeptId());
        zsyzProjectDept.setDeptType(ONE);
        zsyzProjectDept.setIsDel("N");

        List<ZsyzProjectDept> zsyzProjectDepts = projectDeptService.selectZsyzProjectDeptList(zsyzProjectDept);
        ZsyzProjectDept zsyzProjectDept1 = zsyzProjectDepts.get(0);

        zsyzProjectDept1.setShjg(shjg);
        zsyzProjectDept1.setShyj(zsyzShyj.getShyj());

        String isHf = zsyzProjectDept1.getIsHf();
        if (StringUtils.isNotEmpty(isHf) && "Y".equals(isHf)) {
            return AjaxResult.error("当前项目已经回复过");
        }
        zsyzProjectDept1.setIsHf("Y");
        //更改主表信息
        ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
        zsyzSbbzb.setId(zsyzShyj.getXmId());

        //获取该项目要素部门的指派信息
        zsyzProjectDept.setDeptId(null);
        zsyzProjectDept.setDeptType(TWO);
        zsyzProjectDept.setIsDel("N");
        List<ZsyzProjectDept> zsyzProjectDeptsYsbm = projectDeptService.selectZsyzProjectDeptList(zsyzProjectDept);
        //修改中间表数据
        projectDeptService.updateZsyzProjectDept(zsyzProjectDept1);
        if (TWO.equals(shjg)) {
            //默认通过
            zsyzSbbzb.setProgress(TWO);
            //承接地审核通过去查询中间表 是否所有指派的要素部门都回复且通过
            if (zsyzProjectDeptsYsbm != null && zsyzProjectDeptsYsbm.size() > 0) {
                Map<String, List<ZsyzProjectDept>> collect = zsyzProjectDeptsYsbm.stream().collect(Collectors.groupingBy(ZsyzProjectDept::getIsHf));
                List<ZsyzProjectDept> yList = collect.get("Y");
                if (yList != null && zsyzProjectDeptsYsbm.size() == yList.size()) {
                    //说明都回复过
                    Map<String, List<ZsyzProjectDept>> shjpMap = yList.stream().collect(Collectors.groupingBy(ZsyzProjectDept::getShjg));
                    // 判断是否都同意
                    if (shjpMap != null && shjpMap.size() == 1 && shjpMap.get(TWO) != null) {
                        // 全部同意 项目进入承接已审核
                        zsyzSbbzb.setProgress(TWO);
                        zsyzSbbzb.setXmStatus(TWO);
                        zsyzSbbzb.setUpdateTime(DateUtils.getNowDate());
                    } else if (shjpMap != null && shjpMap.size() > 0) {
                        //说明 没有都同意 项目回到 项目首谈已审核,清空承接地信息
                        zsyzSbbzb.setProgress(ONE);
                        zsyzSbbzb.setXmStatus(TWO);
                        zsyzSbbzb.setUpdateTime(DateUtils.getNowDate());
                        //清空要素部门和引资单位中间表
                        projectDeptService.deleteZsyzProjectDeptByXmId(zsyzShyj.getXmId());
                    }
                }
            } else {
                //说明没有指派要素部门
                zsyzSbbzb.setXmStatus(TWO);
            }
        } else if (THR.equals(shjg)) {
            //承接地不通过 不改变项目进度 改变项目状态为3:被退回.
            //驳回
            zsyzSbbzb.setProgress(zsyzShyj.getXmjd());
            zsyzSbbzb.setXmStatus(THR);
            zsyzSbbzb.setShjg(TWO);
            zsyzSbbzb.setUpdateTime(DateUtils.getNowDate());
            //需要去中间表zsyz_project_dept判断要素部门是否都回复过
            if (zsyzProjectDeptsYsbm != null && zsyzProjectDeptsYsbm.size() > 0) {
                Map<String, List<ZsyzProjectDept>> collect = zsyzProjectDeptsYsbm.stream().collect(Collectors.groupingBy(ZsyzProjectDept::getIsHf));
                List<ZsyzProjectDept> yList = collect.get("Y");
                if (yList != null && zsyzProjectDeptsYsbm.size() == yList.size()) {
                    //说明都回复过 项目回到 项目首谈已审核,清空承接地信息
                    zsyzSbbzb.setProgress(ONE);
                    zsyzSbbzb.setXmStatus(TWO);
                    zsyzSbbzb.setUpdateTime(DateUtils.getNowDate());
                    //清空要素部门和引资单位中间表
                    projectDeptService.deleteZsyzProjectDeptByXmId(zsyzShyj.getXmId());
                }
            }else {
                //如果没有要素部门 项目回到 项目首谈已审核,清空承接地信息
                zsyzSbbzb.setProgress(ONE);
                zsyzSbbzb.setXmStatus(TWO);
                zsyzSbbzb.setUpdateTime(DateUtils.getNowDate());
                //清空要素部门和引资单位中间表
                projectDeptService.deleteZsyzProjectDeptByXmId(zsyzShyj.getXmId());
            }
        }

        //修改主表数据
        zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);

        //往审核意见表插入数据
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysDept dept = loginUser.getUser().getDept();
        zsyzShyj.setDeptId(dept.getDeptId());
        zsyzShyj.setShrxm(dept.getDeptName());
        zsyzShyj.setXmjd(xmjd(progress));
        zsyzShyj.setShsj(DateUtils.getNowDate());
        zsyzShyjMapper.insertZsyzShyj(zsyzShyj);
        //插入流程记录表
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zsyzSbbzb.getId());
        zsyzLcjl.setXmmc(zsyzShyj.getXmmc());
        zsyzLcjl.setXmbh(zsyzShyj.getXmbh());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(zsyzSbbzb.getProgress());
        zsyzLcjl.setXmStatus(zsyzSbbzb.getXmStatus());
        zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);
        return AjaxResult.success();
    }

    /**
     * 要素部门的项目审核/驳回接口
     * 参数是审核建议
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult xsshYsbm(ZsyzShyj zsyzShyj) {
        //先获取角色
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        String roleName = "";
        if (roles != null && roles.size() > 0) {
            roleName = roles.get(0).getRoleKey();
        }
        if (!YSBM.equals(roleName)) {
            return AjaxResult.error("当前用户不是要素部门角色");
        }
        //判断项目阶段
        String progress = zsyzShyj.getXmjd();
        if (!TWO.equals(progress)) {
            return AjaxResult.error("当前项目进度异常,要素部门无法审核");
        }
        //判断是否回复过
        ZsyzProjectDept zsyzProjectDept = new ZsyzProjectDept();
        zsyzProjectDept.setXmId(zsyzShyj.getXmId());
        zsyzProjectDept.setDeptId(SecurityUtils.getDeptId());
        zsyzProjectDept.setDeptType(TWO);
        zsyzProjectDept.setIsDel("N");
        List<ZsyzProjectDept> zsyzProjectDepts = projectDeptService.selectZsyzProjectDeptList(zsyzProjectDept);
        if (zsyzProjectDepts == null || zsyzProjectDepts.size() == 0){
            return AjaxResult.error("您不是当前项目得指定的部门,无法审核");
        }
        ZsyzProjectDept zsyzProjectDept1 = zsyzProjectDepts.get(0);
        if (zsyzProjectDept1 != null && "Y".equals(zsyzProjectDept1.getIsHf())) {
            return AjaxResult.error("当前项目已经回复过");
        }
        //审核结果
        String shjg = zsyzShyj.getShjg();
        zsyzProjectDept1.setIsHf("Y");
        zsyzProjectDept1.setShjg(shjg);
        zsyzProjectDept1.setShyj(zsyzShyj.getShyj());
        //定义主表修改对象
        ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
        zsyzSbbzb.setId(zsyzShyj.getXmId());

        //需要去中间表查询该项目 要素部门和承接地的信息
        zsyzProjectDept.setDeptId(null);
        List<ZsyzProjectDept> zsyzProjectDeptsYsbm = projectDeptService.selectZsyzProjectDeptList(zsyzProjectDept);
        zsyzProjectDept.setDeptType(ONE);
        List<ZsyzProjectDept> zsyzProjectDeptsCjd = projectDeptService.selectZsyzProjectDeptList(zsyzProjectDept);
        //修改中间表数据
        projectDeptService.updateZsyzProjectDept(zsyzProjectDept1);
        Long deptId = SecurityUtils.getDeptId();
        if (TWO.equals(shjg)) {
            //如果承接地也同意/自然是回复过的
            zsyzSbbzb.setProgress(TWO);
            //审核通过
            //判断是否只有一个要素部门是不是当前人
            if (zsyzProjectDeptsYsbm != null && zsyzProjectDeptsYsbm.size() == 1) {
                if (zsyzProjectDeptsYsbm.get(0).getDeptId().equals(deptId)) {
                    //说明是我自己在回复且要素部门就我一个
                    //判断承接地是否回复过
                    if (TWO.equals(zsyzProjectDeptsCjd.get(0).getShjg())) {
                        //如果承接地同意/自然是回复过的
                        // 全部同意 项目进入承接已审核
                        zsyzSbbzb.setProgress(TWO);
                        zsyzSbbzb.setXmStatus(TWO);
                        zsyzSbbzb.setUpdateTime(DateUtils.getNowDate());
                    } else if (THR.equals(zsyzProjectDeptsCjd.get(0).getShjg())) {
                        //要素同意承接地不同意 且要素部门就一个人
                        //说明都回复过 项目回到 项目首谈已审核,清空承接地信息
                        zsyzSbbzb.setProgress(ONE);
                        zsyzSbbzb.setXmStatus(TWO);
                        zsyzSbbzb.setUpdateTime(DateUtils.getNowDate());
                        //清空要素部门和引资单位中间表
                        projectDeptService.deleteZsyzProjectDeptByXmId(zsyzShyj.getXmId());
                    }
                }
            } else {
                if (zsyzProjectDeptsYsbm != null && zsyzProjectDeptsYsbm.size() > 0) {
                    //要素部门有多个
                    //判断除我外是否全部回复
                    Map<String, List<ZsyzProjectDept>> collect = zsyzProjectDeptsYsbm.stream().collect(Collectors.groupingBy(ZsyzProjectDept::getIsHf));
                    List<ZsyzProjectDept> yList = collect.get("Y");
                    if (yList != null && (zsyzProjectDeptsYsbm.size() - 1) == yList.size()) {
                        Map<String, List<ZsyzProjectDept>> shjpMap = yList.stream().collect(Collectors.groupingBy(ZsyzProjectDept::getShjg));
                        // 判断是否都同意
                        if (shjpMap != null && shjpMap.size() == 1 && shjpMap.get(TWO) != null) {
                            //要素部门都同意/判断承接地是否同意
                            if (TWO.equals(zsyzProjectDeptsCjd.get(0).getShjg())) {
                                //如果承接地也同意/自然是回复过的,项目进入已审核
                                zsyzSbbzb.setProgress(TWO);
                                zsyzSbbzb.setXmStatus(TWO);
                            } else if (THR.equals(zsyzProjectDeptsCjd.get(0).getShjg())) {
                                //要素同意承接地不同意
                                //说明都回复过 项目回到 项目首谈已审核,清空承接地信息
                                zsyzSbbzb.setProgress(ONE);
                                zsyzSbbzb.setXmStatus(TWO);
                                zsyzSbbzb.setUpdateTime(DateUtils.getNowDate());
                                //清空要素部门和引资单位中间表
                                projectDeptService.deleteZsyzProjectDeptByXmId(zsyzShyj.getXmId());
                            }
                        } else {
                            //要素部门都回复了但是没有都同意
                            //判断承接地有没有回复
                            if ("Y".equals(zsyzProjectDeptsCjd.get(0).getIsHf())) {
                                //如果承接地回复了
                                //说明都回复过 项目回到 项目首谈已审核,清空承接地信息
                                zsyzSbbzb.setProgress(ONE);
                                zsyzSbbzb.setXmStatus(TWO);
                                zsyzSbbzb.setUpdateTime(DateUtils.getNowDate());
                                //清空要素部门和引资单位中间表
                                projectDeptService.deleteZsyzProjectDeptByXmId(zsyzShyj.getXmId());
                            }
                        }
                    }
                }
            }
        } else if (THR.equals(shjg)) {
            //如果不同意,判断是否都回复过
            zsyzSbbzb.setProgress(zsyzShyj.getXmjd());
            zsyzSbbzb.setXmStatus(THR);
            zsyzSbbzb.setShjg(TWO);
            //需要去中间表zsyz_project_dept判断要素部门是否都回复过
            if (zsyzProjectDeptsYsbm != null && zsyzProjectDeptsYsbm.size() > 0) {
                //判断是否只有一个要素部门是不是当前人
                if (zsyzProjectDeptsYsbm.size() == 1) {
                    if (zsyzProjectDeptsYsbm.get(0).getDeptId().equals(deptId)) {
                        //说明是我自己在回复且要素部门就我一个
                        //判断承接地是否回复过
                        if (THR.equals(zsyzProjectDeptsCjd.get(0).getShjg())) {
                            //如果承接地也不同意/自然是回复过的
                            // 项目回到 项目首谈已审核,清空承接地信息
                            zsyzSbbzb.setProgress(ONE);
                            zsyzSbbzb.setXmStatus(TWO);
                            //清空要素部门和引资单位中间表
                            projectDeptService.deleteZsyzProjectDeptByXmId(zsyzShyj.getXmId());
                        }
                    }
                } else {
                    //要素部门有多个
                    Map<String, List<ZsyzProjectDept>> collect = zsyzProjectDeptsYsbm.stream().collect(Collectors.groupingBy(ZsyzProjectDept::getIsHf));
                    List<ZsyzProjectDept> yList = collect.get("Y");
                    if (yList != null && (zsyzProjectDeptsYsbm.size() - 1) == yList.size()) {
                        //说明出我之外都回复过
                        //判断承接地是否回复过
                        if ("Y".equals(zsyzProjectDeptsCjd.get(0).getIsHf())) {
                            // 项目回到 项目首谈已审核,清空承接地信息
                            zsyzSbbzb.setProgress(ONE);
                            zsyzSbbzb.setXmStatus(TWO);
                            //清空要素部门和引资单位中间表
                            projectDeptService.deleteZsyzProjectDeptByXmId(zsyzShyj.getXmId());
                        }
                    }
                }
            }
        }
        //修改主表数据
        zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);

        //往审核意见表插入数据
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysDept dept = loginUser.getUser().getDept();
        zsyzShyj.setDeptId(dept.getDeptId());
        zsyzShyj.setShrxm(dept.getDeptName());
        zsyzShyj.setXmjd(xmjd(progress));
        zsyzShyj.setShsj(DateUtils.getNowDate());
        zsyzShyjMapper.insertZsyzShyj(zsyzShyj);
        //插入流程记录表
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zsyzSbbzb.getId());
        zsyzLcjl.setXmmc(zsyzShyj.getXmmc());
        zsyzLcjl.setXmbh(zsyzShyj.getXmbh());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(zsyzSbbzb.getProgress());
        zsyzLcjl.setXmStatus(shjg);
        zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);
        return AjaxResult.success();
    }

    /**
     * 首页统计接口
     *
     * @return
     */
    @Override
    public AjaxResult census(ZsyzSbbzb zsyzSbbzb) {
        Long deptId = SecurityUtils.getDeptId();
        Map<String, Object> map = new HashMap<>();
        //定义返回值
        int sk = 0;
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        List<ZsyzSbbzb> zsyzSbbzbs = new ArrayList<>();
        String roleKey = "";
        // 如果是超级管理员/管理员，则不过滤数据
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (roles != null && roles.size() > 0) {
            roleKey = roles.get(0).getRoleKey();
        }
        if (StringUtils.isNotNull(currentUser) && (currentUser.isAdmin() || MANAGE.equals(roleKey) || LEADER.equals(roleKey))) {
            //如果是admin/或者平台管理员/或者领导 获取到所有数据
            zsyzSbbzbs = zsyzSbbzbMapper.selectZsyzSbbzbList(zsyzSbbzb);
            //查询省库数据
            ZsyzSkxm zsyzSkxm = new ZsyzSkxm();
            zsyzSkxm.setDeptId(deptId);
            List<ZsyzSkxm> zsyzSkxms = zsyzSkxmMapper.selectZsyzSkxmList(zsyzSkxm);
            if (zsyzSkxms != null && zsyzSkxms.size() > 0) {
                sk = zsyzSkxms.size();
            }

        } else {
            //如果不是admin/或者平台管理员 判断是什么角色
            //如果是FALSE 则包含承接地
            if (CJD.equals(roleKey)) {
                //只查询承接地数据和自己上报的线索
                zsyzSbbzb.setCjdId(deptId);
                zsyzSbbzbs = zsyzSbbzbMapper.selectZsyzSbbzbCjdList(zsyzSbbzb);
            } else {
                zsyzSbbzb.setDeptId(deptId);
                //查询指派给要素和自己上报的线索
                zsyzSbbzbs = zsyzSbbzbMapper.selectZsyzSbbzbListByZp(zsyzSbbzb);
            }
        }
        //对数据进行分组统计
        int st = 0;
        //int cj = 0;
        int qy = 0;
        int kg = 0;
        int tc = 0;
        int all = 0;
        int ps = 0;
        if (zsyzSbbzbs != null && zsyzSbbzbs.size() > 0) {
            Map<String, List<ZsyzSbbzb>> collect = zsyzSbbzbs.stream().collect(Collectors.groupingBy(ZsyzSbbzb::getProgress));

            List<ZsyzSbbzb> stList = collect.get(ONE);
            if (stList != null) {
                st = stList.size();
            }
            List<ZsyzSbbzb> cjList = collect.get(TWO);
            if (cjList != null) {
                st = st + cjList.size();
            }
            List<ZsyzSbbzb> qyList = collect.get(THR);
            if (qyList != null) {
                qy = qyList.size();
            }
            List<ZsyzSbbzb> kgList = collect.get(FOR);
            if (kgList != null) {
                kg = kgList.size();
            }
            List<ZsyzSbbzb> tcList = collect.get(FIV);
            if (tcList != null) {
                tc = tcList.size();
            }
            all = zsyzSbbzbs.size();
        }
        map.put(ST, st);
        //map.put(CJ, cj);
        map.put(QY, qy);
        map.put(KG, kg);
        map.put(TC, tc);
        map.put(SK, sk);
        ZsyzShyj zsyzShyj = new ZsyzShyj();
        String xmxsmc = zsyzSbbzb.getXmxsmc();
        if (StringUtils.isNotEmpty(xmxsmc)) {
            zsyzShyj.setXmmc(xmxsmc);
        }
        Map<String, Object> params = zsyzSbbzb.getParams();
        if (params != null && params.size() > 0) {
            zsyzShyj.setParams(params);
        }
        //问题项目,只要是被驳回的都是问题项目
        ZsyzSbbzb zsyzSbbzbWt = new ZsyzSbbzb();
        zsyzSbbzbWt.setXmStatus(THR);
        List<ZsyzSbbzb> zsyzSbbzbsWt = zsyzSbbzbService.selectZsyzSbbzbList(zsyzSbbzbWt);
        int wt = 0;
        if (zsyzSbbzbsWt != null) {
            wt = zsyzSbbzbsWt.size();
        }
        map.put("wt", wt);
        //统计批示项目数量
        for (ZsyzSbbzb sbbzb : zsyzSbbzbs) {
            if(StringUtils.isNotEmpty(sbbzb.getPsnr())){
                ps++;
            }
        }
        map.put("ps", ps);
        map.put("all", all);
        return AjaxResult.success(map);
    }

    //首页统计折线图
    @Override
    public AjaxResult censusXy() {
        //1.创建一个放所有日期的集合
        List<String> dates = new ArrayList();
        dates.add("第一季度");
        dates.add("第二季度");
        dates.add("第三季度");
        dates.add("第四季度");
        Map map = new HashMap(2);
        map.put("x", dates);
        Map<String, List<Integer>> yMpa = new HashMap();
        List<Integer> st = new ArrayList();
        List<Integer> cj = new ArrayList();
        List<Integer> qy = new ArrayList();
        List<Integer> kg = new ArrayList();
        List<Integer> tc = new ArrayList();
        //查询对应季度所有正常的项目
        List<Map<String, Object>> dateList = new ArrayList<>();
        String year = DateUtils.getTime();
        //获取当前年
        Map<String, Object> timeMapOne = new HashMap<>(2);
        Map<String, Object> timeMapTwo = new HashMap<>(2);
        Map<String, Object> timeMapThr = new HashMap<>(2);
        Map<String, Object> timeMapFor = new HashMap<>(2);
        timeMapOne.put("beginTime", year + "0101");
        timeMapOne.put("endTime", year + "0331");
        timeMapTwo.put("beginTime", year + "0401");
        timeMapTwo.put("endTime", year + "0630");
        timeMapThr.put("beginTime", year + "0701");
        timeMapThr.put("endTime", year + "0930");
        timeMapFor.put("beginTime", year + "1001");
        timeMapFor.put("endTime", year + "1231");
        dateList.add(timeMapOne);
        dateList.add(timeMapTwo);
        dateList.add(timeMapThr);
        dateList.add(timeMapFor);
        for (int i = 0; i < 4; i++) {
            ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
            zsyzSbbzb.setParams(dateList.get(i));
            List<ZsyzSbbzb> zsyzSbbzbs = zsyzSbbzbService.selectZsyzSbbzbList(zsyzSbbzb);
            if (zsyzSbbzbs != null && zsyzSbbzbs.size() > 0) {
                int stNub = 0;
                int cjNub = 0;
                int qyNub = 0;
                int kgNub = 0;
                int tcNub = 0;
                Map<String, List<ZsyzSbbzb>> collect = zsyzSbbzbs.stream().collect(Collectors.groupingBy(ZsyzSbbzb::getProgress));
                List<ZsyzSbbzb> stList = collect.get(ONE);
                if (stList != null) {
                    stNub = stList.size();
                }
                List<ZsyzSbbzb> cjList = collect.get(TWO);
                if (cjList != null) {
                    stNub = stNub + cjList.size();
                }
                List<ZsyzSbbzb> qyList = collect.get(THR);
                if (qyList != null) {
                    qyNub = qyList.size();
                }
                List<ZsyzSbbzb> kgList = collect.get(FOR);
                if (kgList != null) {
                    kgNub = kgList.size();
                }
                List<ZsyzSbbzb> tcList = collect.get(FIV);
                if (tcList != null) {
                    tcNub = tcList.size();
                }
                st.add(stNub);
                cj.add(cjNub);
                qy.add(qyNub);
                kg.add(kgNub);
                tc.add(tcNub);
            }
        }
        yMpa.put(ST, st);
        //yMpa.put(CJ, cj);
        yMpa.put(QY, qy);
        yMpa.put(KG, kg);
        yMpa.put(TC, tc);
        map.put("y", yMpa);
        return AjaxResult.success(map);
    }


    /**
     * 项目指派
     *
     * @return
     */
    @Override
    public AjaxResult xszp(ZpVo zpVo) {
        //项目指派接口
        //必须指派给承接地
        //先去查主表有没有承接地id
        List<ZsyzProjectDept> cjdList = zpVo.getCjdList();
        if (cjdList != null) {
            ZsyzSbbzb zsyzSbbzb = zsyzSbbzbMapper.selectZsyzSbbzbById(cjdList.get(0).getXmId());
            if (zsyzSbbzb != null) {
                Long cjdId = zsyzSbbzb.getCjdId();
                if (cjdId != null) {
                    return AjaxResult.error("该线索已指派了承接地,请勿重复指派");
                }
            }
            //把这个项目的指派中间表数据,全部删除
            projectDeptService.deleteZsyzProjectDeptByXmId(cjdList.get(0).getXmId());
            List<SendSmsConfig> sendSmsConfigs = new ArrayList<>();
            for (ZsyzProjectDept zsyzProjectDept : cjdList) {
                zsyzProjectDept.setDeptType(ONE);
                //说明没有指派过承接地
                projectDeptService.insertZsyzProjectDept(zsyzProjectDept);
                //更改项目进度和状态,为承接地赋值
                ZsyzSbbzb zsyzSbbzbCjd = new ZsyzSbbzb();
                zsyzSbbzbCjd.setId(zsyzProjectDept.getXmId());
                zsyzSbbzbCjd.setCjdId(zsyzProjectDept.getDeptId());
                zsyzSbbzbCjd.setCjdName(zsyzProjectDept.getDeptName());
                zsyzSbbzbCjd.setProgress(TWO);
                zsyzSbbzbCjd.setXmStatus(ONE);
                //发送短信
                List<SysUser> sysUsers = sysUserService.selectUserByDeptId(zsyzProjectDept.getDeptId());
                if (sysUsers != null) {
                    //发极光推送
                    String notificationTitle = "消息通知";
                    String msgTitle = "项目承接";
                    String msgContent = "您有一条项目承接信息，请及时处理！";
                    String jPushVO = "";
                    List<String> listS = sysUsers.stream().map(SysUser::getJgId).collect(Collectors.toList());
                    listS.removeAll(Collections.singleton(null));
                    if (listS != null && listS.size() > 0) {
                        String[] registrationId = listS.toArray(new String[listS.size()]);
                        JPushToolUtil.sendToRegistrationId(notificationTitle, msgTitle, msgContent, jPushVO, registrationId);
                    }
                    for (SysUser sysUser : sysUsers) {
                        if (StringUtils.isNotEmpty(sysUser.getPhonenumber())) {
                            SendSmsConfig sendSmsConfig = new SendSmsConfig();
                            sendSmsConfig.setPhone(sysUser.getPhonenumber());
                            sendSmsConfigs.add(sendSmsConfig);
                        }
                    }
                    if (sendSmsConfigs.size() > 0) {
                        AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs, "SMS_271465081"));
                    }
                }
                zsyzSbbzbMapper.updateZsyzSbbzbCjd(zsyzSbbzbCjd);
            }
        }
        //要素部门
        List<ZsyzProjectDept> ysbmList = zpVo.getYsbmList();
        if (ysbmList != null) {
            List<SendSmsConfig> sendSmsConfigs = new ArrayList<>();
            for (ZsyzProjectDept zsyzProjectDept : ysbmList) {
                zsyzProjectDept.setDeptType(TWO);
                projectDeptService.insertZsyzProjectDept(zsyzProjectDept);
                //发送短信
                List<SysUser> sysUsers = sysUserService.selectUserByDeptId(zsyzProjectDept.getDeptId());
                if (sysUsers != null) {
                    //发极光推送
                    String notificationTitle = "消息通知";
                    String msgTitle = "项目审核";
                    String msgContent = "您有一条项目审核信息，请及时处理！";
                    String jPushVO = "";
                    List<String> listS = sysUsers.stream().map(SysUser::getJgId).collect(Collectors.toList());
                    listS.removeAll(Collections.singleton(null));
                    if (listS != null && listS.size() > 0) {
                        String[] registrationId = listS.toArray(new String[listS.size()]);
                        JPushToolUtil.sendToRegistrationId(notificationTitle, msgTitle, msgContent, jPushVO, registrationId);
                    }
                    for (SysUser sysUser : sysUsers) {
                        if (StringUtils.isNotEmpty(sysUser.getPhonenumber())) {
                            SendSmsConfig sendSmsConfig = new SendSmsConfig();
                            sendSmsConfig.setPhone(sysUser.getPhonenumber());
                            sendSmsConfigs.add(sendSmsConfig);
                        }
                    }
                }
                if (sendSmsConfigs.size() > 0) {
                    AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs, "SMS_271465081"));
                }
            }
        }
        //引荐单位
        List<ZsyzProjectDept> yqdwList = zpVo.getYjdwList();
        if (yqdwList != null) {
            for (ZsyzProjectDept zsyzProjectDept : yqdwList) {
                zsyzProjectDept.setDeptType(THR);
                projectDeptService.insertZsyzProjectDept(zsyzProjectDept);
            }
        }
        //插入流程记录表
        SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zpVo.getXmId());
        zsyzLcjl.setXmmc(zpVo.getCjdList().get(0).getXmmc());
        zsyzLcjl.setXmbh(zpVo.getXmbh());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(TWO);
        zsyzLcjl.setXmStatus(ONE);
        zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);
        return AjaxResult.success();
    }

    /**
     * 撤回
     *
     * @param zsyzSbbzb
     * @return
     */
    @Override
    public AjaxResult xsch(ZsyzSbbzb zsyzSbbzb) {
        String progress = zsyzSbbzb.getProgress();
        String xmStatus = zsyzSbbzb.getXmStatus();
        //只有项目状态是已通过
        if (!TWO.equals(xmStatus)) {
            return AjaxResult.error("当前状态不能撤回");
        }
        if (TWO.equals(progress)) {
            return AjaxResult.error("当前项目进度不能撤回");
        }
        zsyzSbbzb.setXmStatus(ONE);
        zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);

        //插入流程记录表
        SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zsyzSbbzb.getId());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(progress);
        zsyzLcjl.setXmStatus(zsyzSbbzb.getXmStatus());
        zsyzLcjl.setRemark("撤回操作");
        zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);
        return AjaxResult.success();
    }

    /**
     * 招商引资_情况月统计表
     * @return
     */
    //@Override
    public List<Map<String, Object>> statisticsMonth(String year,String month) {
        //Long deptId = SecurityUtils.getDeptId();
        //SysUser user = SecurityUtils.getLoginUser().getUser();
        List<Map<String, Object>> mapList = new ArrayList<>();
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        String roleKey = "";
        // 如果是超级管理员/管理员，则不过滤数据
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (roles != null && roles.size() > 0) {
            roleKey = roles.get(0).getRoleKey();
        }
        if (StringUtils.isNotNull(currentUser) && (currentUser.isAdmin() || MANAGE.equals(roleKey) || LEADER.equals(roleKey))) {
            //如果是admin/或者平台管理员/或者领导 获取到所有数据
            //查找年份的所有数据
            ZsyzQktj zsyzQktj = new ZsyzQktj();
            zsyzQktj.setYear(year);
            List<ZsyzQktj> zsyzQktjList = zsyzQktjMapper.selectZsyzQktjList(zsyzQktj);
            //将所有部门数据分部门存放
            Map<Long, List<ZsyzQktj>> zsyzQktjBmMap = zsyzQktjList.stream().collect(Collectors.groupingBy(ZsyzQktj::getDeptId));
            //获取所有部门
            List<SysDept> sysDeptList = sysDeptMapper.selectDeptList(new SysDept());
            //查询本年所有线索审核通过的项目线索
            List<ZsyzSbbzb> zsyzSbbzbList = zsyzSbbzbMapper.selectZsyzSbbzbListTz(year,null);
            //将所有项目数据分部门存放
            Map<Long, List<ZsyzSbbzb>> zsyzSbbzbBmMap = zsyzSbbzbList.stream().collect(Collectors.groupingBy(ZsyzSbbzb::getDeptId));

            for (SysDept sysDept : sysDeptList) {
                //部门不为产业链县干招商组，市直部门，承接地，市级领导，要素部门一级部门时纳入统计
                if(sysDept.getDeptId()!=101 && sysDept.getDeptId()!=102 && sysDept.getDeptId()!=103 && sysDept.getDeptId()!=200 && sysDept.getDeptId()!=201){
                    Map<String, Object> mapMonth = new HashMap<>();
                    mapMonth.put("name",sysDept.getDeptName());
                    if(zsyzQktjBmMap.get(sysDept.getDeptId())!=null && zsyzQktjBmMap.get(sysDept.getDeptId()).size()>0){
                        //部门存在数据
                        Long zswcYear =0L;//招商外出天数(年)
                        Long sjksYear =0L;//对接客商次数(年)
                        Long zswcMonth =0L;//招商外出天数(年)
                        Long sjksMonth =0L;//对接客商次数(年)
                        for (ZsyzQktj qktj : zsyzQktjBmMap.get(sysDept.getDeptId())) {
                            zswcYear = zswcYear + qktj.getNumberOutings();
                            sjksYear = sjksYear + qktj.getNumberDocking();
                            if(qktj.getMonth().equals(month)){
                                zswcMonth = qktj.getNumberOutings();
                                sjksMonth = qktj.getNumberDocking();
                            }

                        }
                        mapMonth.put("zswcMonth",zswcMonth);//招商外出天数(月)
                        mapMonth.put("sjksMonth",sjksMonth);//对接客商次数(月)
                        mapMonth.put("zswcYear",zswcYear);//招商外出天数(年)
                        mapMonth.put("sjksYear",sjksYear);//对接客商次数(年)
                    }else{
                        //部门不存在数据
                        mapMonth.put("zswcMonth",0);//招商外出天数(月)
                        mapMonth.put("zswcYear",0);//招商外出天数(年)
                        mapMonth.put("sjksMonth",0);//对接客商次数(月)
                        mapMonth.put("sjksYear",0);//对接客商次数(年)
                    }

                    if(zsyzSbbzbBmMap.get(sysDept.getDeptId())!=null && zsyzSbbzbBmMap.get(sysDept.getDeptId()).size()>0){
                        Long yxxsMonth = 0L;//有效线索(月)
                        Long yxxsYear = Long.valueOf(zsyzSbbzbBmMap.get(sysDept.getDeptId()).size());//有效线索(年)
                        for (ZsyzSbbzb zsyzSbbzb : zsyzSbbzbBmMap.get(sysDept.getDeptId())) {
                            if(zsyzSbbzb.getShsj().contains(year+"-"+month)){
                                yxxsMonth++;
                            }
                        }
                        mapMonth.put("yxxsMonth",yxxsMonth);//有效线索(月)
                        mapMonth.put("yxxsYear",yxxsYear);//有效线索(年)

                    }else{
                        mapMonth.put("yxxsMonth",0);//有效线索(月)
                        mapMonth.put("yxxsYear",0);//有效线索(年)
                    }
                    mapList.add(mapMonth);
                }

            }
            //合计
            Map<String, Object> mapMonth = new HashMap<>();
            mapMonth.put("name","合计");
            Long zswcYear =0L;//招商外出天数(年)
            Long zswcMonth =0L;//招商外出天数(月)
            Long sjksYear =0L;//对接客商次数(年)
            Long sjksMonth =0L;//对接客商次数(月)
            Long yxxsMonth = 0L;//有效线索(月)
            Long yxxsYear = 0L;//有效线索(年)

            for (Map<String, Object> stringObjectMap : mapList) {
                zswcYear =zswcYear + Long.valueOf(stringObjectMap.get("zswcYear").toString()) ;
                zswcMonth =zswcMonth + Long.valueOf(stringObjectMap.get("zswcMonth").toString());
                sjksYear =sjksYear + Long.valueOf(stringObjectMap.get("sjksYear").toString());
                sjksMonth =sjksMonth +  Long.valueOf(stringObjectMap.get("sjksMonth").toString());
                yxxsMonth =yxxsMonth +  Long.valueOf(stringObjectMap.get("yxxsMonth").toString());
                yxxsYear =yxxsYear + Long.valueOf(stringObjectMap.get("yxxsYear").toString());
            }
            mapMonth.put("zswcMonth",zswcMonth);//招商外出天数(月)
            mapMonth.put("zswcYear",zswcYear);//招商外出天数(年)
            mapMonth.put("sjksMonth",sjksMonth);//对接客商次数(月)
            mapMonth.put("sjksYear",sjksYear);//对接客商次数(年)
            mapMonth.put("yxxsMonth",yxxsMonth);//有效线索(月)
            mapMonth.put("yxxsYear",yxxsYear);//有效线索(年)
            mapList.add(mapMonth);

        }else{
            //只查看自己部门数据
            //查找年份的所有数据
            ZsyzQktj zsyzQktj = new ZsyzQktj();
            zsyzQktj.setYear(year);
            zsyzQktj.setDeptId(currentUser.getDeptId());
            List<ZsyzQktj> zsyzQktjList = zsyzQktjMapper.selectZsyzQktjList(zsyzQktj);
            //查询本年当前部门所有线索审核通过的项目线索
            List<ZsyzSbbzb> zsyzSbbzbList = zsyzSbbzbMapper.selectZsyzSbbzbListTz(year,currentUser.getDeptId());
            Map<String, Object> mapMonth = new HashMap<>();
            mapMonth.put("name",currentUser.getDept().getDeptName());
            mapMonth.put("zswcMonth",0);//招商外出天数(月)
            mapMonth.put("sjksMonth",0);//对接客商次数(月)
            Long zswcYear =0L;//招商外出天数(年)
            Long sjksYear =0L;//对接客商次数(年)
            for (ZsyzQktj qktj : zsyzQktjList) {
                zswcYear = zswcYear + qktj.getNumberOutings();
                sjksYear = sjksYear + qktj.getNumberDocking();
            }
            mapMonth.put("zswcYear",zswcYear);//招商外出天数(年)
            mapMonth.put("sjksYear",sjksYear);//对接客商次数(年)
            //统计线索数量
            Long yxxsMonth = 0L;//有效线索(月)
            Long yxxsYear = Long.valueOf(zsyzSbbzbList.size());//有效线索(年)
            for (ZsyzSbbzb zsyzSbbzb : zsyzSbbzbList) {
                if(zsyzSbbzb.getShsj().contains(year+"-"+month)){
                    yxxsMonth++;
                }
            }
            mapMonth.put("yxxsMonth",yxxsMonth);//有效线索(月)
            mapMonth.put("yxxsYear",yxxsYear);//有效线索(年)
            mapList.add(mapMonth);

            //合计
            Map<String, Object> mapMonthhj = new HashMap<>();
            mapMonth.put("name","合计");
            Long zswcYearhj =0L;//招商外出天数(年)
            Long zswcMonthhj =0L;//招商外出天数(月)
            Long sjksYearhj =0L;//对接客商次数(年)
            Long sjksMonthhj =0L;//对接客商次数(月)
            Long yxxsMonthhj = 0L;//有效线索(月)
            Long yxxsYearhj = 0L;//有效线索(年)

            for (Map<String, Object> stringObjectMap : mapList) {
                zswcYearhj =zswcYearhj + Long.valueOf(stringObjectMap.get("zswcYear").toString()) ;
                zswcMonthhj =zswcMonthhj + Long.valueOf(stringObjectMap.get("zswcMonth").toString());
                sjksYearhj =sjksYearhj + Long.valueOf(stringObjectMap.get("sjksYear").toString());
                sjksMonthhj =sjksMonthhj +  Long.valueOf(stringObjectMap.get("sjksMonth").toString());
                yxxsMonthhj =yxxsMonthhj +  Long.valueOf(stringObjectMap.get("yxxsMonth").toString());
                yxxsYearhj =yxxsYearhj + Long.valueOf(stringObjectMap.get("yxxsYear").toString());
            }
            mapMonthhj.put("zswcMonth",zswcMonthhj);//招商外出天数(月)
            mapMonthhj.put("zswcYear",zswcYearhj);//招商外出天数(年)
            mapMonthhj.put("sjksMonth",sjksMonthhj);//对接客商次数(月)
            mapMonthhj.put("sjksYear",sjksYearhj);//对接客商次数(年)
            mapMonthhj.put("yxxsMonth",yxxsMonthhj);//有效线索(月)
            mapMonthhj.put("yxxsYear",yxxsYearhj);//有效线索(年)
            mapList.add(mapMonthhj);
        }
        return mapList;
    }


    /**
     * 招商引资_情况月统计表
     * @return
     */
    @Override
    public List<Map<String, Object>> statisticsMonthNew(String year,String month) {
        //Long deptId = SecurityUtils.getDeptId();
        //SysUser user = SecurityUtils.getLoginUser().getUser();
        List<Map<String, Object>> mapList = new ArrayList<>();
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        String roleKey = "";
        // 如果是超级管理员/管理员，则不过滤数据
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (roles != null && roles.size() > 0) {
            roleKey = roles.get(0).getRoleKey();
        }
        if (StringUtils.isNotNull(currentUser) && (currentUser.isAdmin() || MANAGE.equals(roleKey) || LEADER.equals(roleKey))) {
            //如果是admin/或者平台管理员/或者领导 获取到所有数据
            //查找年份的所有数据
            //外出招商
            ZsyzWczs zsyzWczs = new ZsyzWczs();
            zsyzWczs.setYear(year);
            List<ZsyzWczs> zsyzWczsList = zsyzWczsMapper.selectZsyzWczsList(zsyzWczs);
            //将所有部门数据分部门存放
            Map<Long, List<ZsyzWczs>> zsyzWczsBmMap = zsyzWczsList.stream().collect(Collectors.groupingBy(ZsyzWczs::getDeptId));

            //对接客商
            ZsyzDjks zsyzDjks = new ZsyzDjks();
            zsyzDjks.setYear(year);
            List<ZsyzDjks> zsyzDjksList = zsyzDjksMapper.selectZsyzDjksList(zsyzDjks);
            //将所有部门数据分部门存放
            Map<Long, List<ZsyzDjks>> zsyzDjksBmMap = zsyzDjksList.stream().collect(Collectors.groupingBy(ZsyzDjks::getDeptId));


            //获取所有部门
            List<SysDept> sysDeptList = sysDeptMapper.selectDeptList(new SysDept());
            //查询本年所有线索审核通过的项目线索
            List<ZsyzSbbzb> zsyzSbbzbList = zsyzSbbzbMapper.selectZsyzSbbzbListTz(year,null);
            //将所有项目数据分部门存放
            Map<Long, List<ZsyzSbbzb>> zsyzSbbzbBmMap = zsyzSbbzbList.stream().collect(Collectors.groupingBy(ZsyzSbbzb::getDeptId));

            //合计
            int zswcYearhjc =0;//招商外出次数(年)
            int zswcYearhj =0;//招商外出天数(年)
            int zswcMonthjc =0;//招商外出次数(月)
            int zswcMonthj =0;//招商外出天数(月)
            int sjksYearhj =0;//对接客商次数(年)
            int sjksMonthj =0;//对接客商次数(月)
            int yxxsMonthj = 0;//有效线索(月)
            int yxxsYearhj = 0;//有效线索(年)

            for (SysDept sysDept : sysDeptList) {
                //部门不为产业链县干招商组，市直部门，承接地，市级领导，要素部门一级部门时纳入统计
                if(sysDept.getDeptId()!=101 && sysDept.getDeptId()!=102 && sysDept.getDeptId()!=103 && sysDept.getDeptId()!=200 && sysDept.getDeptId()!=201){
                    Map<String, Object> mapMonth = new HashMap<>();
                    mapMonth.put("name",sysDept.getDeptName());
                    int zswcMonth =0;//招商外出天数(月)
                    int zswcMonthc =0;//招商外出次数(月)
                    int zswcYear =0;//招商外出天数(年)
                    int zswcYearc =0;//招商外出次数(年)
                    int sjksMonth =0;//对接客商次数(月)
                    int sjksYear =0;//对接客商次数(年)
                    //计算外出招商数据
                    if(zsyzWczsBmMap.get(sysDept.getDeptId())!=null && zsyzWczsBmMap.get(sysDept.getDeptId()).size()>0){
                        zswcYearc = zsyzWczsBmMap.get(sysDept.getDeptId()).size();
                        for (ZsyzWczs wczs : zsyzWczsBmMap.get(sysDept.getDeptId())) {
                            zswcYear = zswcYear + wczs.getNumberOutings();
                            if(wczs.getMonth().equals(month)){
                                zswcMonthc++;
                                zswcMonth = zswcMonth + wczs.getNumberOutings();
                            }

                        }
                    }
                    //计算对接客商数据
                    if(zsyzDjksBmMap.get(sysDept.getDeptId())!=null && zsyzDjksBmMap.get(sysDept.getDeptId()).size()>0){
                        sjksYear = zsyzDjksBmMap.get(sysDept.getDeptId()).size();
                        for (ZsyzDjks djks : zsyzDjksBmMap.get(sysDept.getDeptId())) {
                            if(djks.getMonth().equals(month)){
                                sjksMonth++;
                            }
                        }
                    }

                    mapMonth.put("zswcMonth",zswcMonthc+"/"+zswcMonth);//招商外出天数/次数(月)
                    mapMonth.put("sjksMonth",sjksMonth);//对接客商次数(月)
                    mapMonth.put("zswcYear",zswcYearc+"/"+zswcYear);//招商外出天数/次数(年)
                    mapMonth.put("sjksYear",sjksYear);//对接客商次数(年)

                    zswcYearhjc = zswcYearhjc + zswcYearc;
                    zswcYearhj = zswcYearhj + zswcYear;
                    zswcMonthjc = zswcMonthjc + zswcMonthc;
                    zswcMonthj = zswcMonthj + zswcMonth;
                    sjksYearhj = sjksYearhj + sjksYear;
                    sjksMonthj = sjksMonthj + sjksMonth;


                    if(zsyzSbbzbBmMap.get(sysDept.getDeptId())!=null && zsyzSbbzbBmMap.get(sysDept.getDeptId()).size()>0){
                        int yxxsMonth = 0;//有效线索(月)
                        int yxxsYear = zsyzSbbzbBmMap.get(sysDept.getDeptId()).size();//有效线索(年)
                        for (ZsyzSbbzb zsyzSbbzb : zsyzSbbzbBmMap.get(sysDept.getDeptId())) {
                            if(zsyzSbbzb.getShsj().contains(year+"-"+month)){
                                yxxsMonth++;
                            }
                        }
                        mapMonth.put("yxxsMonth",yxxsMonth);//有效线索(月)
                        mapMonth.put("yxxsYear",yxxsYear);//有效线索(年)

                        yxxsMonthj = yxxsMonthj + yxxsMonth;
                        yxxsYearhj = yxxsYearhj + yxxsYear;

                    }else{
                        mapMonth.put("yxxsMonth",0);//有效线索(月)
                        mapMonth.put("yxxsYear",0);//有效线索(年)
                    }
                    mapList.add(mapMonth);


                }

            }

            //合计
            Map<String, Object> mapMonth = new HashMap<>();
            mapMonth.put("name","合计");
            mapMonth.put("zswcMonth",zswcMonthjc+"/"+zswcMonthj);//招商外出天数/次数(月)
            mapMonth.put("zswcYear",zswcYearhjc+"/"+zswcYearhj);//招商外出天数/次数(年)
            mapMonth.put("sjksMonth",sjksMonthj);//对接客商次数(月)
            mapMonth.put("sjksYear",sjksYearhj);//对接客商次数(年)
            mapMonth.put("yxxsMonth",yxxsMonthj);//有效线索(月)
            mapMonth.put("yxxsYear",yxxsYearhj);//有效线索(年)
            mapList.add(mapMonth);

        }else{
            //只查看自己部门数据
            //查找年份的所有数据
            //外出招商
            ZsyzWczs zsyzWczs = new ZsyzWczs();
            zsyzWczs.setYear(year);
            zsyzWczs.setDeptId(currentUser.getDeptId());
            List<ZsyzWczs> zsyzWczsList = zsyzWczsMapper.selectZsyzWczsList(zsyzWczs);

            //对接客商
            ZsyzDjks zsyzDjks = new ZsyzDjks();
            zsyzDjks.setYear(year);
            zsyzDjks.setDeptId(currentUser.getDeptId());
            List<ZsyzDjks> zsyzDjksList = zsyzDjksMapper.selectZsyzDjksList(zsyzDjks);

            //查询本年当前部门所有线索审核通过的项目线索
            List<ZsyzSbbzb> zsyzSbbzbList = zsyzSbbzbMapper.selectZsyzSbbzbListTz(year,currentUser.getDeptId());

            Map<String, Object> mapMonth = new HashMap<>();
            mapMonth.put("name",currentUser.getDept().getDeptName());
            int zswcMonth =0;//招商外出天数(月)
            int zswcMonthc =0;//招商外出次数(月)
            int zswcYear =0;//招商外出天数(年)
            int zswcYearc =0;//招商外出次数(年)
            int sjksMonth =0;//对接客商次数(月)
            int sjksYear =0;//对接客商次数(年)

            //招商外出
            zswcYearc = zsyzDjksList.size();
            for (ZsyzWczs wczs : zsyzWczsList) {
                zswcYear = zswcYear + wczs.getNumberOutings();
                if(wczs.getMonth().equals(month)){
                    zswcMonthc++;
                    zswcMonth = zswcMonth + wczs.getNumberOutings();
                }

            }

            //对接客商
            sjksYear = zsyzDjksList.size();
            for (ZsyzDjks djks : zsyzDjksList) {
                if(djks.getMonth().equals(month)){
                    sjksMonth++;
                }
            }
            mapMonth.put("zswcMonth",zswcMonthc+"/"+zswcMonth);//招商外出天数/次数(月)
            mapMonth.put("sjksMonth",sjksMonth);//对接客商次数(月)
            mapMonth.put("zswcYear",zswcYearc+"/"+zswcYear);//招商外出天数/次数(年)
            mapMonth.put("sjksYear",sjksYear);//对接客商次数(年)

            //统计线索数量
            int yxxsMonth = 0;//有效线索(月)
            int yxxsYear = zsyzSbbzbList.size();//有效线索(年)
            for (ZsyzSbbzb zsyzSbbzb : zsyzSbbzbList) {
                if(zsyzSbbzb.getShsj().contains(year+"-"+month)){
                    yxxsMonth++;
                }
            }
            mapMonth.put("yxxsMonth",yxxsMonth);//有效线索(月)
            mapMonth.put("yxxsYear",yxxsYear);//有效线索(年)
            mapList.add(mapMonth);

            //合计
            Map<String, Object> mapMonthhj = new HashMap<>();
            mapMonthhj.put("name","合计");
            mapMonthhj.put("zswcMonth",zswcMonthc+"/"+zswcMonth);//招商外出天数/次数(月)
            mapMonthhj.put("sjksMonth",sjksMonth);//对接客商次数(月)
            mapMonthhj.put("zswcYear",zswcYearc+"/"+zswcYear);//招商外出天数/次数(年)
            mapMonthhj.put("sjksYear",sjksYear);//对接客商次数(年)
            mapMonthhj.put("yxxsMonth",yxxsMonth);//有效线索(月)
            mapMonthhj.put("yxxsYear",yxxsYear);//有效线索(年)
            mapList.add(mapMonthhj);
        }
        return mapList;
    }

    //根据项目进度判断项目阶段
    public String xmjd(String code) {
        if (ONE.equals(code)) {
            return XMJD_ST;
        } else if (TWO.equals(code)) {
            return XMJD_CJ;
        } else if (THR.equals(code)) {
            return XMJD_QY;
        } else if (FOR.equals(code)) {
            return XMJD_KG;
        } else if (FIV.equals(code)) {
            return XMJD_TC;
        }
        return "入归阶段";
    }
}
