package cn.superdesk.uniorder.dubbo.provider.invoice.customer.state;

import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.common.singleBilling.SzhtxxInvoiceFptxxOpSingleBillingForm;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.common.singleSelect.SzhtxxInvoiceFptxxOpSingleSelectForm;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxListItemVo;
import cn.superdesk.uniorder.dubbo.facade.invoice.customer.state.DubboInvoiceApplyFieldMakeInvoiceStatusService;
import cn.superdesk.uniorder.dubbo.facade.sys.notify.DubboSysNotifyService;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice.dto.request.SingleBillingRequestBodyKpContent;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.request.SID17SingleSelect.SID17SingleSelectRequestBody;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.request.SID1SingleBilling.SID1SingleBillingRequestBody;
import cn.superdesk.uniorder.service.invoice.customer.*;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingOffLineService;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxElectronicInvoiceService;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxPaperInvoiceService;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.CompletableFuture;


/**
 * @Description: 简易状态机 星型结构
 * @Author: linjinyu
 * @CreateDate: 10/12/20 4:28 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 10/12/20 4:28 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboInvoiceApplyFieldMakeInvoiceStatusServiceImpl implements DubboInvoiceApplyFieldMakeInvoiceStatusService {

    @Autowired
    private InvoiceApplyCustomerFieldMakeInvoiceStatusService                                 invoiceApplyFieldMakeInvoiceStatusService;
    @Autowired
    private InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingOffLineService                 invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingOffLineService;
    @Autowired
    private InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxElectronicInvoiceService invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxElectronicInvoiceService;
    @Autowired
    private InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxPaperInvoiceService      invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxPaperInvoiceService;

    @Autowired
    private InvoiceApplyCustomerExportExcelService invoiceApplyCustomerExportExcelService;

    /****************************************** 引用自身 Dubbo provider start ******************************************/

    @Reference(version = "1.0.0", timeout = 10000, retries = -1)
    private DubboSysNotifyService dubboSysNotifyService;

    /****************************************** 引用自身 Dubbo provider   end ******************************************/


    /****************************************** 状态机 外部使用 类门面 start ******************************************/

    // 状态流转为 待开票(0, "待开票")
    @Override
    public SysNotify opInit(InvoiceApply entity, LoginSysUser loginSysUser) {
        return invoiceApplyFieldMakeInvoiceStatusService.opInit(entity, loginSysUser);
    }

    // 状态流转为 已作废(2, "已作废")
    @Override
    public SysNotify opBecomeInvalid(InvoiceApply entity, LoginSysUser loginSysUser) {
        return invoiceApplyFieldMakeInvoiceStatusService.opBecomeInvalid(entity, loginSysUser);
    }

    /**************************************** 电子普通发票-深圳航信电子发票 start ****************************************/

    // 状态流转为 开票中(10, "开票中")
    @Override
    public SingleBillingRequestBodyKpContent opOngoingInvoiceTypeEq1InvoiceModeEqSzhtxxElectronicInvoice(SzhtxxInvoiceFptxxOpSingleBillingForm form, InvoiceApply entity, LoginSysUser loginSysUser) {
        return invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxElectronicInvoiceService.opOngoingInvoiceTypeEq1InvoiceModeEqSzhtxxElectronicInvoice(form, entity, loginSysUser);
    }

    @Override
    public String exportOngoingInvoiceTypeEq1InvoiceModeEqSzhtxxElectronicInvoice(SzhtxxInvoiceFptxxOpSingleBillingForm form, InvoiceApply entity, LoginSysUser loginSysUser) {

        CompletableFuture.runAsync(() -> {

            try {
                // 三步走 mark by linjinyu at 20210305
                // 1. 数据
                // 2. 生成excel
                // 3. 通知
                SingleBillingRequestBodyKpContent singleBillingRequestBodyKpContent = opOngoingInvoiceTypeEq1InvoiceModeEqSzhtxxElectronicInvoice(form, entity, loginSysUser);

                SysNotify sysNotify = invoiceApplyCustomerExportExcelService.exportOngoingInvoiceTypeEq1InvoiceModeEqSzhtxxElectronicInvoice(singleBillingRequestBodyKpContent);

                // 通知
                dubboSysNotifyService.notifyExportExcelUnifiedCustomerInvoice2CreditDetail(sysNotify, loginSysUser);


            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 exportOngoingInvoiceTypeEq1InvoiceModeEqSzhtxxElectronicInvoice \n{}\n{} -> 执行 异常:{}", JSONUtil.toJsonStr(form), JSONUtil.toJsonStr(entity), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";
    }

    @Override
    public SingleBillingRequestBodyKpContent opOngoingInvoiceTypeEq1InvoiceModeEqSzhtxxElectronicInvoiceRedWrite(SzhtxxInvoiceFptxxOpSingleBillingForm form, SzhtxxElectronicInvoiceFptxxListItemVo fptxxListItemVo, InvoiceApply entity, LoginSysUser loginSysUser) throws UnifiedOrderRuntimeException {
        return invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxElectronicInvoiceService.opOngoingInvoiceTypeEq1InvoiceModeEqSzhtxxElectronicInvoiceRedWrite(form, fptxxListItemVo, entity, loginSysUser);
    }

    // 状态流转为 开票中(10, "开票中")
    @Override
    public SysNotify opOngoing(SzhtxxInvoiceFptxxOpSingleBillingForm form, InvoiceApply entity, LoginSysUser loginSysUser) {
        return invoiceApplyFieldMakeInvoiceStatusService.opOngoing(form, entity, loginSysUser);
    }

    /**************************************** 电子普通发票-深圳航信电子发票   end ****************************************/


    /**************************************** 纸质专用发票-深圳航信纸质发票 start ****************************************/

    @Override
    public SID1SingleBillingRequestBody opOngoingInvoiceTypeEq2InvoiceModeEqSzhtxxPaperInvoice(
            SzhtxxInvoiceFptxxOpSingleBillingForm form,
            InvoiceApply entity,
            LoginSysUser loginSysUser) {
        return invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxPaperInvoiceService
                .opOngoingInvoiceTypeEq2InvoiceModeEqSzhtxxPaperInvoiceRequest(
                        form,
                        entity,
                        loginSysUser
                );
    }

    @Override
    public SID17SingleSelectRequestBody opCompleteInvoiceTypeEq2InvoiceModeEqSzhtxxPaperInvoiceRequest(
            SzhtxxInvoiceFptxxOpSingleSelectForm form,
            InvoiceApply entity,
            LoginSysUser loginSysUser) {
        return invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxPaperInvoiceService
                .opCompleteInvoiceTypeEq2InvoiceModeEqSzhtxxPaperInvoiceRequest(
                        form,
                        entity,
                        loginSysUser
                );
    }

    @Override
    public SysNotify opCompleteInvoiceTypeEq2InvoiceModeEqSzhtxxPaperInvoiceResponse(
            SzhtxxInvoiceFptxxOpSingleSelectForm form,
            InvoiceApply entity,
            LoginSysUser loginSysUser) {
        return invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxPaperInvoiceService
                .opCompleteInvoiceTypeEq2InvoiceModeEqSzhtxxPaperInvoiceResponse(
                        form,
                        entity,
                        loginSysUser
                );
    }

    /**************************************** 纸质专用发票-深圳航信纸质发票   end ****************************************/


    // 状态流转为 已开票(11, "已开票")
    @Override
    public SysNotify opComplete(InvoiceApply entity, LoginSysUser loginSysUser) {
        return invoiceApplyFieldMakeInvoiceStatusService.opComplete(entity, loginSysUser);
    }

    /****************************************** 状态机 外部使用 类门面   end ******************************************/


}
