package com.ruoyi.monitoring.controller;

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

import java.util.List;

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

    @Inject
    ICmcTemperatureService cmcTemperatureService;

    /**
     * 查询水温监测列表
     */
    @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 List<CmcTemperature> listByInfo(CmcTemperature cmcTemperature, String Info, Integer pageSize, Integer pageNum) {
        QueryWrapper qw = getQW(cmcTemperature, Info, pageSize, pageNum);
        return cmcTemperatureService.list(qw);
    }

    private QueryWrapper getQW(CmcTemperature cmcTemperature, String Info, Integer pageSize, Integer pageNum) {
        QueryWrapper qw = QueryWrapper.create();
        qw.select(CmcTemperature::getId, CmcTemperature::getGroupNo, CmcTemperature::getSensorNo, CmcTemperature::getData, CmcTemperature::getVolt, CmcTemperature::getCSQ, CmcTemperature::getDatetime)
            .from(CmcTemperature.class)
            .leftJoin(CmcChannel.class)
            .on(CmcChannel::getSensorNo, CmcTemperature::getSensorNo)
            .where(CmcChannel::getInfo).eq(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).desc().limit((pageNum - 1) * pageSize, pageSize);
        return qw;
    }

}