package com.ruoyi.monitoring.controller;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.web.domain.PageData;
import com.ruoyi.monitoring.domain.CmcChannel;
import com.ruoyi.monitoring.domain.CmcTemperature;
import com.ruoyi.monitoring.service.ICmcChannelService;
import com.ruoyi.monitoring.service.ICmcTemperatureService;
import com.ruoyi.common.web.controller.BaseController;
import org.noear.solon.annotation.*;

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

/**
 * 水温监测
 * @author cmc
 */
@Controller
@Mapping("monitoring/temperature")
public class CmcTemperatureController extends BaseController {

    @Inject
    ICmcTemperatureService cmcTemperatureService;

    @Inject
    ICmcChannelService cmcChannelService;

    /**
     * 查询水温监测列表
     */
    @Get
    @Mapping("list")
    public List<CmcTemperature> list(CmcTemperature cmcTemperature) {
        QueryWrapper qw = getQW(cmcTemperature);
        return cmcTemperatureService.list(qw);
    }

    private QueryWrapper getQW(CmcTemperature cmcTemperature) {
        QueryWrapper qw = QueryWrapper.create();
        qw.and(CmcTemperature::getId).eq(cmcTemperature.getId());
        qw.and(CmcTemperature::getGroupNo).eq(cmcTemperature.getGroupNo());
        qw.and(CmcTemperature::getSensorNo).eq(cmcTemperature.getSensorNo());
        qw.and(CmcTemperature::getData).eq(cmcTemperature.getData());
        qw.and(CmcTemperature::getVolt).eq(cmcTemperature.getVolt());
        qw.and(CmcTemperature::getCSQ).eq(cmcTemperature.getCSQ());
        qw.and(CmcTemperature::getDatetime).ge(cmcTemperature.getStartTime());
        qw.and(CmcTemperature::getDatetime).le(cmcTemperature.getEndTime());
        qw.orderBy(CmcTemperature::getDatetime).desc();
        return qw;
    }

    /**
     * 根据点位查询水温监测列表
     */
    @Get
    @Mapping("listByInfo")
    public PageData<CmcTemperature> listByInfo(Page<CmcTemperature> page, CmcTemperature cmcTemperature, String Info) {
        CmcChannel channel = new CmcChannel();
        channel.setInfo(Info);
        int channelSize = cmcChannelService.listChannel(channel).size();
        page.setPageSize(page.getPageSize() * channelSize);
        QueryWrapper qw = getQW(cmcTemperature, Info);
        List<CmcTemperature> records = cmcTemperatureService.list(qw);
        List<CmcTemperature> temperatureList = new ArrayList<>();
        if (records.size() > 2) {
            long diffInMillis;
            // 第30条与第31条数据时间差 毫秒
            if (channelSize > 30) {
                diffInMillis = records.get(30).getDatetime().getTime() - records.get(29).getDatetime().getTime();
                // 时间差超过15分钟，删最后10条数据
                if (diffInMillis / 1000 / 60 > 15)
                    records.subList(records.size() - channelSize + 30, records.size()).clear();
            }
            // 第1条数据与0时时间差 毫秒
            diffInMillis = records.get(0).getDatetime().getTime() - cmcTemperature.getStartTime().getTime();
            if (diffInMillis / 1000 / 60 > 15)
                records.subList(records.size() - channelSize, records.size()).clear();
            CmcTemperature temperature = new CmcTemperature();
            long fifteenMinutesAgoMillis = cmcTemperature.getStartTime().getTime() - (15 * 60 * 1000);
            Date fifteenMinutesAgo = new Date(fifteenMinutesAgoMillis);
            temperature.setStartTime(fifteenMinutesAgo);
            temperature.setEndTime(cmcTemperature.getStartTime());
            QueryWrapper qw1 = getQW(temperature, Info);
            temperatureList = cmcTemperatureService.list(qw1);
            temperatureList.addAll(records);
            if (channelSize > 30)
                for (int i = 0; i < temperatureList.size() - channelSize + 1; i = i + channelSize) {
                    Date minTime = temperatureList.get(i).getDatetime();
                    if (temperatureList.get(i + 10).getDatetime().getTime() > minTime.getTime()) {
                        for (int j = i + 10; j < i + channelSize; j ++) {
                            temperatureList.get(j).setDatetime(minTime);
                        }
                    }
                    else {
                        if (temperatureList.get(i + 30).getDatetime().getTime() > minTime.getTime()) {
                            for (int j = i + 30; j < i + channelSize; j++) {
                                temperatureList.get(j).setDatetime(minTime);
                            }
                        }
                    }
                }
                if (page.getPageNumber() > 1)
                    temperatureList.subList(0, (int) (page.getPageSize() * (page.getPageNumber() - 1))).clear();
                else
                    temperatureList.subList((int) page.getPageSize() * 1, temperatureList.size()).clear();
        }
        return getPageData(temperatureList).total(temperatureList.size() / channelSize);
    }

    private QueryWrapper getQW(CmcTemperature cmcTemperature, String Info) {
        QueryWrapper qw = QueryWrapper.create();
        qw.select(CmcTemperature::getId, CmcTemperature::getGroupNo, CmcTemperature::getSensorNo, CmcTemperature::getData, CmcTemperature::getVolt, CmcTemperature::getCSQ, CmcTemperature::getDatetime)
            .select(CmcChannel::getInfo)
            .from(CmcTemperature.class)
            .leftJoin(CmcChannel.class)
            .on(CmcChannel::getSensorNo, CmcTemperature::getSensorNo)
            .where(CmcChannel::getInfo).like(Info)
            .and(CmcTemperature::getId).eq(cmcTemperature.getId())
            .and(CmcTemperature::getGroupNo).eq(cmcTemperature.getGroupNo())
            .and(CmcTemperature::getSensorNo).eq(cmcTemperature.getSensorNo())
            .and(CmcTemperature::getData).eq(cmcTemperature.getData())
            .and(CmcTemperature::getVolt).eq(cmcTemperature.getVolt())
            .and(CmcTemperature::getCSQ).eq(cmcTemperature.getCSQ())
            .and(CmcTemperature::getDatetime).ge(cmcTemperature.getStartTime())
            .and(CmcTemperature::getDatetime).le(cmcTemperature.getEndTime())
            .orderBy(CmcTemperature::getDatetime).asc()
            .orderBy(CmcTemperature::getSensorNo).asc();
        return qw;
    }

}