package com.ruoyi.system.service.impl.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.system.domain.projectV2.*;
import com.ruoyi.system.mapper.projectV2.ZsyzLcjlMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzSbbzbMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzShyjMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzSkxmMapper;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

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 IZsyzShyjService zsyzShyjService;
    @Autowired
    private IZsyzProjectDeptService projectDeptService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ZsyzLcjlMapper zsyzLcjlMapper;

    //主要功能, 对线索进度进行控制
    @Override
    public AjaxResult xssh(ZsyzShyj zsyzShyj) {
        //先获取角色
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        String roleName = "";
        if (roles != null && roles.size() > 0){
            for (SysRole role : roles) {
                roleName = role.getRoleKey();
            }
        }
        //获取审核结果
        String shjg = zsyzShyj.getShjg();
        //判断项目阶段
        String progress = zsyzShyj.getXmjd();
        //根据角色和项目进度来判断能不能审核,判断是否通过
        ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
        zsyzSbbzb.setId(zsyzShyj.getXmId());
        //如果是要素部门处理,需要修改中间表的回复字段
        if (SZBM.equals(roleName)){
            ZsyzProjectDept zsyzProjectDept = new ZsyzProjectDept();
            zsyzProjectDept.setXmId(zsyzShyj.getXmId());
            zsyzProjectDept.setDeptId(SecurityUtils.getDeptId());
            //查询是否回复过最新的一条
            ZsyzProjectDept zsyzProjectDeptOld = projectDeptService.selectZsyzProjectDeptByXmId(zsyzProjectDept);
            if (zsyzProjectDeptOld != null && "Y".equals(zsyzProjectDeptOld.getIsHf())){
                return AjaxResult.error("当前已审核");
            }
            projectDeptService.updateZsyzProjectDeptByXmId(zsyzProjectDept);
        }else {
            if (ONE.equals(shjg)) {
                //通过
                //审核通过需要进入下一阶段/要素部门审核通过,不进行进度前进
                //判断项目进度
                String progressResult = changeProgress(progress, roleName);
                if (ERROR.equals(progressResult)){
                    return AjaxResult.error("当前状态下您没有审核权限");
                }
                zsyzSbbzb.setProgress(progressResult);
                zsyzSbbzb.setShjg(ONE);
                zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);
            } else if (TWO.equals(shjg)) {
                String progressResult = changeProgress(progress, roleName);
                if (ERROR.equals(progressResult)){
                    return AjaxResult.error("当前状态下您没有审核权限");
                }
                //驳回
                zsyzSbbzb.setProgress(progressResult);
                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(progress));
        zsyzShyjMapper.insertZsyzShyj(zsyzShyj);
        //插入流程记录表
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zsyzSbbzb.getId());
        zsyzLcjl.setXmmc(zsyzSbbzb.getXmxsmc());
        zsyzLcjl.setXmbh(zsyzSbbzb.getXmbh());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(zsyzSbbzb.getProgress());
        zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);
        return AjaxResult.success();
    }

    /**
     * 首页统计接口
     *
     * @return
     */
    @Override
    public AjaxResult census() {
        Long deptId = SecurityUtils.getDeptId();
        //先去redis 中查询
        Map<String, Object> map = redisCache.getCacheObject(INDEX+deptId);
        if (map == null) {
            map = new HashMap<>();
            //定义返回值
            int sk = 0;
            SysUser currentUser = SecurityUtils.getLoginUser().getUser();
            ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
            List<ZsyzSbbzb> zsyzSbbzbs = new ArrayList<>();
            String roleKey = "";
            // 如果是超级管理员/管理员，则不过滤数据
            List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
            if (roles != null && roles.size() > 0) {
                for (SysRole role : roles) {
                    //获取角色的权限字符串
                    roleKey = roleKey + "," + role.getRoleKey();
                    if (MANAGE.equals(roleKey)) {
                        roleKey = MANAGE;
                        break;
                    }
                }
            }
            boolean flag = true;
            if (StringUtils.isNotNull(currentUser) && (!currentUser.isAdmin() || MANAGE.equals(roleKey))) {
                //如果不是admin/或者平台管理员 判断是什么角色
                if (StringUtils.isNotEmpty(roleKey)) {
                    String[] split = roleKey.split(",");
                    for (int i = 0; i < split.length; i++) {
                        if (SZBM.equals(split[i])) {
                            //如果是市直部门则包含承接地数据
                            flag = false;
                            break;
                        }
                    }
                }
                //如果是FALSE 则包含承接地
                if (flag) {
                    //只查询承接地数据
                    zsyzSbbzb.setCjdId(deptId);
                    zsyzSbbzbs = zsyzSbbzbMapper.selectZsyzSbbzbCjdList(zsyzSbbzb);
                } else {
                    zsyzSbbzb.setDeptId(deptId);
                    //查询指派给市值
                    zsyzSbbzbs = zsyzSbbzbMapper.selectZsyzSbbzbListByZp(zsyzSbbzb);
                }
            } else {
                //如果是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();
                }
            }
            //对数据进行分组统计
            int xs = 0;
            int qy = 0;
            int kg = 0;
            int tc = 0;
            int all = 0;
            if (zsyzSbbzbs != null && zsyzSbbzbs.size() > 0) {
                for (ZsyzSbbzb sbbzb : zsyzSbbzbs) {
                    String progress = sbbzb.getProgress();
                    if (ONE.equals(progress) || TWO.equals(progress) || THR.equals(progress) || FOR.equals(progress) || FIV.equals(progress)) {
                        xs = xs + 1;
                    } else if (SIX.equals(progress) || SEV.equals(progress)) {
                        qy = qy + 1;
                    } else if (EIG.equals(progress) || NIN.equals(progress)) {
                        kg = kg + 1;
                    } else if (TEN.equals(progress) || ELE.equals(progress)) {
                        tc = tc + 1;
                    }
                }
                all = zsyzSbbzbs.size();
            }
            map.put("xs", xs);
            map.put("qy", qy);
            map.put("kg", kg);
            map.put("tc", tc);
            map.put("sk", sk);
            //todo 问题项目  只有要素部门意见不通过和承接地不承接的是问题项目
            List<ZsyzShyj> zsyzShyjs = zsyzShyjService.selectZsyzShyjListByWt(new ZsyzShyj());
            int wt = 0;
            if (zsyzShyjs != null) {
                wt = zsyzShyjs.size();
            }
            map.put("wt", wt);
            //统计批示项目数量
            long ps = zsyzSbbzbMapper.ldpsListCount(new ZsyzSbbzb());
            map.put("ps", ps);
            map.put("all", all);
            redisCache.setCacheObject(INDEX+deptId, map, 1, TimeUnit.HOURS);
        }
        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> xs = 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 xsNub = 0;
                int qyNub = 0;
                int kgNub = 0;
                int tcNub = 0;
                for (ZsyzSbbzb sbbzb : zsyzSbbzbs) {
                    String progress = sbbzb.getProgress();
                    String xmjd = xmjd(progress);
                    if (XMJD_XS.equals(xmjd)){
                        xsNub = xsNub + 1;
                    }else if (XMJD_QY.equals(xmjd)){
                        qyNub = qyNub +1;
                    }else if (XMJD_KG.equals(xmjd)){
                        kgNub = kgNub +1;
                    }else if (XMJD_TC.equals(xmjd)){
                        tcNub = tcNub +1;
                    }
                }
                xs.add(xsNub);
                qy.add(qyNub);
                kg.add(kgNub);
                tc.add(tcNub);
            }
        }
        yMpa.put("xs", xs);
        yMpa.put("qy", qy);
        yMpa.put("kg", kg);
        yMpa.put("tc", tc);
        map.put("y",yMpa);
        return AjaxResult.success(map);
    }

    //更改项目进度前进
    public String changeProgress(String progress,String roleName) {
        if (ONE.equals(progress) && (MANAGE.equals(roleName)||ADMIN.equals(roleName))) {
            progress = TWO;
        } else if (FOR.equals(progress) && (CJD.equals(roleName)|| ADMIN.equals(roleName))) {
            progress = FIV;
        } else if (SIX.equals(progress) && (MANAGE.equals(roleName)||ADMIN.equals(roleName))) {
            progress = SEV;
        } else if (EIG.equals(progress) && (MANAGE.equals(roleName)||ADMIN.equals(roleName))) {
            progress = NIN;
        } else if (TEN.equals(progress) && (MANAGE.equals(roleName)||ADMIN.equals(roleName))) {
            progress = ELE;
        }//项目入归暂时保留
        else {
            progress = ERROR;
        }
        return progress;
    }

    //更改项目进度后退
    public String changeProgressBack(String progress,String roleName) {
        if (TWO.equals(progress) && (MANAGE.equals(roleName)||ADMIN.equals(roleName))) {
            progress = ONE;
        } else if (FOR.equals(progress) && (CJD.equals(roleName)|| ADMIN.equals(roleName))) {
            progress = TWO;
        } else if (SIX.equals(progress) && (MANAGE.equals(roleName)||ADMIN.equals(roleName))) {
            progress = FIV;
        } else if (EIG.equals(progress) && (MANAGE.equals(roleName)||ADMIN.equals(roleName))) {
            progress = SEV;
        } else if (TEN.equals(progress) && (MANAGE.equals(roleName)||ADMIN.equals(roleName))) {
            progress = NIN;
        }//项目入归暂时保留
        else {
            progress = ERROR;
        }
        return progress;
    }

    //根据项目进度判断项目阶段
    public String xmjd(String code) {
        if (ONE.equals(code) || TWO.equals(code) || THR.equals(code) || FOR.equals(code) || FIV.equals(code)) {
            return XMJD_XS;
        } else if (SIX.equals(code) || SEV.equals(code)) {
            return XMJD_QY;
        } else if (EIG.equals(code) || NIN.equals(code)) {
            return XMJD_KG;
        } else if (TEN.equals(code) || ELE.equals(code)) {
            return XMJD_TC;
        }
        return "入归阶段";
    }
}
