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

    <cache/>

    <resultMap id="gridRelationDTO" type="ink.wgink.module.map.pojo.dtos.grid.GridRelationDTO">
        <id column="relation_id" property="relationId"/>
        <result column="grid_id" property="gridId"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `map_grid_relation` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `grid_id` char(36) DEFAULT NULL COMMENT '网格ID',
            `relation_id` char(36) DEFAULT NULL COMMENT '关联ID',
            `gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='网格关联表';
    </update>

    <!-- 保存网格关联关系 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO map_grid_relation(
            grid_id,
            relation_id,
            gmt_create
        ) VALUES(
            #{gridId},
            #{relationId},
            #{gmtCreate}
        )
    </insert>

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

    <!-- 网格关联详情 -->
    <select id="get" parameterType="map" resultMap="gridRelationDTO" useCache="true">
        SELECT
            relation_id,
            LEFT(gmt_create, 19) gmt_create,
            grid_id
        FROM
            map_grid_relation
        WHERE
            grid_id = #{gridId}
        AND
            relation_id = #{relationId}
    </select>

    <!-- 获取关联关系列表 -->
    <select id="list" parameterType="map" resultMap="gridRelationDTO" useCache="true">
        SELECT
            relation_id,
            LEFT(gmt_create, 19) gmt_create,
            grid_id
        FROM
            map_grid_relation
        <where>
            <if test="relationId != null">
                relation_id = #{relationId}
            </if>
            <if test="relationIds != null and relationIds.size > 0">
            AND
                relation_id IN
                <foreach collection="relationIds" index="index" open="(" separator="," close=")">
                    #{relationIds[${index}]}
                </foreach>
            </if>
            <if test="gridId != null and gridId != ''">
            AND
                grid_id = #{gridId}
            </if>
            <if test="gridIds != null and gridIds.size > 0">
            AND
                grid_id IN
                <foreach collection="gridIds" index="index" open="(" separator="," close=")">
                    #{gridIds[${index}]}
                </foreach>
            </if>
        </where>
    </select>

</mapper>