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

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.fgw.*;
import com.ruoyi.system.mapper.fgw.*;
import com.ruoyi.system.service.fgw.IFgwShyjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * 发改委_审核意见Service业务层处理
 *
 * @author boman
 * @date 2023-03-22
 */
@Service
public class FgwShyjServiceImpl implements IFgwShyjService {
    @Autowired
    private FgwShyjMapper fgwShyjMapper;

    @Autowired
    private FgwXmsbMapper fgwXmsbMapper;
    @Autowired
    private FgwFjMapper fgwFjMapper ;

    @Autowired
    private FgwJzqkMapper fgwJzqkMapper;
    @Autowired
    private FgwJdapMapper fgwJdapMapper;

    /**
     * 查询发改委_审核意见
     *
     * @param id 发改委_审核意见主键
     * @return 发改委_审核意见
     */
    @Override
    public FgwShyj selectFgwShyjById(Long id) {
        return fgwShyjMapper.selectFgwShyjById(id);
    }

    /**
     * 查询发改委_审核意见列表
     *
     * @param fgwShyj 发改委_审核意见
     * @return 发改委_审核意见
     */
    @Override
    public List<FgwShyj> selectFgwShyjList(FgwShyj fgwShyj) {
        return fgwShyjMapper.selectFgwShyjList(fgwShyj);
    }

    /**
     * 新增发改委_审核意见
     *
     * @param fgwShyj 发改委_审核意见
     * @return 结果
     */
    @Override
    public int insertFgwShyj(FgwShyj fgwShyj) {
        fgwShyj.setCreateTime(DateUtils.getNowDate());
        fgwShyj.setDeptId(SecurityUtils.getDeptId());
        return fgwShyjMapper.insertFgwShyj(fgwShyj);
    }

    /**
     * 修改发改委_审核意见
     *
     * @param fgwShyj 发改委_审核意见
     * @return 结果
     */
    @Override
    public int updateFgwShyj(FgwShyj fgwShyj) {
        fgwShyj.setUpdateTime(DateUtils.getNowDate());
        return fgwShyjMapper.updateFgwShyj(fgwShyj);
    }

    /**
     * 批量删除发改委_审核意见
     *
     * @param ids 需要删除的发改委_审核意见主键
     * @return 结果
     */
    @Override
    public int deleteFgwShyjByIds(Long[] ids) {
        return fgwShyjMapper.deleteFgwShyjByIds(ids);
    }

    /**
     * 删除发改委_审核意见信息
     *
     * @param id 发改委_审核意见主键
     * @return 结果
     */
    @Override
    public int deleteFgwShyjById(Long id) {
        return fgwShyjMapper.deleteFgwShyjById(id);
    }

    /**
     * 发改委审核
     *
     * @param fgwShyj
     * @return
     */
    @Override
    public AjaxResult xssh(FgwShyj fgwShyj) {
        //获取审核意见
        String shjg = fgwShyj.getShjg();
        //查询项目当前状态
        FgwXmsb fgwXmsb = fgwXmsbMapper.selectFgwXmsbById(fgwShyj.getXmId());
        if (TWO.equals(fgwXmsb.getStatus())) {
            return AjaxResult.error("当前项目已审核");
        }
        if (TWO.equals(shjg)) {
            //审核通过修改项目状态
            fgwXmsb.setStatus(TWO);
            fgwXmsb.setType(TWO);
            fgwXmsb.setProgress(TWO);
        } else if (THR.equals(shjg)) {
            fgwXmsb.setStatus(THR);
        }
        //修改主表状态
        fgwXmsbMapper.updateFgwXmsbStatus(fgwXmsb);
        fgwShyj.setCreateTime(DateUtils.getNowDate());
        fgwShyj.setDeptId(SecurityUtils.getDeptId());
        fgwShyj.setShrxm(SecurityUtils.getUsername());
        fgwShyj.setShsj(DateUtils.getNowDate());
        fgwShyjMapper.insertFgwShyj(fgwShyj);
        return AjaxResult.success();
    }

