package com.jwipc.finance.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
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.contract.client.ContractPaymentApplyClient;
import com.jwipc.finance.entity.PaymentAdvanceApply;
import com.jwipc.finance.entity.PaymentChange;
import com.jwipc.finance.entity.PaymentCodApply;
import com.jwipc.finance.logic.PaymentCodApplyLogic;
import com.jwipc.finance.service.IPaymentAdvanceApplyService;
import com.jwipc.finance.service.IPaymentChangeService;
import com.jwipc.finance.service.IPaymentCodApplyService;
import com.jwipc.log.dto.ShareCallBackDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

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

    private final IPaymentChangeService paymentChangeService;
    private final IPaymentCodApplyService paymentCodApplyService;
    private final IPaymentAdvanceApplyService paymentAdvanceApplyService;
    private final ContractPaymentApplyClient contractPaymentApplyClient;


    @Override
    public ApiResult processor(String workflowId, ShareStatus status, String remark, String processCode, String data1,
                               String jsonData, String data, String data2)
    {
        if (StrUtil.isBlank(processCode)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入变更流程号").build();
        }

        PaymentChange paymentChange = paymentChangeService.getOne(new LambdaQueryWrapper<PaymentChange>().eq(PaymentChange::getShareNo, processCode).last(" limit 1"));
        if (ObjectUtil.isNull(paymentChange)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到对应的付款变更单").build();
        }
        ShareCallBackDTO.ItData itData = JSONObject.parseObject(data2, ShareCallBackDTO.ItData.class);
        if (ObjectUtil.isNull(itData.getCauditState())){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("请传入变更单审批状态").build();
        }
        Integer aa = itData.getCauditState();
        paymentChange.setChangeStatus(aa);
        if (paymentChange.getBusinessType()==0) {
            PaymentCodApply paymentCodApply = paymentCodApplyService.getOne(new LambdaQueryWrapper<PaymentCodApply>().eq(PaymentCodApply::getCpApplyNo, paymentChange.getAppNo()));
            paymentCodApply.setChangeStatus(aa);
            if (aa == 1) {
                paymentCodApply.setStatus(7);
                paymentCodApply.setPaymentCompanyName(paymentChange.getPaymentCompanyName());
                paymentCodApply.setSupBankName(paymentChange.getSupBankName());
                paymentCodApply.setSupBankAccount(paymentChange.getSupBankAccount());
                paymentCodApply.setSupBankCode(paymentChange.getSupBankCode());
                paymentCodApply.setBankCode(paymentChange.getSupBankCode());
                paymentCodApply.setBankInterCode(paymentChange.getBankInterCode());
                paymentCodApply.setBankAddress(paymentChange.getBankAddress());
            }
            paymentCodApplyService.updateById(paymentCodApply);
        //合同付款变更回调
        } else if (paymentChange.getBusinessType()==3) {
            Map updateMap = Maps.newHashMap();
            updateMap.put("changeStatus", aa);
            updateMap.put("appNo",paymentChange.getAppNo());
            if (aa==1){
                updateMap.put("status",7);
                updateMap.put("paymentCompanyName",paymentChange.getPaymentCompanyName());
                updateMap.put("supBankName",(paymentChange.getSupBankName()));
                updateMap.put("supBankAccount",(paymentChange.getSupBankAccount()));
                updateMap.put("supBankCode",(paymentChange.getSupBankCode()));
                updateMap.put("bankCode",(paymentChange.getSupBankCode()));
                updateMap.put("bankInterCode",(paymentChange.getBankInterCode()));
                updateMap.put("bankAddress",(paymentChange.getBankAddress()));
            }
            contractPaymentApplyClient.updateContractPaymentApply(updateMap);
        }else {
            PaymentAdvanceApply paymentAdvanceApply = paymentAdvanceApplyService.getOne(new LambdaQueryWrapper<PaymentAdvanceApply>().eq(PaymentAdvanceApply::getApApplyNo, paymentChange.getAppNo()));
            paymentAdvanceApply.setChangeStatus(aa);
            if (aa==1){
                paymentAdvanceApply.setStatus(7);
                paymentAdvanceApply.setPaymentCompanyName(paymentChange.getPaymentCompanyName());
                paymentAdvanceApply.setSupBankName(paymentChange.getSupBankName());
                paymentAdvanceApply.setSupBankAccount(paymentChange.getSupBankAccount());
                paymentAdvanceApply.setSupBankCode(paymentChange.getSupBankCode());
                paymentAdvanceApply.setBankCode(paymentChange.getSupBankCode());
                paymentAdvanceApply.setBankInterCode(paymentChange.getBankInterCode());
                paymentAdvanceApply.setBankAddress(paymentChange.getBankAddress());
            }
            paymentAdvanceApplyService.updateById(paymentAdvanceApply);
        }
        paymentChangeService.updateById(paymentChange);
        log.info("{}", "PaymentShareCallBack");
        log.info("{}", workflowId);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

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

}
