<?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.zcxk.dao.AreaMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.entity.Area" id="AreaMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="parentId" column="parent_id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="mangerName" column="manger_name" jdbcType="VARCHAR"/>
        <result property="shortName" column="short_name" jdbcType="VARCHAR"/>
        <result property="mangerShortName" column="manger_short_name" jdbcType="VARCHAR"/>
        <result property="levelType" column="level_type" jdbcType="INTEGER"/>
        <result property="cityCode" column="city_code" jdbcType="VARCHAR"/>
        <result property="zipCode" column="zip_code" jdbcType="VARCHAR"/>
        <result property="pinYin" column="pin_yin" jdbcType="VARCHAR"/>
        <result property="jianPin" column="jian_pin" jdbcType="VARCHAR"/>
        <result property="firstChar" column="first_char" jdbcType="VARCHAR"/>
        <result property="lng" column="lng" jdbcType="VARCHAR"/>
        <result property="lat" column="lat" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        id, parent_id, name, manger_name, short_name, manger_short_name, level_type, city_code, zip_code, pin_yin, jian_pin, first_char, lng, lat, remark    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="AreaMap">
        select
        <include refid="Base_Column_List"/>
        from uims_area
        where id = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="AreaMap">
        select
        <include refid="Base_Column_List"/>
        from uims_area
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.dto.AreaDto">
        select
        id code,parent_id pid,name
        from uims_area
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="parentId != null">
                and parent_id = #{parentId}
            </if>
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
            <if test="mangerName != null and mangerName != ''">
                and manger_name = #{mangerName}
            </if>
            <if test="shortName != null and shortName != ''">
                and short_name = #{shortName}
            </if>
            <if test="mangerShortName != null and mangerShortName != ''">
                and manger_short_name = #{mangerShortName}
            </if>
            <if test="levelType != null">
                and level_type = #{levelType}
            </if>
            <if test="cityCode != null and cityCode != ''">
                and city_code = #{cityCode}
            </if>
            <if test="zipCode != null and zipCode != ''">
                and zip_code = #{zipCode}
            </if>
            <if test="pinYin != null and pinYin != ''">
                and pin_yin = #{pinYin}
            </if>
            <if test="jianPin != null and jianPin != ''">
                and jian_pin = #{jianPin}
            </if>
            <if test="firstChar != null and firstChar != ''">
                and first_char = #{firstChar}
            </if>
            <if test="lng != null and lng != ''">
                and lng = #{lng}
            </if>
            <if test="lat != null and lat != ''">
                and lat = #{lat}
            </if>
            <if test="remark != null and remark != ''">
                and remark = #{remark}
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into uims_area(id, parent_id, name, manger_name, short_name, manger_short_name, level_type, city_code, zip_code, pin_yin, jian_pin, first_char, lng, lat, remark)
        values ( #{id}, #{parentId}, #{name}, #{mangerName}, #{shortName}, #{mangerShortName}, #{levelType}, #{cityCode}, #{zipCode}, #{pinYin}, #{jianPin}, #{firstChar}, #{lng}, #{lat}, #{remark})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_area(id, parent_id, name, manger_name, short_name, manger_short_name, level_type, city_code,
        zip_code, pin_yin, jian_pin, first_char, lng, lat, remark)
        values
        <foreach collection="areas" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.parentId}, #{item.name}, #{item.mangerName}, #{item.shortName}, #{item.mangerShortName},
            #{item.levelType}, #{item.cityCode}, #{item.zipCode}, #{item.pinYin}, #{item.jianPin}, #{item.firstChar},
            #{item.lng}, #{item.lat}, #{item.remark} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims.uims_area
        <set>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="mangerName != null and mangerName != ''">
                manger_name = #{mangerName},
            </if>
            <if test="shortName != null and shortName != ''">
                short_name = #{shortName},
            </if>
            <if test="mangerShortName != null and mangerShortName != ''">
                manger_short_name = #{mangerShortName},
            </if>
            <if test="levelType != null">
                level_type = #{levelType},
            </if>
            <if test="cityCode != null and cityCode != ''">
                city_code = #{cityCode},
            </if>
            <if test="zipCode != null and zipCode != ''">
                zip_code = #{zipCode},
            </if>
            <if test="pinYin != null and pinYin != ''">
                pin_yin = #{pinYin},
            </if>
            <if test="jianPin != null and jianPin != ''">
                jian_pin = #{jianPin},
            </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 and remark != ''">
                remark = #{remark},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from uims_area where id = #{id}
    </delete>

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_area
    </select>
    <select id="selectPage" resultMap="AreaMap">
        select
        <include refid="Base_Column_List"/>
        from uims_area
        <where>
            <if test="area.id != null">
                and id = #{area.id}
            </if>
            <if test="area.parentId != null">
                and parent_id = #{area.parentId}
            </if>
            <if test="area.name != null and area.name != ''">
                and name = #{area.name}
            </if>
            <if test="area.mangerName != null and area.mangerName != ''">
                and manger_name = #{area.mangerName}
            </if>
            <if test="area.shortName != null and area.shortName != ''">
                and short_name = #{area.shortName}
            </if>
            <if test="area.mangerShortName != null and area.mangerShortName != ''">
                and manger_short_name = #{area.mangerShortName}
            </if>
            <if test="area.levelType != null">
                and level_type = #{area.levelType}
            </if>
            <if test="area.cityCode != null and area.cityCode != ''">
                and city_code = #{area.cityCode}
            </if>
            <if test="area.zipCode != null and area.zipCode != ''">
                and zip_code = #{area.zipCode}
            </if>
            <if test="area.pinYin != null and area.pinYin != ''">
                and pin_yin = #{area.pinYin}
            </if>
            <if test="area.jianPin != null and area.jianPin != ''">
                and jian_pin = #{area.jianPin}
            </if>
            <if test="area.firstChar != null and area.firstChar != ''">
                and first_char = #{area.firstChar}
            </if>
            <if test="area.lng != null and area.lng != ''">
                and lng = #{area.lng}
            </if>
            <if test="area.lat != null and area.lat != ''">
                and lat = #{area.lat}
            </if>
            <if test="area.remark != null and area.remark != ''">
                and remark = #{area.remark}
            </if>
        </where>
    </select>
</mapper>