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

import java.math.BigDecimal;
import java.util.Date;
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.system.domain.project.BmProject;
import com.ruoyi.system.domain.project.BmProjectOrderinfo;
import com.ruoyi.system.mapper.project.BmProjectMapper;
import com.ruoyi.system.mapper.project.BmProjectOrderinfoMapper;
import com.ruoyi.system.service.project.IBmProjectOrderinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目签约Service业务层处理
 *
 * @author ruoyi
 * @date 2021-03-05
 */
@Service
public class BmProjectOrderinfoServiceImpl implements IBmProjectOrderinfoService {
    @Autowired
    private BmProjectOrderinfoMapper bmProjectOrderinfoMapper;

    @Autowired
    private BmProjectMapper bmProjectMapper;

    /**
     * 查询项目签约
     *
     * @param id 项目签约ID
     * @return 项目签约
     */
    @Override
    public BmProjectOrderinfo selectBmProjectOrderinfoById(Long id) {
        return bmProjectOrderinfoMapper.selectBmProjectOrderinfoById(id);
    }

    /**
     * 查询项目签约列表
     *
     * @param bmProjectOrderinfo 项目签约
     * @return 项目签约
     */
    @Override
    public List<BmProjectOrderinfo> selectBmProjectOrderinfoList(BmProjectOrderinfo bmProjectOrderinfo) {
        return bmProjectOrderinfoMapper.selectBmProjectOrderinfoList(bmProjectOrderinfo);
    }

    /**
     * 新增项目签约
     *
     * @param bmProjectOrderinfo 项目签约
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertBmProjectOrderinfo(BmProjectOrderinfo bmProjectOrderinfo) {
        bmProjectOrderinfo.setCreateTime(DateUtils.getNowDate());
        bmProjectOrderinfo.setCreateBy(SecurityUtils.getUsername());
        //提取总投资额度更新到bm_project表中
        //合同约定竣工时间
        Date agreementEndDate = bmProjectOrderinfo.getAgreementEnddate();
        //合同约定开工时间
        Date agreementStartDate = bmProjectOrderinfo.getAgreementStartdate();

        Long bmProjectId = bmProjectOrderinfo.getBmProjectId();
        if (bmProjectId == null) {
            return AjaxResult.error("缺少项目id");
        }
        bmProjectOrderinfoMapper.insertBmProjectOrderinfo(bmProjectOrderinfo);
        return updateBmProject(bmProjectOrderinfo);
    }

    /**
     * 修改项目签约
     *
     * @param bmProjectOrderinfo 项目签约
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateBmProjectOrderinfo(BmProjectOrderinfo bmProjectOrderinfo) {
        bmProjectOrderinfo.setUpdateTime(DateUtils.getNowDate());
        bmProjectOrderinfo.setUpdateBy(SecurityUtils.getUsername());
        //提取总投资额度更新到bm_project表中
        Long bmProjectId = bmProjectOrderinfo.getBmProjectId();
        if (bmProjectId == null) {
            return AjaxResult.error("缺少项目id");
        }
        bmProjectOrderinfoMapper.updateBmProjectOrderinfo(bmProjectOrderinfo);
        return updateBmProject(bmProjectOrderinfo);
    }

    /**
     * 批量删除项目签约
     *
     * @param ids 需要删除的项目签约ID
     * @return 结果
     */
    @Override
    public int deleteBmProjectOrderinfoByIds(Long[] ids) {
        return bmProjectOrderinfoMapper.deleteBmProjectOrderinfoByIds(ids);
    }

    /**
     * 删除项目签约信息
     *
     * @param id 项目签约ID
     * @return 结果
     */
    @Override
    public int deleteBmProjectOrderinfoById(Long id) {
        return bmProjectOrderinfoMapper.deleteBmProjectOrderinfoById(id);
    }

    /**
     * 提取参数更新BmProject表字段
     *
     * @param bmProjectOrderInfo
     */
    public AjaxResult updateBmProject(BmProjectOrderinfo bmProjectOrderInfo) {
        Long bmProjectId = bmProjectOrderInfo.getBmProjectId();
        BigDecimal totInvestment = bmProjectOrderInfo.getTotInvestment();
        Date agreementStartDate = bmProjectOrderInfo.getAgreementStartdate();
        Date agreementEndDate = bmProjectOrderInfo.getAgreementEnddate();
        if (totInvestment != null && agreementStartDate != null && agreementEndDate != null) {
            BmProject bmProject = new BmProject();
            bmProject.setId(bmProjectId);
            bmProject.setTotAmt(totInvestment);
            bmProject.setOrderBeginTime(agreementStartDate);
            bmProject.setOrderEndTime(agreementEndDate);
            bmProjectMapper.updateBmProject(bmProject);
        } else {
            return AjaxResult.error("缺少参数");
        }
        return AjaxResult.success();
    }
}
