<?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.boman.web.core.mapper.QianshanGridMapper">
    
    <resultMap type="com.boman.domain.QianshanGrid" id="QianshanGridResult">
        <result property="id"    column="id"    />
        <result property="gridName"    column="grid_name"    />
        <result property="gridMember"    column="grid_member"    />
        <result property="gridPhone"    column="grid_phone"    />
        <result property="gridLevel"    column="grid_level"    />
        <result property="parentGridId"    column="parent_grid_id"    />
        <result property="parentGridName"    column="parent_grid_name"    />

        <result property="regionId" column="region_id"/>
        <result property="region" column="region"/>
        <result property="villageTownsId" column="village_towns_id"/>
        <result property="villageTowns" column="village_towns"/>
        <result property="villageId" column="village_id"/>
        <result property="village" column="village"/>

        <result property="areaName"    column="area_name"    />
        <result property="areaId"    column="area_id"    />
        <result property="parentAreaId"    column="parent_area_id"    />
        <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="isDel"    column="is_del"    />
        <result property="sort"    column="sort"    />

        <result property="num" column="num" />
    </resultMap>

    <sql id="selectQianshanGridVo">
        select id, grid_name, grid_member, grid_phone, grid_level, parent_grid_id,parent_grid_name, region_id, region,
               village_towns_id, village_towns, village_id, village, area_name, area_id, parent_area_id, create_by,
               create_time, update_by, update_time, is_del,sort
        from qianshan_grid
    </sql>

    <select id="selectQianshanGridList" parameterType="com.boman.domain.QianshanGrid" resultMap="QianshanGridResult">
        <include refid="selectQianshanGridVo"/>
        <where>
            <if test="gridName != null  and gridName != ''"> and grid_name like concat('%', #{gridName}, '%')</if>
            <if test="gridMember != null  and gridMember != ''"> and grid_member = #{gridMember}</if>
            <if test="gridPhone != null  and gridPhone != ''"> and grid_phone = #{gridPhone}</if>
            <if test="gridLevel != null  and gridLevel != ''"> and grid_level = #{gridLevel}</if>
            <if test="parentGridId != null  and parentGridId != ''"> and parent_grid_id = #{parentGridId}</if>
            <if test="areaName != null  and areaName != ''"> and area_name like concat('%', #{areaName}, '%')</if>
            <if test="areaId != null  and areaId != ''"> and area_id = #{areaId}</if>
            <if test="parentAreaId != null  and parentAreaId != ''"> and parent_area_id = #{parentAreaId}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
        </where>
        order by sort
    </select>
    
    <select id="selectQianshanGridById" parameterType="Long" resultMap="QianshanGridResult">
        <include refid="selectQianshanGridVo"/>
        where id = #{id}
    </select>

    <select id="validateHasChirld" parameterType="java.util.List" resultMap="QianshanGridResult">
        SELECT a.id as id, count(1) as num FROM qianshan_grid a LEFT JOIN qianshan_grid b on a.id = b.parent_grid_id WHERE b.parent_grid_id in
        <foreach collection="qianshanGrids" item="qianshanGrid" index="index" separator="," open="(" close=")">
            #{qianshanGrid.id}
        </foreach>
        GROUP BY a.id
    </select>


    <insert id="insertQianshanGrid" parameterType="com.boman.domain.QianshanGrid">
        insert into qianshan_grid
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="gridName != null">grid_name,</if>
            <if test="gridMember != null">grid_member,</if>
            <if test="gridPhone != null">grid_phone,</if>
            <if test="gridLevel != null">grid_level,</if>
            <if test="parentGridId != null">parent_grid_id,</if>
            <if test="parentGridName != null">parent_grid_name,</if>

            <if test="regionId != null">region_id,</if>
            <if test="region != null">region,</if>
            <if test="villageTownsId != null">village_towns_id,</if>
            <if test="villageTowns != null">village_towns,</if>
            <if test="villageId != null">village_id,</if>
            <if test="village != null">village,</if>


            <if test="areaName != null">area_name,</if>
            <if test="areaId != null">area_id,</if>
            <if test="parentAreaId != null">parent_area_id,</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>
            <if test="isDel != null">is_del,</if>
            <if test="sort != null">sort,</if>

         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="gridName != null">#{gridName},</if>
            <if test="gridMember != null">#{gridMember},</if>
            <if test="gridPhone != null">#{gridPhone},</if>
            <if test="gridLevel != null">#{gridLevel},</if>
            <if test="parentGridId != null">#{parentGridId},</if>
            <if test="parentGridName != null">#{parentGridName},</if>

            <if test="regionId != null">#{regionId},</if>
            <if test="region != null">#{region},</if>
            <if test="villageTownsId != null">#{villageTownsId},</if>
            <if test="villageTowns != null">#{villageTowns},</if>
            <if test="villageId != null">#{villageId},</if>
            <if test="village != null">#{village},</if>

            <if test="areaName != null">#{areaName},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="parentAreaId != null">#{parentAreaId},</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>
            <if test="isDel != null">#{isDel},</if>
            <if test="sort != null">#{sort},</if>
         </trim>
    </insert>

    <update id="updateQianshanGrid" parameterType="com.boman.domain.QianshanGrid">
        update qianshan_grid
        <trim prefix="SET" suffixOverrides=",">
            <if test="gridName != null">grid_name = #{gridName},</if>
            <if test="gridMember != null">grid_member = #{gridMember},</if>
            <if test="gridPhone != null">grid_phone = #{gridPhone},</if>
            <if test="gridLevel != null">grid_level = #{gridLevel},</if>
            <if test="parentGridId != null">parent_grid_id = #{parentGridId},</if>
            <if test="parentGridName != null">parent_grid_name = #{parentGridName},</if>

            <if test="regionId != null">region_id = #{regionId},</if>
            <if test="region != null">region = #{region},</if>
            <if test="villageTownsId != null">village_towns_id = #{villageTownsId},</if>
            <if test="villageTowns != null">village_towns = #{villageTowns},</if>
            <if test="villageId != null">village_id = #{villageId},</if>
            <if test="village != null">village = #{village},</if>

            <if test="areaName != null">area_name = #{areaName},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="parentAreaId != null">parent_area_id = #{parentAreaId},</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>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="sort != null">sort = #{sort},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQianshanGridById" parameterType="Long">
        delete from qianshan_grid where id = #{id}
    </delete>

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