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

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedWarning;
import cn.superdesk.uniorder.dto.vo.unified.WarningOrderInvoiceVo;
import cn.superdesk.uniorder.dubbo.consumer.unified.UnifiedWarningDubboConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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


/**
 * @Description: 发票逾期
 * @Author: linjinyu
 * @CreateDate: 2021/9/3 下午3:41
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/9/3 下午3:41
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class StatisticsJobWarningOrderInvoiceTask {


	@Resource
	private UnifiedWarningDubboConsumerService unifiedWarningDubboConsumerService;

	public void run(String params) {


//		0 0 7,23 * * ?
//		0 0 * * * ? *
		Integer year  = DateUtil.year(DateUtil.date());
		Integer month = DateUtil.month(DateUtil.date()) + 1;
		Integer hour  = DateUtil.hour(DateUtil.date(), true);

		if (hour == 23 || hour == 17) {
			log.info("{}:>>>>>----开始执行统计......", DateUtil.now());
			runTask(year, month);
		} else {
			log.info("{}:>>>>>----未在统计时间内----<<<<< {}", DateUtil.now(), getClass());
			return;
		}
	}

	private synchronized void runTask(Integer year, Integer month) {

		/****************************************** StatisticsJobWarningOrderInvoice start ******************************************/

		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("year", year);
		queryMap.put("month", month);

		// 发票逾期
		List<WarningOrderInvoiceVo> warningOrderInvVoList = unifiedWarningDubboConsumerService.queryWarningOrderInvoice(queryMap);

		if (warningOrderInvVoList != null && warningOrderInvVoList.size() > 0) {

			for (WarningOrderInvoiceVo warningOrderInvVo : warningOrderInvVoList) {

				UnifiedWarning unifiedWarning = new UnifiedWarning();

				unifiedWarning.setCreateTime(DateUtil.date());
				unifiedWarning.setCreateUserId(1L);
				unifiedWarning.setInvNo(warningOrderInvVo.getInvoiceNumber());
				unifiedWarning.setState(2);
				unifiedWarning.setVirtId(warningOrderInvVo.getVirtId());
				unifiedWarning.setWarningType(1);

				unifiedWarningDubboConsumerService.addObj(unifiedWarning);
			}
		}
	}
}
