package cn.superdesk.uniorder.executor.scheduler.kafka._inner.accounting.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.AccountingScoreCustomerKafkaEventEnum;
import cn.superdesk.uniorder.executor.scheduler.kafka._inner.accounting.customer.biz.AccountingScoreCustomerKafkaEventHandlerService;
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 AccountingScoreCustomerKafkaEventHandler {

    private final AccountingScoreCustomerKafkaEventHandlerService accountingScoreCustomerKafkaEventHandlerService;

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