package com.bonus.canteen.core.report.statistics.order.analysis.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.auth.api.MgrAuthApi;
import com.bonus.canteen.core.auth.po.MgrUserAuthPO;
import com.bonus.canteen.core.common.page.PageVO;
import com.bonus.canteen.core.order.common.constants.DetailTypeEnum;
import com.bonus.canteen.core.order.mq.po.OrderChangePO;
import com.bonus.canteen.core.report.api.dto.ReportOrderDetailDTO;
import com.bonus.canteen.core.report.api.vo.ReportOrderDetaiVO;
import com.bonus.canteen.core.report.api.vo.ReportOrderDishesSumVO;
import com.bonus.canteen.core.report.statistics.config.mq.ReportOrderConsumeService;
import com.bonus.canteen.core.report.statistics.order.analysis.mapper.ReportAnalysisDishesSaleMapper;
import com.bonus.canteen.core.report.statistics.order.analysis.model.ReportAnalysisDishesSale;
import com.bonus.canteen.core.report.statistics.order.analysis.param.ReportAnalysisDishesSaleParam;
import com.bonus.canteen.core.report.statistics.order.analysis.vo.ReportAnalysisDishesSaleVO;
import com.bonus.canteen.core.report.statistics.order.basic.dto.ReportOrderInfoDTO;
import com.bonus.canteen.core.report.statistics.order.basic.service.ReportOrderInfoService;
import com.bonus.canteen.core.report.statistics.param.ReportBaseParam;
import com.bonus.canteen.core.report.vo.ReportBaseTotalVO;
import com.github.pagehelper.page.PageMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ReportAnalysisDishesSaleService extends ServiceImpl<ReportAnalysisDishesSaleMapper, ReportAnalysisDishesSale> implements ReportOrderConsumeService {
   private static final Logger log = LoggerFactory.getLogger(ReportAnalysisDishesSaleService.class);
   private static final int ORDER = 51;
   @Autowired
   private ReportAnalysisDishesSaleMapper reportAnalysisDishesSaleMapper;
   @Autowired
   private ReportOrderInfoService reportOrderInfoService;
   @Autowired
   private MgrAuthApi mgrAuthApi;

   public int getOrder() {
      return 51;
   }

   public ReportBaseTotalVO<ReportAnalysisDishesSaleVO> pageSummary(ReportAnalysisDishesSaleParam param) {
      MgrUserAuthPO authPO = this.mgrAuthApi.getUserAuthPO();
      PageMethod.startPage(param.getPage());
      List<ReportAnalysisDishesSaleVO> list = this.reportAnalysisDishesSaleMapper.listSummary(param, authPO);
      list.forEach((vo) -> {
         vo.setDetailTypeName(DetailTypeEnum.getDesc(vo.getDetailType()));
      });
      ReportAnalysisDishesSaleVO total = this.reportAnalysisDishesSaleMapper.getSummaryTotal(param, authPO);
      return (new ReportBaseTotalVO()).setResultPage(PageVO.of(list)).setTotalLine(total);
   }

   public void consume(OrderChangePO payload, ReportOrderInfoDTO baseInfo) {
      log.info("报表MQ消费-菜品销售排行：开始");
      ReportAnalysisDishesSaleParam param = (ReportAnalysisDishesSaleParam)this.reportOrderInfoService.initParam(payload, baseInfo, ReportAnalysisDishesSaleParam.class);
      LocalDateTime startTime = payload.getOrderInfo().getPayTime().toLocalDate().atStartOfDay();
      LocalDateTime endTime = payload.getOrderInfo().getPayTime().toLocalDate().atTime(23, 59, 59);
      List<ReportAnalysisDishesSale> newSum = this.reportAnalysisDishesSaleMapper.init(param, startTime, endTime);
      this.remove(((((((Wrappers.lambdaQuery(ReportAnalysisDishesSale.class).eq(ReportAnalysisDishesSale::getStatisticDate, param.getStartDate())).eq(ReportAnalysisDishesSale::getCanteenId, param.getCanteenId())).eq(ReportAnalysisDishesSale::getStallId, param.getStallId())).eq(ReportAnalysisDishesSale::getOrgId, param.getOrgId())).in(ReportAnalysisDishesSale::getAgeType, param.getAgeTypes())).in(CollUtil.isNotEmpty(param.getDishesIds()), ReportAnalysisDishesSale::getGoodsDishesId, param.getDishesIds())).eq(ReportAnalysisDishesSale::getMealtimeType, Objects.isNull(param.getMealtimeType()) ? -1 : param.getMealtimeType()));
      this.saveBatch(newSum);
      log.info("报表MQ消费-菜品销售排行：结束");
   }

   public void fix(ReportBaseParam param) {
      log.info("报表修复数据-菜品销售排行：开始");
      LocalDateTime startTime = param.getStartDate().atStartOfDay();
      LocalDateTime endTime = param.getEndDate().atTime(23, 59, 59);
      this.remove(Wrappers.lambdaQuery(ReportAnalysisDishesSale.class).between(ReportAnalysisDishesSale::getStatisticDate, param.getStartDate(), param.getEndDate()));
      ((ReportAnalysisDishesSaleMapper)this.baseMapper).initFix((ReportAnalysisDishesSaleParam)null, startTime, endTime);
      log.info("报表修复数据-菜品销售排行：结束");
   }

   public List<ReportOrderDishesSumVO> getDishesMonthSum(LocalDate firstDay, LocalDate lastDay, List<Long> dishesIdList) {
      return this.reportAnalysisDishesSaleMapper.getDishesMonthSum(firstDay, lastDay, dishesIdList);
   }

   public List<ReportOrderDetaiVO> getOrderMealtimeDetail(ReportOrderDetailDTO content) {
      return this.reportAnalysisDishesSaleMapper.getOrderMealtimeDetail(content);
   }
}
