<?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="ink.wgink.module.map.dao.grid.IGridDao">

    <cache/>

    <resultMap id="gridDTO" type="ink.wgink.module.map.pojo.dtos.grid.GridDTO">
        <id column="grid_id" property="gridId"/>
        <result column="grid_id" property="gridId"/>
        <result column="grid_name" property="gridName"/>
        <result column="grid_summary" property="gridSummary"/>
        <result column="grid_group_id" property="gridGroupId"/>
        <result column="grid_duty" property="gridDuty"/>
        <result column="grid_code" property="gridCode"/>
        <result column="grid_square" property="gridSquare"/>
        <result column="area_code" property="areaCode"/>
        <result column="area_name" property="areaName"/>
        <result column="fill_color" property="fillColor"/>
        <result column="relation_id" property="relationId"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
    </resultMap>

    <resultMap id="gridPO" type="ink.wgink.module.map.pojo.pos.grid.GridPO">
        <id column="id" property="id"/>
        <result column="grid_id" property="gridId"/>
        <result column="grid_name" property="gridName"/>
        <result column="grid_summary" property="gridSummary"/>
        <result column="grid_group_id" property="gridGroupId"/>
        <result column="grid_duty" property="gridDuty"/>
        <result column="grid_code" property="gridCode"/>
        <result column="grid_square" property="gridSquare"/>
        <result column="area_code" property="areaCode"/>
        <result column="area_name" property="areaName"/>
        <result column="fill_color" property="fillColor"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `map_grid` (
            `id` bigint(36) unsigned NOT NULL AUTO_INCREMENT,
            `grid_id` char(36) DEFAULT NULL COMMENT '主键',
            `grid_name` varchar(255) DEFAULT NULL COMMENT '网格名称',
            `grid_summary` varchar(255) DEFAULT NULL COMMENT '网格描述',
            `grid_group_id` char(36) DEFAULT NULL COMMENT '网格组ID',
            `grid_duty` varchar(255) DEFAULT NULL COMMENT '网格责任',
            `grid_code` varchar(255) DEFAULT NULL COMMENT '网格编码',
            `grid_square` double(11,2) DEFAULT NULL COMMENT '网格面积',
            `area_code` varchar(255) DEFAULT NULL COMMENT '区域编码',
            `area_name` varchar(255) DEFAULT NULL COMMENT '区域名称',
            `fill_color` varchar(7) DEFAULT NULL COMMENT '填充颜色',
            `creator` char(36) DEFAULT NULL COMMENT '创建人',
            `gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
            `modifier` char(36) DEFAULT NULL COMMENT '修改人',
            `gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',
            `is_delete` int(1) DEFAULT '0' COMMENT '是否删除',
            PRIMARY KEY (`id`),
            UNIQUE KEY `grid_id` (`grid_id`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='网格表';
    </update>

    <!-- 保存网格 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO map_grid(
            grid_id,
            grid_name,
            grid_summary,
            grid_group_id,
            grid_duty,
            grid_code,
            grid_square,
            area_code,
            area_name,
            fill_color,
            gmt_create,
            creator,
            gmt_modified,
            modifier,
            is_delete
        ) VALUES(
            #{gridId},
            #{gridName},
            #{gridSummary},
            #{gridGroupId},
            #{gridDuty},
            #{gridCode},
            #{gridSquare},
            #{areaCode},
            #{areaName},
            #{fillColor},
            #{gmtCreate},
            #{creator},
            #{gmtModified},
            #{modifier},
            #{isDelete}
        )
    </insert>

    <!-- 删除网格 -->
    <update id="remove" parameterType="map" flushCache="true">
        UPDATE
            map_grid
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
        <if test="gridIds != null and gridIds.size > 0">
            grid_id IN
            <foreach collection="gridIds" index="index" open="(" separator="," close=")">
                #{gridIds[${index}]}
            </foreach>
        </if>
    </update>

    <!-- 删除网格 -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            map_grid
        WHERE
        <if test="gridId != null and gridId != ''">
            grid_id = #{gridId}
        </if>
        <if test="gridIds != null and gridIds.size > 0">
            grid_id IN
            <foreach collection="gridIds" index="index" open="(" separator="," close=")">
            #{gridIds[${index}]}
            </foreach>
        </if>
    </delete>

    <!-- 更新网格 -->
    <delete id="update" parameterType="map" flushCache="true">
        UPDATE
            map_grid
        SET
        <if test="gridName != null and gridName != ''">
            grid_name = #{gridName},
        </if>
        <if test="gridSummary != null">
            grid_summary = #{gridSummary},
        </if>
        <if test="gridGroupId != null">
            grid_group_id = #{gridGroupId},
        </if>
        <if test="gridDuty != null">
            grid_duty = #{gridDuty},
        </if>
        <if test="gridSquare != null">
            grid_square = #{gridSquare},
        </if>
        <if test="fillColor != null and fillColor != ''">
            fill_color = #{fillColor},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier}
        WHERE
            grid_id = #{gridId}
    </delete>

    <!-- 网格详情 -->
    <select id="get" parameterType="map" resultMap="gridDTO" useCache="true">
        SELECT
            t1.grid_id,
            t1.grid_name,
            t1.grid_summary,
            t1.grid_group_id,
            t1.grid_duty,
            t1.grid_code,
            t1.grid_square,
            t1.area_code,
            t1.area_name,
            t1.fill_color
        FROM
            map_grid t1
        WHERE
            t1.is_delete = 0
        <if test="gridId != null and gridId != ''">
        AND
            t1.grid_id = #{gridId}
        </if>
    </select>

    <!-- 网格详情 -->
    <select id="getPO" parameterType="map" resultMap="gridPO" useCache="true">
        SELECT
            grid_id,
            grid_name,
            grid_summary,
            grid_group_id,
            grid_duty,
            grid_code,
            grid_square,
            area_code,
            area_name,
            fill_color,
            gmt_create,
            creator,
            gmt_modified,
            modifier,
            is_delete
        FROM
            map_grid
        WHERE
            is_delete = 0
        <if test="gridId != null and gridId != ''">
        AND
            grid_id = #{gridId}
        </if>
        <if test="keywords != null and keywords != ''">
        AND (
            grid_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
    </select>

    <!-- 网格列表 -->
    <select id="list" parameterType="map" resultMap="gridDTO" useCache="true">
        SELECT
            t1.grid_id,
            t1.grid_name,
            t1.grid_summary,
            t1.grid_group_id,
            t1.grid_duty,
            t1.grid_code,
            t1.grid_square,
            t1.area_code,
            t1.area_name,
            t1.fill_color,
            LEFT(t1.gmt_create, 19) gmt_create,
            LEFT(t1.gmt_modified, 19) gmt_modified
        FROM
            map_grid t1
        WHERE
            t1.is_delete = 0
        <if test="gridIds != null and gridIds.size > 0">
        AND
            t1.grid_id IN
            <foreach collection="gridIds" index="index" open="(" separator="," close=")">
                #{gridIds[${index}]}
            </foreach>
        </if>
        <if test="areaCode != null and areaCode != ''">
        AND
            t1.area_code = #{areaCode}
        </if>
        <if test="areaCodeLike != null and areaCodeLike != ''">
        AND
            t1.area_code LIKE CONCAT(#{areaCodeLike}, '%')
        </if>
        <if test="excludeAreaCode != null and excludeAreaCode != ''">
        AND
            t1.area_code != #{excludeAreaCode}
        </if>
    </select>

    <!-- 网格列表（group） -->
    <select id="listGroup" parameterType="map" resultMap="gridDTO" useCache="true">
        SELECT
            t1.grid_id,
            t1.fill_color,
            t1.grid_name
        FROM
            map_grid t1
        WHERE
            t1.is_delete = 0
        <if test="gridIds != null and gridIds.size > 0">
        AND
            t1.grid_id IN
            <foreach collection="gridIds" index="index" open="(" separator="," close=")">
                #{gridIds[${index}]}
            </foreach>
        </if>
        GROUP BY
            t1.grid_id,
            t1.fill_color,
            t1.grid_name
    </select>

    <!-- 区域最后网格 -->
    <select id="getAreaLastPO" parameterType="map" resultMap="gridPO" useCache="false">
        SELECT
            grid_id,
            grid_name,
            grid_summary,
            grid_group_id,
            grid_duty,
            grid_code,
            grid_square,
            area_code,
            area_name,
            fill_color,
            gmt_create,
            creator,
            gmt_modified,
            modifier,
            is_delete
        FROM
            map_grid
        WHERE
            is_delete = 0
        AND
            area_code = #{areaCode}
        ORDER BY
            grid_code DESC
        LIMIT 1
    </select>

</mapper>