package com.bonus.core.report.api;

import cn.hutool.core.collection.CollUtil;
import com.bonus.core.common.utils.JavaxValidateUtils;
import com.bonus.core.report.api.dto.ReportCustConsumeSumDTO;
import com.bonus.core.report.statistics.order.basic.model.ReportOrderInfo;
import com.bonus.core.report.statistics.order.basic.service.ReportOrderInfoService;
import com.github.pagehelper.page.PageMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

@Service
public class ReportOrderApi {
   private static final Logger log = LoggerFactory.getLogger(ReportOrderApi.class);
//   @Autowired
//   @Lazy
//   protected DeviceApi deviceApi;
//   @Autowired
//   @Lazy
//   protected AllocMealtimeApi allocMealtimeApi;
//   @Autowired
//   @Lazy
//   private ReportSumMachineService reportSumMachineService;
   @Autowired
   private ReportOrderInfoService reportOrderInfoService;
//   @Autowired
//   private ReportAnalysisTurnoverService reportAnalysisTurnoverService;
//   @Autowired
//   private ReportAnalysisDishesSaleService reportAnalysisDishesSaleService;
//   @Lazy
//   @Autowired
//   protected CustInfoApi custInfoApi;
//   @Resource(
//      name = "yunshitangTaskExecutor"
//   )
//   private AsyncTaskExecutor asyncTaskExecutor;

//   public ReportDeviceOrderSumVO reportDeviceOrderSum(ReportOrderSumQueryDTO content) {
//      JavaxValidateUtils.validate(content);
//      ReportDeviceOrderSumVO result = new ReportDeviceOrderSumVO();
//      DeviceInfoVO macMachine = this.deviceApi.getDeviceInfoVoBySnCacheable(content.getMachineSn());
//      AllocMealtimeModel mealtime = this.allocMealtimeApi.getAssignTimeMealtime(LocalTime.now(), macMachine.getFirstCanteenId(), macMachine.getFirstShopstallId());
//      LocalDate nowDate = LocalDate.now();
//      List<CompletableFuture<Void>> futures = new ArrayList();
//      CompletableFuture daySumFuture;
//      if (Objects.nonNull(mealtime)) {
//         daySumFuture = CompletableFuture.supplyAsync(() -> {
//            return this.reportSumMachineService.getReportOrderSumForDevice((new ReportDeviceOrderSumDTO()).setMachineSn(content.getMachineSn()).setMealtimeType(mealtime.getMealtimeType()).setStartDate(nowDate).setEndDate(nowDate));
//         }, this.asyncTaskExecutor);
//         futures.add(daySumFuture.thenAccept((mealSum) -> {
//            result.setCurrentMealTotalAmount(mealSum.getTotalAmount());
//            result.setCurrentMealTotalCount(mealSum.getTotalCount());
//         }));
//      }
//
//      daySumFuture = CompletableFuture.supplyAsync(() -> {
//         return this.reportSumMachineService.getReportOrderSumForDevice((new ReportDeviceOrderSumDTO()).setMachineSn(content.getMachineSn()).setStartDate(nowDate).setEndDate(nowDate));
//      }, this.asyncTaskExecutor);
//      futures.add(daySumFuture.thenAccept((daySum) -> {
//         result.setCurrentDayTotalAmount(daySum.getTotalAmount());
//         result.setCurrentDayTotalCount(daySum.getTotalCount());
//      }));
//      CompletableFuture<ReportOrderSumVO> monthSumFuture = CompletableFuture.supplyAsync(() -> {
//         return this.reportSumMachineService.getReportOrderSumForDevice((new ReportDeviceOrderSumDTO()).setMachineSn(content.getMachineSn()).setStartDate(nowDate.with(TemporalAdjusters.firstDayOfMonth())).setEndDate(nowDate.with(TemporalAdjusters.lastDayOfMonth())));
//      }, this.asyncTaskExecutor);
//      futures.add(monthSumFuture.thenAccept((monthSum) -> {
//         result.setCurrentMonthTotalAmount(monthSum.getTotalAmount());
//         result.setCurrentMonthTotalCount(monthSum.getTotalCount());
//      }));
//      CompletableFuture<ReportOrderSumVO> lastMonthSumFuture = CompletableFuture.supplyAsync(() -> {
//         return this.reportSumMachineService.getReportOrderSumForDevice((new ReportDeviceOrderSumDTO()).setMachineSn(content.getMachineSn()).setStartDate(nowDate.minusMonths(1L).with(TemporalAdjusters.firstDayOfMonth())).setEndDate(nowDate.minusMonths(1L).with(TemporalAdjusters.lastDayOfMonth())));
//      }, this.asyncTaskExecutor);
//      futures.add(lastMonthSumFuture.thenAccept((lastMonthSum) -> {
//         result.setLastMonthTotalAmount(lastMonthSum.getTotalAmount());
//         result.setLastMonthTotalCount(lastMonthSum.getTotalCount());
//      }));
//      CompletableFuture.allOf((CompletableFuture[])futures.toArray(new CompletableFuture[0])).join();
//      return result;
//   }
//
//   public PageVO<DeviceOrderDishesSumVO> reportOrderDishesSum(DeviceOrderDishesSumQueryDTO content) {
//      JavaxValidateUtils.validate(content);
//      if (CollUtil.isEmpty(content.getMealtimeTypeList())) {
//         DeviceInfoVO macMachine = this.deviceApi.getDeviceInfoVoBySnCacheable(content.getAvailableMachineSn());
//         AllocMealtimeModel mealtime = this.allocMealtimeApi.getAssignTimeMealtime(LocalTime.now(), macMachine.getFirstCanteenId(), macMachine.getFirstShopstallId());
//         if (mealtime != null) {
//            content.setMealtimeTypeList(CollUtil.toList(new Integer[]{mealtime.getMealtimeType()}));
//         }
//      }
//
//      PageMethod.startPage(content.getPageNum(), content.getPageSize());
//      return PageVO.of(this.reportOrderInfoService.getOrderDishesSumForDevice(content));
//   }
//
//   public ReportTurnoverPO reportTurnoverTotal(ReportAnalysisTurnoverParam reportAnalysisTurnoverParam) {
//      return this.reportAnalysisTurnoverService.getTurnoverTotal(reportAnalysisTurnoverParam);
//   }

