package com.chushang.inspection.project.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.common.core.exception.ResultException;
import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.util.DateUtils;
import com.chushang.common.mybatis.enums.Operator;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.inspection.project.dto.PaymentDTO;
import com.chushang.inspection.project.po.WrkProject;
import com.chushang.inspection.project.po.WrkProjectContract;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.inspection.project.po.WrkProjectPayment;
import com.chushang.inspection.project.mapper.WrkProjectPaymentMapper;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;

/**
  * @auther: zhao
  * @date: 2024/6/15 10:51
  */
@Slf4j
@Service
public class WrkProjectPaymentService extends ServiceImpl<WrkProjectPaymentMapper, WrkProjectPayment> implements IService<WrkProjectPayment> {
    @Resource
    WrkProjectService projectService;
    @Resource
    WrkProjectContractService contractService;

    /**
     * 保存回款
     */
    public Long save(PaymentDTO payment)
    {
        WrkProjectPayment projectPayment = new WrkProjectPayment();
        BeanUtil.copyProperties(payment, projectPayment);
        Long projectId = payment.getProjectId();
        Long contractId = payment.getContractId();

        WrkProject project = new WrkProject();
        project.setProjectId(projectId);

        LambdaQueryWrapper<WrkProject> projectSql = WrapperUtils.builder(project, null);
        projectSql.last(Operator.LIMIT_ONE.getCharacter());
        project = projectService.getOne(projectSql);
        AssertUtil.invalidate(null == project, "查询不到对应的项目");
        WrkProjectContract projectContract = new WrkProjectContract();
        projectContract.setProjectId(projectId);
        projectContract.setContractId(contractId);
        LambdaQueryWrapper<WrkProjectContract> contractSql = WrapperUtils.builder(projectContract, null);
        contractSql.last(Operator.LIMIT_ONE.getCharacter());
        projectContract = contractService.getOne(contractSql);
        AssertUtil.invalidate(null == projectContract, "查询不到对应的项目合同");

        projectPayment.setProjectName(project.getProjectName());
        projectPayment.setDeptId(project.getDeptId());
        LocalDate now = LocalDate.now();
        // 日
        projectPayment.setPaymentDate(DateUtils.format(now));
        // 月
        projectPayment.setPaymentMonth(DateUtils.format(now, "yyyy-MM"));
        // 合同金额
        BigDecimal contractAmount = projectContract.getContractAmount();
        // 已经回款的金额
        BigDecimal countPaymentAmount = projectPayment.getPaymentAmount();
        // 当前回款金额
        BigDecimal paymentAmount = payment.getPaymentAmount();
        // 待回款金额
        BigDecimal subtract = contractAmount.subtract(countPaymentAmount);
        int i = subtract.compareTo(paymentAmount);
        // 说明已经回款完毕
        if (i == 0){
            // 总回款金额
            projectPayment.setPaymentAmount(paymentAmount.add(countPaymentAmount));
            // 修改 回款 金额完成
            WrkProjectContract contract = new WrkProjectContract();
            contract.setContractId(contractId);
            contract.setPaymentState(1);
            contractService.updateById(contract);
        }
        // 未回款完成
        else if (i > 0){
            projectPayment.setPaymentAmount(paymentAmount.add(countPaymentAmount));
        }
        // 回款超了
        else {
            throw new ResultException("回款金额已经大于合同金额");
        }
        save(projectPayment);
        return projectPayment.getPaymentId();
    }

    public PageResult pageList(Long projectId, Long contractId, CommonParam commonParam)
    {
        WrkProjectPayment projectPayment = new WrkProjectPayment();
        projectPayment.setPaymentId(projectId);
        projectPayment.setContractId(contractId);
        return pageList(projectPayment, commonParam);
    }

    public PageResult pageList(WrkProjectPayment projectPayment, CommonParam commonParam)
    {
        LambdaQueryWrapper<WrkProjectPayment> queryWrapper = WrapperUtils.builder(projectPayment, commonParam);
        IPage<WrkProjectPayment> page = this.page(
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(commonParam.getPage(), commonParam.getLimit()),
                queryWrapper
        );
        return new PageResult(page);
    }

}
