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

import java.math.BigDecimal;
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;

/**
 * 项目签约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
    public AjaxResult insertBmProjectOrderinfo(BmProjectOrderinfo bmProjectOrderinfo) {
        bmProjectOrderinfo.setCreateTime(DateUtils.getNowDate());
        bmProjectOrderinfo.setCreateBy(SecurityUtils.getUsername());
        //提取总投资额度更新到bm_project表中
        Long bmProjectId = bmProjectOrderinfo.getBmProjectId();
        if (bmProjectId == null) {
            return AjaxResult.error("缺少项目id");
        }
        BigDecimal totInvestment = bmProjectOrderinfo.getTotInvestment();
        if (totInvestment != null){
            BmProject bmProject = new BmProject();
            bmProject.setId(bmProjectId);
            bmProject.setTotAmt(totInvestment);
            bmProjectMapper.updateBmProject(bmProject);
        }
        bmProjectOrderinfoMapper.insertBmProjectOrderinfo(bmProjectOrderinfo);
        return AjaxResult.success();
    }

    /**
     * 修改项目签约
     *
     * @param bmProjectOrderinfo 项目签约
     * @return 结果
     */
    @Override
    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");
        }
        BigDecimal totInvestment = bmProjectOrderinfo.getTotInvestment();
        if (totInvestment != null){
            BmProject bmProject = new BmProject();
            bmProject.setId(bmProjectId);
            bmProject.setTotAmt(totInvestment);
            bmProjectMapper.updateBmProject(bmProject);
        }
         bmProjectOrderinfoMapper.updateBmProjectOrderinfo(bmProjectOrderinfo);
         return AjaxResult.success();
    }

    /**
     * 批量删除项目签约
     *
     * @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);
    }
}
