<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.PersonDeviceLogDAO">
    <sql id="Base_Column_List">
	device_no,
	person_id,
	person_name,
	id_,
	identity_no,
	create_by,
	create_time,
	update_by,
	update_time,
	is_effective,
	del_flag,
	device_no,
	person_id,
	temperature_,
	match_status,
	match_msg,
	match_face_id,
	face_image,
	record_time,
    remark_
</sql>
    <delete id="delete">
        delete from base_person_device_log where id_=#{0}
    </delete>
    <resultMap id="SimpleMap" type="com.jpsoft.shinestar.modules.base.entity.PersonDeviceLog">
        <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="isEffective" column="is_effective" />
        <result property="delFlag" column="del_flag" />
        <result property="deviceNo" column="device_no" />
        <result property="personId" column="person_id" />
        <result property="personName" column="person_name"/>
        <result property="temperature" column="temperature_" />
        <result property="identityNo" column="identity_no"/>
        <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="personFlag" column="person_flag" />
        <result property="remark" column="remark_" />
    </resultMap>
    <resultMap id="PersonDeviceLogMap" extends="SimpleMap" type="com.jpsoft.shinestar.modules.base.entity.PersonDeviceLog">
        <association property="device" column="device_no"
                     select="com.jpsoft.shinestar.modules.base.dao.DeviceInfoDAO.getByDeviceNo">
        </association>
        <association property="person" column="person_id"
                     select="com.jpsoft.shinestar.modules.base.dao.PersonInfoDAO.get">
        </association>
    </resultMap>

    <resultMap id="PersonDeviceLogMap2" extends="SimpleMap" type="com.jpsoft.shinestar.modules.base.entity.PersonDeviceLog">
        <association property="person" column="person_id"
                     select="com.jpsoft.shinestar.modules.base.dao.PersonInfoDAO.get">
        </association>
    </resultMap>
    <resultMap id="PersonDeviceLogListMap" extends="SimpleMap" type="com.jpsoft.shinestar.modules.base.entity.PersonDeviceLog">
        <association property="device"
            resultMap="com.jpsoft.shinestar.modules.base.dao.DeviceInfoDAO.DeviceInfoMap"
            columnPrefix="device_"
        />
        <association
                property="person"
                resultMap="com.jpsoft.shinestar.modules.base.dao.PersonInfoDAO.PersonInfoMap"
                columnPrefix="person_"
        />
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonDeviceLog">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_person_device_log
	    (id_,create_by,create_time,update_by,update_time,is_effective,del_flag,
	    device_no,person_id,person_name,temperature_,identity_no,match_status,match_msg,match_face_id,face_image,record_time,
	    remark_)
		values
		(
        #{id,jdbcType=VARCHAR}
        ,#{createBy,jdbcType=VARCHAR}
        ,#{createTime,jdbcType= TIMESTAMP }
        ,#{updateBy,jdbcType=VARCHAR}
        ,#{updateTime,jdbcType= TIMESTAMP }
        ,#{isEffective,jdbcType= NUMERIC }
        ,#{delFlag,jdbcType= NUMERIC }
        ,#{deviceNo,jdbcType=VARCHAR}
        ,#{personId,jdbcType=VARCHAR}
        ,#{personName,jdbcType=VARCHAR}
        ,#{temperature,jdbcType=NUMERIC}
        ,#{identityNo,jdbcType=VARCHAR}
        ,#{matchStatus,jdbcType=INTEGER}
        ,#{matchMsg,jdbcType=VARCHAR}
        ,#{matchFaceId,jdbcType= INTEGER}
        ,#{faceImage,jdbcType=VARCHAR}
        ,#{recordTime,jdbcType=TIMESTAMP}
        ,#{remark,jdbcType=VARCHAR}
		)
	]]>
    </insert>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.PersonDeviceLog">
        update base_person_device_log
        <set>
            <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>
            <if test="remark!=null">
                remark_=#{remark,jdbcType=VARCHAR},
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="simpleSearch" parameterType="hashmap" resultMap="SimpleMap">
        select * from base_person_device_log a
        <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>
        </where>
    </select>
    <select id="findByPersonAndDate" resultMap="SimpleMap">
		select * from base_person_device_log
        where person_id = #{personId}
		and del_flag=false
		<![CDATA[
        and device_no<>'0'
        and record_time >= #{startDate} and record_time <= #{endDate}
        ]]>
        order by record_time asc
	</select>

    <select id="search" parameterType="hashmap" resultMap="PersonDeviceLogListMap">
        <![CDATA[
			select a.*,
	        b.alias_name as device_alias_name,
			c.name_ as person_name_,
			c.phone_ as person_phone_,
			c.id_card as person_id_card,
			c.open_id as person_open_id,
			c.job_number as person_job_number,
			c.position1_ as person_position1_,
			c.position2_ as person_position2_,
	        c.position3_ as person_position3_,
	        c.position4_ as person_position4_,
	        c.position5_ as person_position5_,
	        c.face_image_url as person_face_image_url
			from base_person_device_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.deviceNoList != null">
                and a.device_no in
                <foreach item="item" collection="searchParams.deviceNoList"  open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <trim prefix="and (" prefixOverrides="AND|OR" suffix=")">
                <if test="searchParams.companyCode != null">
                    and exists (
                        select t1.id_ from base_person_popedom t1,base_company_info t2
                        where t1.company_id =t2.id_
                        and t2.code_ like #{searchParams.companyCode}
                        and t1.del_flag=0 and t1.person_id = a.person_id
                    )
                </if>
                <!--未指定查询部门，才查询单位关联设备下记录-->
                <if test="searchParams.companyId == null and searchParams.companyCode!= null">
                    or exists (
                        select t1.id_ from base_device_info t1,base_company_info t2
                        where t1.company_id =t2.id_
                        and t2.code_ like #{searchParams.companyCode}
                        and t1.device_no = a.device_no
                    )
                </if>
                <if test="searchParams.relateCompanyList!=null">
                    <foreach collection="searchParams.relateCompanyList" item="item" open="or c.company_id in(" separator="," close=")">
                        #{item.id}
                    </foreach>
                </if>
            </trim>
            <if test="searchParams.personId != null">
                and a.person_id = #{searchParams.personId}
            </if>
            <if test="searchParams.deviceNo != null">
                and a.device_no like #{searchParams.deviceNo}
            </if>
            <if test="searchParams.aliasName != null">
                and b.alias_name like #{searchParams.aliasName}
            </if>
            <if test="searchParams.personName != null">
                and c.name_ like #{searchParams.personName}
            </if>
            <if test="searchParams.matchStatus != null">
                and a.match_status = #{searchParams.matchStatus}
            </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.minTemperature != null">
                <![CDATA[
                  and a.temperature_ >= #{searchParams.minTemperature}
                ]]>
            </if>
            <if test="searchParams.maxTempedistinctrature != null">
                <![CDATA[
                  and a.temperature_ <= #{searchParams.maxTemperature}
                ]]>
            </if>
            <if test='searchParams.matchMsg == "1"'>
                <![CDATA[
                    and a.person_id<>0
                ]]>
            </if>
            <if test='searchParams.matchMsg == "0"'>
                <![CDATA[
                    and a.person_id=0
                ]]>
            </if>
            <if test="searchParams.distinct">
                and a.rownum_ in (
                    SELECT max(rownum_)
                    from base_person_device_log
                    <where>
                        del_flag=0
                        <if test="searchParams.beginTime != null">
                        <![CDATA[
                          and record_time >= #{searchParams.beginTime}
                        ]]>
                        </if>
                        <if test="searchParams.endTime != null">
                        <![CDATA[
                          and record_time <= #{searchParams.endTime}
                        ]]>
                        </if>
                    </where>
                    GROUP BY person_id
                )
            </if>
            <if test="searchParams.jobNumbers!=null">
                <foreach collection="searchParams.jobNumbers" item="item" open="and c.job_number in(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        <foreach item="sort" collection="sortList"  open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>

    <select id="get" resultMap="SimpleMap">
        select * from base_person_device_log where id_=#{0}
    </select>
    <select id="queryUnAttendanceList" resultType="long">
        <![CDATA[
        select distinct(a.id_)
        from base_person_info a
        inner join base_person_popedom c on c.person_id = a.id_
        inner join base_company_info b on b.id_ = c.company_id
        where a.del_flag=0
        and c.del_flag=0
        and b.code_ like #{companyCode}
        and a.id_ not in (
            select c.person_id from base_person_device_log c
            where c.record_time>=#{startTime}
            and c.record_time<=#{endTime}
            and c.del_flag=0
        )
        ]]>
        <if test="excludeCompanyList!=null">
            <foreach collection="excludeCompanyList" index="index" item="item" open="and b.id_ not in (" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="queryAttendanceList" resultType="long">
        <![CDATA[
        select distinct(a.id_)
        from base_person_info a
        inner join base_person_popedom c on c.person_id = a.id_
        inner join base_company_info b on b.id_ = c.company_id
        where a.del_flag=0
        and b.code_ like #{companyCode}
        and a.id_ in (
            select c.person_id from base_person_device_log c
            where c.record_time>=#{startTime}
            and c.record_time<=#{endTime}
            and c.del_flag=0
        )
        ]]>
        <foreach collection="excludeCompanyList" index="index" item="item" open="and b.id_ not in (" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="findLastPersonLog" resultMap="PersonDeviceLogMap">
        <![CDATA[
            select * from base_person_device_log
            where person_id=#{personId} and temperature_<=#{temperatureMax} and del_flag=0
            order by record_time desc
            limit 1
        ]]>
    </select>
    <select id="getDayCheckedVistorNumByDeviceNoList" resultType="Integer">
        <![CDATA[
            select count(*) from base_person_device_log where match_face_id = 0
            and record_time >=#{startTime}
            and record_time <= #{endTime}
            and temperature_ > 0
        ]]>
        <foreach collection="list" index="index" item="item" open="and device_no in(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="findAllPersonLogByCompanyIdAndDeviceNo" resultMap="PersonDeviceLogMap">
        <![CDATA[
        select * from base_person_device_log
        where rownum_ in (
            SELECT max(m.rownum_)
            from base_person_device_log m inner join base_person_info n ON m.person_id = n.id_
            where m.record_time>=#{startTime}
            and m.record_time <=#{endTime}
            and n.del_flag = 0
            and m.del_flag = 0
        ]]>
        <foreach collection="companyList" index="index" item="item" open=" and n.company_id in (" separator="," close=")">
            #{item.id}
        </foreach>
        <![CDATA[
            and m.temperature_>0 GROUP BY m.person_id
        )
        ]]>
        union all
        <![CDATA[
            select * from base_person_device_log
            where person_id = 0
            and record_time >= #{startTime}
            and record_time <= #{endTime}
            and temperature_>0
            and del_flag = 0
        ]]>
        <foreach collection="deviceNoList" index="index" item="item" open="and device_no in(" separator="," close=")">
            #{item}
        </foreach>
        order by record_time desc
    </select>

    <select id="findAllPersonLogByDeviceNo" resultMap="PersonDeviceLogMap2">

        <![CDATA[
            select person_id,face_Image,max(record_Time) as record_Time from base_person_device_log
            where 1=1
            and person_id!=0
            and del_flag = 0
            and record_time >= #{startTime}
            and record_time <= #{endTime}

            and device_no=#{deviceNoList}
        ]]>

        group by person_id
        order by record_time desc


    </select>



    <select id="findLastPersonLogByCompanyList" resultMap="PersonDeviceLogMap">
        <![CDATA[
        select * from base_person_device_log
        where rownum_ in (
            SELECT max(m.rownum_)
            from base_person_device_log m inner join base_person_info n ON m.person_id = n.id_
            where m.record_time>=#{startTime}
            and m.record_time <=#{endTime}
            and n.del_flag = 0
            and n.guest_enabled is not true
        ]]>
        <foreach collection="companyList" index="index" item="item" open=" and n.company_id in (" separator="," close=")">
            #{item.id}
        </foreach>
        <![CDATA[
            and m.temperature_>0 GROUP BY m.person_id
        )
        ]]>
        order by record_time desc
    </select>
    <select id="findVisitorLogByDeviceNoList" resultMap="PersonDeviceLogMap">
        <![CDATA[
            select * from base_person_device_log
            where match_face_id = 0
            and record_time >= #{startTime}
            and record_time <= #{endTime}
        ]]>
        <if test='filter=="0"'>
            and temperature_>0
        </if>
        <if test='filter=="1"'>
             <![CDATA[
                 and temperature_<= #{max}
                  and temperature_>0
              ]]>
        </if>
        <if test='filter=="2"'>
            and temperature_> #{max}
        </if>

        <foreach collection="list" index="index" item="item" open="and device_no in(" separator="," close=")">   #{item}
        </foreach>
        ORDER BY record_time desc
    </select>
    <select id="findLastTeacherLogByCompanyList" resultMap="PersonDeviceLogMap">

        <![CDATA[
        select * from base_person_device_log
        where rownum_ in (
            SELECT max(m.rownum_)
            from base_person_device_log m inner join base_person_info n ON m.person_id = n.id_
            where m.record_time>=#{startTime}
            and m.record_time <=#{endTime}
            and n.popedom_ like '%2%'
            and n.guest_enabled is not true
        ]]>
        <foreach collection="list" index="index" item="item" open=" and n.company_id in (" separator="," close=")">
            #{item.id}
        </foreach>
        <![CDATA[
            and m.temperature_>0 GROUP BY m.person_id
        )
        ]]>
        order by record_time desc
<!--       &lt;!&ndash; <![CDATA[-->
<!--        SELECT * from-->
<!--        (-->
<!--            SELECT m.*,n.company_id as company_id,n.popedom_ as popedom_-->
<!--            from base_person_device_log m LEFT JOIN base_person_info n-->
<!--            ON m.person_id = n.id_-->
<!--            where m.record_time>=#{startTime}-->
<!--            and m.record_time <= #{endTime}-->
<!--            and popedom_ like '%2%'-->
<!--            and m.temperature_>0-->
<!--        ]]>-->
<!--            <foreach collection="list" index="index" item="item" open="and company_id in(" separator="," close=")">-->
<!--                #{item.id}-->
<!--            </foreach>-->
<!--        ) a-->
<!--        <![CDATA[-->
<!--        where person_id<>0 and temperature_>0  GROUP BY a.person_id ORDER BY record_time desc-->
<!--        ]]>&ndash;&gt;-->
    </select>

    <select id="findLastStudentLogByCompanyList" resultMap="PersonDeviceLogMap">
        <![CDATA[
        select * from base_person_device_log
        where rownum_ in (
            SELECT max(m.rownum_)
            from base_person_device_log m inner join base_person_info n ON m.person_id = n.id_
            where m.record_time>=#{startTime}
            and m.record_time <=#{endTime}
            and n.popedom_ = '1'
            and n.guest_enabled is not true
        ]]>
        <foreach collection="list" index="index" item="item" open=" and n.company_id in (" separator="," close=")">
            #{item.id}
        </foreach>
        <![CDATA[
            and m.temperature_>0 GROUP BY m.person_id
        )
        ]]>
        order by record_time desc

        <!--<![CDATA[
        SELECT * from
        (
            SELECT m.*,n.company_id as company_id,n.popedom_ as popedom_
            from base_person_device_log m LEFT JOIN base_person_info n
            ON m.person_id = n.id_
            where record_time>=#{startTime}
            and record_time <= #{endTime}
            and popedom_ = '1'
        ]]>
        <foreach collection="list" index="index" item="item" open="and company_id in(" separator="," close=")">   #{item.id}
        </foreach>
        <![CDATA[
        ) a
        where person_id <> 0 and temperature_>0  GROUP BY a.person_id ORDER BY record_time desc
        ]]>-->
    </select>
    <select id="getDayCheckedPersonNumByCompanyList" resultType="Integer">
        select count(*) from base_person_device_log a left join base_person_info b
        on a.person_id = b.id_
        where a.record_time >=#{startTime}
        and a.record_time &lt;= #{endTime}
        and b.del_flag = 0
        and b.guest_enabled is not true
        <foreach collection="list" index="index" item="item" open=" and b.company_id in (" separator="," close=")">   #{item.id}
        </foreach>
        and a.del_flag = 0
        <if test="popedom!=null">
            and b.popedom_ like #{popedom}
        </if>
    </select>
    <select id="findExpiredVisitorRecord" resultMap="PersonDeviceLogMap">
        <![CDATA[
            select * from base_person_device_log
            where datediff(now(),record_time)>1
            and person_id=0
            order by record_time asc limit ${limit}
        ]]>
    </select>
    <select id="countByAttendance" resultType="long">
        <![CDATA[
            select count(distinct(c.person_id)) from
            base_person_popedom a,base_company_info b,base_person_device_log c,base_person_info d
            where a.company_id = b.id_
            and a.person_id = c.person_id
            and b.code_ like #{companyCode}
            and a.del_flag=0
            and c.record_time>=#{beginDate}
            and c.record_time<=#{endDate}
            and c.del_flag=0
            and a.person_id = d.id_
            and d.del_flag=0
        ]]>
    </select>
    <select id="getDayAbnormalNumByDeviceNoList" resultType="Integer">
        <![CDATA[
        select count(*) from base_person_device_log a
        where a.record_time >=#{startTime}
        and a.record_time <= #{endTime}
        and a.temperature_>#{temperatureMax}
        and a.person_id = 0
        ]]>
        <foreach collection="list" index="index" item="item" open="and a.device_no in(" separator="," close=")">#{item}
        </foreach>
        and a.del_flag = 0
    </select>

    <select id="findByPersonAndRecordTime" resultMap="PersonDeviceLogMap">
        <![CDATA[
        select * from base_person_device_log
        where del_flag = 0
        and person_id = #{personId}
        and record_time = #{recordTime}
        limit 1
        ]]>
    </select>
    <select id="queryAttendanceListByCompanyId" resultType="long">
        <![CDATA[
            select distinct(a.id_)
            from base_person_info a
            inner join base_person_popedom c on c.person_id = a.id_
            where a.del_flag=0
            and c.del_flag=0
            and c.company_id=#{companyId}
            and c.enable_attendance = 1
            and a.id_ in (
                select c.person_id from base_person_device_log c
                where c.record_time>=#{startTime}
                and c.record_time<=#{endTime}
                and c.del_flag=0
            )
        ]]>
    </select>
    <select id="queryUnAttendanceListByCompanyId" resultType="long">
        <![CDATA[
            select distinct(a.id_)
            from base_person_info a
            inner join base_person_popedom c on c.person_id = a.id_
            where a.del_flag=0
            and c.del_flag=0
            and c.company_id=#{companyId}
            and c.enable_attendance = 1
            and a.id_ not in (
                select c.person_id from base_person_device_log c
                where c.record_time>=#{startTime}
                and c.record_time<=#{endTime}
                and c.del_flag=0
            )
        ]]>
    </select>
    <select id="findByPersonAndDateAndScene" resultMap="PersonDeviceLogMap">
        <![CDATA[
            select t1.* from base_person_device_log t1
            inner join base_device_info t2 on t1.device_no = t2.device_no
            where t1.person_id = #{personId}
            and t1.del_flag=false
            and t1.record_time >= #{beginTime}
            and t1.record_time <= #{endTime}
            and find_in_set(#{scene},t2.scene_)<>0
            order by t1.record_time asc
        ]]>
    </select>

    <select id="importData"  resultMap="PersonDeviceLogMap">
        <![CDATA[
            SELECT * from base_person_device_log
            WHERE record_time>='2021-06-01' and record_time<'2021-06-03 11:30:00'
            and device_no in (
            SELECT c.device_no FROM base_canteen_info a
            left join base_canteen_device b on a.id_=b.canteen_id
            left join base_device_info c on b.device_id=c.id_
            WHERE a.del_flag=FALSE and b.del_flag=FALSE and c.del_flag=FALSE)
            ORDER BY record_time desc
        ]]>
    </select>
</mapper>