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.constant.ShareProcessType;
import com.jwipc.finance.entity.PaymentAdvanceApply;
import com.jwipc.finance.entity.PaymentChange;
import com.jwipc.finance.service.*;
import com.jwipc.log.dto.ShareCallBackDTO;
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("PaymentAdvanceShareCallBack")
@Slf4j
public class PaymentAdvanceShareCallBack implements ShareCallBackLogic {

    private final IPaymentAdvanceApplyService paymentAdvanceApplyService;
    private final IPaymentChangeService paymentChangeService;

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

        PaymentAdvanceApply paymentAdvanceApply = paymentAdvanceApplyService.getOne(new LambdaQueryWrapper<PaymentAdvanceApply>().eq(PaymentAdvanceApply::getOaProcessId, workflowId).last(" limit 1"));
        if (ObjectUtil.isNull(paymentAdvanceApply)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到对应的预付款申请单").build();
        }
        ShareCallBackDTO.ItData itData = JSONObject.parseObject(data2, ShareCallBackDTO.ItData.class);
        if (status.getStatus()==1){
            paymentAdvanceApply.setStatus(3);
        } else if (status.getStatus()==2) {
            paymentAdvanceApply.setStatus(4);
        } else if (status.getStatus()==3) {
            paymentAdvanceApply.setStatus(6);
            //废弃的时候，等同删除操作，需要更新付款计划本月已请款金额，减少
            paymentAdvanceApplyService.initPaymentPlanSummary(paymentAdvanceApply,false);
        } else if (status.getStatus()==4) {
            paymentAdvanceApply.setStatus(7);
        } else if (status.getStatus()==5) {
            paymentAdvanceApply.setStatus(8);
            paymentAdvanceApply.setOaAleradyPayMoney(new BigDecimal(itData.getPaymentAmount()));
            //oa审批同意，需要更新付款计划本月已付款金额数。
            paymentAdvanceApplyService.initPaymentPlanSummary(paymentAdvanceApply, 4);
        } else if (status.getStatus()==6) {
            paymentAdvanceApply.setStatus(5);
            paymentAdvanceApply.setOaAleradyPayMoney(new BigDecimal(itData.getPaymentAmount()));
            //oa审批同意，需要更新付款计划本月已付款金额数。
            paymentAdvanceApplyService.initPaymentPlanSummary(paymentAdvanceApply, 4);
            //查询是否有变更单
            PaymentChange paymentChange = paymentChangeService.getOne(new LambdaQueryWrapper<PaymentChange>()
                    .eq(PaymentChange::getAppNo, paymentAdvanceApply.getApApplyNo()));
            if (ObjectUtil.isNotNull(paymentChange)){
                paymentAdvanceApply.setPaymentCompanyName(paymentChange.getPaymentCompanyName());
                paymentAdvanceApply.setSupBankName(paymentChange.getSupBankName());
                paymentAdvanceApply.setSupBankAccount(paymentChange.getSupBankAccount());
                paymentAdvanceApply.setSupBankCode(paymentChange.getSupBankCode());
                paymentAdvanceApply.setBankInterCode(paymentChange.getBankInterCode());
                paymentAdvanceApply.setBankAddress(paymentChange.getBankAddress());
            }
        }else if (status.getStatus()==7) {
            paymentAdvanceApply.setStatus(9);
        } else if (status.getStatus()==8) {
            paymentAdvanceApply.setStatus(10);
        }
        paymentAdvanceApply.setOaAuditMessage(remark);
        paymentAdvanceApply.setFreezeStatus(itData.getDjzt());
        paymentAdvanceApplyService.updateById(paymentAdvanceApply);
        log.info("{}", "PaymentAdvanceShareCallBack");
        log.info("{}", workflowId);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

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

}
