package cn.superdesk.uniorder.openapi.service.qchjc.accounting;

import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerBackAccountStatusEnum;
import cn.superdesk.uniorder.common.exception.OpenApiException;
import cn.superdesk.uniorder.dubbo.facade.thirdpart.qchjc.accounting.DubboQchjcSupplierStatementService;
import cn.superdesk.uniorder.openapi.api._thirdpart.qchjc._dto.accounting.QchjcSupplierStatementDto;
import cn.superdesk.uniorder.openapi.api._thirdpart.qchjc._dto.accounting.QchjcSupplierStatementStatusDto;
import cn.superdesk.uniorder.openapi.api._thirdpart.qchjc._dto.order.QchjcJdOrderParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.UndeclaredThrowableException;

/**
 * @author xc
 * @description
 * @date 2022/4/1 18:32
 */
@Slf4j
@Service
public class SupplierStatementService {

    @Reference(version = "1.0.0", timeout = 300000, retries = -1)
    private DubboQchjcSupplierStatementService dubboQchjcSupplierStatementService;

    /**
     * 供应商对账单接收
     * @param dto
     */
    public void receive(QchjcSupplierStatementDto dto) {
        try {
            //保存到中间表-待入库状态
            dubboQchjcSupplierStatementService.receive(JSON.toJSONString(dto));
        }catch (UndeclaredThrowableException e) {
            Throwable cause = e.getUndeclaredThrowable().getCause();
            if (cause instanceof OpenApiException) {
                throw (OpenApiException) cause;
            }else {
                throw e;
            }
        }
    }

    /**
     * 供应商对账单状态同步 集采 -> 结算中心
     * @param dto
     */
    public void syncStatus(QchjcSupplierStatementStatusDto dto) {
        try {
            verifyParam(dto);
            dubboQchjcSupplierStatementService.syncStatus(JSON.toJSONString(dto));
        } catch (UndeclaredThrowableException e) {
            Throwable cause = e.getUndeclaredThrowable().getCause();
            if (cause instanceof OpenApiException) {
                throw (OpenApiException) cause;
            } else {
                throw e;
            }
        }
    }

    private void verifyParam(QchjcSupplierStatementStatusDto dto) {
        Integer backAccountModeRecord = dto.getBackAccountModeRecord();
        Integer backAccountStatus = dto.getBackAccountStatus();
        if (!InvoiceApplyCustomerBackAccountStatusEnum.未回款.getKey().equals(backAccountStatus)
                && !InvoiceApplyCustomerBackAccountStatusEnum.已回款.getKey().equals(backAccountStatus)) {
            throw new OpenApiException(500, "回款状态值错误");
        }
        if (0 != backAccountModeRecord && 10 != backAccountModeRecord && 11 != backAccountModeRecord) {
            throw new OpenApiException(500, "回款登记方式值错误");
        }
    }

    public void receiveJdOrder(QchjcJdOrderParam param) {
        try {
            dubboQchjcSupplierStatementService.receiveJdOrder(JSON.toJSONString(param));
        }catch (UndeclaredThrowableException e) {
            Throwable cause = e.getUndeclaredThrowable().getCause();
            if (cause instanceof OpenApiException) {
                throw (OpenApiException) cause;
            }else {
                throw e;
            }
        }
    }

    public void fix(Integer limit, String supplierAccountingOrderSn) {
        dubboQchjcSupplierStatementService.fix(limit,supplierAccountingOrderSn);
    }
}
