package cn.superdesk.uniorder.service.invoice.customer.state.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerInvoiceProgressingStatusEnum;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerMakeInvoiceStatusEnum;
import cn.superdesk.uniorder.common.enums.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceResponseCodeEnum;
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.executor.scheduler.kafka._thirdpart.qchjc.QchjcStatementKafkaEventPublisher;
import cn.superdesk.uniorder.executor.scheduler.kafka._thirdpart.qchjc.dto.accounting.customer.QchjcCustomerStatementEvent;
import cn.superdesk.uniorder.executor.scheduler.kafka._thirdpart.qchjc.dto.accounting.customer.QchjcCustomerStatementEventSingleStructVo;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerService;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingOffLineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 线下开票
 * InvoiceTypeMoudelXModeEnum
 * @Author: linjinyu
 * @CreateDate: 2022/4/26 10:12 上午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/4/26 10:12 上午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingOffLineServiceImpl implements InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingOffLineService {


    @Autowired
    private InvoiceApplyCustomerService invoiceApplyCustomerService;

    /**************************************** superdesk-uniorder-executor start ****************************************/

    @Autowired
    private QchjcStatementKafkaEventPublisher qchjcStatementEventPublisher;

    /**************************************** superdesk-uniorder-executor   end ****************************************/

    /**
     * op确认开票(电子普通发票...)(线下开票)
     *
     * @param invoiceApplyCustomerEntity
     * @param loginSysUser
     * @return
     * @link opOngoingByStateMachine 分发
     */
    @Override
    public SysNotify submitAndRecord(InvoiceApply invoiceApplyCustomerEntity, LoginSysUser loginSysUser) {

        DateTime now = DateUtil.date();

        InvoiceApply forUpdate = new InvoiceApply();
        forUpdate.setId(invoiceApplyCustomerEntity.getId());

        // TODO 更新状态逻辑...

        forUpdate.setInvoiceStatus(InvoiceApplyCustomerInvoiceProgressingStatusEnum.待开票.getKey());
        forUpdate.setInvoiceProgressingUserId(loginSysUser.getSysUserId());
        forUpdate.setInvoiceProgressingTime(now);

        forUpdate.setMakeInvoiceStatus(InvoiceApplyCustomerMakeInvoiceStatusEnum.开票中.getKey());
        forUpdate.setMakeInvoiceUserId(loginSysUser.getSysUserId());
        forUpdate.setMakeInvoiceTime(now);

        forUpdate.setUpdateUserId(loginSysUser.getSysUserId());
        forUpdate.setUpdateTime(now);

        invoiceApplyCustomerService.updateInvoiceApplyByPrimaryKeySelective(forUpdate);

        // 触发事件点;集采账单状态推送 #此处只是触点，触点背后会有逻辑判定要不要调用~集采的接口 特此说明# at 20220616
        // 1.来源于“企业商城”的开票申请在**发起开票成功时**：若该对账单的申请状态为“全部申请”**&**所有有效的开票申请的申请状态均为“待回款”时
        // 2.来源于“企业商城”的对账单在**“积分结算-发票管理-【运营寄票】”成功时**：若该对账单的申请状态为“全部申请”**&**所有有效的开票申请的申请状态均为“待回款”时；
        qchjcStatementEventPublisher.customerStatementEventMakeInvoiceStatusSync(
                QchjcCustomerStatementEvent
                        .builder()
                        .msg("积分结算v2.3-发票申请管理-op申请开票-op确认开票(发票种类::任意)(开票方式::线下开票)")

                        .singleStructVo(
                                QchjcCustomerStatementEventSingleStructVo
                                        .builder()

                                        .accountingOrderId(0L)
                                        .accountingOrderSn("")

                                        .invoiceApplyId(invoiceApplyCustomerEntity.getId())
                                        .invoiceApplySn(invoiceApplyCustomerEntity.getInvoiceApplySn())

                                        .build()
                        )

                        .loginSysUser(JSONUtil.toJsonStr(loginSysUser))
                        .touchTime(now)
                        .build()

        );

        return SysNotify.builder()
                .title(SzhtxxElectronicInvoiceResponseCodeEnum.SUCCESS.getReturnCode())
                .content("成功")
                .build();
    }
}
