package com.fjhx.sale.flow;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.fjhx.account.entity.account.enums.PaymentStatusEnum;
import com.fjhx.account.entity.account.po.AccountPayment;
import com.fjhx.account.service.account.AccountPaymentService;
import com.fjhx.common.enums.CodingRuleEnum;
import com.fjhx.common.enums.FlowStatusEnum1;
import com.fjhx.common.service.coding.CodingRuleService;
import com.fjhx.common.utils.Assert;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.sale.entity.after.dto.AfterSalesDto;
import com.fjhx.sale.entity.after.po.AfterSales;
import com.fjhx.sale.entity.contract.po.Contract;
import com.fjhx.sale.entity.contract.po.ContractProduct;
import com.fjhx.sale.service.SaleService;
import com.fjhx.sale.service.after.AfterSalesService;
import com.fjhx.sale.service.contract.ContractProductService;
import com.fjhx.sale.service.contract.ContractService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

@Component
public class AfterSalesFlow extends FlowDelegate {

    private final AfterSalesService afterSalesService;
    private final CodingRuleService codingRuleService;
    private final ContractProductService contractProductService;
    private final ISysUserService userService;
    private final AccountPaymentService accountPaymentService;
    private final ContractService contractService;

    private final SaleService saleService;

    @Autowired
    public AfterSalesFlow(AfterSalesService afterSalesService, CodingRuleService codingRuleService, ContractProductService contractProductService, ISysUserService userService, AccountPaymentService accountPaymentService, ContractService contractService, SaleService saleService) {
        this.afterSalesService = afterSalesService;
        this.codingRuleService = codingRuleService;
        this.contractProductService = contractProductService;
        this.userService = userService;
        this.accountPaymentService = accountPaymentService;
        this.contractService = contractService;
        this.saleService = saleService;
    }

    @Override
    public String getFlowKey() {
        return "after_sales_flow";
    }

    @Override
    public Long start(Long flowId, JSONObject submitData) {
        AfterSalesDto afterSalesDto = submitData.toJavaObject(AfterSalesDto.class);

        afterSalesDto.setFlowId(flowId);

        Long contractProductId = afterSalesDto.getContractProductId();
        ContractProduct contractProduct = contractProductService.getById(contractProductId);
        Assert.notEmpty(contractProduct, "查询不到订单产品信息！");

        afterSalesDto.setCode(codingRuleService.createCode(CodingRuleEnum.AFTER_SALES.getKey(), null));

        //赋值信息
        afterSalesDto.setContractId(contractProduct.getContractId());
        afterSalesDto.setProductId(contractProduct.getProductId());

        afterSalesDto.setCompanyId(SecurityUtils.getCompanyId());

        afterSalesService.save(afterSalesDto);

        //回填数据
        submitData.put("code", afterSalesDto.getCode());

        return afterSalesDto.getId();
    }

    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        AfterSales afterSales = afterSalesService.getById(businessId);
        afterSales.setStatus(FlowStatusEnum1.PASS.getKey());


        Long contractProductId = afterSales.getContractProductId();
        ContractProduct contractProduct = contractProductService.getById(contractProductId);
        Assert.notEmpty(contractProduct, "查询不到合同产品明细信息！");

        //计算售后价格
        BigDecimal multiply = contractProduct.getPrice().multiply(afterSales.getQuantity());

        //如果售后类型=仅退款30/退货退款40。审批通过后生成打款数据
        if (ObjectUtil.equals(afterSales.getType(), "30") || ObjectUtil.equals(afterSales.getType(), "40")) {
            // 添加一条付款流水
            AccountPayment accountPayment = new AccountPayment();

            SysUser sysUser = userService.getById(afterSales.getUserId());
            if (ObjectUtil.isNotEmpty(sysUser)) {
                accountPayment.setDepartmentId(sysUser.getDeptId());
            }

            accountPayment.setBusinessId(afterSales.getId());
            accountPayment.setPaymentTime(new Date());
            accountPayment.setCurrency("CNY");
            accountPayment.setStatus(PaymentStatusEnum.UNDER_REVIEW.getKey());
            accountPayment.setType("40");//售后
            accountPayment.setPaymentRemark(afterSales.getRemark());
            accountPayment.setIncomeAmount(multiply);
            accountPayment.setName(afterSales.getAccountName());
            accountPayment.setOpeningBank(afterSales.getOpeningBank());
            accountPayment.setAccountOpening(afterSales.getAccountOpening());
            accountPayment.setDataUser(afterSales.getUserId());
            accountPayment.setAmount(BigDecimal.ZERO);
            accountPayment.setCompanyId(afterSales.getCompanyId());

            accountPaymentService.save(accountPayment);

            afterSales.setPaymentId(accountPayment.getId());
        }
        //如果售后类型=补发10/换货20。审批通过后，生成一条销售订单，与原销售订单信息基本一致：商品清单只有这一条，数量=售后数量，价格=0。默认审批通过
        if (ObjectUtil.equals(afterSales.getType(), "10") || ObjectUtil.equals(afterSales.getType(), "20")) {
            //合同信息
            Contract contract1 = contractService.getById(contractProduct.getContractId());
            Assert.notEmpty(contract1, "查询不到合同信息！");
            contract1.setId(null);
            contract1.setAmount(BigDecimal.ZERO);
            contract1.setCode(codingRuleService.createCode(CodingRuleEnum.CONTRACT.getKey(), contract1.getBuyCorporationId()));
            contractService.save(contract1);
            afterSales.setNewContractId(contract1.getId());

            //合同产品
            ContractProduct contractProduct1 = contractProductService.getById(contractProductId);
            contractProduct1.setId(null);
            contractProduct1.setContractId(contract1.getId());
            contractProduct1.setPrice(BigDecimal.ZERO);
            contractProduct1.setQuantity(afterSales.getQuantity());
            contractProductService.save(contractProduct1);

            //直接生产
            saleService.directProd(contract1.getId(), new Date(), contract1.getCompanyId());
        }

        afterSalesService.updateById(afterSales);
    }
}
