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.entity.refund.po.Refund;
import com.fjhx.purchase.entity.refund.po.RefundDetail;
import com.fjhx.purchase.service.pay.PayDetailService;
import com.fjhx.purchase.service.pay.PayService;
import com.fjhx.purchase.service.refund.RefundDetailService;
import com.fjhx.purchase.service.refund.RefundService;
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 RefundFlow extends FlowDelegate {

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


    /**
     * 发起流程
     * @param flowId 流程ID
     * @param submitData 数据
     * @return
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {
        RefundService refundService = SpringUtil.getBean(RefundService.class);
        RefundDetailService refundDetailService = SpringUtil.getBean(RefundDetailService.class);
        Refund refund = submitData.toJavaObject(Refund.class);
        refund.setStatus(PayStatusEnum.UNDER_REVIEW.getKey());
        refundService.save(refund);
        List<RefundDetail> refundDetailList = refund.getRefundDetailList();
        if(CollectionUtils.isNotEmpty(refundDetailList)){
            for(RefundDetail s : refundDetailList){
                s.setRefundId(refund.getId());
            }
            refundDetailService.saveBatch(refundDetailList);
        }
        return refund.getId();
    }

    /**
     * 结束流程
     * @param flowId 流程ID
     * @param businessId 业务ID
     * @param submitData 数据
     */
    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        RefundService refundService = SpringUtil.getBean(RefundService.class);
        //通过业务ID查询申购数据
        Refund refund = refundService.getById(businessId);
        if(ObjectUtils.isEmpty(refund)){
            throw new ServiceException("退款单不存在");
        }
        //修改采购状态为审批通过
        refund.setStatus(PayStatusEnum.PASS.getKey());
        refund.setApprovedDate(new Date());
        refundService.updateById(refund);
    }
}
