package com.jwipc.contract.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.contract.entity.ContractPaymentApply;
import com.jwipc.contract.service.IContractPaymentApplyService;
import com.jwipc.finance.entity.PaymentChange;
import com.jwipc.finance.service.IPaymentChangeService;
import com.jwipc.inquiry.client.InquiryClient;
import com.jwipc.log.dto.ShareCallBackDTO;
import com.jwipc.supplier.client.TenderClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 功能描述:资产合同回调
 *
 * @Author: 邓京术
 * @Date: 2025/12/05 0021 16:18
 */
@RequiredArgsConstructor
@Service("ContractPaymentShareCallBack")
@Slf4j
public class ContractPayemntShareCallBackImpl implements ShareCallBackLogic {

    @Autowired
    IContractPaymentApplyService contractPaymentApplyService;
    @Autowired
    TenderClient tenderClient;
    @Autowired
    InquiryClient inquiryClient;
    @Autowired
    private final IPaymentChangeService paymentChangeService;


    @Override
    public ApiResult processor(String workflowId, ShareStatus status, String remark,
                               String processCode, String data1, String jsonData, String data,String data2)
    {
        //0：同意 1：拒绝  2:审批中  3:废弃
        ContractPaymentApply contractPaymentApply = contractPaymentApplyService.getOne(
                new LambdaQueryWrapper<ContractPaymentApply>().eq(ContractPaymentApply::getOaProcessId, workflowId));
        if (Objects.isNull(contractPaymentApply)) {
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配流程!").build();
        }
        //审批中1
        if (status.getStatus() == ShareStatus.REFUSE.getStatus()) {
            contractPaymentApply.setStatus(3);
        }
        //驳回或拒绝2
        if (status.getStatus() == ShareStatus.RUNNING.getStatus()) {
            contractPaymentApply.setStatus(4);
        }
        //作废3
        if (status.getStatus() == ShareStatus.DISCARDED.getStatus()) {
            contractPaymentApply.setStatus(6);
        }
        //待付款4
        if (status.getStatus() == ShareStatus.WAIT_PAY.getStatus()) {
            contractPaymentApply.setStatus(7);
        }
        //部分付款5
        if (status.getStatus() == ShareStatus.PAY_PART.getStatus()) {
            contractPaymentApply.setStatus(8);
            ShareCallBackDTO.ItData itData = JSONObject.parseObject(data2, ShareCallBackDTO.ItData.class);
            contractPaymentApply.setOaAleradyPayMoney(new BigDecimal(itData.getPaymentAmount()));
        }
        ShareCallBackDTO.ItData itData = JSONObject.parseObject(data2, ShareCallBackDTO.ItData.class);
        //付款成功6
        if (status.getStatus() == ShareStatus.PAY_SUCCESS.getStatus()) {
            contractPaymentApply.setStatus(5);
            contractPaymentApply.setOaAleradyPayMoney(new BigDecimal(itData.getPaymentAmount()));
            //查询是否有变更单
            PaymentChange paymentChange = paymentChangeService.getOne(new LambdaQueryWrapper<PaymentChange>()
                    .eq(PaymentChange::getAppNo, contractPaymentApply.getApApplyNo()));
            if (ObjectUtil.isNotNull(paymentChange)){
                contractPaymentApply.setPaymentCompanyName(paymentChange.getPaymentCompanyName());
                contractPaymentApply.setSupBankName(paymentChange.getSupBankName());
                contractPaymentApply.setSupBankAccount(paymentChange.getSupBankAccount());
                contractPaymentApply.setSupBankCode(paymentChange.getSupBankCode());
                contractPaymentApply.setBankInterCode(paymentChange.getBankInterCode());
                contractPaymentApply.setBankAddress(paymentChange.getBankAddress());
            }
        }
        //付款失败7
        if (status.getStatus() == ShareStatus.PAY_FAIL.getStatus()) {
            contractPaymentApply.setStatus(9);
        }
        //终止付款8
        if (status.getStatus() == ShareStatus.PAY_END.getStatus()) {
            contractPaymentApply.setStatus(10);
        }
        contractPaymentApply.setOaPayRemark(remark);
//        contractPaymentApply.setOaProcessCode(processCode);
//        contractPaymentApply.setOaAuditMessage(remark);
        contractPaymentApply.setFreezeStatus(itData.getDjzt());
        contractPaymentApplyService.updateById(contractPaymentApply);
        log.info("{}", workflowId);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

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

}
