<?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="org.springblade.modules.business.monitor.mapper.WaterFlowRateMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="waterFlowRateResultMap"
               type="org.springblade.modules.business.monitor.entity.WaterFlowRateInfoEntity">
        <result column="rtu_code" property="mpcd"/>
        <result column="tm" property="tm"/>
        <result column="mp_q" property="mpq"/>
        <result column="acc_w" property="accw"/>
        <result column="in_stp_q" property="instpq"/>
        <result column="acc_pq_w" property="accpqw"/>
        <result column="spe_reg_data" property="speregdata"/>
        <result column="ts" property="ts"/>
        <result column="packet_uuid" property="packetuuid"/>
        <result column="hour_w" property="hourw"/>
        <result column="water_level" property="waterLevel"/>
        <result column="flow_speed" property="flowSpeed"/>
    </resultMap>


    <resultMap id="waterFlowRateVOResultMap" type="org.springblade.modules.business.monitor.vo.WaterFlowRateInfoVO">
        <result column="rtu_code" property="mpcd"/>
        <result column="tm" property="tm"/>
        <result column="mp_q" property="mpq"/>
        <result column="acc_w" property="accw"/>
        <result column="in_stp_q" property="instpq"/>
        <result column="acc_pq_w" property="accpqw"/>
        <result column="spe_reg_data" property="speregdata"/>
        <result column="qr_ts" property="ts"/>
        <result column="packet_uuid" property="packetuuid"/>
        <result column="hour_w" property="hourw"/>
        <result column="water_level" property="waterLevel"/>
        <result column="flow_speed" property="flowSpeed"/>

        <result column="rtu_name" property="mpnm"/>
        <result column="run_status_id" property="runStatusId"/>
        <result column="last_up_time" property="lastUpTime"/>

        <result column="project_name" property="projectName"/>

    </resultMap>


    <select id="selectPage" resultMap="waterFlowRateVOResultMap">
        SELECT
        i.*,n.water_level,n.flow_speed,n.tm,n.mp_q,n.acc_w,n.in_stp_q,n.acc_pq_w,n.spe_reg_data,n.ts qr_ts,n.packet_uuid,n.hour_w,s.run_status_id,s.last_up_time,p.project_name
        FROM
        rtu_info i
        LEFT JOIN  data_mp_qr_cache n ON i.rtu_code = n.mp_cd and n.is_deleted=0
        LEFT JOIN rtu_status s ON s.rtu_code = i.rtu_code and s.is_deleted=0
        LEFT JOIN project_info p ON p.id = i.project_id and p.is_deleted=0
        WHERE
        i.is_deleted =0
        <if test="dto.projectId!=null ">
            and i.project_id = #{dto.projectId}
        </if>
        <if test="dto.mpnm!=null ">
            and i.rtu_name LIKE concat(concat('%', #{dto.mpnm}), '%')
        </if>
        <if test="dto.mpcd!=null">
            and i.rtu_code like concat(concat('%', #{dto.mpcd}), '%')
        </if>
        <choose>
            <when test="dto.order!=null and dto.order=='ascending'">
                <if test="dto.prop!=null and dto.prop=='tm'">
                    order by n.tm
                </if>
            </when>
            <when test="dto.order!=null and dto.order=='descending'">
                <if test="dto.prop!=null and dto.prop=='tm'">
                    order by n.tm desc
                </if>
            </when>
            <otherwise>
                order by s.run_status_id,n.tm desc
            </otherwise>
        </choose>
    </select>
</mapper>
