package com.fjhx.service.flow;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.entity.contract.Contract;
import com.fjhx.entity.contract.ContractDetails;
import com.fjhx.enums.ContractStatusEnum;
import com.fjhx.enums.FlowStatusEnum;
import com.fjhx.params.JumpVo;
import com.fjhx.params.contract.ContractVo;
import com.fjhx.service.contract.ContractDetailsService;
import com.fjhx.service.contract.ContractService;
import com.fjhx.utils.Assert;
import com.fjhx.utils.ExampleAbstract;
import com.fjhx.utils.FlowConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 合同审批
 */
@Service
public class ContractFlowService {

    @Autowired
    private ContractService contractService;

    @Autowired
    private ContractDetailsService contractDetailsService;


    private final FlowConstructor flowConstructor = FlowConstructor.init(new ExampleAbstract() {

        @Override
        public String getCode() {
            return "js_contract";
        }

        @Override
        public void end() {
            Contract contract = getCacheData(Contract.class);
            contract.setStatus(ContractStatusEnum.HAVE_IN_HAND.getType());
            contract.setFlowStatus(FlowStatusEnum.ADOPT.getType());
            contractService.updateById(contract);
        }

        @Override
        public void adopt() {
            Contract contract = getCacheData(Contract.class);
            contract.setFlowStatus(FlowStatusEnum.UNDER_WAY.getType());
            contractService.updateById(contract);
        }

        @Override
        public void reject() {
            Contract contract = getCacheData(Contract.class);
            contract.setFlowStatus(FlowStatusEnum.REJECT.getType());
            contractService.updateById(contract);
        }

    });


    @Transactional(rollbackFor = Exception.class)
    public void create(ContractVo contractVo) {

        Long id = contractVo.getId();
        Long supplierId = contractVo.getSupplierId();
        BigDecimal price = contractVo.getPrice();
        BigDecimal quantity = contractVo.getQuantity();

        Assert.notEmpty(id, "合同id不能为空");
        Assert.notEmpty(supplierId, "供应商id不能为空");
        Assert.notEmpty(contractVo.getAccountDate(), "账期不能为空");
        Assert.notEmpty(contractVo.getSignTime(), "签约日期不能为空");
        Assert.notEmpty(contractVo.getDeliverTime(), "交付日期不能为空");
        Assert.notEmpty(price, "单价不能为空");
        Assert.notEmpty(quantity, "采购数量不能为空");

        BigDecimal amountMoney = price.multiply(quantity);

        contractVo.setAmountMoney(amountMoney);
        contractVo.setAdjustAmount(amountMoney);
        contractVo.setStatus(ContractStatusEnum.UNDER_APPROVAL.getType());
        contractVo.setFlowStatus(FlowStatusEnum.UNDER_WAY.getType());

        contractService.updateById(contractVo);

        contractDetailsService.update(Wrappers.<ContractDetails>lambdaUpdate()
                .eq(ContractDetails::getContractId, id)
                .set(ContractDetails::getPrice, price)
                .set(ContractDetails::getSupplierId, supplierId));

        flowConstructor.create(id, "合同审批", contractVo.getRemark(), contractVo);
    }

    @Transactional(rollbackFor = Exception.class)
    public void jump(JumpVo jumpVo) {
        flowConstructor.jump(jumpVo);
    }


}
