<?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="com.ruoyi.ems.mapper.AreaBuildingZoningMapper">
    
    <resultMap type="com.ruoyi.ems.domain.AreaBuildingZoning" id="AreaBuildingZoningResult">
        <result property="id"    column="id"    />
        <result property="bldgCode"    column="bldg_code"    />
        <result property="bldgName"    column="bldg_name"    />
        <result property="zoningCode"    column="zoning_code"    />
        <result property="zoningName"    column="zoning_name"    />
        <result property="floor"    column="floor"    />
        <result property="bldgLdArea"    column="bldg_ld_area"    />
        <result property="usageDetail"    column="usage_detail"    />
        <result property="tagCodes"    column="tag_codes"    />
        <result property="tagNames"    column="tag_names"    />
    </resultMap>

    <sql id="selectAreaBuildingZoningVo">
        select
            z.`id`,
            z.`bldg_code`,
            b.`bldg_name`,
            z.`zoning_code`,
            z.`zoning_name`,
            z.`floor`,
            z.`bldg_ld_area`,
            z.`usage_detail`,
            COALESCE(GROUP_CONCAT(r.tag_code SEPARATOR ','), '') AS tag_codes,
            COALESCE(GROUP_CONCAT(t.tag_name SEPARATOR ','), '') AS tag_names
        from adm_area_building_zoning z
            LEFT JOIN adm_area_building b ON z.bldg_code = b.bldg_code
            LEFT JOIN adm_obj_tag_rel r ON z.zoning_code = r.obj_code
            LEFT JOIN dim_ems_tag t ON t.tag_code = r.tag_code
    </sql>

    <select id="selectAreaBuildingZoningList" parameterType="com.ruoyi.ems.domain.AreaBuildingZoning" resultMap="AreaBuildingZoningResult">
        <include refid="selectAreaBuildingZoningVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''"> and b.`area_code` = #{areaCode}</if>
            <if test="bldgCode != null  and bldgCode != ''"> and z.`bldg_code` = #{bldgCode}</if>
            <if test="zoningCode != null  and zoningCode != ''"> and z.`zoning_code` = #{zoningCode}</if>
            <if test="zoningName != null  and zoningName != ''"> and z.`zoning_name` like concat('%', #{zoningName}, '%')</if>
            <if test="floor != null "> and z.`floor` = #{floor}</if>
            <if test="bldgLdArea != null "> and z.`bldg_ld_area` = #{bldgLdArea}</if>
            <if test="usageDetail != null  and usageDetail != ''"> and z.`usage_detail` = #{usageDetail}</if>
            <if test="tagCodeList != null"> and r.tag_code in
              <foreach item="code" collection="tagCodeList" open="(" separator="," close=")">
                #{code}
              </foreach>
             </if>
        </where>
        GROUP BY z.zoning_code
        ORDER BY bldg_code,zoning_code
    </select>
    
    <select id="selectAreaBuildingZoningById" parameterType="Long" resultMap="AreaBuildingZoningResult">
        <include refid="selectAreaBuildingZoningVo"/>
        where z.`id` = #{id}
    </select>

    <select id="selectAreaBuildingZoningByCode" parameterType="String" resultMap="AreaBuildingZoningResult">
        <include refid="selectAreaBuildingZoningVo"/>
        where z.`zoning_code` = #{code}
    </select>

    <select id="selectByCodes"  resultMap="AreaBuildingZoningResult">
        select
            z.`id`,
            z.`bldg_code`,
            z.`zoning_code`,
            z.`zoning_name`,
            z.`floor`,
            z.`bldg_ld_area`,
            z.`usage_detail`
        from adm_area_building_zoning z
        where z.`zoning_code` = in
        <foreach item="code" collection="codes" open="(" separator="," close=")">
            #{code}
        </foreach>
    </select>

    <insert id="insertAreaBuildingZoning" parameterType="com.ruoyi.ems.domain.AreaBuildingZoning" useGeneratedKeys="true" keyProperty="id">
        insert into adm_area_building_zoning
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bldgCode != null and bldgCode != ''">bldg_code,</if>
            <if test="zoningCode != null and zoningCode != ''">zoning_code,</if>
            <if test="zoningName != null and zoningName != ''">zoning_name,</if>
            <if test="floor != null">floor,</if>
            <if test="bldgLdArea != null">bldg_ld_area,</if>
            <if test="usageDetail != null">usage_detail,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="bldgCode != null and bldgCode != ''">#{bldgCode},</if>
            <if test="zoningCode != null and zoningCode != ''">#{zoningCode},</if>
            <if test="zoningName != null and zoningName != ''">#{zoningName},</if>
            <if test="floor != null">#{floor},</if>
            <if test="bldgLdArea != null">#{bldgLdArea},</if>
            <if test="usageDetail != null">#{usageDetail},</if>
         </trim>
    </insert>

    <update id="updateAreaBuildingZoning" parameterType="com.ruoyi.ems.domain.AreaBuildingZoning">
        update adm_area_building_zoning
        <trim prefix="SET" suffixOverrides=",">
            <if test="bldgCode != null and bldgCode != ''">bldg_code = #{bldgCode},</if>
            <if test="zoningCode != null and zoningCode != ''">zoning_code = #{zoningCode},</if>
            <if test="zoningName != null and zoningName != ''">zoning_name = #{zoningName},</if>
            <if test="floor != null">floor = #{floor},</if>
            <if test="bldgLdArea != null">bldg_ld_area = #{bldgLdArea},</if>
            <if test="usageDetail != null">usage_detail = #{usageDetail},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAreaBuildingZoningById" parameterType="Long">
        delete from adm_area_building_zoning where id = #{id}
    </delete>

    <delete id="deleteAreaBuildingZoningByBuilding" parameterType="java.lang.String">
        delete from adm_area_building_zoning where bldg_code = #{code}
    </delete>

    <delete id="deleteAreaBuildingZoningByIds" parameterType="String">
        delete from adm_area_building_zoning where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>