package com.bonus.canteen.core.report.datascreen.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.allocation.api.AllocCanteenApi;
import com.bonus.canteen.core.allocation.api.AllocMealtimeApi;
import com.bonus.canteen.core.allocation.canteen.model.AllocCanteen;
import com.bonus.canteen.core.common.enums.AllocMealtimeTypeEnum;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.menu.model.AllocMealtimeModel;
import com.bonus.canteen.core.report.dashboard.constant.DashboardDataKeyEnum;
import com.bonus.canteen.core.report.datascreen.dto.DatascreenDTO;
import com.bonus.canteen.core.report.datascreen.dto.v2.DataScreenBaseDTO;
import com.bonus.canteen.core.report.datascreen.mapper.DatascreenMapper;
import com.bonus.canteen.core.report.datascreen.service.DatascreenService;
import com.bonus.canteen.core.report.datascreen.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DatascreenServiceImpl implements DatascreenService {
   private static final Logger log = LoggerFactory.getLogger(DatascreenServiceImpl.class);
   @Autowired
   @Lazy
   private RedisTemplate<Object, Object> redisTemplate;
   @Autowired
   @Lazy
   private DatascreenMapper datascreenMapper;
   @Autowired
   private AllocMealtimeApi allocMealtimeApi;
   @Lazy
   @Autowired
   private AllocCanteenApi allocCanteenApi;
   @Autowired
   private DzDevicePassengerFlowServiceImpl dzDevicePassengerFlowService;

   /**
    * 定制-获取客流量
    */
   public List<CurrentMealtimeVO> getCurrentMealtimeNum(List<DsAuthorityCanteenVO> canteenList) {
      List<CurrentMealtimeVO> list = new ArrayList<>();
      if (CollUtil.isEmpty(canteenList)) {
         return list;
      }
      List<Long> canteenIdList = canteenList.stream()
              .map(DsAuthorityCanteenVO::getCanteenId)
              .distinct()
              .collect(Collectors.toList());
      List<DzDevicePassengerFlowVO> dzDevicePassengerFlowVOList = dzDevicePassengerFlowService.getDevicePassengerFlowData(canteenList.stream().map(DsAuthorityCanteenVO::getCanteenId).collect(Collectors.toSet()));
      //排序使dzDevicePassengerFlowVOList中的canteenId和canteenIdList中的canteenId顺序一致
      List<DzDevicePassengerFlowVO> finalDzDevicePassengerFlowVOList = dzDevicePassengerFlowVOList;
      dzDevicePassengerFlowVOList = canteenIdList.stream().map(canteen -> finalDzDevicePassengerFlowVOList.stream().filter(e -> e.getCanteenId().equals(canteen)).findFirst().orElse(null)).collect(Collectors.toList());
      dzDevicePassengerFlowVOList.forEach(e -> {
         CurrentMealtimeVO mealtimeVO = new CurrentMealtimeVO();
         mealtimeVO.setCanteenId(e.getCanteenId());
         mealtimeVO.setCanteenName(e.getCanteenName());
         Integer presentNum = e.getDiningNum() >= 0 ? e.getDiningNum() : 0;
         mealtimeVO.setMealNum(presentNum);
         mealtimeVO.setCurrentSeat(e.getRemainingSeats());
         Integer capacity = e.getRemainingSeats() + presentNum;
         if (capacity.equals(0)) {
            mealtimeVO.setBit(BigDecimal.ZERO);
         } else {
            if (presentNum > capacity) {
               mealtimeVO.setBit(BigDecimal.ONE);
            } else {
               mealtimeVO.setBit(BigDecimal.valueOf(presentNum).divide(BigDecimal.valueOf(capacity), 3, BigDecimal.ROUND_FLOOR));
            }
         }
         List<String> pictures = datascreenMapper.getDashboardPictures(666L);
         List<String> newPictures = new ArrayList<>();
         for (String picture : pictures) {
            newPictures.add(picture.replace("https://10.138.55.103:8888/lnyst", "http://sgwpdm.ah.sgcc.com.cn/canteen/lnyst"));
         }
         mealtimeVO.setPictures(newPictures);
         list.add(mealtimeVO);
      });
      return list;
   }
}
