<?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.IGridGroupDao">

    <cache/>

    <resultMap id="gridGroupDTO" type="ink.wgink.module.map.pojo.dtos.grid.GridGroupDTO">
        <id column="grid_group_id" property="gridGroupId"/>
        <result column="grid_group_name" property="gridGroupName"/>
        <result column="grid_group_code" property="gridGroupCode"/>
        <result column="grid_group_summary" property="gridGroupSummary"/>
        <result column="grid_group_duty" property="gridGroupDuty"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <resultMap id="gridGroupPO" type="ink.wgink.module.map.pojo.pos.grid.GridGroupPO">
        <id column="id" property="id"/>
        <result column="grid_group_id" property="gridGroupId"/>
        <result column="grid_group_name" property="gridGroupName"/>
        <result column="grid_group_code" property="gridGroupCode"/>
        <result column="grid_group_summary" property="gridGroupSummary"/>
        <result column="grid_group_duty" property="gridGroupDuty"/>
        <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_group` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `grid_group_id` char(36) DEFAULT NULL COMMENT '主键',
            `grid_group_name` varchar(255) DEFAULT NULL COMMENT '名称',
            `grid_group_code` varchar(255) DEFAULT NULL COMMENT '编码',
            `grid_group_summary` varchar(255) DEFAULT NULL COMMENT '描述',
            `grid_group_duty` varchar(255) 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_type_id` (`grid_group_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='网格组';
    </update>

    <!-- 新增网格组 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO map_grid_group(
            grid_group_id,
            grid_group_name,
            grid_group_code,
            grid_group_summary,
            grid_group_duty,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{gridGroupId},
            #{gridGroupName},
            #{gridGroupCode},
            #{gridGroupSummary},
            #{gridGroupDuty},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除网格组 -->
    <update id="remove" parameterType="map" flushCache="true">
        UPDATE
            map_grid_group
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            grid_group_id = #{gridGroupId}
    </update>

    <!-- 修改网格组 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            map_grid_group
        SET
        <if test="gridGroupName != null and gridGroupName != ''">
            grid_group_name = #{gridGroupName},
        </if>
        <if test="gridGroupSummary != null">
            grid_group_summary = #{gridGroupSummary},
        </if>
        <if test="gridGroupDuty != null">
            grid_group_duty = #{gridGroupDuty},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier}
        WHERE
            grid_group_id = #{gridGroupId}
    </update>

    <!-- 网格详情 -->
    <select id="get" parameterType="map" resultMap="gridGroupDTO" useCache="true">
        SELECT
            grid_group_id,
            grid_group_name,
            grid_group_code,
            grid_group_summary,
            grid_group_duty,
            LEFT(gmt_create, 19) gmt_create
        FROM
            map_grid_group
        WHERE
            is_delete = 0
        <if test="gridGroupId != null and gridGroupId != ''">
        AND
            grid_group_id = #{gridGroupId}
        </if>
    </select>

    <!-- 网格详情 -->
    <select id="getPO" parameterType="map" resultMap="gridGroupPO" useCache="true">
        SELECT
            id,
            grid_group_id,
            grid_group_name,
            grid_group_code,
            grid_group_summary,
            grid_group_duty,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        FROM
            map_grid_group
        WHERE
            is_delete = 0
        <if test="gridGroupId != null and gridGroupId != ''">
        AND
            grid_group_id = #{gridGroupId}
        </if>
        <if test="gridGroupCode != null and gridGroupCode != ''">
        AND
            grid_group_code = #{gridGroupCode}
        </if>
    </select>

    <!-- 网格组列表 -->
    <select id="list" parameterType="map" resultMap="gridGroupDTO">
        SELECT
            grid_group_id,
            grid_group_name,
            grid_group_code,
            grid_group_summary,
            grid_group_duty,
            LEFT(gmt_create, 19) gmt_create
        FROM
            map_grid_group
        WHERE
            is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            grid_group_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
    </select>

    <!-- 网格组列表 -->
    <select id="listPO" parameterType="map" resultMap="gridGroupPO">
        SELECT
            id,
            grid_group_id,
            grid_group_name,
            grid_group_code,
            grid_group_summary,
            grid_group_duty,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        FROM
            map_grid_group
        WHERE
            is_delete = 0
    </select>

</mapper>