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

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.QchjcSupplierStatementEventEnum;
import cn.superdesk.uniorder.executor.scheduler.kafka._thirdpart.qchjc.accounting.supplier.biz.QchjcSupplierStatementHandleService;
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 QchjcSupplierStatementHandler {

    @Autowired
    private QchjcSupplierStatementHandleService qchjcSupplierStatementHandleService;


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

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

        if (kafkaMessage.isPresent()) {

            Object message = kafkaMessage.get();

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

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

                switch (QchjcSupplierStatementEventEnum.get(messageKey)) {
                    case 收票状态同步:
                        qchjcSupplierStatementHandleService.receiveInvoiceStatusSyncHandle(messageKey, messageBody, now);
                        break;
                    case 付款状态同步:
                        qchjcSupplierStatementHandleService.paymentStatusSyncHandle(messageKey, messageBody, now);
                        break;
                    case 账单作废同步:
                        qchjcSupplierStatementHandleService.statementCancelSyncHandle(messageKey, messageBody, now);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
