<?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.huashe.park.core.mapper.BdFenceInfoMapper">

    <resultMap type="BdFenceInfo" id="BdFenceInfoResult">
        <result property="id" column="id"/>
        <result property="defenceName" column="defence_name"/>
        <result property="poly" column="poly"/>
        <result property="centerLng" column="center_lng"/>
        <result property="centerLat" column="center_lat"/>
        <result property="fenceType" column="fence_type"/>
        <result property="locationId" column="location_id"/>
        <result property="altitude" column="altitude"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <sql id="selectBdFenceInfoVo">
        select id,
               defence_name,
               ST_AsText(poly) poly,
               center_lng,
               center_lat,
               fence_type,
               location_id,
               altitude,
               update_time,
               create_time,
               create_by,
               update_by
        from bd_fence_info
    </sql>

    <select id="selectBdFenceInfoList" parameterType="BdFenceInfo" resultMap="BdFenceInfoResult">
        <include refid="selectBdFenceInfoVo"/>
        <where>
            <if test="defenceName != null  and defenceName != ''">and defence_name like concat('%', #{defenceName},
                '%')
            </if>
            <if test="fenceType != null  and fenceType != ''">and fence_type = #{fenceType}</if>
            <if test="locationId != null ">and location_id = #{locationId}</if>
        </where>
    </select>

    <select id="selectBdFenceInfoById" parameterType="Long" resultMap="BdFenceInfoResult">
        <include refid="selectBdFenceInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertBdFenceInfo" parameterType="BdFenceInfo" useGeneratedKeys="true" keyProperty="id">
        insert into bd_fence_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="defenceName != null">defence_name,</if>
            <if test="poly != null">poly,</if>
            <if test="centerLng != null">center_lng,</if>
            <if test="centerLat != null">center_lat,</if>
            <if test="fenceType !=null">fence_type,</if>
            <if test="locationId !=null">location_id,</if>
            <if test="altitude != null">altitude,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="defenceName != null">#{defenceName},</if>
            <if test="poly != null">ST_GeomFromText(#{poly}),</if>
            <if test="centerLng != null">#{centerLng},</if>
            <if test="centerLat != null">#{centerLat},</if>
            <if test="fenceType != null">#{fenceType},</if>
            <if test="locationId != null">#{locationId},</if>
            <if test="altitude != null">#{altitude},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
    </insert>

    <update id="updateBdFenceInfo" parameterType="BdFenceInfo">
        update bd_fence_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="defenceName != null">defence_name = #{defenceName},</if>
            <if test="poly != null">poly = ST_GeomFromText(#{poly}),</if>
            <if test="centerLng != null">center_lng = #{centerLng},</if>
            <if test="centerLat != null">center_lat = #{centerLat},</if>
            <if test="fenceType != null">fence_type = #{fenceType},</if>
            <if test="locationId != null">location_id = #{locationId},</if>
            <if test="altitude != null">altitude = #{altitude},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBdFenceInfoById" parameterType="Long">
        delete
        from bd_fence_info
        where id = #{id}
    </delete>

    <delete id="deleteBdFenceInfoByIds" parameterType="String">
        delete from bd_fence_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
