<?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.AreaMapper">
    
    <resultMap type="com.ruoyi.ems.domain.Area" id="areaResult">
        <result property="id"    column="id"    />
        <result property="areaCode"    column="area_code"    />
        <result property="parentCode"    column="parent_code"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="areaName"    column="area_name"    />
        <result property="shortName"    column="short_name"    />
        <result property="desc"    column="desc"    />
        <result property="orderNum"    column="order_num"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectAreaVo">
        select id, area_code, parent_code, ancestors, area_name, short_name, `desc`, order_num, status from adm_area
    </sql>

    <select id="selectAreaList" parameterType="com.ruoyi.ems.domain.Area" resultMap="areaResult">
        <include refid="selectAreaVo"/>
        <where>  
            <if test="areaCode != null  and areaCode != ''"> and area_code = #{areaCode}</if>
            <if test="parentCode != null  and parentCode != ''"> and parent_code = #{parentCode}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="areaName != null  and areaName != ''"> and area_name like concat('%', #{areaName}, '%')</if>
            <if test="shortName != null  and shortName != ''"> and short_name like concat('%', #{shortName}, '%')</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>

    <select id="selectByCodes" resultMap="areaResult">
        select id, area_code, parent_code, ancestors, area_name, short_name, `desc`, order_num, status from adm_area
        <where>
            area_code in
            (
            <foreach collection="set" item="areaCode" index="index" separator=",">
                #{areaCode}
            </foreach>
            )
        </where>
    </select>

    <select id="selectAreaByTag" resultMap="areaResult">
        select
            a.id, a.area_code, a.parent_code, a.ancestors, a.area_name, a.short_name, a.`desc`, a.order_num, a.status
        from adm_area a
          inner join adm_obj_tag_rel r on a.area_code = r.obj_code and r.obj_type = 1
        where r.tag_code = #{tagCode}
    </select>

    <select id="selectAreaById" parameterType="Long" resultMap="areaResult">
        <include refid="selectAreaVo"/>
        where id = #{id}
    </select>

    <select id="selectAreaByCode" parameterType="String" resultMap="areaResult">
        <include refid="selectAreaVo"/>
        where area_code = #{areaCode}
    </select>

    <insert id="insertArea" parameterType="com.ruoyi.ems.domain.Area" useGeneratedKeys="true" keyProperty="id">
        insert into adm_area
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="parentCode != null">parent_code,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="areaName != null and areaName != ''">area_name,</if>
            <if test="shortName != null">short_name,</if>
            <if test="desc != null">`desc`,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="parentCode != null">#{parentCode},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="areaName != null and areaName != ''">#{areaName},</if>
            <if test="shortName != null">#{shortName},</if>
            <if test="desc != null">#{desc},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>

    <update id="updateArea" parameterType="com.ruoyi.ems.domain.Area">
        update adm_area
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="parentCode != null">parent_code = #{parentCode},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="areaName != null and areaName != ''">area_name = #{areaName},</if>
            <if test="shortName != null">short_name = #{shortName},</if>
            <if test="desc != null">`desc` = #{desc},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAreaById" parameterType="Long">
        delete from adm_area where id = #{id}
    </delete>

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