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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
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.IFgwXmsbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.constant.CommonConstants.ONE;
import static com.ruoyi.common.constant.CommonConstants.TWO;


/**
 * 发改委_申报_主Service业务层处理
 *
 * @author boman
 * @date 2023-03-22
 */
@Service
public class FgwXmsbServiceImpl implements IFgwXmsbService {
    @Autowired
    private FgwXmsbMapper fgwXmsbMapper;
    @Autowired
    private FgwFjMapper fgwFjMapper;
    @Autowired
    private FgwDbdMapper fgwDbdMapper;
    @Autowired
    private FgwJdapMapper fgwJdapMapper;
    @Autowired
    private FgwYjpzMapper fgwYjpzMapper;

    /**
     * 查询发改委_申报_主
     *
     * @param id 发改委_申报_主主键
     * @return 发改委_申报_主
     */
    @Override
    public FgwXmsb selectFgwXmsbById(Long id) {
        FgwXmsb fgwXmsb = fgwXmsbMapper.selectFgwXmsbById(id);
        List<FgwFj> fgwFjs = fgwFjMapper.selectFgwFjByXmId(fgwXmsb.getId());
        if (fgwFjs != null && fgwFjs.size() > 0) {
            fgwXmsb.setFjList(fgwFjs);
        }
        return fgwXmsb;

    }

    /**
     * 查询发改委_申报_主列表
     *
     * @param fgwXmsb 发改委_申报_主
     * @return 发改委_申报_主
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<FgwXmsb> selectFgwXmsbList(FgwXmsb fgwXmsb) {
        return fgwXmsbMapper.selectFgwXmsbList(fgwXmsb);
    }

    /**
     * 统计合格项目
     *
     * @param fgwXmsb
     * @return
     */
    @Override
    public List<FgwXmsb> selectFgwXmsbListTj(FgwXmsb fgwXmsb) {
        return fgwXmsbMapper.selectFgwXmsbListTj(fgwXmsb);
    }

