package cn.iocoder.yudao.module.iot.dal.mysql.mqttrecord;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.iot.controller.admin.mqttrecord.vo.MqttRecordPageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.mqttrecord.MqttRecordDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 数采记录 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface MqttRecordMapper extends BaseMapperX<MqttRecordDO> {

    default PageResult<MqttRecordDO> selectPage(MqttRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MqttRecordDO>()
                .likeIfPresent(MqttRecordDO::getMachineName, reqVO.getMachineName())
                .betweenIfPresent(MqttRecordDO::getDeviceDataTime, reqVO.getDeviceDataTime())
                .betweenIfPresent(MqttRecordDO::getDeviceDataTimeLong, reqVO.getDeviceDataTimeLong())
                .eqIfPresent(MqttRecordDO::getDeviceData, reqVO.getDeviceData())
                .eqIfPresent(MqttRecordDO::getDeviceCode, reqVO.getDeviceCode())
                .likeIfPresent(MqttRecordDO::getDeviceName, reqVO.getDeviceName())
                .likeIfPresent(MqttRecordDO::getWorkerName, reqVO.getWorkerName())
                .eqIfPresent(MqttRecordDO::getGatewayCode, reqVO.getGatewayCode())
                .betweenIfPresent(MqttRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MqttRecordDO::getId));
    }
    default MqttRecordDO selectLastOne(String code) {
        return selectOne(new LambdaQueryWrapperX<MqttRecordDO>()
                .eqIfPresent(MqttRecordDO::getDeviceCode, code)
                .orderByDesc(MqttRecordDO::getId)
                .last("limit 1")
        );
    }
}