<?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.bonus.canteen.core.basic.mapper.BasicAreaMapper">
    <resultMap type="com.bonus.canteen.core.basic.domain.BasicArea" id="BasicAreaResult">
        <result property="areaId"    column="area_id"    />
        <result property="areaName"    column="area_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="manager"    column="manager"    />
        <result property="description"    column="description"    />
        <result property="contactTel"    column="contact_tel"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectBasicAreaVo">
        select area_id, area_name, parent_id, manager, description, contact_tel, del_flag,
               create_by, create_time, update_by, update_time
        from basic_area
    </sql>

    <select id="selectBasicAreaList" parameterType="com.bonus.canteen.core.basic.domain.BasicArea" resultMap="BasicAreaResult">
        <include refid="selectBasicAreaVo"/>
        <where>  
            <if test="areaName != null  and areaName != ''"> and area_name like concat('%', #{areaName}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="manager != null  and manager != ''"> and manager = #{manager}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="contactTel != null  and contactTel != ''"> and contact_tel = #{contactTel}</if>
            <if test="delFlag != null  and delFlag != ''"> and del_flag = #{delFlag}</if>
        </where>
    </select>

    <select id="selectBasicAreaByAreaName" parameterType="String" resultMap="BasicAreaResult">
        <include refid="selectBasicAreaVo"/>
        where del_flag = 0 and area_name = #{areaName}
    </select>
    
    <select id="selectBasicAreaByAreaId" parameterType="Long" resultMap="BasicAreaResult">
        <include refid="selectBasicAreaVo"/>
        where del_flag = 0 and area_id = #{areaId}
    </select>
        
    <insert id="insertBasicArea" parameterType="com.bonus.canteen.core.basic.domain.BasicArea" useGeneratedKeys="true" keyProperty="areaId">
        insert into basic_area
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaName != null and areaName != ''">area_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="manager != null">manager,</if>
            <if test="description != null">description,</if>
            <if test="contactTel != null">contact_tel,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaName != null and areaName != ''">#{areaName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="manager != null">#{manager},</if>
            <if test="description != null">#{description},</if>
            <if test="contactTel != null">#{contactTel},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateBasicArea" parameterType="com.bonus.canteen.core.basic.domain.BasicArea">
        update basic_area
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaName != null and areaName != ''">area_name = #{areaName},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="manager != null">manager = #{manager},</if>
            <if test="description != null">description = #{description},</if>
            <if test="contactTel != null">contact_tel = #{contactTel},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where area_id = #{areaId}
    </update>

    <delete id="deleteBasicAreaByAreaId" parameterType="Long">
        update basic_area set del_flag = '2' where area_id = #{areaId}
    </delete>

    <delete id="deleteBasicAreaByAreaIds" parameterType="String">
        update basic_area set del_flag = '2' where area_id in
        <foreach item="areaId" collection="array" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </delete>
</mapper>