package com.jwipc.finance.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.api.ShareCallBackLogic;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.constant.ShareStatus;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.log.dto.ShareCallBackDTO;
import com.jwipc.constant.ShareProcessType;
import com.jwipc.finance.entity.PaymentCodApply;
import com.jwipc.finance.logic.PaymentCodApplyLogic;
import com.jwipc.finance.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 功能描述:
 *
 * @Author: 邓京术
 * @Date: 2025/12/05 0021 16:18
 */
@RequiredArgsConstructor
@Service("PaymentCodShareCallBack")
@Slf4j
public class PaymentCodShareCallBack implements ShareCallBackLogic {

    private final IPaymentCodApplyService paymentCodApplyService;
    private final PaymentCodApplyLogic paymentCodApplyLogic;


    @Override
    public ApiResult processor(String workflowId, ShareStatus status, String remark, String processCode, String data1,
                               String jsonData, String data, String data2)
    {

        PaymentCodApply paymentCodApply = paymentCodApplyService.getOne(new LambdaQueryWrapper<PaymentCodApply>().eq(PaymentCodApply::getOaProcessId, workflowId).last(" limit 1"));
        if (ObjectUtil.isNull(paymentCodApply)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到对应的付款申请单").build();
        }
        ShareCallBackDTO.ItData itData = JSONObject.parseObject(data2, ShareCallBackDTO.ItData.class);
        if (status.getStatus()==1){
            paymentCodApply.setStatus(3);
        } else if (status.getStatus()==2) {
            paymentCodApply.setStatus(4);
        } else if (status.getStatus()==3) {
            paymentCodApply.setStatus(6);
            //废弃的时候，等同删除操作，需要更新付款计划本月已请款金额，减少
            paymentCodApplyLogic.initPaymentPlanSummary(paymentCodApply,false);
        } else if (status.getStatus()==4) {
            paymentCodApply.setStatus(7);
        } else if (status.getStatus()==5) {
            paymentCodApply.setStatus(8);
            paymentCodApply.setOaAleradyPayMoney(new BigDecimal(itData.getPaymentAmount()));
            //oa审批同意，需要更新付款计划本月已付款金额数。
            paymentCodApplyLogic.initPaymentPlanSummary(paymentCodApply, 4);
        } else if (status.getStatus()==6) {
            paymentCodApply.setStatus(5);
            paymentCodApply.setOaAleradyPayMoney(new BigDecimal(itData.getPaymentAmount()));
            //oa审批同意，需要更新付款计划本月已付款金额数。
            paymentCodApplyLogic.initPaymentPlanSummary(paymentCodApply, 4);
        }else if (status.getStatus()==7) {
            paymentCodApply.setStatus(9);
        } else if (status.getStatus()==8) {
            paymentCodApply.setStatus(10);
        }
        paymentCodApply.setOaProcessMessage(remark);
        paymentCodApply.setFreezeStatus(itData.getDjzt());
        paymentCodApplyService.updateById(paymentCodApply);
        log.info("{}", "PaymentShareCallBack");
        log.info("{}", workflowId);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

    @Override
    public ShareProcessType getProcessType() {
        return ShareProcessType.CONTRACT_PAYABLE;
    }

}
