package com.ycwl.basic.service.pc.impl;

import cn.hutool.core.date.DateUtil;
import com.ycwl.basic.mapper.ScenicDeviceStatsMapper;
import com.ycwl.basic.model.pc.device.entity.DeviceEntity;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
import com.ycwl.basic.model.pc.scenicDeviceStats.resp.ScenicDeviceStatsListResp;
import com.ycwl.basic.model.pc.scenicDeviceStats.resp.ScenicDeviceStatsResp;
import com.ycwl.basic.repository.DeviceRepository;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.pc.DeviceStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class DeviceStatsServiceImpl implements DeviceStatsService {
    @Autowired
    private ScenicDeviceStatsMapper mapper;
    @Autowired
    private ScenicRepository scenicRepository;
    @Autowired
    private DeviceRepository deviceRepository;

    @Override
    public ScenicDeviceStatsListResp queryByScenicId(Long scenicId, Date start, Date end) {
        if (start == null) {
            start = DateUtil.beginOfDay(new Date());
        }
        if (end == null) {
            end = DateUtil.endOfDay(new Date());
        }
        ScenicDeviceStatsListResp resp = new ScenicDeviceStatsListResp();
        if (DateUtil.isIn(start, DateUtil.tomorrow(), DateUtil.yesterday()) && DateUtil.isIn(end, DateUtil.tomorrow(), DateUtil.yesterday())) {
            resp.setRealtime(true);
            List<ScenicDeviceStatsResp> data = mapper.countRealtimeStatsByScenicId(scenicId, start, end);
            data.forEach(item -> {
                ScenicV2DTO scenic = scenicRepository.getScenicBasic(item.getScenicId());
                if (scenic != null) {
                    item.setScenicName(scenic.getName());
                }
                DeviceEntity device = deviceRepository.getDevice(item.getDeviceId());
                if (device != null) {
                    item.setDeviceName(device.getName());
                }
            });
            resp.setData(data);
        } else {
            resp.setRealtime(false);
            List<ScenicDeviceStatsResp> data = mapper.countCachedStatsByScenicId(scenicId, start, end);
            data.forEach(item -> {
                ScenicV2DTO scenic = scenicRepository.getScenicBasic(item.getScenicId());
                if (scenic != null) {
                    item.setScenicName(scenic.getName());
                }
                DeviceEntity device = deviceRepository.getDevice(item.getDeviceId());
                if (device != null) {
                    item.setDeviceName(device.getName());
                }
            });
            resp.setData(data);
        }
        resp.getData().stream()
                .mapToInt(ScenicDeviceStatsResp::getCount).max()
                .ifPresent((max) -> resp.getData()
                        .forEach(item -> item.setRate(
                                BigDecimal.valueOf(item.getCount()).divide(BigDecimal.valueOf(max), 6, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)))
                        )
                );
        return resp;
    }
}
