<?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.usky.fire.mapper.PatrolInspectionAttendanceMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.fire.domain.PatrolInspectionAttendance">
        <id column="id" property="id"/>
        <result column="dept_id" property="deptId"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="operator" property="operator"/>
        <result column="operator_id" property="operatorId"/>
        <result column="operate_date" property="operateDate"/>
        <result column="sign_in_type" property="signInType"/>
        <result column="operate_code" property="operateCode"/>
        <result column="device_code" property="deviceCode"/>
        <result column="image_path" property="imagePath"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="remarks" property="remarks"/>
        <result column="identification_number" property="identificationNumber"/>
    </resultMap>

    <select id="signIn" resultType="java.lang.Integer">
    SELECT COUNT(*) AS total_sign_outs
    FROM (
        SELECT a.operator_id
        FROM patrol_inspection_attendance a
        INNER JOIN (
            SELECT operator_id, MAX(operate_date) AS latest_date
            FROM patrol_inspection_attendance
            GROUP BY operator_id
        ) b ON a.operator_id = b.operator_id AND a.operate_date = b.latest_date
        WHERE a.sign_in_type = 0 AND tenant_id = #{tenantId}
    ) c;
</select>

    <select id="signOut" resultType="java.lang.Integer">
    SELECT COUNT(*) AS total_sign_outs
    FROM (
        SELECT a.operator_id
        FROM patrol_inspection_attendance a
        INNER JOIN (
            SELECT operator_id, MAX(operate_date) AS latest_date
            FROM patrol_inspection_attendance
            GROUP BY operator_id
        ) b ON a.operator_id = b.operator_id AND a.operate_date = b.latest_date
        WHERE a.sign_in_type = 1 AND tenant_id = #{tenantId}
    ) c;
</select>

</mapper>
