//package com.cyksj.task;
//
//import cn.hutool.core.date.DateTime;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.StrUtil;
//import com.cyksj.common.constant.Constant;
//import com.cyksj.mapper.manage.CustomerServiceEveryMonthDetailMapper;
//import com.cyksj.mapper.manage.StatisticsCustomerServiceEveryMonthMapper;
//import com.cyksj.model.entity.CustomerServiceEveryMonthDetail;
//import com.cyksj.model.entity.CustomerServiceRenewOrderRecord;
//import com.cyksj.model.entity.RealGoodsOrderHandle;
//import com.cyksj.model.entity.StatisticsCustomerServiceEveryMonth;
//import com.ejlchina.searcher.BeanSearcher;
//import com.ejlchina.searcher.param.Operator;
//import com.ejlchina.searcher.util.MapUtils;
//import com.google.common.util.concurrent.AtomicDouble;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.math.BigDecimal;
//import java.util.List;
//import java.util.Map;
//import java.util.Optional;
//import java.util.stream.Collectors;
//
///*
// *项目名: netflix
// *文件名: OrderCustomerHandleScheduler
// *创建者: JavaZou
// *创建时间:2022/11/29 16:44
// */
//@Component
//@Slf4j
//@RequiredArgsConstructor
//public class OrderCustomerHandleScheduler {
//	private final CustomerServiceEveryMonthDetailMapper detailMapper;
//
//	private final StatisticsCustomerServiceEveryMonthMapper statisticsEveryMonthMapper;
//
//	private final BeanSearcher beanSearcher;
//
//	/**
//	 * 客服业绩数据统计
//	 */
//	@Scheduled(cron = "59 59 23 L * ?")
//	public void orderCustomerHandle() {
//		DateTime now = DateTime.now();
//		DateTime begin = DateUtil.beginOfMonth(now);
//		String statisticsDate = DateUtil.format(now, "yyyy-MM");
//		List<RealGoodsOrderHandle> realGoodsOrderHandles = beanSearcher.searchAll(RealGoodsOrderHandle.class, MapUtils.builder()
//				.field(RealGoodsOrderHandle::getStatus, Constant.noOrderAllStatus).op(Operator.NotIn)
//				.field(RealGoodsOrderHandle::getCreatedTime, begin, now).op(Operator.Between)
//				.build());
//		Map<String, List<RealGoodsOrderHandle>> before = realGoodsOrderHandles.stream().collect(Collectors.groupingBy(RealGoodsOrderHandle::getBeforeServiceName));
//		Map<String, List<RealGoodsOrderHandle>> after = realGoodsOrderHandles.stream().collect(Collectors.groupingBy(RealGoodsOrderHandle::getAfterServiceName));
//		StatisticsCustomerServiceEveryMonth statistics = new StatisticsCustomerServiceEveryMonth();
//		statistics.setStatisticsData(statisticsDate);
//		statisticsEveryMonthMapper.insert(statistics);
//		before.forEach((k, v) -> {
//			customerServiceRecord(k, v, 1, statistics.getId());
//		});
//		after.forEach((k, v) -> {
//			customerServiceRecord(k, v, 2, statistics.getId());
//		});
//		//续费业绩
//		List<CustomerServiceRenewOrderRecord> customerServiceRecords = beanSearcher.searchAll(CustomerServiceRenewOrderRecord.class, MapUtils.builder()
//				.field(RealGoodsOrderHandle::getCreatedTime, begin, now).op(Operator.Between)
//				.build());
//		Map<String, List<CustomerServiceRenewOrderRecord>> renews = customerServiceRecords.stream().collect(Collectors.groupingBy(CustomerServiceRenewOrderRecord::getCustomerService));
//		renews.forEach((k,v)->{
//			renewServiceOrderRecord(k, v, 3, statistics.getId());
//		});
//	}
//
//
//	public void customerServiceRecord(String serviceName, List<RealGoodsOrderHandle> handlers, Integer type, Long statisticsId) {
//		if (StrUtil.isNotBlank(serviceName)) {
//			CustomerServiceEveryMonthDetail record = new CustomerServiceEveryMonthDetail();
//			record.setType(type);
//			record.setCustomerServiceName(serviceName);
//			record.setStatisticsId(statisticsId);
//			AtomicDouble numOfOrder = new AtomicDouble(0);
//			handlers.forEach(handler -> {
//				if ("red_book".equals(handler.getLabelSource())) {
//					numOfOrder.addAndGet(1.5);
//					return;
//				}
//				numOfOrder.addAndGet(1);
//			});
//			record.setNumOfOrder(BigDecimal.valueOf(numOfOrder.doubleValue()));
//			Optional<BigDecimal> money = handlers.stream().map(RealGoodsOrderHandle::getMoney).reduce(BigDecimal::add);
//			record.setMoney(money.isPresent() ? money.get() : BigDecimal.ZERO);
//			detailMapper.insert(record);
//		}
//	}
//
//	public void renewServiceOrderRecord(String serviceName, List<CustomerServiceRenewOrderRecord> renewOrderRecords, Integer type, Long statisticsId) {
//		if (StrUtil.isNotBlank(serviceName)) {
//			CustomerServiceEveryMonthDetail record = new CustomerServiceEveryMonthDetail();
//			record.setType(type);
//			record.setCustomerServiceName(serviceName);
//			record.setStatisticsId(statisticsId);
//			record.setNumOfOrder(BigDecimal.valueOf(renewOrderRecords.size()));
//			record.setMoney(BigDecimal.ZERO);
//			detailMapper.insert(record);
//		}
//	}
//}
