<?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="net.shapelight.modules.ten.dao.TenPersonSyncDao">

    <!-- 可根据自己的需求,是否要使用 -->
    <resultMap type="net.shapelight.modules.ten.entity.TenPersonSyncEntity" id="tenPersonSyncMap">
        <result property="personSyncId" column="person_sync_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="deviceSn" column="device_sn"/>
        <result property="personId" column="person_id"/>
        <result property="state" column="state"/>
        <result property="lastUpdateTime" column="last_update_time"/>
        <result property="tenantId" column="tenant_id"/>
    </resultMap>

    <resultMap id="idupdatetime" type="net.shapelight.modules.vo.TenPersonIdUpdateAllVo">
        <result column="person_id" jdbcType="BIGINT" property="uid"/>
        <result column="last_update_time" jdbcType="TIMESTAMP" property="last_update_stamp"/>
    </resultMap>


    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO ten_person_sync_${cellId}
        (device_id,
        device_sn,
        person_id,
        state,
        last_update_time,
        tenant_id)
        VALUES
        <foreach collection="list" item="item" open="(" close=")" separator="),(">
            #{item.deviceId},
            #{item.deviceSn},
            #{item.personId},
            #{item.state},
            #{item.lastUpdateTime},
            #{item.tenantId}
        </foreach>
    </insert>

    <insert id="insert" parameterType="net.shapelight.modules.ten.entity.TenPersonSyncEntity">
        insert into ten_person_sync_${cellId}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="personSyncId != null">
                person_sync_id,
            </if>
            <if test="deviceId != null">
                device_id,
            </if>
            <if test="deviceSn != null">
                device_sn,
            </if>
            <if test="personId != null">
                person_id,
            </if>
            <if test="state != null">
                state,
            </if>
            <if test="lastUpdateTime != null">
                last_update_time,
            </if>
            <if test="tenantId != null">
                tenant_id
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="personSyncId != null">
                #{personSyncId,jdbcType=BIGINT},
            </if>
            <if test="deviceId != null">
                #{deviceId,jdbcType=BIGINT},
            </if>
            <if test="deviceSn != null">
                #{deviceSn},
            </if>
            <if test="personId != null">
                #{personId,jdbcType=BIGINT},
            </if>
            <if test="state != null">
                #{state,jdbcType=TINYINT},
            </if>
            <if test="lastUpdateTime != null">
                #{lastUpdateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="tenantId != null">
                #{tenantId,jdbcType=BIGINT},
            </if>
        </trim>
    </insert>


    <update id="updateById" parameterType="net.shapelight.modules.ten.entity.TenPersonSyncEntity">
        update ten_person_sync_${cellId}
        <set>
            <if test="personSyncId != null">
                person_sync_id = #{personSyncId,jdbcType=BIGINT},
            </if>
            <if test="deviceId != null">
                device_id = #{deviceId,jdbcType=BIGINT},
            </if>
            <if test="deviceSn != null">
                device_sn = #{deviceSn},
            </if>
            <if test="personId != null">
                person_id = #{personId,jdbcType=BIGINT},
            </if>
            <if test="state != null">
                state = #{state,jdbcType=TINYINT},
            </if>
            <if test="lastUpdateTime != null">
                last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="tenantId != null">
                tenant_id = #{tenantId,jdbcType=TIMESTAMP},
            </if>
        </set>
        where person_sync_id = #{personSyncId,jdbcType=BIGINT}
    </update>


    <select id="getDeviceAllPersonIdUpdateTime" resultMap="idupdatetime">
        select person_id,last_update_time from ten_person_sync_${cellId}
        where 1=1
        <if test="deviceId != null and deviceId!=''">
            and device_id = #{deviceId}
        </if>
        and state &lt;&gt; 3
    </select>

    <delete id="removeAllDeletePersons">
    delete from ten_person_sync_${cellId}
    where device_id = #{deviceId}
    and state = 3
  </delete>


    <delete id="removeByDeviceId">
    delete from ten_person_sync_${cellId}
    where device_id = #{deviceId}
  </delete>

    <delete id="removePersonSyncId">
    delete from ten_person_sync_${cellId}
    where person_sync_id = #{personSyncId}
  </delete>


    <select id="findByDeviceIdAndPersonId" resultType="net.shapelight.modules.ten.entity.TenPersonSyncEntity">
        select * from ten_person_sync_${cellId}
        where device_id = #{deviceId}
        and person_id = #{personId}
        order by last_update_time desc
        limit 1
    </select>

    <select id="findByPersonId" resultType="net.shapelight.modules.ten.entity.TenPersonSyncEntity">
        select * from ten_person_sync_${cellId}
        where person_id = #{personId}
    </select>


    <select id="findGroupDevicePersons" resultType="map">
        select device_sn as deviceSn,group_concat(person_id,"_",last_update_time separator ",") as plist
        from ten_person_sync_${cellId} where 1= 1
        <if test="personIds != null">
            and person_id in
            <foreach item="personId" collection="personIds" open="(" separator="," close=")">
                #{personId}
            </foreach>
        </if>
        GROUP BY device_sn
    </select>

    <select id="findGroupDevicePerson" resultType="map">
        select device_sn as deviceSn,group_concat(person_id,"_",last_update_time separator ",") as plist
        from ten_person_sync_${cellId} where 1= 1
        <if test="personIds != null">
            and person_id = #{personId}
        </if>
    </select>

    <select id="findDeviceAllDeletePersons" resultType="net.shapelight.modules.ten.entity.TenPersonSyncEntity">
        select * from ten_person_sync_${cellId}
        where device_id = #{deviceId}
        and state = 3
    </select>

    <select id="findDeviceAllAddPersons" resultType="net.shapelight.modules.ten.entity.TenPersonSyncEntity">
        select * from ten_person_sync_${cellId}
        where device_id = #{deviceId}
        and state = 1
    </select>

    <select id="findTwo" resultType="net.shapelight.modules.ten.entity.TenPersonSyncEntity">
        select device_id, person_id, count(*) from ten_person_sync_${cellId} group by device_id, person_id having count(*) > 1
    </select>

    <select id="findByDeviceIdAndPersonIdTwo" resultType="net.shapelight.modules.ten.entity.TenPersonSyncEntity">
        select * from ten_person_sync_${cellId}
        where device_id = #{deviceId}
        and person_id = #{personId}
    </select>

</mapper>