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.WarningOrderAmountVo;
import cn.superdesk.uniorder.dubbo.consumer.marketing.UnifiedStatisticsVirtCommonDubboConsumerService;
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 StatisticsJobWarningOrderAmountTask {

	@Resource
	private UnifiedStatisticsVirtCommonDubboConsumerService unifiedStatisticsVirtDubboConsumerService;
	@Resource
	private UnifiedWarningDubboConsumerService              unifiedWarningDubboConsumerService;

	public void run(String params) {

		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) {

		/****************************************** StatisticsJobWarningOrderAmount start ******************************************/

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


		// 信用金额
		List<WarningOrderAmountVo> warningOrderAmountVoList = unifiedStatisticsVirtDubboConsumerService.getWarningOrderAmount(queryMap);


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

			for (WarningOrderAmountVo warningOrderAmountVo : warningOrderAmountVoList) {

				UnifiedWarning unifiedWarning = new UnifiedWarning();

				unifiedWarning.setCreateTime(DateUtil.date());
				unifiedWarning.setCreateUserId(1L);
				unifiedWarning.setCreditPass(warningOrderAmountVo.getAmount().subtract(warningOrderAmountVo.getCredit()));
				unifiedWarning.setState(2);
				unifiedWarning.setVirtId(warningOrderAmountVo.getVirtId());
				unifiedWarning.setWarningType(2);

				unifiedWarningDubboConsumerService.addObj(unifiedWarning);
			}
		}
	}
}
