<?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.BdDevcTrailUwbMapper">
    
    <resultMap type="BdDevcTrailUwb" id="BdDevcTrailUwbResult">
        <result property="id"    column="id"    />
        <result property="devcKey"    column="devc_key"    />
        <result property="lat"    column="lat"    />
        <result property="lng"    column="lng"    />
        <result property="dt"    column="dt"    />
        <result property="stepIndex"    column="step_index"    />
        <result property="tp"    column="tp"    />
        <result property="roomIndex"    column="room_index"    />
        <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="selectBdDevcTrailUwbVo">
        select id, devc_key, lat, lng, dt, step_index, tp, room_index, update_time, create_time, create_by, update_by from bd_devc_trail_uwb
    </sql>

    <select id="selectBdDevcTrailUwbList" parameterType="BdDevcTrailUwb" resultMap="BdDevcTrailUwbResult">
        <include refid="selectBdDevcTrailUwbVo"/>
        <where>  
            <if test="devcKey != null  and devcKey != ''"> and devc_key = #{devcKey}</if>
            <if test="dt != null  and dt != ''"> and dt = #{dt}</if>
        </where>
    </select>
    
    <select id="selectBdDevcTrailUwbById" parameterType="Long" resultMap="BdDevcTrailUwbResult">
        <include refid="selectBdDevcTrailUwbVo"/>
        where id = #{id}
    </select>

    <insert id="insertBdDevcTrailUwb" parameterType="BdDevcTrailUwb" useGeneratedKeys="true" keyProperty="id">
        insert into bd_devc_trail_uwb
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="devcKey != null">devc_key,</if>
            <if test="lat != null">lat,</if>
            <if test="lng != null">lng,</if>
            <if test="dt != null">dt,</if>
            <if test="stepIndex != null">step_index,</if>
            <if test="tp != null">tp,</if>
            <if test="roomIndex != null">room_index,</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="devcKey != null">#{devcKey},</if>
            <if test="lat != null">#{lat},</if>
            <if test="lng != null">#{lng},</if>
            <if test="dt != null">#{dt},</if>
            <if test="stepIndex != null">#{stepIndex},</if>
            <if test="tp != null">#{tp},</if>
            <if test="roomIndex != null">#{roomIndex},</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="updateBdDevcTrailUwb" parameterType="BdDevcTrailUwb">
        update bd_devc_trail_uwb
        <trim prefix="SET" suffixOverrides=",">
            <if test="devcKey != null">devc_key = #{devcKey},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="dt != null">dt = #{dt},</if>
            <if test="stepIndex != null">step_index = #{stepIndex},</if>
            <if test="tp != null">tp = #{tp},</if>
            <if test="roomIndex != null">room_index = #{roomIndex},</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="deleteBdDevcTrailUwbById" parameterType="Long">
        delete from bd_devc_trail_uwb where id = #{id}
    </delete>

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

    <insert id="saveObjTail" parameterType="String">
        SET SESSION group_concat_max_len = 1024000;
        INSERT INTO bd_devc_trail (devc_key, dt, poly, update_time, create_time)
        SELECT devc_key, dt, poly, NOW(), NOW()
        FROM (SELECT devc_key,
                     dt,
                     concat(
                             '[',
                             GROUP_CONCAT(
                                     CONCAT(
                                             '[',
                                             lng,
                                             ',',
                                             lat,
                                             ',',
                                             COALESCE(altitude, '-1'),
                                             ',',
                                             step_index,
                                             ',',
                                             COALESCE(room_index, '-1'),
                                             ']'
                                         ) ORDER BY
			step_index SEPARATOR ','
                                 ),
                             ']'
                         ) AS poly
              FROM bd_devc_trail_uwb
              WHERE dt = #{dt}
              GROUP BY devc_key,
                       dt) res ON DUPLICATE KEY
        UPDATE poly =
        VALUES (poly), update_time =
        VALUES (update_time);
    </insert>
</mapper>