package com.zsc.edu.gateway.modules.iot.record.repo;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zsc.edu.gateway.modules.iot.record.entity.RecordData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhuang
 */
public interface RecordDataRepository extends BaseMapper<RecordData> {
    @Select("SELECT COUNT(*) FROM iot_record_data WHERE content->>'warning' IS NOT NULL AND (content->>'warning')::int & 1 = 0 AND ((content->>'warning')::int & ~1) > 0")
    long countWarnings();

    @Select("SELECT COUNT(*) FROM iot_record_data WHERE content->>'warning' IS NOT NULL AND ((content->>'warning')::int & 1 = 0) AND ((content->>'warning')::int & ~1) > 0 AND record_time >= #{todayStart}")
    long countTodayWarnings(@Param("todayStart") LocalDateTime todayStart);

    @Select("SELECT COUNT(*) FROM iot_record_data WHERE content->>'warning' IS NOT NULL AND (content->>'warning')::int & #{bitPosition} = #{bitPosition}")
    long countWarningsByBit(@Param("bitPosition") int bitPosition);

    List<RecordData> selectByClientId(@Param("clientId") String clientId);
}