    /**
     * 前期工作手续审核
     * @param fgwShyj
     * @return
     */
    @Override
    public AjaxResult sxsh(FgwShyj fgwShyj) {
        //获取审核意见
        String shjg = fgwShyj.getShjg();
        Long xmId = fgwShyj.getXmId();
        //修改对应附件状态
        String wjlx = fgwShyj.getWjlx();
        //查询附件表
        FgwFj fgwFj = new FgwFj();
        fgwFj.setXmId(xmId);
        fgwFj.setType(wjlx);
        List<FgwFj> fgwFjs = fgwFjMapper.selectFgwFjList(fgwFj);
        if (fgwFjs != null && fgwFjs.size() > 0){
            if (TWO.equals(shjg)) {
                for (FgwFj fj : fgwFjs) {
                    fj.setIsSh(TWO);
                    fj.setRemark(fgwShyj.getShyj());
                    fgwFjMapper.updateFgwFj(fj);
                }
            } else if (THR.equals(shjg)) {
                for (FgwFj fj : fgwFjs) {
                    fj.setIsSh(THR);
                    fj.setRemark(fgwShyj.getShyj());
                    fgwFjMapper.updateFgwFj(fj);
                }
            }
        }
        fgwShyj.setCreateTime(DateUtils.getNowDate());
        fgwShyj.setDeptId(SecurityUtils.getDeptId());
        fgwShyj.setShrxm(SecurityUtils.getUsername());
        fgwShyj.setShsj(DateUtils.getNowDate());
        fgwShyjMapper.insertFgwShyj(fgwShyj);
        return AjaxResult.success();
    }

    /**
     * 建设阶段审核
     * @param fgwShyj
     * @return
     */
    @Override
    public AjaxResult jssh(FgwShyj fgwShyj) {
        //获取审核意见
        String shjg = fgwShyj.getShjg();
        //查询项目当前状态
        FgwXmsb fgwXmsb = fgwXmsbMapper.selectFgwXmsbById(fgwShyj.getXmId());
        if (!THR.equals(fgwXmsb.getIsJs())) {
            return AjaxResult.error("当前项目已经是建设阶段或者还未申请,请勿审核");
        }
        if (TWO.equals(shjg)) {
            //审核通过修改是否建设
            fgwXmsb.setIsJs(TWO);
        } else if (THR.equals(shjg)) {
            fgwXmsb.setIsJs(ONE);
        }
        //修改主表状态
        fgwXmsbMapper.updateFgwXmsbStatus(fgwXmsb);
        fgwShyj.setCreateTime(DateUtils.getNowDate());
        fgwShyj.setDeptId(SecurityUtils.getDeptId());
        fgwShyj.setShrxm(SecurityUtils.getUsername());
        fgwShyj.setShsj(DateUtils.getNowDate());
        fgwShyjMapper.insertFgwShyj(fgwShyj);
        return AjaxResult.success();
    }

    /**
     * 竣工审核
     * @param fgwShyj
     * @return
     */
    @Override
    public AjaxResult jgsh(FgwShyj fgwShyj) {
        //获取审核意见
        String shjg = fgwShyj.getShjg();
        //查询项目当前状态
        FgwXmsb fgwXmsb = fgwXmsbMapper.selectFgwXmsbById(fgwShyj.getXmId());
        if (THR.equals(fgwXmsb.getProgress()) && TWO.equals(fgwXmsb.getStatus())) {
            return AjaxResult.error("当前项目已经竣工,请勿审核");
        }
        if (!(THR.equals(fgwXmsb.getProgress()) && ONE.equals(fgwXmsb.getStatus()))) {
            return AjaxResult.error("当前项目未申请竣工,请勿审核");
        }
        if (TWO.equals(shjg)) {
            //审核通过修改项目状态
            fgwXmsb.setStatus(TWO);
        } else if (THR.equals(shjg)) {
            fgwXmsb.setStatus(THR);
        }
        //修改主表状态
        fgwXmsbMapper.updateFgwXmsbStatus(fgwXmsb);
        fgwShyj.setCreateTime(DateUtils.getNowDate());
        fgwShyj.setDeptId(SecurityUtils.getDeptId());
        fgwShyj.setShrxm(SecurityUtils.getUsername());
        fgwShyj.setShsj(DateUtils.getNowDate());
        fgwShyjMapper.insertFgwShyj(fgwShyj);
        return AjaxResult.success();
    }

