package cn.superdesk.uniorder.executor.scheduler.task;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.mq.kafka.producer.FakeSuperdeskUniOrderProducerHandler;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dto.param.invoice.customer.invoiced.InvoiceInvoicedQueryCriteria;
import cn.superdesk.uniorder.dto.vo.invoice.customer.invoiced.InvoiceInvoicedExcelExportVo;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.invoiced.InvoiceInvoicedDubboConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 应收账款 逾期任务
 * #触发#
 * @Author: linjinyu
 * @CreateDate: 2021/8/11 上午10:09
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/8/11 上午10:09
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class InvoiceInvoicedsOverdueTask {


	private InvoiceInvoicedsOverdueTask() {
		log.info("call InvoiceInvoicedsOverdueTask 构建方法");
	}

	@Resource
	private InvoiceInvoicedDubboConsumerService  invoiceInvoicedDubboConsumerService;
	@Resource
	private FakeSuperdeskUniOrderProducerHandler fakeSuperdeskUniOrderProducerHandler;

	@Resource
	private Snowflake snowflake;


	public void run(String params) {

		// TODO 正常是数据库配置
		InvoiceInvoicedQueryCriteria criteria = AnJsonUtils.toObject(params, InvoiceInvoicedQueryCriteria.class);

		// TODO 测试先这样写死吧
//		InvoiceInvoicedQueryCriteria criteria = new InvoiceInvoicedQueryCriteria();
//		// 假定这个企业账期 30天
////		param.getConditions().setOverdueDateSub(30);
//		// 是否作废-是否作废:0-正常;1-作废;为空-全部;
//		criteria.setIsInvalid(InvoiceInvoicedIsInvalidEnum.正常.getKey());
//		criteria.setIsBackCollectionIds(Arrays.asList(
//				InvoiceApplyBackAccountStatusEnum.未回款.getKey(),
//				InvoiceApplyBackAccountStatusEnum.部分回款.getKey()));

		log.info("InvoiceInvoicedQueryCriteria {}", JSONUtil.toJsonStr(criteria));

		/****************************************** 条件 start ******************************************/

		// 是否回款_回款状态:0-未回款;1-部分回款;2-全部回款;69-红冲发票;
		// is_back_collection
		// 回款时间 eg 2021-03-03 10:02:34
		// back_collection_time 如果是空的
		// 已回款金额 = 0
		// back_collection_amount_already
		// 待回款金额
		// back_collection_amount_wait_for
		// 是否逾期 0<=逾期天数 =IF(0<=B917,已逾期,未逾期):0-未逾期;1-已逾期;
		// is_overdue
		// 逾期天数 当前日期-开票日期-30
		// overdue_days
		// 是否作废:0-正常;1-作废
		// is_invalid
		// 软删除:0-未删除;1-已删除
		// deleted

		/****************************************** 推导 start ******************************************/

		// 更新的是
		// 是否逾期 0<=逾期天数 =IF(0<=B917,已逾期,未逾期):0-未逾期;1-已逾期;
		// is_overdue
		// 逾期天数 当前日期-开票日期-30
		// overdue_days


		log.info("pageQueryInvoiceInvoiced:\n{}", JSONUtil.toJsonStr(criteria));

		R<List<InvoiceInvoicedExcelExportVo>> result = new R<>();

		// TODO 参数合法性判定

		List<InvoiceInvoicedExcelExportVo> todoList = invoiceInvoicedDubboConsumerService.queryByCriteria(criteria);

		String batchId = snowflake.nextIdStr();

		todoList.forEach(taskParams -> {

			taskParams.setBatchId(batchId);
			fakeSuperdeskUniOrderProducerHandler.kafkaInvoiceInvoicedsOverdueTask(taskParams);
		});

	}
}
