<?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.ghy.system.mapper.SysAreaMapper">
    <resultMap id="SysAreaResult" type="com.ghy.system.domain.SysArea">
        <result property="areaId"            column="area_id"     />
        <result property="areaCode"          column="area_code"     />
        <result property="areaName"          column="area_name"     />
        <result property="parentCode"        column="parent_code"     />
        <result property="mergerName"        column="merger_name"     />
        <result property="shortName"         column="short_name"     />
        <result property="mergerShortName"   column="merger_short_name"     />
        <result property="levelType"         column="level_type"     />
        <result property="pinyin"            column="pinyin"     />
        <result property="firstChar"         column="first_char"     />
        <result property="lng"               column="lng"     />
        <result property="lat"               column="lat"     />
        <result property="createBy"          column="create_by"     />
        <result property="createTime"        column="create_time"   />
        <result property="updateBy"          column="update_by"     />
        <result property="updateTime"        column="update_time"   />
        <result property="remark"            column="remark"        />
    </resultMap>

    <sql id="selectSysArea">
        SELECT area_id, area_code, area_name, parent_code, merger_name, short_name, merger_short_name,
               level_type, pinyin, first_char, lng, lat, create_by, create_time, remark
        FROM sys_area
    </sql>

    <select id="selectSysAreaList" parameterType="com.ghy.system.domain.SysArea" resultMap="SysAreaResult">
        <include refid="selectSysArea" />
        <where>
            <if test="areaCode != null and areaCode != ''">
                AND area_code like concat('%', #{goodsCode}, '%')
            </if>
            <if test="areaName != null and areaName != ''">
                AND area_name like concat('%', #{areaName}, '%')
            </if>
            <if test="parentCode != null and parentCode != ''">
                AND parent_code = #{parentCode}
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="SysAreaResult">
        <include refid="selectSysArea"/>
        <where>
            <if test="areaId != null and areaId != 0">
                AND area_id = #{areaId}
            </if>
        </where>
    </select>

    <select id="selectByAreaCode" parameterType="string" resultMap="SysAreaResult">
        <include refid="selectSysArea"/>
        <where>
            <if test="areaCode != null and areaCode != ''">
                AND area_code = #{areaCode}
            </if>
        </where>
    </select>

    <delete id="deleteSysAreaByIds" parameterType="Long">
        delete from sys_area where area_id in
        <foreach collection="array" item="areaId" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </delete>

    <update id="updateSysArea" parameterType="com.ghy.system.domain.SysArea">
        update sys_area
        <set>
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="areaName != null and areaName != ''">area_name = #{areaName},</if>
            <if test="parentCode != null and parentCode != ''">parent_code = #{parentCode},</if>
            <if test="mergerName != null and mergerName != ''">merger_name = #{mergerName},</if>
            <if test="shortName != null and shortName != ''">short_name = #{shortName},</if>
            <if test="mergerShortName != null and mergerShortName != ''">merger_short_name = #{mergerShortName},</if>
            <if test="levelType != null and levelType != ''">level_type = #{levelType},</if>
            <if test="pinyin != null and pinyin != ''">pinyin = #{pinyin},</if>
            <if test="firstChar != null and firstChar != ''">first_char = #{firstChar},</if>
            <if test="lng != null and lng != ''">lng = #{lng},</if>
            <if test="lat != null and lat != ''">lat = #{lat},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where goods_id = #{goodsId}
    </update>


    <insert id="insertSysArea" parameterType="com.ghy.system.domain.SysArea" useGeneratedKeys="true" keyProperty="areaId">
        insert into sys_area(
        <if test="areaId != null and areaId != 0">area_id,</if>
        <if test="areaCode != null and areaCode != ''">area_code,</if>
        <if test="areaName != null and areaName != ''">area_name,</if>
        <if test="parentCode != null and parentCode != ''">parent_code,</if>
        <if test="mergerName != null and mergerName != ''">merger_name,</if>
        <if test="shortName != null and shortName != ''">short_name,</if>
        <if test="mergerShortName != null and mergerShortName != ''">merger_short_name,</if>
        <if test="levelType != null and levelType != ''">level_type,</if>
        <if test="pinyin != null and pinyin != ''">pinyin,</if>
        <if test="firstChar != null and firstChar != ''">first_char,</if>
        <if test="lng != null and lng != ''">lng,</if>
        <if test="lat != null and lat != ''">lat,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="areaId != null and areaId != 0">#{areaId},</if>
        <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
        <if test="areaName != null and areaName != ''">#{areaName},</if>
        <if test="parentCode != null and parentCode != ''">#{parentCode},</if>
        <if test="mergerName != null and mergerName != ''">#{mergerName},</if>
        <if test="shortName != null and shortName != ''">#{shortName},</if>
        <if test="mergerShortName != null and mergerShortName != ''">#{mergerShortName},</if>
        <if test="levelType != null and levelType != ''">#{levelType},</if>
        <if test="pinyin != null and pinyin != ''">#{pinyin},</if>
        <if test="firstChar != null and firstChar != ''">#{firstChar},</if>
        <if test="lng != null and lng != ''">#{lng},</if>
        <if test="lat != null and lat != ''">#{lat},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

</mapper>