package com.ruoyi.system.service.impl.fgw;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.fgw.*;
import com.ruoyi.system.domain.projectV2.ZsyzSbbzb;
import com.ruoyi.system.mapper.fgw.*;
import com.ruoyi.system.service.fgw.IFgwCommonService;
import com.ruoyi.system.service.fgw.IFgwJzqkService;
import com.ruoyi.system.service.fgw.IFgwXmsbService;
import com.ruoyi.system.service.projectV2.IZsyzSbbzbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

/**
 * @Author: tjf
 * @Date: 2023/3/23 15:34
 * @Describe:
 */
@Service
public class FgwCommonServiceImpl implements IFgwCommonService {

    @Autowired
    private IFgwXmsbService fgwXmsbService;

    @Autowired
    private IZsyzSbbzbService zsyzSbbzbService;

    @Autowired
    private IFgwJzqkService fgwJzqkService;
    @Autowired
    private FgwDbdMapper fgwDbdMapper;

    @Autowired
    private FgwXmsbMapper fgwXmsbMapper;

    @Autowired
    private FgwYjpzMapper fgwYjpzMapper;

    @Autowired
    private FgwJdapMapper fgwJdapMapper;

    @Autowired
    private FgwJdapDwMapper fgwJdapDwMapper;

    /**
     * 首页统计
     *
     * @return
     */
    @Override
    public AjaxResult index() {
        Map<String, Object> map = new HashMap<>();
        map.put("wtxm", 0);
        map.put("sbxm", 0);
        map.put("xmk", 0);
        map.put("zfxm", 0);
        map.put("zsxm", 0);
        //领导批示项目
        map.put("psxm", 0);
        //预警项目
        map.put("yjxm", 0);
        //超期项目
        map.put("cqxm", 0);

        FgwXmsb xmsb = new FgwXmsb();
        List<FgwXmsb> fgwXmsbs = fgwXmsbService.selectFgwXmsbList(xmsb);
        if (fgwXmsbs != null && fgwXmsbs.size() > 0) {
            Map<String, List<FgwXmsb>> collect = fgwXmsbs.stream().collect(Collectors.groupingBy(FgwXmsb::getIsWt));
            if (collect != null && collect.size() > 0) {
                List<FgwXmsb> fgwXmsbsWt = collect.get("Y");
                if (fgwXmsbsWt != null && fgwXmsbsWt.size() > 0) {
                    map.put("wtxm", fgwXmsbsWt.size());
                }
            }
            Map<String, List<FgwXmsb>> collectType = fgwXmsbs.stream().collect(Collectors.groupingBy(FgwXmsb::getType));
            if (collectType != null && collectType.size() > 0) {
                List<FgwXmsb> fgwXmsbsSb = collectType.get(ONE);
                if (fgwXmsbsSb != null && fgwXmsbsSb.size() > 0) {
                    map.put("sbxm", fgwXmsbsSb.size());
                }
                List<FgwXmsb> fgwXmsbsXmk = collectType.get(TWO);
                if (fgwXmsbsXmk != null && fgwXmsbsXmk.size() > 0) {
                    map.put("xmk", fgwXmsbsXmk.size());
                }
            }
            map.put("zfxm", fgwXmsbs.size());
            //领导批示
            int psxm = 0;
            for (FgwXmsb fgwXmsb : fgwXmsbs) {
                if (StringUtils.isNotEmpty(fgwXmsb.getPsnr())) {
                    psxm++;
                }
            }
            map.put("psxm", psxm);

        }
        //预警项目
        //查询预警配置时间
        FgwYjpz fgwYjpz = fgwYjpzMapper.selectFgwYjpzNew();
        if(fgwYjpz==null){
            fgwYjpz = new FgwYjpz();
            fgwYjpz.setYzcqts(7L);
        }
        List<FgwJdap> fgwJdapList = fgwJdapMapper.selectFgwJdapNum(fgwYjpz.getYjts());
        if (fgwJdapList != null && fgwJdapList.size() > 0) {
            map.put("yjxm", fgwJdapList.size());
        }

        //超期项目
        List<FgwJdap> fgwJdapCqList = fgwJdapMapper.selectFgwJdapCqxmNum();
        if (fgwJdapCqList != null && fgwJdapCqList.size() > 0) {
            map.put("cqxm", fgwJdapCqList.size());
        }

        ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();

        List<ZsyzSbbzb> zsyzSbbzbs = zsyzSbbzbService.selectZsyzSbbzbList(zsyzSbbzb);
        if (zsyzSbbzbs != null && zsyzSbbzbs.size() > 0) {
            map.put("zsxm", zsyzSbbzbs.size());
        }


        return AjaxResult.success(map);
    }

