package com.fjhx.purchase.flow;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fjhx.account.entity.account.enums.PaymentStatusEnum;
import com.fjhx.account.entity.account.enums.PaymentTypeEnum;
import com.fjhx.account.entity.account.po.AccountPayment;
import com.fjhx.account.service.account.AccountPaymentService;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.purchase.entity.pay.enums.PayStatusEnum;
import com.fjhx.purchase.entity.pay.po.Pay;
import com.fjhx.purchase.entity.pay.po.PayDetail;
import com.fjhx.purchase.service.pay.PayDetailService;
import com.fjhx.purchase.service.pay.PayService;
import com.obs.services.internal.ServiceException;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 采购付款流程
 * @Author:caozj
 * @DATE:2023/4/3 17:38
 */
@DS(SourceConstant.PURCHASE)
@Component
public class PayFlow extends FlowDelegate {

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


    /**
     * 发起流程
     * @param flowId 流程ID
     * @param submitData 采购付款数据
     * @return
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {
        PayService payService = SpringUtil.getBean(PayService.class);
        PayDetailService payDetailService = SpringUtil.getBean(PayDetailService.class);
        Pay pay = submitData.toJavaObject(Pay.class);
        pay.setStatus(PayStatusEnum.UNDER_REVIEW.getKey());
        payService.save(pay);
        List<PayDetail> payDetailList = pay.getPayDetailList();
        if(CollectionUtils.isNotEmpty(payDetailList)){
            for(PayDetail s : payDetailList){
                s.setPayId(pay.getId());
            }
            payDetailService.saveBatch(payDetailList);
        }
        ObsFileUtil.saveFile(pay.getFileList(),pay.getId());
        return pay.getId();
    }

    /**
     * 结束流程
     * @param flowId 流程ID
     * @param businessId 业务ID
     * @param submitData 数据
     */
    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        PayService payService = SpringUtil.getBean(PayService.class);
        AccountPaymentService accountPaymentService = SpringUtil.getBean(AccountPaymentService.class);
        //通过业务ID查询申购数据
        Pay pay = payService.getById(businessId);
        if(ObjectUtils.isEmpty(pay)){
            throw new ServiceException("采购付款单不存在");
        }
        //修改采购状态为审批通过
        pay.setStatus(PayStatusEnum.PASS.getKey());
        pay.setApprovedDate(new Date());
        payService.updateById(pay);
        //添加一条付款流水
        AccountPayment payment = new AccountPayment();
        payment.setAccountManagementId(pay.getAccountManagementId());
        payment.setBusinessId(pay.getId());
        payment.setCurrency(pay.getCurrency());
        payment.setPaymentRemark(pay.getRemark());
        payment.setType(PaymentTypeEnum.REJECT.getKey());
        payment.setStatus(PaymentStatusEnum.UNDER_REVIEW.getKey());
        payment.setAmount(pay.getAmount());
        payment.setIncomeAmount(pay.getAmount());
        payment.setName(pay.getName());
        payment.setBusinessManagementId(pay.getAccountManagementId());
        payment.setOpeningBank(pay.getOpeningBank());
        payment.setBusinessCurrency(pay.getCurrency());
        payment.setAccountOpening(pay.getAccountOpening());
        payment.setInterbankNumber(pay.getInterbankNumber());
        payment.setPaymentMethod(pay.getPayType()==null?null:pay.getPayType().toString());
        accountPaymentService.save(payment);
    }
}