   public List<ReportOrderInfo> getCustConsumeSum(ReportCustConsumeSumDTO content) {
      JavaxValidateUtils.validate(content);
      return this.reportOrderInfoService.getCustConsumeSum(content);
   }

//   public List<ReportOrderDishesSumVO> getDishesMonthSum(List<Long> dishesIdList) {
//      LocalDate nowDate = LocalDate.now();
//      LocalDate firstDay = nowDate.with(TemporalAdjusters.firstDayOfMonth());
//      LocalDate lastDay = nowDate.with(TemporalAdjusters.lastDayOfMonth());
//      return this.reportAnalysisDishesSaleService.getDishesMonthSum(firstDay, lastDay, dishesIdList);
//   }
//
//   public PageVO<ReportOrderDetaiVO> getOrderMealtimeDetail(ReportOrderDetailDTO content) {
//      PageMethod.startPage(content.getPageNum(), content.getPageSize());
//      return PageVO.of(this.reportAnalysisDishesSaleService.getOrderMealtimeDetail(content));
//   }
//
//   public List<ReportOrderDetaiVO> drpDishesSum(OrderDrpDishesSumDTO content) {
//      return this.reportOrderInfoService.drpDishesSum(content);
//   }
//
//   public PageVO<OrderBusStatVO> listOrderBusStat(PageDTO pageDTO, OrderStatBusDTO param) {
//      PageMethod.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
//      List<OrderBusStatVO> voList = this.reportOrderInfoService.listOrderStatForBus(param);
//      return PageVO.of(voList);
//   }
//
//   public OrderBookStatMobileVO getBookStat(Long custId) {
//      return this.reportOrderInfoService.getBookStat(custId);
//   }
//
//   public OrderBookRefundStatMobileVO getBookRefundStat(Long custId) {
//      return this.reportOrderInfoService.getBookRefundStat(custId);
//   }
}
