<?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.jpsoft.shinestar.modules.base.dao.PersonDeviceFilterLogDAO">
    <sql id="Base_Column_List">
	id_,
	create_by,
	create_time,
	update_by,
	update_time,
	del_flag,
	device_no,
	person_id,
	temperature_,
	match_status,
	match_msg,
	match_face_id,
	face_image,
	record_time,
	time_zones
    </sql>
    <delete id="deleteExpiredVisitorRecord">
        <![CDATA[
            delete from base_person_device_filter_log
            where datediff(now(),record_time)>1
            and person_id=0
            order by record_time asc limit ${limit}
        ]]>
    </delete>
    <resultMap id="PersonDeviceFilterLogMap" type="com.jpsoft.shinestar.modules.base.entity.PersonDeviceFilterLog">
        <id property="id" column="id_"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="deviceNo" column="device_no"/>
        <result property="personId" column="person_id"/>
        <result property="name" column="name_"/>
        <result property="temperature" column="temperature_"/>
        <result property="matchStatus" column="match_status"/>
        <result property="matchMsg" column="match_msg"/>
        <result property="matchFaceId" column="match_face_id"/>
        <result property="faceImage" column="face_image"/>
        <result property="recordTime" column="record_time"/>
        <result property="timeZones" column="time_zones"/>
    </resultMap>

    <select id="get" parameterType="long" resultMap="PersonDeviceFilterLogMap">
        select * from base_person_device_filter_log where id_=#{0}
    </select>

    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonDeviceFilterLog">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_person_device_filter_log
	    (create_by,create_time,update_by,update_time,del_flag,device_no,person_id,temperature_,match_status,match_msg,match_face_id,face_image,record_time,time_zones)
		values
		(
         #{createBy,jdbcType=VARCHAR}
        ,#{createTime,jdbcType= TIMESTAMP }
        ,#{updateBy,jdbcType=VARCHAR}
        ,#{updateTime,jdbcType= TIMESTAMP }
        ,#{delFlag,jdbcType= NUMERIC }
        ,#{deviceNo,jdbcType=VARCHAR}
        ,#{personId,jdbcType=VARCHAR}
        ,#{temperature,jdbcType=NUMERIC}
        ,#{matchStatus,jdbcType=INTEGER}
        ,#{matchMsg,jdbcType=VARCHAR}
        ,#{matchFaceId,jdbcType= INTEGER}
        ,#{faceImage,jdbcType=VARCHAR}
        ,#{recordTime,jdbcType=TIMESTAMP}
        ,#{timeZones,jdbcType=VARCHAR}
		)
	]]>
    </insert>

    <select id="findByPersonIdAndDeviceNoAndTimeZones" resultMap="PersonDeviceFilterLogMap">
        select
        <include refid="Base_Column_List"/>
        from base_person_device_filter_log
        where person_id=#{personId} and device_no=#{deviceNo} and time_zones=#{timeZones} and del_flag is false
    </select>

    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonDeviceFilterLog">
        update base_person_device_filter_log
        <set>
            <if test="deviceNo != null">
                device_no=#{deviceNo,jdbcType=VARCHAR},
            </if>
            <if test="personId != null">
                person_id=#{personId,jdbcType=VARCHAR},
            </if>
            <if test="temperature != null">
                temperature_=#{temperature,jdbcType=VARCHAR},
            </if>
            <if test="matchStatus != null">
                match_status=#{matchStatus,jdbcType=NUMERIC},
            </if>
            <if test="matchMsg!=null">
                match_msg=#{matchMsg,jdbcType= VARCHAR },
            </if>
            <if test="matchFaceId!=null">
                match_face_id=#{matchFaceId,jdbcType= NUMERIC },
            </if>
            <if test="faceImage!=null">
                face_image=#{faceImage,jdbcType= VARCHAR },
            </if>
            <if test="recordTime!=null">
                record_time=#{recordTime,jdbcType= TIMESTAMP },
            </if>
            <if test="timeZones!=null">
                time_zones=#{timeZones,jdbcType= VARCHAR },
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
        </set>
        where id_=#{id}
    </update>

    <select id="findByPersonAndDate" resultMap="PersonDeviceFilterLogMap">
		select * from base_person_device_filter_log where person_id = #{personId}
		and del_flag=false
		<![CDATA[
           and record_time >= #{startDate} and record_time <= #{endDate}
        ]]>
        order by record_time desc
	</select>

    <select id="findByPersonOrderTemperature" resultMap="PersonDeviceFilterLogMap">
		select * from base_person_device_filter_log where person_id = #{personId}
		and del_flag=false
		<![CDATA[
           and record_time >= #{startDate} and record_time <= #{endDate}
        ]]>
        order by temperature_ desc limit 1
	</select>


    <select id="search" parameterType="hashmap" resultMap="PersonDeviceFilterLogMap">
        <![CDATA[
			select a.*,c.name_ from base_person_device_filter_log a left join base_device_info b
			on  a.device_no = b.device_no left join base_person_info c
			on a.person_id = c.id_
		]]>
        <where>
            and a.del_flag = false
            <if test="searchParams.personId != null">
                and a.person_id = #{searchParams.personId}
            </if>
            <if test="searchParams.beginTime != null">
                <![CDATA[
                  and a.record_time >= #{searchParams.beginTime}
                ]]>
            </if>
            <if test="searchParams.endTime != null">
                <![CDATA[
                  and a.record_time <= #{searchParams.endTime}
                ]]>
            </if>
            <if test="searchParams.fever==true">
                <![CDATA[
                and a.temperature_>#{searchParams.temperatureMax}
                ]]>
            </if>
            <if test="searchParams.fever==false">
                <![CDATA[
                and a.temperature_<= #{searchParams.temperatureMax}
                ]]>
            </if>
            <if test="searchParams.companyId != null">
                <![CDATA[
                and c.company_id = #{searchParams.companyId}
                ]]>
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
    <select id="temperatureNormalStatistics" resultMap="PersonDeviceFilterLogMap">
        <![CDATA[

                    select count(*) from base_person_device_filter_log a
                    LEFT JOIN base_person_info b on a.person_id=b.id_
                    LEFT JOIN base_company_info c on b.company_id=c.id_
                    where a.del_flag=false
                    and c.code_ like #{companyCode}
                    and a.temperature_ < #{temperature}
                    and a.record_time >= #{startDate}
                    and a.record_time <= #{endDate}

        ]]>
    </select>
    <select id="temperatureUnusualStatistics" resultMap="PersonDeviceFilterLogMap">
        <![CDATA[

                    select count(*) from base_person_device_filter_log a
                    LEFT JOIN base_person_info b on a.person_id=b.id_
                    LEFT JOIN base_company_info c on b.company_id=c.id_
                    where a.del_flag=false
                    and c.code_ like #{companyCode}
                    and a.temperature_ >= #{temperature}
                    and a.record_time >= #{startDate}
                    and a.record_time <= #{endDate}

        ]]>
    </select>
    <select id="list" resultMap="PersonDeviceFilterLogMap">
        select * from base_person_device_filter_log where del_flag=false
    </select>
    <select id="unusualStatistics" resultType="java.util.HashMap">
        <![CDATA[
        select
        c.id_ as id,
        c.name_ as name,
        date_format(a.record_time,'%Y-%m-%d') as date,
        count(b.id_) as personCount
        from base_person_device_filter_log a
        LEFT JOIN base_person_info b on a.person_id=b.id_
        LEFT JOIN base_company_info c on b.company_id=c.id_
            where a.del_flag=0
            and b.del_flag=0
            and a.temperature_ > #{temperature}
            and a.record_time >= #{startDate}
            and a.record_time < #{endDate}
        ]]>
        and (
        c.code_ like #{companyCode}
        <if test="personId!=null">
            or c.id_ in (
            select d.company_id from base_person_company d where d.person_id = #{personId}
            )
        </if>
        )
        GROUP BY c.id_,date_format(a.record_time,'%Y-%m-%d')
    </select>
    <select id="lastPersonLog" resultMap="PersonDeviceFilterLogMap">
        SELECT * FROM base_person_device_filter_log
        WHERE del_flag = FALSE AND person_id = #{personId} and temperature_ > 0
        ORDER BY record_time desc LIMIT 1
    </select>
    <select id="getByPersonIdAndDate" resultMap="PersonDeviceFilterLogMap">
        SELECT * FROM base_person_device_filter_log
        WHERE del_flag=FALSE AND person_id= #{personId}
        <![CDATA[
          AND record_time >= #{startDate} AND record_time < #{endDate}
        ]]>
        ORDER BY record_time desc LIMIT 1
    </select>
    <select id="countByTimeRangeAndCompanyCode" resultType="long">
        <![CDATA[
            select count(*) from base_person_device_filter_log a
            LEFT JOIN base_person_info b on a.person_id=b.id_
            LEFT JOIN base_company_info c on b.company_id=c.id_
            where a.del_flag=false
            and record_time >= #{startDate}
            and record_time <= #{endDate}
        ]]>
        and (
        c.code_ like #{companyCode}
        <if test="personId!=null">
            or c.id_ in (
            select d.company_id from base_person_company d where d.person_id = #{personId}
            )
        </if>
        )
        <if test="status==1">
            <![CDATA[
            and a.temperature_<=#{temperatureMax}
        ]]>
        </if>
        <if test="status==-1">
            <![CDATA[
            and a.temperature_>#{temperatureMax}
        ]]>
        </if>
    </select>
    <select id="countUndetectedByCompanyCode" resultType="long">
        <![CDATA[
            select count(*) from base_person_info a,base_company_info b
            where a.company_id = b.id_
            and b.del_flag=0
            and a.del_flag=0
            and a.id_ not in (
                select person_id from base_person_device_filter_log
                where record_time>=#{startTime}
                and record_time <= #{endTime}
            )
        ]]>
        and (
        b.code_ like #{companyCode}
        <!--        <if test="personId!=null">
                    or b.id_ in (
                        select d.company_id from base_person_company d where d.person_id = #{personId}
                    )
                </if>-->
        <if test="personCompanyList!=null">
            <foreach collection="personCompanyList" index="index" item="item" open=" or b.id_ in(" separator=","
                     close=")">
                #{item.companyId}
            </foreach>
        </if>
        )
    </select>
    <select id="getDayAbnormalNum" resultType="java.lang.Integer">
        <![CDATA[
            select count(DISTINCT person_id) from base_person_device_filter_log
            where record_time >=#{startTime}
            and record_time <= #{endTime}
            and temperature_>#{temperatureMax}
        ]]>
    </select>
    <select id="getDayAbnormalNumByCompanyList" resultType="Integer">
        select count(DISTINCT person_id) from base_person_device_filter_log a left join base_person_info b
        on a.person_id = b.id_
        <foreach collection="list" index="index" item="item" open="and b.company_id in(" separator="," close=")">
            #{item.id}
        </foreach>
        <![CDATA[
        where a.record_time >=#{startTime}
        and a.record_time <= #{endTime}
        and a.temperature_>#{temperatureMax}
        and a.del_flag = 0
        ]]>
    </select>

    <select id="getDayCheckedPersonNumByCompanyList" resultType="Integer">
        select count(DISTINCT person_id) from base_person_device_log a left join base_person_info b
        on a.person_id = b.id_ and b.company_id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">#{item.id}
        </foreach>
        <if test="popedom!=null">
            and b.popedom_ like #{popedom}
        </if>
        and b.guest_enabled is not true
        and b.del_flag = 0
        where a.record_time >=#{startTime}
        and a.record_time &lt;= #{endTime}
        and a.del_flag = 0
        and a.temperature_ > 0
    </select>

    <select id="getDayNormalNumByCompanyId" resultType="Integer">
        select count(DISTINCT person_id) from base_person_device_filter_log a
        left join base_person_info b
        on a.person_id = b.id_ and b.company_id = #{companyId}
        where a.record_time >=#{startTime}
        and a.record_time &lt;= #{endTime}
        and a.temperature_&lt;= #{temperatureMax}
        and a.del_flag = 0
    </select>
    <select id="countDayByPersonIdAndDate" resultType="Integer">
        <![CDATA[
            select count(distinct(DATE_FORMAT(a.record_time,'%y-%m-%d'))) from base_person_device_filter_log a
            where a.person_id=#{personId}
            and a.record_time>=#{startDate}
            and a.record_time<#{endDate}
            and a.del_flag=0
        ]]>
    </select>
    <select id="countDetectedByCompanyId" resultType="int">
        <![CDATA[
            select count(distinct(a.person_id)) from base_person_device_filter_log a
            left join base_person_info b
            on a.person_id = b.id_ and b.company_id=#{companyId}
            where
            a.record_time>=#{startDate}
            and a.record_time<#{endDate}
            and a.del_flag=0
            and b.del_flag=0
        ]]>
    </select>
    <select id="findUndetectedByCompanyCode"
            parameterType="hashmap"
            resultMap="com.jpsoft.shinestar.modules.base.dao.PersonInfoDAO.PersonInfoMap">
        <![CDATA[
            select a.* from base_person_info a,base_company_info b
            where a.company_id = b.id_
            and b.del_flag=0
            and a.del_flag=0
            and a.id_ not in (
                select person_id from base_person_device_filter_log
                where record_time>=#{searchParams.startTime}
                and record_time <= #{searchParams.endTime}
            )
        ]]>
        and (
        b.code_ like #{searchParams.companyCode}
        <!--        <if test="personId!=null">
                    or b.id_ in (
                    select d.company_id from base_person_company d where d.person_id = #{personId}
                    )
                </if>-->
        <if test="searchParams.personCompanyList!=null">
            <foreach collection="searchParams.personCompanyList" index="index" item="item" open=" or b.id_ in("
                     separator="," close=")">
                #{item.companyId}
            </foreach>
        </if>
        )
        order by a.id_ asc
    </select>

    <select id="getDayCheckedPersonByCompanyList" resultMap="PersonDeviceFilterLogMap">
        <![CDATA[
            select * from base_person_device_filter_log a left join base_person_info b
            on a.person_id = b.id_
            where  a.record_time>=#{startDate}
            and a.record_time<#{endDate}
            and a.del_flag=0
            and b.company_id = #{companyInfo.id}
        ]]>
        group by a.person_id order by a.record_time desc
    </select>
    <select id="countDetectedByCompanyIds" resultType="long">
        <![CDATA[
            SELECT count(distinct(b.id_)) from base_person_device_filter_log a ,base_person_info b
            where a.person_id = b.id_
            and a.del_flag = 0
            and b.del_flag = 0
            and a.record_time>=#{startTime}
            and a.record_time<=#{endTime}
        ]]>
        <foreach collection="companyIds" index="index" item="item" open="and b.company_id in(" separator=","
                 close=")">
            #{item}
        </foreach>
    </select>

    <select id="findUndetectedPersonListByCompanyIds" resultMap="com.jpsoft.shinestar.modules.base.dao.PersonInfoDAO.PersonInfoMap">
        <![CDATA[
        SELECT * from base_person_info where id_ not in (
            SELECT b.id_ from base_person_device_filter_log a ,base_person_info b
            where a.person_id = b.id_
            and a.del_flag = 0
            and b.del_flag = 0
            and a.record_time>=#{startTime}
            and a.record_time<=#{endTime}
        ]]>
        <foreach collection="companyIds" index="index" item="item" open="and b.company_id in(" separator="," close=")">
            #{item}
        </foreach>
        <![CDATA[
         GROUP BY a.person_id )
            and base_person_info.del_flag = 0
        ]]>
        <foreach collection="companyIds" index="index" item="item" open="and base_person_info.company_id in(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="findByPersonAndDeviceNoAndTime" resultMap="PersonDeviceFilterLogMap">
        select * from base_person_device_filter_log where
        person_id = #{personId}
        and device_no= #{deviceNo}
        and del_flag=false
        <![CDATA[
           and record_time >= #{startTime} and record_time <= #{endTime}
        ]]>
        order by record_time desc
    </select>
    <select id="countDetectedByCompanyCode" resultType="long">
        <![CDATA[
            select count(distinct(a.person_id)) from base_person_device_filter_log a
            inner join base_person_info b on a.person_id = b.id_
            inner join base_company_info c on b.company_id = c.id_
            where instr(c.code_,#{companyCode})=1
            and a.record_time>=#{startTime}
            and a.record_time<#{endTime}
            and a.del_flag=0
            and b.del_flag=0
            and c.del_flag=0
        ]]>
    </select>

    <select id="findByTimeAndPersonId" resultMap="PersonDeviceFilterLogMap">
        select * from base_person_device_filter_log a
        where a.del_flag = 0
        and a.person_id = #{personId}
        and DATE_FORMAT(a.create_time,'%Y-%m-%d') =  DATE_FORMAT(#{queryTime},'%Y-%m-%d')
    </select>
</mapper>
