package com.jwipc.finance.logic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.api.OaCallBackLogic;
import com.jwipc.api.constant.OaStatus;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.constant.OaProcessType;
import com.jwipc.finance.entity.PaymentCodApply;
import com.jwipc.finance.logic.PaymentCodApplyLogic;
import com.jwipc.finance.service.IPaymentCodApplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author shenyang.ou
 * @date 2024/4/1 14:39
 * @desc 对账单付款申请审批回调
 */
@Service("PaymentCodApplyCallBack")
@RequiredArgsConstructor
public class PaymentCodApplyCallBack implements OaCallBackLogic {
    private final IPaymentCodApplyService paymentCodApplyService;
    private final PaymentCodApplyLogic paymentCodApplyLogic;
    /**
     * oa回调接口
     *
     * @param workflowId  流程id
     * @param status      状态
     * @param remark      审批意见
     * @param processCode 流程编号
     * @param data1       回传的数据1
     * @param data2       回传数据2
     * @return
     */
    @Override
    public ApiResult processor(String workflowId, OaStatus status, String remark, String processCode, String data1, String data2) {

        final List<PaymentCodApply> paymentCodApplyList = paymentCodApplyService.list(
                new LambdaQueryWrapper<PaymentCodApply>().eq(PaymentCodApply::getOaProcessCode, workflowId));
        if (Objects.isNull(paymentCodApplyList)) {
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配的流程数据!").build();
        }
        RestCode code = RestCode.SAP_SUCCESS;
        String mesg = "";
        try {
            for (PaymentCodApply paymentCodApply : paymentCodApplyList) {
                paymentCodApply.setOaProcessId(processCode);
                if (status.equals(OaStatus.REFUSE)) {
                    paymentCodApply.setStatus(4);
                } else if (status.equals(OaStatus.PASS)) {
                    paymentCodApply.setOaAleradyPayMoney(paymentCodApply.getOaBorrowMoney());
                    paymentCodApply.setStatus(5);
                    //oa审批同意，需要更新付款计划本月已付款金额数。
                    paymentCodApplyLogic.initPaymentPlanSummary(paymentCodApply, 4);
                } else if (status.equals(OaStatus.DISCARDED)) {
                    paymentCodApply.setStatus(6);
                } else if (status.equals(OaStatus.RUNNING)) {
                    paymentCodApply.setStatus(3);
                }
                paymentCodApplyService.updateById(paymentCodApply);
                if (status.equals(OaStatus.DISCARDED)) {
                    //废弃的时候，等同删除操作，需要更新付款计划本月已请款金额，减少
                    paymentCodApplyLogic.initPaymentPlanSummary(paymentCodApply,false);
                }
                //oa审批同意，需要更新付款计划本月已付款金额数。
                if (status.equals(OaStatus.RUNNING) && paymentCodApply.getPaymentWayDesc().equals("承兑汇票")) {
                    paymentCodApplyLogic.initPaymentPlanSummary(paymentCodApply, 4);
                }
            }
        } catch (Exception e) {
            code = RestCode.SAP_ERROR;
            mesg = e.getMessage();
            e.printStackTrace();
        }
        return ApiResult.builder().code(code.getCode()).message(mesg).build();
    }

    /**
     * 当前服务是属于哪个oa流程
     *
     * @return
     */
    @Override
    public OaProcessType getProcessType() {
        return OaProcessType.PAYMENT_COD_APPLY;
    }
}
