<?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.chinaweal.aiccs.aiccs.market.mapper.AreaSlicePolygonMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.market.entity.AreaSlicePolygon">
        <id column="g_id" property="gId" />
        <result column="slice_id" property="sliceId" />
        <result column="polygon_data" property="polygonData" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        g_id, slice_id, dmgeo.ST_AsBinary(polygon_data) polygon_data, create_time, update_time
    </sql>
    <update id="updatePolygonByWKT">
        update area_slice_polygon
        set polygon_data = dmgeo.ST_PolyFromText(#{wkt}, 0),
        update_time = CURRENT_TIMESTAMP
        where g_id = #{gId}
    </update>
    <select id="selectPolygonInDistance"
            resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from area_slice_polygon p
        where exists(
        select 1 from area_slice a where a.slice_id = p.slice_id and a.state = '1' and a.is_deleted = '0'
        <if test="pSliceId != null and pSliceId != ''">
            and a.p_slice_id = #{pSliceId}
        </if>
        <if test="sliceType != null and sliceType != ''">
            and a.slice_type = #{sliceType}
        </if>
        )
        <if test="distance != null">
            and dmgeo.ST_Distance(dmgeo.ST_PolyFromText(polygon_data, 0);, dmgeo.ST_PointFromText('point (' + #{x} #{y} + ')' , 0)) &lt;= #{distance}
        </if>
        <if test="excludeIds != null and excludeIds.size() > 0">
            and g_id not in
            <foreach collection="excludeIds" open="(" close=")" separator="," item="id">
                #{id}
            </foreach>
        </if>
    </select>

    <insert id="insertPolygonByWKT" useGeneratedKeys="false">
        insert into area_slice_polygon(g_id, slice_id, polygon_data, create_time, update_time)
        values(#{gId}, #{sliceId}, dmgeo.ST_PolyFromText(#{wkt}, 0), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
    </insert>

</mapper>
