<?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.baseinfo.monitorpoint.mapper.MonitorPointMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="monitorPointInfoResultMap"
               type="org.springblade.modules.baseinfo.monitorpoint.entity.MonitorPointInfoEntity">

        <result column="mp_cd" property="mpCd"/>
        <result column="mp_nm" property="mpNm"/>
        <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="ts" property="ts"/>
        <result column="nt" property="nt"/>

    </resultMap>

    <resultMap id="monitorPointInfoVOResultMap"
               type="org.springblade.modules.baseinfo.monitorpoint.vo.MonitorPointInfoVO">

        <result column="mp_cd" property="mpCd"/>
        <result column="mp_nm" property="mpNm"/>
        <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="ts" property="ts"/>
        <result column="nt" property="nt"/>
        <result column="org_id" property="orgId"/>
        <result column="dept_name" property="orgName"/>
        <result column="wiu_cd" property="wiuCd"/>
        <result column="wiu_nm" property="wiuNm"/>
        <result column="wiust_cd" property="wiustCd"/>
        <result column="wiust_nm" property="wiustNm"/>
    </resultMap>

    <select id="selectPage" resultMap="monitorPointInfoVOResultMap">
        SELECT
        n.*,w.wiust_cd,w.wiust_nm,u.wiu_cd,u.wiu_nm,d.dept_name,u.org_id
        FROM
        base_mp_info n
        LEFT JOIN base_wiust_mp_rel wp ON wp.mp_cd = n.mp_cd and wp.is_deleted=0
        LEFT JOIN base_wiust_info w ON w.wiust_cd = wp.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 blade_dept d ON d.id = u.org_id and d.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="dto.mpNm!=null">
            and n.mp_nm like concat(concat('%', #{dto.mpNm}), '%')
        </if>
        <if test="dto.mpCd!=null">
            and n.mp_cd like concat(concat('%', #{dto.mpCd}), '%')
        </if>
        <if test="dto.wiustNm!=null">
            and w.wiust_nm like concat(concat('%', #{dto.wiustNm}), '%')
        </if>
        <if test="dto.orgId!=null">
            and (d.id =#{dto.orgId} or FIND_IN_SET(#{dto.orgId} ,d.ancestors) >0 )
        </if>
        order by n.mp_cd
    </select>

    <select id="gtMonitorPointCount" resultType="java.lang.Long">
        SELECT
        count(n.mp_cd)
        FROM
        base_mp_info n
        LEFT JOIN base_wiust_mp_rel wp ON wp.mp_cd = n.mp_cd and wp.is_deleted=0
        LEFT JOIN base_wiust_info w ON w.wiust_cd = wp.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 blade_dept d ON d.id = u.org_id and d.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="dto.orgId!=null">
            and (d.id =#{dto.orgId} or FIND_IN_SET(#{dto.orgId} ,d.ancestors) >0 )
        </if>
    </select>

</mapper>