    /**
     * 项目整体概况
     *
     * @return
     */
    @Override
    @DataScope(deptAlias = "d")
    public AjaxResult xmztgk() {
        Map<String, Object> map = new HashMap<>();
        map.put("xmzs", 0);
        map.put("xmztz", 0);
        map.put("jstz", 0);
        map.put("nttz", 0);

        FgwXmsb xmsb = new FgwXmsb();
        xmsb.setType(TWO);
        List<FgwXmsb> fgwXmsbs = fgwXmsbService.selectFgwXmsbList(xmsb);
        if (fgwXmsbs != null && fgwXmsbs.size() > 0) {
            map.put("xmzs", fgwXmsbs.size());
            long xmztz = fgwXmsbs.stream().mapToLong(FgwXmsb::getXmztz).sum();
            long jstz = fgwXmsbs.stream().mapToLong(FgwXmsb::getNdjh).sum();
            long nttz = fgwXmsbs.stream().mapToLong(FgwXmsb::getNtxmtc).sum();
            map.put("xmztz", xmztz);
            map.put("jstz", jstz);
            map.put("nttz", nttz);
        }
        return AjaxResult.success(map);
    }

    /**
     * 本周汇报进度
     *
     * @return
     */
    @Override
    public AjaxResult hbjd() {
        //获取当前月的开始时间
        String startTime = DateUtils.getYearMonth()+"-01";
        FgwJzqk fgwJzqk = new FgwJzqk();
        Map<String, Object> params = new HashMap<>();
        params.put("beginTime", startTime);
        params.put("endTime", DateUtils.getTime());
        fgwJzqk.setParams(params);
        List<FgwJzqk> fgwJzqks = fgwJzqkService.selectFgwJzqkTj(fgwJzqk);
        List<Long> xmmcList = fgwJzqks.stream().map(FgwJzqk::getXmId).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("yhb", 0);
        map.put("whb", 0);
        map.put("whbId", null);
        //查询所有项目总数
        FgwXmsb fgwXmsb = new FgwXmsb();
        List<FgwXmsb> fgwXmsbs = fgwXmsbService.selectFgwXmsbListTj(fgwXmsb);
        List<Long> xmidList = fgwXmsbs.stream().map(FgwXmsb::getId).collect(Collectors.toList());
        if (fgwJzqks == null || fgwJzqks.size() <= 0) {
            if (fgwXmsbs != null && fgwXmsbs.size() > 0) {
                map.put("whb", fgwXmsbs.size());
                map.put("whbId", xmidList);
            }
            return AjaxResult.success(map);
        }
        map.put("yhb", xmmcList.size());
        List<Long> whbIdList = new ArrayList<>();
        if (fgwXmsbs != null && fgwXmsbs.size() > 0) {
            int whb = 0;
            for (FgwXmsb xmsb : fgwXmsbs) {
                if(!xmsb.getProgress().equals("3")){
                    if(!xmmcList.contains(xmsb.getId())){
                        whb++;
                        whbIdList.add(xmsb.getId());
                    }
                }
            }
            map.put("whb", whb);
            map.put("whbId", whbIdList);
        }
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult sbAndJdNum() {

        //当前时间
        String dataTime = DateUtils.getDate();

        //查询配置时间
        FgwYjpz fgwYjpz = fgwYjpzMapper.selectFgwYjpzNew();
        if (fgwYjpz == null) {
            fgwYjpz = new FgwYjpz();
            fgwYjpz.setYzcqts(15L);
        }

        Map<String, Object> map = new HashMap<>();
        //项目总数
        map.put("xmzs", 0);
        //正常项目
        map.put("zcxm", 0);
        //超期节点项目
        map.put("cqjdxm", 0);
        //严重滞后项目
        map.put("yzzhxm", 0);
        //节点总数
        map.put("jdzs", 0);
        //正常节点
        map.put("zcjd", 0);
        //超期节点
        map.put("cqjd", 0);
        //严重滞后节点
        map.put("yzzhjd", 0);

        //查询所有项目
        FgwXmsb fgwXmsb = new FgwXmsb();
        List<FgwXmsb> fgwXmsbList = fgwXmsbMapper.selectFgwXmsbList(fgwXmsb);
        if (fgwXmsbList != null && fgwXmsbList.size() > 0) {
            map.put("xmzs", fgwXmsbList.size());
            Map<String, List<FgwXmsb>> fgwXmsbZcMap = fgwXmsbList.stream().collect(Collectors.groupingBy(FgwXmsb::getIsWt));
            map.put("zcxm", fgwXmsbZcMap.get("N").size());
        }
        //查询所有的节点
        FgwJdap fgwJdap = new FgwJdap();
        fgwJdap.setShjg("1");
        List<FgwJdap> fgwJdapList = fgwJdapMapper.selectFgwJdapList(fgwJdap);
        if (fgwJdapList != null && fgwJdapList.size() > 0) {
            Set<Long> xmIdCqjdxmList = new HashSet<>();
            Set<Long> xmIdYzzhxmList = new HashSet<>();
            int zcjd = 0;
            int cqjd = 0;
            int yzzhjd = 0;
            for (FgwJdap jdap : fgwJdapList) {
                //判断时间相隔的天数
                if (DateUtils.daysBetween(dataTime, jdap.getJdsj()) > 0 && !"3".equals(jdap.getSfwc())) {
                    xmIdCqjdxmList.add(jdap.getXmId());
                    cqjd++;
                }
                if (DateUtils.daysBetween(dataTime, jdap.getJdsj()) > fgwYjpz.getYzcqts() && !"3".equals(jdap.getSfwc())) {
                    xmIdYzzhxmList.add(jdap.getXmId());
                    yzzhjd++;
                }

                if (!"2".equals(jdap.getSfwc())) {
                    zcjd++;
                }
            }
            map.put("cqjdxm", xmIdCqjdxmList.size());
            map.put("yzzhxm", xmIdYzzhxmList.size());
            map.put("jdzs", fgwJdapList.size());
            map.put("zcjd", zcjd);
            map.put("cqjd", cqjd);
            map.put("yzzhjd", yzzhjd);

        }
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult zrdwCqNum() {
        List<Map<String, Object>> zMap = new ArrayList<>();

        //获取当前时间
        String time = DateUtils.getDate();

        //获取所有的责任单位
        FgwJdapDw fgwJdapDw = new FgwJdapDw();
        fgwJdapDw.setDeptType("0");
        List<FgwJdapDw> fgwJdapDwList = fgwJdapDwMapper.selectFgwJdapDwList(fgwJdapDw);
        if(fgwJdapDwList == null || fgwJdapDwList.size()<=0){
            return AjaxResult.success(zMap);
        }
        Map<String, List<FgwJdapDw>> fgwJdapDwMap = fgwJdapDwList.stream().collect(Collectors.groupingBy(FgwJdapDw::getDeptName));
        List<Long> jdapIdList = fgwJdapDwList.stream().map(FgwJdapDw::getDeptId).collect(Collectors.toList());
        //查找所有未完成的节点
        List<FgwJdap> fgwJdapList = fgwJdapMapper.selectFgwJdapListByIdList(jdapIdList);

        for (String key : fgwJdapDwMap.keySet()) {
            List<FgwJdapDw> fgwJdapDws = fgwJdapDwMap.get(key);
            List<Long> jdapIds = fgwJdapDws.stream().map(FgwJdapDw::getDeptId).collect(Collectors.toList());
            boolean bl = false;
            Map<String, Object> fMap = new HashMap<>();
            fMap.put("name",key);
            int cq = 0;
            int yzzh = 0;
            for (FgwJdap fgwJdap : fgwJdapList) {
                if (jdapIds.contains(fgwJdap.getId())) {
                    if (DateUtils.daysBetween(fgwJdap.getJdsj(), time) > 0) {
                        cq++;
                        bl = true;
                    }
                    if (DateUtils.daysBetween(fgwJdap.getJdsj(), time) > 15) {
                        yzzh++;
                        bl = true;
                    }
                }
            }
            if(bl){
                fMap.put("jdzs",fgwJdapDws.size());
                fMap.put("cqjd",cq);
                fMap.put("yazhjd",yzzh);
                zMap.add(fMap);
            }
        }

        return AjaxResult.success(zMap);
    }


    /**
     * 传入时间戳获取日期格式时间
     *
     * @param time
     * @return String
     * @author xyl
     * @date 2023/1/4 13:24
     */
    public static String conversion(Long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(time);
    }
}
