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

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.fgw.FgwFj;
import com.ruoyi.system.domain.fgw.FgwJdap;
import com.ruoyi.system.domain.fgw.FgwJzqk;
import com.ruoyi.system.mapper.fgw.FgwFjMapper;
import com.ruoyi.system.mapper.fgw.FgwJdapMapper;
import com.ruoyi.system.mapper.fgw.FgwJzqkMapper;
import com.ruoyi.system.service.fgw.IFgwJzqkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 发改委_进展情况Service业务层处理
 *
 * @author boman
 * @date 2023-03-22
 */
@Service
public class FgwJzqkServiceImpl implements IFgwJzqkService {
    @Autowired
    private FgwJzqkMapper fgwJzqkMapper;

    @Autowired
    private FgwFjMapper fgwFjMapper;
    @Autowired
    private FgwJdapMapper fgwJdapMapper;


    /**
     * 查询发改委_进展情况
     *
     * @param id 发改委_进展情况主键
     * @return 发改委_进展情况
     */
    @Override
    public FgwJzqk selectFgwJzqkById(Long id) {
        FgwJzqk fgwJzqk = fgwJzqkMapper.selectFgwJzqkById(id);
        if (fgwJzqk != null) {
            List<FgwFj> fgwFjs = fgwFjMapper.selectFgwFjBySourceId(fgwJzqk.getId());
            if (fgwFjs != null && fgwFjs.size() > 0) {
                fgwJzqk.setFjList(fgwFjs);
            }
        }
        return fgwJzqk;
    }

    /**
     * 查询发改委_进展情况列表
     *
     * @param fgwJzqk 发改委_进展情况
     * @return 发改委_进展情况
     */
    @Override
    public List<FgwJzqk> selectFgwJzqkList(FgwJzqk fgwJzqk) {
        List<FgwJzqk> fgwJzqks = fgwJzqkMapper.selectFgwJzqkList(fgwJzqk);
        if (fgwJzqks != null && fgwJzqks.size() > 0) {
            for (FgwJzqk jzqk : fgwJzqks) {
                if (fgwJzqk != null) {
                    List<FgwFj> fgwFjs = fgwFjMapper.selectFgwFjBySourceId(jzqk.getId());
                    if (fgwFjs != null && fgwFjs.size() > 0) {
                        jzqk.setFjList(fgwFjs);
                    }
                }
            }
        }
        return fgwJzqks;
    }

    /**
     * 统计本周汇报进度
     * @param fgwJzqk
     * @return
     */
    @Override
    public List<FgwJzqk> selectFgwJzqkTj(FgwJzqk fgwJzqk) {
        return  fgwJzqkMapper.selectFgwJzqkTj(fgwJzqk);
    }

    /**
     * 新增发改委_进展情况
     *
     * @param fgwJzqk 发改委_进展情况
     * @return 结果
     */
    @Override
    public int insertFgwJzqk(FgwJzqk fgwJzqk) {

        if(StringUtils.isNotEmpty(fgwJzqk.getIsJd())){
            //修改节点安排完成情况
            FgwJdap fgwJdap = new FgwJdap();
            fgwJdap.setId(fgwJzqk.getJdId());
            fgwJdap.setSfwc("1");
            fgwJdapMapper.updateFgwJdap(fgwJdap);
        }

        fgwJzqk.setHbTime(DateUtils.getNowDate());
        fgwJzqk.setDeptId(SecurityUtils.getDeptId());
        int i = fgwJzqkMapper.insertFgwJzqk(fgwJzqk);
        fgwJzqk.setDeptId(SecurityUtils.getDeptId());
        fgwJzqk.setCreateTime(DateUtils.getNowDate());
        List<FgwFj> fjList = fgwJzqk.getFjList();
        if (fjList != null && fjList.size() > 0) {
            for (FgwFj fgwFj : fjList) {
                fgwFj.setType("18");
                fgwFj.setSourceId(fgwJzqk.getId());
                fgwFjMapper.insertFgwFj(fgwFj);
            }
        }
        return i;
    }

    /**
     * 修改发改委_进展情况
     *
     * @param fgwJzqk 发改委_进展情况
     * @return 结果
     */
    @Override
    public int updateFgwJzqk(FgwJzqk fgwJzqk) {
        fgwJzqk.setUpdateTime(DateUtils.getNowDate());
        fgwJzqk.setIsSh(ONE);
        List<FgwFj> fjList = fgwJzqk.getFjList();
        fgwFjMapper.deleteFgwFjByXmId(fgwJzqk.getXmId(),18L);
        if (fjList != null && fjList.size() > 0) {
            for (FgwFj fgwFj : fjList) {
                fgwFj.setType("18");
                fgwFj.setSourceId(fgwJzqk.getId());
                fgwFjMapper.insertFgwFj(fgwFj);
            }
        }
        return fgwJzqkMapper.updateFgwJzqk(fgwJzqk);
    }

    /**
     * 批量删除发改委_进展情况
     *
     * @param ids 需要删除的发改委_进展情况主键
     * @return 结果
     */
    @Override
    public int deleteFgwJzqkByIds(Long[] ids) {
        return fgwJzqkMapper.deleteFgwJzqkByIds(ids);
    }

    /**
     * 删除发改委_进展情况信息
     *
     * @param id 发改委_进展情况主键
     * @return 结果
     */
    @Override
    public int deleteFgwJzqkById(Long id) {
        return fgwJzqkMapper.deleteFgwJzqkById(id);
    }
}
