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.common.constant.SourceConstant;
import com.fjhx.common.enums.FlowStatusEnum1;
import com.fjhx.common.utils.Assert;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.flow.enums.FlowStatusEnum;
import com.fjhx.purchase.entity.pay.enums.PayStatusEnum;
import com.fjhx.purchase.entity.refund.po.Refund;
import com.fjhx.purchase.entity.refund.po.RefundDetail;
import com.fjhx.purchase.service.refund.RefundDetailService;
import com.fjhx.purchase.service.refund.RefundService;
import com.ruoyi.common.core.domain.BasePo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 {

    @Autowired
    private RefundService refundService;
    @Autowired
    private RefundDetailService refundDetailService;

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


    /**
     * 发起流程
     *
     * @param flowId     流程ID
     * @param submitData 数据
     * @return
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {
        Refund refund = submitData.toJavaObject(Refund.class);
        refund = commStart(refund, 0);
        return refund.getId();
    }

    /**
     * 开始公共代码抽取
     *
     * @param opType 操作类型 0直接发起 1重新发起
     */
    private Refund commStart(Refund refund, Integer opType) {
        if (opType == 1) {
            Assert.notEmpty(refund.getId(), "退款id不能为空");
        }
        refund.setStatus(PayStatusEnum.UNDER_REVIEW.getKey());
        refundService.saveOrUpdate(refund);
        List<RefundDetail> refundDetailList = refund.getRefundDetailList();
        if (CollectionUtils.isNotEmpty(refundDetailList)) {
            if (opType == 1) {
                //先删除被删除的产品
                refundDetailService.editLinked(refundDetailList, RefundDetail::getRefundId, refund.getId());
            }
            for (RefundDetail s : refundDetailList) {
                s.setRefundId(refund.getId());
            }
            refundDetailService.saveOrUpdateBatch(refundDetailList);
        }
        return refund;
    }

    /**
     * 结束流程
     *
     * @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);
    }

    @Override
    public void relaunch(Long flowId, Long businessId, FlowStatusEnum flowStatus, JSONObject submitData) {
        super.relaunch(flowId, businessId, flowStatus, submitData);
        Refund refund = submitData.toJavaObject(Refund.class);
        commStart(refund, 1);
    }

    @Override
    public void reject(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
        super.reject(flowId, businessId, flowStatus);
        refundService.update(q -> q
                .eq(Refund::getId, businessId)
                .set(Refund::getStatus, FlowStatusEnum1.REJECT.getKey())
                .set(BasePo::getUpdateTime, new Date())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
        );
    }

    @Override
    public void cancellation(Long flowId, Long businessId, FlowStatusEnum flowStatus) {
        super.cancellation(flowId, businessId, flowStatus);
        refundService.update(q -> q
                .eq(Refund::getId, businessId)
                .set(Refund::getStatus, FlowStatusEnum1.CANCELLATION.getKey())
                .set(BasePo::getUpdateTime, new Date())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
        );
    }
}
