package cn.superdesk.uniorder.executor.scheduler.kafka._thirdpart.qchjc;

import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.executor.scheduler.kafka._thirdpart.qchjc._constants.ThirdpartQchjcTopicChannelConstants;
import cn.superdesk.uniorder.executor.scheduler.kafka._thirdpart.qchjc._constants.enums.QchjcCustomerStatementEventEnum;
import cn.superdesk.uniorder.executor.scheduler.kafka._thirdpart.qchjc._constants.enums.QchjcSupplierStatementEventEnum;
import cn.superdesk.uniorder.executor.scheduler.kafka._thirdpart.qchjc.dto.accounting.customer.QchjcCustomerStatementEvent;
import cn.superdesk.uniorder.executor.scheduler.kafka._thirdpart.qchjc.dto.accounting.supplier.QchjcSupplierStatementEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @Description: 集采账单状态推送 QchjcStatementEventPublisher
 * @Author: linjinyu
 * @CreateDate: 2022/5/10 2:31 下午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/5/10 2:31 下午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class QchjcStatementKafkaEventPublisher {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;


    private void tmpLog(String topic, String messageKey, String messageBody) {

        log.info("\n\n\n\n------------- QchjcStatementEventPublisher info start -------------" +
                        "\ntopic:        {}" +
                        "\nmessage_key:  {}" +
                        "\nmessage_body: {}",
                topic,
                messageKey,
                messageBody
        );
    }

    /**************************************** 积分结算 start ****************************************/

    /**
     * 唉，大概的意思就是票好了没，要去收钱 ~
     * 触发事件点;
     * 1.来源于“企业商城”的开票申请在**发起开票成功时**：若该对账单的申请状态为“全部申请”**&**所有有效的开票申请的申请状态均为“待回款”时
     * 1.来源于“企业商城”的对账单在**“积分结算-发票管理-【运营寄票】”成功时**：若该对账单的申请状态为“全部申请”**&**所有有效的开票申请的申请状态均为“待回款”时；
     *
     * @param event
     * @return
     */
    public int customerStatementEventMakeInvoiceStatusSync(QchjcCustomerStatementEvent event) {

        String topic       = ThirdpartQchjcTopicChannelConstants.TOPIC_EVENT_CHANNEL_QCHJC_CUSTOMER_STATEMENT;
        String messageKey  = QchjcCustomerStatementEventEnum.开票状态同步.getKey();
        String messageBody = JSONUtil.toJsonStr(event);

        tmpLog(topic, messageKey, messageBody);

        kafkaTemplate.send(topic, messageKey, messageBody);

        return 1;

    }

    /**
     * 收钱后 ~
     * 触发事件点;
     * 1. 来源于“企业商城”的对账单在**“积分结算-发票管理-【回款登记】”成功时**：若该对账单的申请状态为“全部申请”**&**所有有效的开票申请的申请状态均为“已回款”时；
     *
     * @param event
     * @return
     */
    public int customerStatementEventReceivePaymentStatusSync(QchjcCustomerStatementEvent event) {

        String topic       = ThirdpartQchjcTopicChannelConstants.TOPIC_EVENT_CHANNEL_QCHJC_CUSTOMER_STATEMENT;
        String messageKey  = QchjcCustomerStatementEventEnum.收款状态同步.getKey();
        String messageBody = JSONUtil.toJsonStr(event);

        tmpLog(topic, messageKey, messageBody);

        kafkaTemplate.send(topic, messageKey, messageBody);

        return 1;
    }

    /**
     * 触发事件点;
     * 1. 来源于“企业商城”的对账单在**“积分结算-对账单-【重新对账】”成功时**；
     *
     * @param event
     * @return
     */
    public int customerStatementEventStatementCancelSync(QchjcCustomerStatementEvent event) {

        String topic       = ThirdpartQchjcTopicChannelConstants.TOPIC_EVENT_CHANNEL_QCHJC_CUSTOMER_STATEMENT;
        String messageKey  = QchjcCustomerStatementEventEnum.账单作废同步.getKey();
        String messageBody = JSONUtil.toJsonStr(event);

        tmpLog(topic, messageKey, messageBody);

        kafkaTemplate.send(topic, messageKey, messageBody);

        return 1;

    }

    /**************************************** 积分结算   end ****************************************/


    /**************************************** 供应商结算 start ****************************************/

    /**
     * 触发事件点;
     * 1. 来源于“企业商城”的对账单在**“积分结算-发票登记管理-【收票登记】”成功时**：若该对账单所有有效的开票申请的申请状态均为“待付款”时；
     *
     * @param event
     * @return
     */
    public int supplierStatementEventReceiveInvoiceStatusSync(QchjcSupplierStatementEvent event) {

        String topic       = ThirdpartQchjcTopicChannelConstants.TOPIC_EVENT_CHANNEL_QCHJC_SUPPLIER_STATEMENT;
        String messageKey  = QchjcSupplierStatementEventEnum.收票状态同步.getKey();
        String messageBody = JSONUtil.toJsonStr(event);

        tmpLog(topic, messageKey, messageBody);

        kafkaTemplate.send(topic, messageKey, messageBody);

        return 1;
    }

    /**
     * 触发事件点;
     * 1. 来源于“企业商城”的对账单在**“供应商结算-货款管理-【确认付款】”成功时**：若该对账单的所有有效的开票申请的申请状态均为“已付款”时；（“付款中”状态无需同步。）
     *
     * @param event
     * @return
     */
    public int supplierStatementEventPaymentStatusSync(QchjcSupplierStatementEvent event) {

        String topic       = ThirdpartQchjcTopicChannelConstants.TOPIC_EVENT_CHANNEL_QCHJC_SUPPLIER_STATEMENT;
        String messageKey  = QchjcSupplierStatementEventEnum.付款状态同步.getKey();
        String messageBody = JSONUtil.toJsonStr(event);

        tmpLog(topic, messageKey, messageBody);

        kafkaTemplate.send(topic, messageKey, messageBody);

        return 1;
    }

    /**
     * 触发事件点;
     * 1. 来源于“企业商城”的对账单在**“供应商结算-对账单-【重新对账】”成功时**；
     *
     * @param event
     * @return
     */
    public int supplierStatementEventStatementCancelSync(QchjcSupplierStatementEvent event) {

        String topic       = ThirdpartQchjcTopicChannelConstants.TOPIC_EVENT_CHANNEL_QCHJC_SUPPLIER_STATEMENT;
        String messageKey  = QchjcSupplierStatementEventEnum.账单作废同步.getKey();
        String messageBody = JSONUtil.toJsonStr(event);

        tmpLog(topic, messageKey, messageBody);

        kafkaTemplate.send(topic, messageKey, messageBody);

        return 1;
    }


    /**************************************** 供应商结算   end ****************************************/


}