    /**
     * 进展情况审核
     * @param fgwShyj
     * @return
     */
    @Override
    @Transactional
    public AjaxResult jzqksh(FgwShyj fgwShyj) {
        //id 就是进展情况id
        Long id = fgwShyj.getId();
        FgwJzqk fgwJzqk = fgwJzqkMapper.selectFgwJzqkById(id);
        //获取审核意见
        String shjg = fgwShyj.getShjg();
        //查询项目当前状态
        FgwXmsb fgwXmsb = fgwXmsbMapper.selectFgwXmsbById(fgwShyj.getXmId());
        if (!ONE.equals(fgwJzqk.getIsSh())) {
            return AjaxResult.error("当前进展情况已审核或已驳回,请勿审核");
        }
        if (TWO.equals(shjg)) {
            //审核通过修改是否建设
            fgwJzqk.setIsSh(TWO);
            //判断纳统投资
            String isNt = fgwJzqk.getIsNt();
            if ("Y".equals(isNt)){
                String nttz = fgwJzqk.getNttz();
                fgwXmsb.setNttz(nttz);
                Long ntxmtc = fgwXmsb.getNtxmtc();
                if (ntxmtc != null){
                    long result = ntxmtc + Long.parseLong(nttz);
                    fgwXmsb.setNtxmtc(result);
                }
                //修改主表状态
                fgwXmsbMapper.updateFgwXmsbStatus(fgwXmsb);
            }
            if(StringUtils.isNotEmpty(fgwJzqk.getIsJd())){
                //修改节点安排完成情况
                FgwJdap fgwJdap = new FgwJdap();
                fgwJdap.setId(fgwJzqk.getJdId());
                fgwJdap.setUpdateTime(DateUtils.getNowDate());
                if("N".equals(fgwJzqk.getIsJd())){
                    fgwJdap.setSfwc("2");
                }else{
                    fgwJdap.setSfwc("3");
                }
                fgwJdapMapper.updateFgwJdap(fgwJdap);
            }

        } else if (THR.equals(shjg)) {
            fgwJzqk.setIsSh(THR);
            if(StringUtils.isNotEmpty(fgwJzqk.getIsJd())){
                //修改节点安排完成情况
                FgwJdap fgwJdap = new FgwJdap();
                fgwJdap.setId(fgwJzqk.getJdId());
                fgwJdap.setSfwc("0");
                fgwJdapMapper.updateFgwJdap(fgwJdap);
            }
        }
        fgwJzqkMapper.updateFgwJzqk(fgwJzqk);
        fgwShyj.setCreateTime(DateUtils.getNowDate());
        fgwShyj.setDeptId(SecurityUtils.getDeptId());
        fgwShyj.setShrxm(SecurityUtils.getUsername());
        fgwShyj.setShsj(DateUtils.getNowDate());
        fgwShyjMapper.insertFgwShyj(fgwShyj);
        return AjaxResult.success();
    }

    /**
     * 竣工申请
     * @param fgwXmsb
     * @return
     */
    @Override
    public AjaxResult jgsq(FgwXmsb fgwXmsb) {
        FgwFj fgwFj = new FgwFj();
        fgwFj.setXmId(fgwXmsb.getId());
        fgwFj.setType("17");
        fgwFj.setIsSh(TWO);
        //查询项目竣工申请附件有没有上传
        List<FgwFj> fgwFjs = fgwFjMapper.selectFgwFjList(fgwFj);
        if (fgwFjs == null || fgwFjs.size() == 0){
            return AjaxResult.error("当前项目竣工资料未审核通过");
        }
        fgwXmsb.setProgress(THR);
        fgwXmsb.setStatus(ONE);
        fgwXmsbMapper.updateFgwXmsbStatus(fgwXmsb);
        return AjaxResult.success();
    }
}
