<?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.cm.plugin.map.dao.IGridDao">

    <resultMap id="gridRelationDTO" type="com.cm.plugin.map.pojo.dto.GridRelationDTO">
        <id column="relation_id" property="relationId"/>
        <result column="grid_id" property="gridId"/>
    </resultMap>

    <resultMap id="gridDTO" type="com.cm.plugin.map.pojo.dto.GridDTO" extends="gridRelationDTO">
        <id column="grid_id" property="gridId"/>
        <result column="fill_color" property="fillColor"/>
        <result column="grid_name" property="gridName"/>
        <result column="relation_id" property="relationId"/>
    </resultMap>

    <resultMap id="gridPointDTO" type="com.cm.plugin.map.pojo.dto.GridPointDTO">
        <id column="grid_id" property="gridId"/>
        <result column="lng" property="lng"/>
        <result column="lat" property="lat"/>
    </resultMap>

    <!-- 保存网格 -->
    <insert id="saveGrid" parameterType="map">
        INSERT INTO map_grid(
            grid_id,
            grid_name,
            fill_color,
            gmt_create,
            creator,
            gmt_modified,
            modifier,
            is_delete
        ) VALUES(
            #{gridId},
            #{gridName},
            #{fillColor},
            #{gmtCreate},
            #{creator},
            #{gmtModified},
            #{modifier},
            #{isDelete}
        )
    </insert>

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

    <!-- 删除网格 -->
    <delete id="deleteGrid" parameterType="map">
        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="deleteGridPoint" parameterType="map">
        DELETE FROM
            map_grid_point
        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="deleteGridRelation" parameterType="map">
        DELETE FROM
            map_grid_relation
        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>
        <if test="relationId != null and relationId != ''">
            relation_id = #{relationId}
        </if>
    </delete>

    <!-- 保存网格点 -->
    <insert id="saveGridPoint" parameterType="map">
        INSERT INTO map_grid_point(
            grid_id,
            lng,
            lat
        ) VALUES(
            #{gridId},
            #{lng},
            #{lat}
        )
    </insert>

    <!-- 获取关联关系列表 -->
    <select id="listGridRelation" parameterType="map" resultMap="gridRelationDTO">
        SELECT
            relation_id,
            grid_id
        FROM
            map_grid_relation
        WHERE
            1 = 1
        <if test="relationId != null">
        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>
    </select>

    <!-- 获取网格列表（通过关联ID） -->
    <select id="listGridByRelationId" parameterType="java.lang.String" resultMap="gridDTO">
        SELECT
            t1.grid_id,
            t1.fill_color,
            t1.grid_name,
            t2.relation_id
        FROM
            map_grid t1
        LEFT JOIN
            map_grid_relation t2
        ON
            t1.grid_id = t2.grid_id
        WHERE
            t1.is_delete = 0
        AND
            t2.relation_id = #{_parameter}
    </select>

    <select id="listGridByRelationIds" parameterType="java.util.List" resultMap="gridDTO">
        SELECT
            t1.grid_id,
            t1.fill_color,
            t1.grid_name,
            t2.relation_id
        FROM
            map_grid t1
        LEFT JOIN
            map_grid_relation t2
        ON
            t1.grid_id = t2.grid_id
        WHERE
            t1.is_delete = 0
        AND
            t2.relation_id IN
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{list[${index}]}
        </foreach>
    </select>

    <!-- 获取网格点列表（通过网格ID） -->
    <select id="listPointByGridId" parameterType="java.lang.String" resultMap="gridPointDTO">
        SELECT
            grid_id,
            lng,
            lat
        FROM
            map_grid_point
        WHERE
            grid_id = #{_parameter}
    </select>

    <!-- 获取网格点列表（通过网格ID列表） -->
    <select id="listPointByGridIds" parameterType="java.util.List" resultMap="gridPointDTO">
        SELECT
            grid_id,
            lng,
            lat
        FROM
            map_grid_point
        WHERE
            grid_id IN
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{list[${index}]}
        </foreach>
    </select>

</mapper>