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

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.projectV2.ZsyzFj;
import com.ruoyi.system.domain.projectV2.ZsyzLcjl;
import com.ruoyi.system.domain.projectV2.ZsyzQyxx;
import com.ruoyi.system.domain.projectV2.ZsyzSbbzb;
import com.ruoyi.system.mapper.projectV2.ZsyzFjMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzLcjlMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzQyxxMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzSbbzbMapper;
import com.ruoyi.system.service.projectV2.IZsyzQyxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.ruoyi.common.constant.CommonConstants.FIV;
import static com.ruoyi.common.constant.CommonConstants.SIX;

/**
 * 招商引资_签约信息Service业务层处理
 *
 * @author boman
 * @date 2023-02-22
 */
@Service
public class ZsyzQyxxServiceImpl implements IZsyzQyxxService {
    @Autowired
    private ZsyzQyxxMapper zsyzQyxxMapper;

    @Autowired
    private ZsyzFjMapper zsyzFjMapper;
    @Autowired
    private ZsyzSbbzbMapper zsyzSbbzbMapper;
    @Autowired
    private ZsyzLcjlMapper zsyzLcjlMapper;

    /**
     * 查询招商引资_签约信息
     *
     * @param id 招商引资_签约信息ID
     * @return 招商引资_签约信息
     */
    @Override
    @Transactional
    public ZsyzQyxx selectZsyzQyxxById(Long id) {
        ZsyzQyxx zsyzQyxx = zsyzQyxxMapper.selectZsyzQyxxById(id);
        if (zsyzQyxx != null) {
            ZsyzFj zsyzFj = new ZsyzFj();
            zsyzFj.setSourceId(id);
            List<ZsyzFj> zsyzFjList = zsyzFjMapper.selectZsyzFjList(zsyzFj);
            if (zsyzFjList != null && zsyzFjList.size() > 0) {
                zsyzQyxx.setZsyzFjList(zsyzFjList);
            }
        }
        return zsyzQyxx;
    }

    /**
     * 查询招商引资_签约信息列表
     *
     * @param zsyzQyxx 招商引资_签约信息
     * @return 招商引资_签约信息
     */
    @Override
    public List<ZsyzQyxx> selectZsyzQyxxList(ZsyzQyxx zsyzQyxx) {
        return zsyzQyxxMapper.selectZsyzQyxxList(zsyzQyxx);
    }

    /**
     * 新增招商引资_签约信息
     *
     * @param zsyzQyxx 招商引资_签约信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertZsyzQyxx(ZsyzQyxx zsyzQyxx) {
        zsyzQyxx.setCreateTime(DateUtils.getNowDate());
        ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
        zsyzSbbzb.setId(zsyzQyxx.getXmId());
        zsyzSbbzb.setProgress(SIX);
        zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);
        int count = zsyzQyxxMapper.insertZsyzQyxx(zsyzQyxx);
        //保存附件信息
        List<ZsyzFj> zsyzFjList = zsyzQyxx.getZsyzFjList();
        if (zsyzFjList != null){
            for (ZsyzFj zsyzFj : zsyzFjList) {
                zsyzFj.setSourceId(zsyzQyxx.getId());
                zsyzFj.setType("2");
                zsyzFj.setXmId(zsyzQyxx.getXmId());
                zsyzFj.setXmbh(zsyzQyxx.getXmbh());
                zsyzFjMapper.insertZsyzFj(zsyzFj);
            }
        }
        //插入流程记录表
        SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
        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 count;

    }

    /**
     * 修改招商引资_签约信息
     *
     * @param zsyzQyxx 招商引资_签约信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateZsyzQyxx(ZsyzQyxx zsyzQyxx) {

        zsyzQyxx.setUpdateTime(DateUtils.getNowDate());
        int count = zsyzQyxxMapper.updateZsyzQyxx(zsyzQyxx);
        ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
        zsyzSbbzb.setId(zsyzQyxx.getXmId());
        ZsyzSbbzb zsyzSbbzbOld = zsyzSbbzbMapper.selectZsyzSbbzbById(zsyzQyxx.getXmId());
        if (zsyzSbbzbOld != null && zsyzSbbzbOld.getProgress().equals(FIV)){
            zsyzSbbzb.setProgress(SIX);
            zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);
        }
        //修改附件
        if (zsyzQyxx.getZsyzFjList() != null && zsyzQyxx.getZsyzFjList().size() > 0) {
            //先删除相关附件
            zsyzFjMapper.deleteZsyzFjBySourceId(zsyzQyxx.getId());
            //再将文件新增进数据库
            List<ZsyzFj> zsyzFjList = zsyzQyxx.getZsyzFjList();
            for (ZsyzFj zsyzFj : zsyzFjList) {
                zsyzFj.setSourceId(zsyzQyxx.getId());
                zsyzFj.setType("2");
                zsyzFj.setXmId(zsyzQyxx.getXmId());
                zsyzFj.setXmbh(zsyzQyxx.getXmbh());
                zsyzFjMapper.insertZsyzFj(zsyzFj);
            }
        }
        return count;
    }

    /**
     * 批量删除招商引资_签约信息
     *
     * @param ids 需要删除的招商引资_签约信息ID
     * @return 结果
     */
    @Override
    public int deleteZsyzQyxxByIds(Long[] ids) {
        return zsyzQyxxMapper.deleteZsyzQyxxByIds(ids);
    }

    /**
     * 删除招商引资_签约信息信息
     *
     * @param id 招商引资_签约信息ID
     * @return 结果
     */
    @Override
    public int deleteZsyzQyxxById(Long id) {
        return zsyzQyxxMapper.deleteZsyzQyxxById(id);
    }
}
