package com.jwipc.contract.logic.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.ContractDelivery;
import com.jwipc.contract.entity.ContractPaymentApply;
import com.jwipc.contract.entity.ContractReturnOrder;
import com.jwipc.contract.service.IContractDeliveryService;
import com.jwipc.contract.service.IContractPaymentApplyService;
import com.jwipc.contract.service.IContractReturnOrderService;
import com.jwipc.inquiry.client.InquiryClient;
import com.jwipc.supplier.client.TenderClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 301：收货单资产化记账状态反写
 */
@RequiredArgsConstructor
@Service("ContractReceiptOrReturnCallBack")
@Slf4j
public class ContractReceiptOrReturnCallBackImpl implements ShareCallBackLogic {
    @Autowired
    IContractDeliveryService contractDeliveryService;
    @Autowired
    IContractReturnOrderService contractReturnOrderService;

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

        //根据扩展字段区分是送货单还是推货单
        JSONObject jsonObject = JSONObject.parseObject(data2);
        JSONObject extension = jsonObject.getJSONObject("extension");
        if("01".equals(extension.get("extension1"))){ //收货单
            ContractDelivery delivery = contractDeliveryService.getOne(new LambdaQueryWrapper<ContractDelivery>().eq(ContractDelivery::getSyncGxId, workflowId).eq(ContractDelivery::getDeleteFlag, 0));
            if (Objects.isNull(delivery)) {
                return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配的收货单号!"+processCode).build();
            }
            contractDeliveryService.update( new LambdaUpdateWrapper<ContractDelivery>().set(ContractDelivery::getDeliveryStatus,"3").set(ContractDelivery::getSyncState,0).eq(ContractDelivery::getId,delivery.getId()));
        }
        if("02".equals(extension.get("extension1"))){ //退货单
            ContractReturnOrder returnOrder = contractReturnOrderService.getOne(new LambdaQueryWrapper<ContractReturnOrder>().eq(ContractReturnOrder::getSyncGxId, workflowId).eq(ContractReturnOrder::getDeleteFlag, 0));
            if (Objects.isNull(returnOrder)) {
                return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配的退货单号!"+processCode).build();
            }
            contractReturnOrderService.update( new LambdaUpdateWrapper<ContractReturnOrder>().set(ContractReturnOrder::getDeliveryStatus,5).set(ContractReturnOrder::getSyncState,0).eq(ContractReturnOrder::getId,returnOrder.getId()));

        }
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

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

}
