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

import cn.hutool.core.date.DateUtil;
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.accounting.customer.biz.QchjcCustomerStatementHandleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Optional;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2022/5/10 11:56 上午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/5/10 11:56 上午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class QchjcCustomerStatementHandler {

    @Autowired
    private QchjcCustomerStatementHandleService qchjcCustomerStatementHandleService;


    @KafkaListener(topics = {ThirdpartQchjcTopicChannelConstants.TOPIC_EVENT_CHANNEL_QCHJC_CUSTOMER_STATEMENT})
    public void listenQchjcCustomerStatement(ConsumerRecord<?, ?> record) {

        Optional<?> kafkaMessage = Optional.ofNullable(record.value());

        if (kafkaMessage.isPresent()) {

            Object message = kafkaMessage.get();

            if (!StringUtils.isEmpty(String.valueOf(record.key()))
                    && !StringUtils.isEmpty(String.valueOf(message))) {

                String messageKey  = String.valueOf(record.key());
                String messageBody = String.valueOf(message);
                Date   now         = DateUtil.date();

                switch (QchjcCustomerStatementEventEnum.get(messageKey)) {
                    case 开票状态同步:
                        qchjcCustomerStatementHandleService.makeInvoiceStatusSyncHandle(messageKey, messageBody, now);
                        break;
                    case 收款状态同步:
                        qchjcCustomerStatementHandleService.receivePaymentStatusSyncHandle(messageKey, messageBody, now);
                        break;
                    case 账单作废同步:
                        qchjcCustomerStatementHandleService.statementCancelSyncHandle(messageKey, messageBody, now);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
