/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.coldchain.modular.monitornotice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.coldchain.core.bean.influxdb.SensorData;
import vip.xiaonuo.coldchain.modular.app.param.AppDeviceQueryParams;
import vip.xiaonuo.coldchain.modular.app.param.SensorEchartDataResult;
import vip.xiaonuo.coldchain.modular.app.service.AppDeviceService;
import vip.xiaonuo.coldchain.modular.monitordevice.entity.MonitorDevice;
import vip.xiaonuo.coldchain.modular.monitordevice.service.MonitorDeviceService;
import vip.xiaonuo.coldchain.modular.monitornotice.entity.*;
import vip.xiaonuo.coldchain.modular.monitornotice.enums.DateEnum;
import vip.xiaonuo.coldchain.modular.monitornotice.mapper.MonitorNoticeMapper;
import vip.xiaonuo.coldchain.modular.monitornotice.param.RealtimeParam;
import vip.xiaonuo.coldchain.modular.monitornotice.param.TrendParam;
import vip.xiaonuo.coldchain.modular.monitornotice.service.MonitorNoticeService;
import vip.xiaonuo.coldchain.modular.monitortargetregion.entity.MonitorTargetRegion;
import vip.xiaonuo.coldchain.modular.monitortargetregion.service.MonitorTargetRegionService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 监控通知Service接口实现类
 *
 * @author 黄渊昊
 * @date 2024/11/25 10:58
 **/
@Service
public class MonitorNoticeServiceImpl extends ServiceImpl<MonitorNoticeMapper, MonitorNotice> implements MonitorNoticeService {

    @Resource
    private MonitorNoticeMapper monitorNoticeMapper;
    @Resource
    private MonitorDeviceService monitorDeviceService;
    @Resource
    private MonitorTargetRegionService monitorTargetRegionService;
    @Resource
    private AppDeviceService appDeviceService;

    private static int STP = 3;

    @Override
    public CountWithTime today(String orgId) {
        List<String> timePeriodList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        int timeFlag = 0;
        while (timeFlag != 24) {
            String startTimeStr = getTimeStr(timeFlag);
            timeFlag += STP;
            String endTimeStr = getTimeStr(timeFlag);
            timePeriodList.add(startTimeStr + "-" + endTimeStr);
            Integer toDay = monitorNoticeMapper.getToDay(startTimeStr, endTimeStr, orgId);
            countList.add(toDay);
        }
        CountWithTime countWithTime = new CountWithTime();
        countWithTime.setDateName(DateEnum.TODAY.getValue());
        countWithTime.setCountList(countList);
        countWithTime.setTimePeriodList(timePeriodList);
        return countWithTime;
    }

    @Override
    public CountWithTime yesterday(String orgId) {
        List<NoticeByDate> yesterdayNoticeList = new ArrayList<>();
        List<String> timePeriodList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        int timeFlag = 0;
        while (timeFlag != 24) {
            String startTimeStr = getTimeStr(timeFlag);
            timeFlag += STP;
            String endTimeStr = getTimeStr(timeFlag);
            timePeriodList.add(startTimeStr + "-" + endTimeStr);
            Integer yesterday = monitorNoticeMapper.getYesterday(startTimeStr, endTimeStr, orgId);
            countList.add(yesterday);
        }
        CountWithTime countWithTime = new CountWithTime();
        countWithTime.setDateName(DateEnum.YESTERDAY.getValue());
        countWithTime.setCountList(countList);
        countWithTime.setTimePeriodList(timePeriodList);
        return countWithTime;
    }

    @Override
    public CountWithTime dayBeforeYesterday(String orgId) {
        List<NoticeByDate> yesterdayNoticeList = new ArrayList<>();
        List<String> timePeriodList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        int timeFlag = 0;
        while (timeFlag != 24) {
            String startTimeStr = getTimeStr(timeFlag);
            timeFlag += STP;
            String endTimeStr = getTimeStr(timeFlag);
            timePeriodList.add(startTimeStr + "-" + endTimeStr);
            Integer dayBeforeYesterday = monitorNoticeMapper.getDayBeforeYesterday(startTimeStr, endTimeStr, orgId);
            countList.add(dayBeforeYesterday);
        }
        CountWithTime countWithTime = new CountWithTime();
        countWithTime.setDateName(DateEnum.DAY_BEFORE_YESTERDAY.getValue());
        countWithTime.setCountList(countList);
        countWithTime.setTimePeriodList(timePeriodList);
        return countWithTime;
    }

    @Override
    public List<CountWithTime> getNoticeByDate() {
        String orgId = StpLoginUserUtil.getLoginUser().getOrgId();
        NoticeByDate noticeByDate = new NoticeByDate();
        List<CountWithTime> countWithTimeList = new ArrayList<>();
        countWithTimeList.add(today(orgId));
        countWithTimeList.add(yesterday(orgId));
        countWithTimeList.add(dayBeforeYesterday(orgId));
        return countWithTimeList;
    }

    private String getTimeStr(int timeFlag) {
        String timeStr = timeFlag + ":00:00";
        if (timeStr.length() < 8) {
            timeStr = "0" + timeStr;
        }
        return timeStr;
    }

    @Override
    public Trend getTrend(TrendParam trendParam) {
        MonitorDevice monitorDevice = monitorDeviceService.getById(trendParam.getDeviceId());
        MonitorTargetRegion oneByDeviceCodeAndSensorNo = monitorTargetRegionService.findOneByDeviceCodeAndSensorNo(monitorDevice.getDeviceCode(), trendParam.getRoads());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        AppDeviceQueryParams appDeviceQueryParams = new AppDeviceQueryParams();
        appDeviceQueryParams.setSensorCode(oneByDeviceCodeAndSensorNo.getSensorCode());
        appDeviceQueryParams.setStartTime(format.format(trendParam.getStartTime()));
        appDeviceQueryParams.setEndTime(format.format(trendParam.getEndTime()));
        appDeviceQueryParams.setSensorRoute(trendParam.getRoads());
        appDeviceQueryParams.setAggregationWindow(trendParam.getAggregationWindow());
        SensorEchartDataResult sensorEchartDataResult = appDeviceService.queryDataByDeviceIdAndRoads(appDeviceQueryParams);
        Trend trend = BeanUtil.copyProperties(oneByDeviceCodeAndSensorNo, Trend.class);
        trend.setSensorEchartDataResult(sensorEchartDataResult);
        return trend;
    }

    @Override
    public Realtime getRealtime(RealtimeParam realtimeParam) {
        MonitorDevice monitorDevice = monitorDeviceService.getById(realtimeParam.getDeviceId());
        SensorData sensorData = monitorDeviceService.queryLatestDataByDeviceIdAndRoads(monitorDevice.getDeviceCode(), realtimeParam.getRoads());
        Realtime realtime = new Realtime();
        realtime.setSensorData(sensorData);
        MonitorTargetRegion oneByDeviceCodeAndSensorNo = monitorTargetRegionService.findOneByDeviceCodeAndSensorNo(monitorDevice.getDeviceCode(), realtimeParam.getRoads());
        realtime.setRegionName(oneByDeviceCodeAndSensorNo.getName());
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        realtime.setUpdateTime(format.format(date));
        return realtime;
    }
}
