<?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="mp_cd" 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"/>
    </resultMap>


    <resultMap id="waterFlowRateVOResultMap" type="org.springblade.modules.business.monitor.vo.WaterFlowRateInfoVO">
        <result column="mp_cd" 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="mp_nm" property="mpnm"/>
        <result column="run_status_id" property="runStatusId"/>
        <result column="last_up_time" property="lastUpTime"/>
        <result column="lng" property="lng"/>
        <result column="lat" property="lat"/>
        <result column="mp_addr" property="mpAddr"/>
        <result column="mp_item" property="mpItem"/>
        <result column="is_auto" property="isAuto"/>
        <result column="sour_tp" property="sourTp"/>
        <result column="wiust_nm" property="wiustNm"/>
        <result column="wiu_nm" property="wiuNm"/>
    </resultMap>


    <select id="selectPage" resultMap="waterFlowRateVOResultMap">
--         SELECT
--         s.*,i.mp_cd,n.tm,n.mp_q,n.acc_w,n.in_stp_q,n.acc_pq_w,n.spe_reg_data,n.ts,n.packet_uuid,n.hour_w,i.mp_nm,i.lat,i.lng,i.mp_addr,i.mp_item,i.is_auto,i.sour_tp,w.wiust_cd,w.wiust_nm,u.wiu_cd,u.wiu_nm
--         FROM
--         rtu_status s
--         LEFT JOIN base_wiust_info w ON w.wiust_cd = s.rtu_code and w.is_deleted=0
--         LEFT JOIN base_wiust_mp_rel wm ON wm.wiust_cd = w.wiust_cd and wm.is_deleted=0
--         LEFT JOIN base_mp_info i ON i.mp_cd = wm.mp_cd and i.is_deleted=0
--         LEFT JOIN data_mp_qr_cache n ON n.mp_cd = i.mp_cd and n.is_deleted=0
--         LEFT JOIN base_wiu_wiust_rel ww ON ww.wiust_cd = w.wiust_cd and ww.is_deleted=0
--         LEFT JOIN base_wr_wiu_info u ON u.wiu_cd = ww.wiu_cd and u.is_deleted=0
--         LEFT JOIN rtu_info r ON r.rtu_code = s.rtu_code and r.is_deleted = 0
--         LEFT JOIN blade_dept d ON d.id = r.org_id and d.is_deleted=0
--         WHERE
--         s.is_deleted = 0
        SELECT
        i.*,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,w.wiust_cd,w.wiust_nm,u.wiu_cd,u.wiu_nm,s.run_status_id,s.last_up_time
        FROM
        base_mp_info i
        LEFT JOIN data_mp_qr_cache n ON n.mp_cd = i.mp_cd and n.is_deleted=0
        LEFT JOIN base_wiust_mp_rel wm ON wm.mp_cd = i.mp_cd and wm.is_deleted=0
        LEFT JOIN base_wiust_info w ON w.wiust_cd = wm.wiust_cd and w.is_deleted=0
        LEFT JOIN base_wiu_wiust_rel ww ON ww.wiust_cd = w.wiust_cd and ww.is_deleted=0
        LEFT JOIN base_wr_wiu_info u ON u.wiu_cd = ww.wiu_cd and u.is_deleted=0
        LEFT JOIN rtu_info r ON r.rtu_code = w.wiust_cd and r.is_deleted = 0
        LEFT JOIN blade_dept d ON d.id = r.org_id and d.is_deleted=0
        LEFT JOIN rtu_status s ON s.rtu_code = r.rtu_code and s.is_deleted=0
        WHERE
        i.is_deleted =0
        <if test="dto.wiuNm!=null ">
            and u.wiu_nm LIKE concat(concat('%', #{dto.wiuNm}), '%')
        </if>
        <if test="dto.mpnm!=null ">
            and i.mp_nm LIKE concat(concat('%', #{dto.mpnm}), '%')
        </if>
        <if test="dto.mpcd!=null">
            and i.mp_cd like concat(concat('%', #{dto.mpcd}), '%')
        </if>
        <if test="dto.orgId!=null">
            and (r.org_id =#{dto.orgId} or  FIND_IN_SET(#{dto.orgId} ,d.ancestors) >0 )
        </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,w.wiust_cd
            </otherwise>
        </choose>
    </select>
</mapper>
