<?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.monitorsite.mapper.MonitorSiteMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="monitorSiteInfoResultMap"
               type="org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteInfoEntity">

        <result column="wiust_cd" property="wiustCd"/>
        <result column="wiust_nm" property="wiustNm"/>
        <result column="wiust_tp" property="wiustTp"/>
        <result column="loc" property="loc"/>
        <result column="dat_tp" property="datTp"/>
        <result column="dat_elev" property="datElev"/>
        <result column="mod_bas_val" property="modBasVal"/>
        <result column="mod_para" property="modPara"/>
        <result column="well_tp" property="wellTp"/>
        <result column="well_land_elev" property="wellLandElev"/>
        <result column="est_st_ym" property="estStYm"/>
        <result column="run_cond" property="runCond"/>
        <result column="eng_man_cd" property="engManCd"/>
        <result column="water_type" property="waterType"/>
        <result column="use_type" property="useType"/>
        <result column="ts" property="ts"/>
        <result column="nt" property="nt"/>
        <result column="site_cert_id" property="siteCertId"/>
        <result column="site_owner" property="siteOwner"/>
        <result column="data_share_active" property="dataShareActive"/>

    </resultMap>

    <resultMap id="monitorSiteInfoVOResultMap"
               type="org.springblade.modules.baseinfo.monitorsite.vo.MonitorSiteInfoVO">

        <result column="wiust_cd" property="wiustCd"/>
        <result column="wiust_nm" property="wiustNm"/>
        <result column="wiust_tp" property="wiustTp"/>
        <result column="loc" property="loc"/>
        <result column="dat_tp" property="datTp"/>
        <result column="dat_elev" property="datElev"/>
        <result column="mod_bas_val" property="modBasVal"/>
        <result column="mod_para" property="modPara"/>
        <result column="well_tp" property="wellTp"/>
        <result column="well_land_elev" property="wellLandElev"/>
        <result column="est_st_ym" property="estStYm"/>
        <result column="run_cond" property="runCond"/>
        <result column="eng_man_cd" property="engManCd"/>
        <result column="water_type" property="waterType"/>
        <result column="use_type" property="useType"/>
        <result column="ts" property="ts"/>
        <result column="nt" property="nt"/>
        <result column="site_cert_id" property="siteCertId"/>
        <result column="site_owner" property="siteOwner"/>
        <result column="data_share_active" property="dataShareActive"/>
        <result column="wiu_cd" property="wiuCd"/>
        <result column="wiu_nm" property="wiuNm"/>
        <result column="org_id" property="orgId"/>
        <result column="dept_name" property="orgName"/>

    </resultMap>

    <select id="selectPage" resultMap="monitorSiteInfoVOResultMap">
        SELECT
        n.*,u.wiu_cd,u.wiu_nm,u.org_id,d.dept_name
        FROM
        base_wiust_info n
        LEFT JOIN base_wiu_wiust_rel ww ON ww.wiust_cd = n.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.wiustNm!=null">
            and n.wiust_nm like concat(concat('%', #{dto.wiustNm}), '%')
        </if>
        <if test="dto.wiustCd!=null">
            and n.wiust_cd like concat(concat('%', #{dto.wiustCd}), '%')
        </if>
        <if test="dto.wiuNm!=null">
            and u.wiu_nm like concat(concat('%', #{dto.wiuNm}), '%')
        </if>
        <if test="dto.orgId!=null">
            and (d.id =#{dto.orgId} or  FIND_IN_SET(#{dto.orgId} ,d.ancestors) >0 )
        </if>
        order by n.wiust_cd
    </select>

    <select id="selectAll" resultMap="monitorSiteInfoResultMap">
        SELECT
        n.*
        FROM
        base_wiust_info n
        WHERE
        n.is_deleted = 0
        <if test="dto.wiustNm!=null">
            and n.wiust_nm like concat(concat('%', #{dto.wiustNm}), '%')
        </if>
        <if test="dto.wiustCd!=null">
            and n.wiust_cd like concat(concat('%', #{dto.wiustCd}), '%')
        </if>
        <if test="dto.dataShareActive!=null">
            and n.data_share_active = #{dto.dataShareActive}
        </if>
        order by n.wiust_cd
    </select>

    <select id="getMonitorSiteCount" resultType="java.lang.Long">
        SELECT count(*) FROM
        ( SELECT
        n.wiust_cd
        FROM
        base_wiust_info n
        LEFT JOIN base_wiu_wiust_rel ww ON ww.wiust_cd = n.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>
        group by n.wiust_cd ) a
    </select>
</mapper>
