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

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.dao.entity.marketing.UnifiedStatisticsVirt;
import cn.superdesk.uniorder.dubbo.consumer.marketing.UnifiedStatisticsVirtCommonDubboConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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


/**
 * @Description: 1q搞定 所以此类是废的
 * @Author: linjinyu
 * @CreateDate: 2021/9/7 下午2:29
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/9/7 下午2:29
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Deprecated
@Slf4j
@Component
public class StatisticsJobVirtRefundOrderStatisticalDimDayTask {

	@Resource
	private UnifiedStatisticsVirtCommonDubboConsumerService unifiedStatisticsVirtDubboConsumerService;

	/**
	 * 目前按原做法 配置成 0 0 * * * ? *
	 * 实际上是可以 配置成 0 0 17,23 * * ?
	 *
	 * @param params
	 */
	public void run(String params) {

		log.info("1q搞定 所以此类是废的");

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

		/****************************************** StatisticsJobVirtRefundOrderStatistical start ******************************************/

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

		// 退货统计 TODO 待验证
		List<UnifiedStatisticsVirt> return_ls = unifiedStatisticsVirtDubboConsumerService.queryVirtRefundOrderStatistical(queryMap);

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

			for (UnifiedStatisticsVirt item : return_ls) {

				Map<String, Object> queryStatistics = new HashMap<>();
				queryStatistics.put("virtId", item.getVirtId());
				queryStatistics.put("sYear", year);
				queryStatistics.put("sMonth", month);
				queryStatistics.put("serviceNo", item.getServiceNo());
				List<UnifiedStatisticsVirt> statisticsVirts = unifiedStatisticsVirtDubboConsumerService.findAll(queryStatistics);

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

					// update
					UnifiedStatisticsVirt update = statisticsVirts.get(0);

					/****************************************** mark by linjinyu at 20201125 重构 start ******************************************/
					update.setOrderRefundAmount(item.getOrderRefundAmount());
					update.setOrderRefundCostAmount(item.getOrderRefundCostAmount());

					update.setGoodsRefundAmount(item.getGoodsRefundAmount());
					update.setGoodsRefundCostAmount(item.getGoodsRefundCostAmount());

					/****************************************** mark by linjinyu at 20201125 重构   end ******************************************/

					unifiedStatisticsVirtDubboConsumerService.updateStatisticsVirt(update);

				} else {

					// insert
					UnifiedStatisticsVirt add = new UnifiedStatisticsVirt();

					add.setVirtId(item.getVirtId());
					add.setServiceNo(item.getServiceNo());

					add.setQuantity(0);

					add.setSMonth(month);
					add.setSYear(year);


					/****************************************** mark by linjinyu at 20201125 重构 start ******************************************/

					add.setOrderAmount(new BigDecimal("0"));
					add.setOrderCostAmount(new BigDecimal("0"));
					add.setOrderRefundAmount(item.getOrderRefundAmount());
					add.setOrderRefundCostAmount(item.getOrderRefundCostAmount());
					add.setOrderTransportAmount(new BigDecimal("0"));

					add.setGoodsAmount(new BigDecimal("0"));
					add.setGoodsCostAmount(new BigDecimal("0"));
					add.setGoodsRefundAmount(item.getGoodsRefundAmount());
					add.setGoodsRefundCostAmount(item.getGoodsRefundCostAmount());
					add.setGoodsTransportAmount(new BigDecimal("0"));

					/****************************************** mark by linjinyu at 20201125 重构   end ******************************************/

					add.setPayableAmount(new BigDecimal(0));

					add.setCreateTime(DateUtil.date());

					unifiedStatisticsVirtDubboConsumerService.insertStatisticsVirt(add);
				}
			}
		}

	}
}