    /**
     * 新增发改委_申报_主
     *
     * @param fgwXmsb 发改委_申报_主
     * @return 结果
     */
    @Override
    public int insertFgwXmsb(FgwXmsb fgwXmsb) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        fgwXmsb.setCreateTime(DateUtils.getNowDate());
        fgwXmsb.setUserId(user.getUserId());
        fgwXmsb.setDeptId(user.getDeptId());
        fgwXmsb.setDeptName(user.getDept().getDeptName());
        return fgwXmsbMapper.insertFgwXmsb(fgwXmsb);
    }

    /**
     * 修改发改委_申报_主
     *
     * @param fgwXmsb 发改委_申报_主
     * @return 结果
     */
    @Override
    public AjaxResult updateFgwXmsb(FgwXmsb fgwXmsb) {

        //判断是否填写节点安排
        FgwXmsb xmsb = fgwXmsbMapper.selectFgwXmsbById(fgwXmsb.getId());
        String year = DateUtils.dateToStrLong(xmsb.getCreateTime(),"YYYY");
        FgwJdap fgwJdap = new FgwJdap();
        fgwJdap.setXmId(fgwXmsb.getId());
        fgwJdap.setYear(year);
        fgwJdap.setShjg("1");
        List<FgwJdap> fgwJdapList = fgwJdapMapper.selectFgwJdapList(fgwJdap);
        if(fgwJdapList==null || fgwJdapList.size()<12){
            return AjaxResult.error("上传失败，节点安排信息未完善或节点安排信息审核未通过！");
        }

        fgwXmsb.setUpdateTime(DateUtils.getNowDate());
        String progress = fgwXmsb.getProgress();
        String status = fgwXmsb.getStatus();
        if (ONE.equals(progress) && TWO.equals(status)) {
            //修改主表项目进入前期工作手续 待审核
            fgwXmsb.setProgress(TWO);
            fgwXmsb.setStatus(ONE);
        }
        //修改主表项目进入待审核
        fgwXmsb.setStatus(ONE);
        List<FgwFj> fjList = fgwXmsb.getFjList();
        fgwFjMapper.deleteFgwFjByXmId(fgwXmsb.getId(), 1L);
        if (fjList != null && fjList.size() > 0) {
            //保存新增的
            for (FgwFj fgwFj : fjList) {
                String isSh = fgwFj.getIsSh();
                if (StringUtils.isBlank(isSh) || ONE.equals(isSh)){
                    fgwFjMapper.insertFgwFj(fgwFj);
                }
            }
        }
        int rows = fgwXmsbMapper.updateFgwXmsb(fgwXmsb);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除发改委_申报_主
     *
     * @param ids 需要删除的发改委_申报_主主键
     * @return 结果
     */
    @Override
    public int deleteFgwXmsbByIds(Long[] ids) {
        return fgwXmsbMapper.deleteFgwXmsbByIds(ids);
    }

    /**
     * 删除发改委_申报_主信息
     *
     * @param id 发改委_申报_主主键
     * @return 结果
     */
    @Override
    public int deleteFgwXmsbById(Long id) {
        return fgwXmsbMapper.deleteFgwXmsbById(id);
    }

    @Override
    @DataScope(deptAlias = "d")
    public List<FgwXmsb> ldpsList(FgwXmsb fgwXmsb) {
        return fgwXmsbMapper.ldpsList(fgwXmsb);
    }

    @Override
    @DataScope(deptAlias = "d")
    public AjaxResult selectFgwXmsbListAll(FgwXmsb fgwXmsb) {
        Map<String, Integer> map = new HashMap<>();
        map.put("all", 0);
        map.put("sbk", 0);
        map.put("xmk", 0);
        List<FgwXmsb> fgwXmsbList = fgwXmsbMapper.selectFgwXmsbListAll(fgwXmsb);
        Map<String, List<FgwXmsb>> fgwXmsbMap = fgwXmsbList.stream().collect(Collectors.groupingBy(FgwXmsb::getType));
        if (fgwXmsbList != null && fgwXmsbList.size() > 0) {
            map.put("all", fgwXmsbList.size());
        }
        if (fgwXmsbMap.get("1") != null && fgwXmsbMap.get("1").size() > 0) {
            map.put("sbk", fgwXmsbMap.get("1").size());
        }
        if (fgwXmsbMap.get("2") != null && fgwXmsbMap.get("2").size() > 0) {
            map.put("xmk", fgwXmsbMap.get("2").size());
        }

        return AjaxResult.success(map);
    }

    @Override
    public List<FgwXmsb> xmyjList(FgwXmsb fgwXmsb) {
        //List<FgwDbd> fgwDbdList = fgwDbdMapper.selectFgwDbdYjxmNum();
        //查询预警配置时间
        FgwYjpz fgwYjpz = fgwYjpzMapper.selectFgwYjpzNew();
        if(fgwYjpz==null){
            fgwYjpz = new FgwYjpz();
            fgwYjpz.setYzcqts(7L);
        }
        List<FgwJdap> fgwJdapList = fgwJdapMapper.selectFgwJdapNum(fgwYjpz.getYjts());
        startPage();
        List<FgwXmsb> fgwXmsbList = new ArrayList<>();
        if (fgwJdapList.size() > 0) {
            List<Long> xmidList = fgwJdapList.stream().map(FgwJdap::getXmId).collect(Collectors.toList());
            fgwXmsb.setIdList(xmidList);
            fgwXmsbList = fgwXmsbMapper.selectFgwXmsbList(fgwXmsb);
        }

        return fgwXmsbList;
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageUtils.startPage();
    }

    @Override
    public List<FgwXmsb> cqxmList(FgwXmsb fgwXmsb) {
        //List<FgwDbd> fgwDbdListCq = fgwDbdMapper.selectFgwDbdCqxmNum();
        List<FgwJdap> fgwJdapList = fgwJdapMapper.selectFgwJdapCqxmNum();
        startPage();
        List<FgwXmsb> fgwXmsbList = new ArrayList<>();
        if (fgwJdapList.size() > 0) {
            List<Long> xmidList = fgwJdapList.stream().map(FgwJdap::getXmId).collect(Collectors.toList());
            fgwXmsb.setIdList(xmidList);
            fgwXmsbList = fgwXmsbMapper.selectFgwXmsbList(fgwXmsb);
        }
        return fgwXmsbList;
    }
}
