<?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.ycwl.basic.mapper.SourceMapper">
    <insert id="add">
        insert into source(id, scenic_id, device_id, url, video_url, `type`, face_sample_id, pos_json)
        values (#{id}, #{scenicId}, #{deviceId}, #{url}, #{videoUrl}, #{type}, #{faceSampleId}, #{posJson})
    </insert>
    <insert id="addRelation">
        replace member_source(member_id, source_id, is_buy, type)
        values (#{memberId}, #{sourceId}, #{isBuy}, #{type})
    </insert>
    <insert id="addRelations">
        replace member_source(member_id, source_id, is_buy, type)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.memberId}, #{item.sourceId}, #{item.isBuy}, #{item.type})
        </foreach>
    </insert>
    <update id="update">
        update source
        <set>
            <if test="scenicId!= null">scenic_id = #{scenicId}, </if>
            <if test="deviceId!= null">device_id = #{deviceId}, </if>
            <if test="url!= null">url = #{url}, </if>
            <if test="videoUrl!= null">video_url = #{videoUrl}, </if>
            <if test="isBuy!=null">is_buy = #{isBuy}, </if>
            <if test="type!=null">`type` = #{type}, </if>
            <if test="faceSampleId!= null">face_sample_id = #{faceSampleId}, </if>
            <if test="posJson!= null">pos_json = #{posJson}, </if>
        </set>
            where id = #{id}
    </update>
    <delete id="deleteById">
        delete from source where id = #{id}
    </delete>

    <select id="list" resultType="com.ycwl.basic.model.pc.source.resp.SourceRespVO">
        select so.id, scenic_id, device_id, member_id, url, so.create_time, so.update_time,sc.`name` as scenicName, so.video_url, so.`type`, so.face_sample_id
            from source so
            left join scenic sc on sc.id = so.scenic_id
        <where>
            <if test="scenicId!= null">and scenic_id = #{scenicId} </if>
            <if test="deviceId!= null">and device_id = #{deviceId} </if>
            <if test="url!= null">and url = #{url} </if>
            <if test="isBuy!=null">
               and is_buy = #{isBuy}
            </if>
           <if test="type!=null">and so.type = #{type} </if>
           <if test="faceId!=null">and FIND_IN_SET(so.face_sample_id, (select face.match_sample_ids from face where id = #{faceId}))  </if>
            <if test="startTime!= null">and so.create_time &gt;= #{startTime} </if>
            <if test="endTime!= null">and so.create_time &lt;= #{endTime} </if>
        </where>
        order by so.create_time desc
    </select>
    <select id="userGetById" resultType="com.ycwl.basic.model.pc.source.resp.SourceRespVO">
        select so.id, scenic_id, device_id, ms.member_id, url, so.create_time, so.update_time,sc.`name` as scenicName
        from member_source ms
                 left join source so on ms.source_id = so.id
                 left join scenic sc on sc.id = so.scenic_id
        where so.id = #{id} and ms.member_id = #{userId}
    </select>
    <select id="getById" resultType="com.ycwl.basic.model.pc.source.resp.SourceRespVO">
        select so.id, scenic_id, device_id, url, so.create_time, so.update_time,sc.`name` as scenicName
        from source so
                 left join scenic sc on sc.id = so.scenic_id
        where so.id = #{id}
    </select>
    <select id="listGroupByType" resultType="com.ycwl.basic.model.pc.source.resp.SourceRespVO">
        select so.id, scenic_id,sc.name scenicName, sc.longitude ,sc.latitude,so.type,so.is_buy
        from member_source ms
        left join source so on ms.source_id = so.id
        left join scenic sc on sc.id = so.scenic_id
        <where>
            <if test="scenicId!= null">and so.scenic_id = #{scenicId} </if>
            <if test="memberId!= null">and ms.member_id = #{memberId} </if>
            <if test="isBuy!=null">and ms.is_buy = #{isBuy}</if>
        </where>
        group by so.type
    </select>
    <select id="countByMemberId" resultType="java.lang.Integer">
        select count(1) from member_source where  member_id = #{userId}
    </select>
    <select id="listBySampleIds" resultType="com.ycwl.basic.model.pc.source.entity.SourceEntity">
        select *
        from source
        where face_sample_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="listVideoBySampleIds" resultType="com.ycwl.basic.model.pc.source.entity.SourceEntity">
        select *
        from source
        where face_sample_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and type = 1
    </select>
    <select id="findBySampleId" resultType="com.ycwl.basic.model.pc.source.entity.SourceEntity">
        select *
        from source
        where type = 2 and face_sample_id = #{faceSampleId}
        limit 1
    </select>
    <select id="listUser" resultType="com.ycwl.basic.model.pc.source.resp.SourceRespVO">
        select so.id, scenic_id, device_id, url, so.create_time, so.update_time,sc.`name` as scenicName
        from member_source ms
                 left join source so on ms.source_id = so.id
                 left join scenic sc on sc.id = so.scenic_id
        where
            ms.member_id = #{memberId}
            <if test="scenicId!= null">and so.scenic_id = #{scenicId} </if>
            <if test="deviceId!= null">and so.device_id = #{deviceId} </if>
            <if test="isBuy!=null">and ms.is_buy = #{isBuy}</if>
            <if test="type!=null">and ms.type = #{type} </if>
            <if test="faceId!=null">and FIND_IN_SET(so.face_sample_id, (select face.match_sample_ids from face where id = #{faceId}))  </if>
        order by so.create_time desc
    </select>
</mapper>