<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.limap.core.screen.mapper.ScreenMapper">

    <select id="getUserPonds" resultType="com.limap.core.screen.vo.PondVo">
        SELECT id id,pond_name pondName FROM aqu_pond where user_id = #{userId}
    </select>

    <select id="getUserEquipments" resultType="com.limap.core.screen.vo.AppEquipmentVo">
        select id as id,user_id userId,iot_id iotId, serial_num serialNum,device_name deviceName,device_type deviceType,bind_time bindTime,dead_time deadTime,
        pond_id pondId,is_oxygen_used oxygenUsed,value_dissolved_oxygen dissolvedOxygen,value_temperature temperature,value_saturability saturability,
        ROUND(CAST(value_ph AS numeric),2) ph,value_salinity salinity from aqu_device where user_id = #{userId}
        <if test="deviceType!= null"> and device_type = #{deviceType}::smallint    </if>
        <if test="pondIds != null">
            and pond_id in
            <foreach collection="pondIds" item="pondId" open="(" separator="," close=")">
                #{pondId}
            </foreach>
        </if>
    </select>

    <select id="getEquipmentById" resultType="com.limap.core.screen.vo.AppEquipmentVo">
        select id as id,user_id userId,iot_id iotId, serial_num serialNum,device_name deviceName,device_type deviceType,bind_time bindTime,dead_time deadTime,
        pond_id pondId,is_oxygen_used oxygenUsed,value_dissolved_oxygen dissolvedOxygen,value_temperature temperature,value_saturability saturability,
        ROUND(CAST(value_ph AS numeric),2) ph,value_salinity salinity,oxy_warn_call_open oxyWarnCallOpen,oxy_warn_lower oxyWarnLower,
        oxy_warn_call_no_dis oxyWarnCallNoDis,oxy_warn_lower oxWarnLower,temp_warn_call_open tempWarnCallOpen,
        temp_warn_call_no_dis tempWarnCallNoDis,temp_warn_upper tempWarnUpper,temp_warn_lower tempWarnLower,input_voltage inputVoltage,
        warn_code warnCode
        from aqu_device
        <where>
            <if test="deviceId!= null"> and id = #{deviceId}    </if>
            <if test="deviceName != null"> and serial_num = #{deviceName}    </if>
        </where>

    </select>

    <select id="getWarnList" resultType="com.limap.core.screen.vo.WarnVo">
        SELECT id as id,title title,message message,create_time creatTime
        FROM aqu_message_warn
        where user_id = #{userId}
        order by create_time desc
    </select>
    <select id="getWarnListCount" resultType="java.lang.Long">
        SELECT count(*)
        FROM aqu_message_warn
        where user_id = #{userId}
    </select>

    <select id="getControlSwitch" resultType="com.limap.core.screen.vo.ControlSwitch">
        SELECT id id,device_id deviceId,"index" "index",switch_name switchName,detect_electric_value detectElectricValue,detect_voltage_value detectVoltageValue
        ,connect_voltage_type connectVoltageType,pond_id pondId,rate_electric_value rateElectricValue,is_open openStatus
        FROM aqu_device_switch
        <where>
            <if test="deviceId!= null"> and device_id = #{deviceId}    </if>
        </where>
    </select>

    <select id="getWarnListByDevice" resultType="com.limap.core.screen.vo.WarnVo">

        SELECT id as id,title title,message message,create_time creatTime
        FROM aqu_message_warn
        where device_id = #{deviceId}
        order by create_time desc
    </select>

    <select id="getWarnListCountByDevice" resultType="java.lang.Long">
        SELECT count(*)
        FROM aqu_message_warn
        where device_id = #{deviceId}
    </select>

</mapper>
