<?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.wateruseconsumer.mapper.WaterUseConsumerMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="consumerInfoResultMap"
               type="org.springblade.modules.baseinfo.wateruseconsumer.entity.WaterUseConsumerInfoEntity">

        <result column="wiu_cd" property="wiuCd"/>
        <result column="wiu_nm" property="wiuNm"/>
        <result column="lr_nm" property="lrNm"/>
        <result column="trad_tp" property="tradTp"/>
        <result column="eco_tp" property="ecoTp"/>
        <result column="mon_level" property="monLevel"/>
        <result column="wiu_tp" property="wiuTp"/>
        <result column="adl_cd" property="adlCd"/>
        <result column="ts" property="ts"/>
        <result column="nt" property="nt"/>
        <result column="org_id" property="orgId"/>
        <result column="addr" property="addr"/>
    </resultMap>

    <resultMap id="consumerInfoVOResultMap"
               type="org.springblade.modules.baseinfo.wateruseconsumer.vo.WaterUseConsumerInfoVO">

        <result column="wiu_cd" property="wiuCd"/>
        <result column="wiu_nm" property="wiuNm"/>
        <result column="lr_nm" property="lrNm"/>
        <result column="trad_tp" property="tradTp"/>
        <result column="eco_tp" property="ecoTp"/>
        <result column="mon_level" property="monLevel"/>
        <result column="wiu_tp" property="wiuTp"/>
        <result column="adl_cd" property="adlCd"/>
        <result column="ts" property="ts"/>
        <result column="nt" property="nt"/>
        <result column="org_id" property="orgId"/>
        <result column="dept_name" property="orgName"/>
        <result column="addr" property="addr"/>
    </resultMap>

    <select id="selectPage" resultMap="consumerInfoVOResultMap">
        SELECT
        n.*,d.dept_name
        FROM
        base_wr_wiu_info n
        LEFT JOIN blade_dept d ON d.id = n.org_id and d.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="dto.wiuNm!=null">
            and n.wiu_nm like concat(concat('%', #{dto.wiuNm}), '%')
        </if>
        <if test="dto.wiuCd!=null">
            and n.wiu_cd like concat(concat('%', #{dto.wiuCd}), '%')
        </if>
        <if test="dto.orgId!=null">
            and (d.id =#{dto.orgId} or  FIND_IN_SET(#{dto.orgId} ,d.ancestors) >0 )
        </if>
        order by n.wiu_cd
    </select>

    <select id="selectAll" resultMap="consumerInfoResultMap">
        SELECT
        n.*
        FROM
        base_wr_wiu_info n
        WHERE
        n.is_deleted = 0
        <if test="entity.wiuNm!=null">
            and n.wiu_nm like concat(concat('%', #{entity.wiuNm}), '%')
        </if>
        <if test="entity.wiuCd!=null">
            and n.wiu_cd like concat(concat('%', #{entity.wiuCd}), '%')
        </if>
        order by n.wiu_cd
    </select>

    <select id="getWaterUseConsumerCount" resultType="java.lang.Long">
        SELECT count(*) FROM
        ( SELECT
        w.wiu_cd
        FROM
        base_wr_wiu_info w
        LEFT JOIN blade_dept d ON d.id = w.org_id and d.is_deleted=0
        WHERE
        w.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 w.wiu_cd ) a
    </select>
</mapper>
