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

import cn.hutool.core.collection.CollUtil;
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.utils.LogUtil;
import com.bonus.canteen.core.menu.model.AllocMealtimeModel;
import com.bonus.canteen.core.report.datascreen.mapper.DzDevicePassengerFlowMapper;
import com.bonus.canteen.core.report.datascreen.vo.DeviceQueryPresentNumVo;
import com.bonus.canteen.core.report.datascreen.vo.DzDevicePassengerFlowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DzDevicePassengerFlowServiceImpl {
    @Autowired
    @Lazy
    private DzDevicePassengerFlowMapper dzDevicePassengerFlowMapper;
    @Lazy
    @Autowired
    private AllocCanteenApi allocCanteenApi;
    @Lazy
    @Autowired
    private AllocMealtimeApi allocMealtimeApi;

    public List<DzDevicePassengerFlowVO> getDevicePassengerFlowData(Set<Long> canteenIdList) {
        List<DzDevicePassengerFlowVO> dzDevicePassengerFlowVOS = CollUtil.newArrayList();
        List<AllocCanteen> allocCanteens = allocCanteenApi.queryCanteenByCanteenIdList(canteenIdList);
        AllocMealtimeModel assignTimeMealtime = allocMealtimeApi.getAssignTimeMealtime(LocalTime.now(), null, null);

        if (ObjectUtil.isNotEmpty(assignTimeMealtime)) {
            LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), assignTimeMealtime.getStartTime());
            LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), assignTimeMealtime.getEndTime());
            List<DeviceQueryPresentNumVo> deviceQueryPresentNumVos = dzDevicePassengerFlowMapper.queryPresentNumByCanteenIds(LocalDate.now(),
                    canteenIdList, startTime, endTime);
            Map<Long, List<DeviceQueryPresentNumVo>> collect = deviceQueryPresentNumVos.stream().collect(Collectors.groupingBy(DeviceQueryPresentNumVo::getCanteenId, Collectors.toList()));
            for (AllocCanteen allocCanteen : allocCanteens) {
                DzDevicePassengerFlowVO deviceQueryPresentNumVo = new DzDevicePassengerFlowVO();
                deviceQueryPresentNumVo.setCanteenId(allocCanteen.getCanteenId());
                deviceQueryPresentNumVo.setCanteenName(allocCanteen.getCanteenName());
                if (collect.containsKey(allocCanteen.getCanteenId())) {
                    Integer presentNum = collect.get(allocCanteen.getCanteenId()).get(0).getPresentNum();
                    if (ObjectUtil.isNotEmpty(allocCanteen.getCapacity()) && presentNum > 0) {
                        int remainingSeats = allocCanteen.getCapacity() - presentNum;
                        if (remainingSeats < 0) {
                            deviceQueryPresentNumVo.setRemainingSeats(0);
                        } else {
                            deviceQueryPresentNumVo.setRemainingSeats(remainingSeats);
                        }

                    } else if (ObjectUtil.isEmpty(allocCanteen.getCapacity())) {
                        deviceQueryPresentNumVo.setRemainingSeats(0);
                    } else {
                        deviceQueryPresentNumVo.setRemainingSeats(allocCanteen.getCapacity());
                    }
                    deviceQueryPresentNumVo.setDiningNum(collect.get(allocCanteen.getCanteenId()).get(0).getPresentNum());
                } else {
                    deviceQueryPresentNumVo.setRemainingSeats(ObjectUtil.isEmpty(allocCanteen.getCapacity()) ? 0 : allocCanteen.getCapacity());
                }
                dzDevicePassengerFlowVOS.add(deviceQueryPresentNumVo);
            }
            LogUtil.info("获取餐厅实时人数成功:", dzDevicePassengerFlowVOS);
            return dzDevicePassengerFlowVOS;
        }
        for (AllocCanteen allocCanteen : allocCanteens) {
            DzDevicePassengerFlowVO deviceQueryPresentNumVo = new DzDevicePassengerFlowVO();
            deviceQueryPresentNumVo.setCanteenId(allocCanteen.getCanteenId());
            deviceQueryPresentNumVo.setCanteenName(allocCanteen.getCanteenName());
            deviceQueryPresentNumVo.setRemainingSeats(allocCanteen.getCapacity());
            deviceQueryPresentNumVo.setDiningNum(0);
            dzDevicePassengerFlowVOS.add(deviceQueryPresentNumVo);
        }
        LogUtil.info("获取餐厅实时人数成功:",dzDevicePassengerFlowVOS);
        return dzDevicePassengerFlowVOS;

    }
}
