<?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="cn.reghao.tnb.content.app.geo.db.mapper.GeoChinaMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into geo_china
        (`id`,`pid`,`deep`,`name`,`ext_path`,`geo`,`polygon`)
        values
        (#{id},#{pid},#{deep},#{name},#{extPath},point(#{longitude},#{latitude}),GeomFromText('POLYGON((${polygon}))'))
    </insert>
    <insert id="saveMultiPolygon" useGeneratedKeys="true" keyProperty="id">
        insert into geo_china
        (`id`,`pid`,`deep`,`name`,`ext_path`,`geo`,`polygon`)
        values
        (#{id},#{pid},#{deep},#{name},#{extPath},point(#{longitude},#{latitude}),GeomFromText('MULTIPOLYGON(${multiPolygon})'))
    </insert>
    <insert id="saveGeoJosn" useGeneratedKeys="true" keyProperty="id">
        insert into geo_geo_json
            (`area_code`,`deep`,`geo_json`)
        values
            (#{areaCode},#{deep},#{geoJson})
    </insert>

    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert ignore into geo_china
        (`id`,`pid`,`deep`,`name`,`ext_path`,`geo`,`polygon`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
        (#{item.id},#{item.pid},#{item.deep},#{item.name},#{item.extPath},point(#{item.longitude},#{item.latitude}))
        </foreach>
    </insert>

    <select id="findById" resultType="cn.reghao.tnb.content.app.geo.model.po.GeoChina">
        select *
        from geo_china
        where id=#{id}
    </select>
    <select id="findByPoint" resultType="cn.reghao.tnb.content.app.geo.model.vo.GeoArea">
        select id,pid,deep,`name`,ext_path,x(geo) as lng,y(geo) as lat
        from geo_china
        where deep=2 and st_within(point(#{lng},#{lat}), polygon);
    </select>
    <select id="findAllArea" resultType="cn.reghao.tnb.content.app.geo.model.vo.GeoArea">
        select id,pid,deep,`name`,ext_path,x(geo) as lng,y(geo) as lat
        from geo_china
    </select>
    <select id="findGeoJson" resultType="java.lang.String">
        select geo_json
        from geo_geo_json
        where area_code=#{areaCode} and deep=#{deep}
    </select>
    <select id="findGeoJsonObject" resultType="cn.reghao.tnb.content.app.geo.model.po.GeoJson">
        select geo_geo_json.area_code,geo_geo_json.deep,geo_china.`name`
        from geo_geo_json
        inner join geo_china
        on geo_geo_json.area_code=geo_china.id
    </select>
</mapper>
