package cn.superdesk.uniorder.mq.consumer.superdeskuniorder;

import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.invoiced.InvoiceInvoicedDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.invoiced.InvoiceInvoicedFieldIsOverdueDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.scheduler.SysQuartzScheduleTaskStatementOfAccountDubboConsumerService;
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.Optional;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/8/11 下午2:37
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/8/11 下午2:37
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class InnerTaskConsumerListener {

	@Autowired
	private InvoiceInvoicedDubboConsumerService               invoiceInvoicedDubboConsumerService;
	@Autowired
	private InvoiceInvoicedFieldIsOverdueDubboConsumerService invoiceInvoicedFieldIsOverdueDubboConsumerService;

	@Autowired
	private SysQuartzScheduleTaskStatementOfAccountDubboConsumerService sysQuartzScheduleTaskStatementOfAccountDubboConsumerService;

	/**
	 * 应收账款 逾期
	 *
	 * @param record
	 */
	@KafkaListener(topics = "TASK_INVOICE_INVOICEDS_OVERDUE")
	public void listenInvoiceInvoicedsOverdueTask(ConsumerRecord<String, String> 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))) {

				if ("invoiceInvoicedsOverdueTask".equals(String.valueOf(record.key()))) {
					invoiceInvoicedFieldIsOverdueDubboConsumerService.kafkaInvoiceInvoicedsOverdueTaskReceive(String.valueOf(message));
				}
			}
		}
	}

	/**
	 * 对账单
	 *
	 * @param record
	 */
	@KafkaListener(topics = "TASK_STATEMENT_OF_ACCOUNT_DIM_ORDER_TASK")
	public void listenStatementOfAccountDimOrderTask(ConsumerRecord<String, String> 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))) {

				if ("statementOfAccountDimOrderTask".equals(String.valueOf(record.key()))) {
					sysQuartzScheduleTaskStatementOfAccountDubboConsumerService.kafkaStatementOfAccountDimOrderTaskReceive(String.valueOf(message));
				}
			}
		}
	}


	//	private synchronized void runTask(SysQuartzScheduleTaskStatementOfAccount entity) {
//
//		log.info("\nStatementOfAccountDimOrderTask 二层执行\nrunTask \n{}", JsonUtils.toJson(entity));
//
//		sysQuartzScheduleTaskStatementOfAccountDubboConsumerService.runTaskSysQuartzScheduleTaskStatementOfAccount(JwtUtil.FORGED_TOKEN, entity);
//
//		log.info("StatementOfAccountDimOrderTask 任务完成");
//	}
}
