package cn.superdesk.uniorder.executor.scheduler.kafka._inner.invoice.customer;


import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.executor.scheduler.kafka._inner._constants.InnerTopicChannelConstants;
import cn.superdesk.uniorder.executor.scheduler.kafka._inner._constants.enums.InvoiceApplyCustomerKafkaEventEnum;
import cn.superdesk.uniorder.executor.scheduler.kafka._inner.invoice.customer.biz.InvoiceApplyCustomerKafkaEventHandlerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
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/23 12:17 下午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/5/23 12:17 下午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@RequiredArgsConstructor
@Slf4j
@Component
public class InvoiceApplyCustomerKafkaEventHandler {

    private final InvoiceApplyCustomerKafkaEventHandlerService invoiceApplyCustomerKafkaEventHandlerService;

    @KafkaListener(topics = {InnerTopicChannelConstants.TOPIC_EVENT_CHANNEL_INNER_INVOICE_APPLY_CUSTOMER_SERVICE})
    public void listenInvoiceApplyCustomer(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 (InvoiceApplyCustomerKafkaEventEnum.get(messageKey)) {
                    case 基于发票单触发_检验与更新对账单字段开票申请状态:
                        invoiceApplyCustomerKafkaEventHandlerService.checkAndUpdateAccountingOrderFieldInvoiceStatusEventHandle(messageKey, messageBody, now);
                        break;

                    default:
                        break;
                }
            }
        }
    }
}
