<?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.pm.patrol.mapper.PatrolRecordMapper">

    <resultMap type="pd" id="PatrolRecordResult">
        <result property="id"    column="id"    />
        <result property="recordId"    column="record_id"    />
        <result property="pointCode"    column="point_code"    />
        <result property="pointName"    column="point_name"    />
        <result property="patrolPersonId"    column="patrol_person_id"    />
        <result property="patrolPersonName"    column="patrol_person_name"    />
        <result property="patrolTime"    column="patrol_time"    />
        <result property="patrolStatus"    column="patrol_status"    />
        <result property="gpsLongitude"    column="gps_longitude"    />
        <result property="gpsLatitude"    column="gps_latitude"    />
        <result property="deviceSn"    column="device_sn"    />
        <result property="patrolRouteId"    column="patrol_route_id"    />
        <result property="patrolRouteName"    column="patrol_route_name"    />
        <result property="patrolCount"    column="patrol_count"    />
        <result property="exceptionInfo"    column="exception_info"    />
        <result property="images"    column="images"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectPatrolRecordVo">
        select id, record_id, point_code, point_name, patrol_person_id, patrol_person_name,
        patrol_time, patrol_status, gps_longitude, gps_latitude, device_sn,
        patrol_route_id, patrol_route_name, patrol_count, exception_info,
        images, remark, create_time
        from patrol_record_info
    </sql>

    <select id="selectPatrolRecordList" parameterType="pd" resultMap="PatrolRecordResult">
        <include refid="selectPatrolRecordVo"/>
        <where>
            <if test="pointCode != null and pointCode != ''"> and point_code = #{pointCode}</if>
            <if test="patrolPersonName != null and patrolPersonName != ''">
                and patrol_person_name like concat('%', #{patrolPersonName}, '%')
            </if>
            <if test="patrolRouteId != null and patrolRouteId != ''"> and patrol_route_id = #{patrolRouteId}</if>
            <if test="patrolStatus != null"> and patrol_status = #{patrolStatus}</if>
            <if test="beginTime != null and beginTime != ''">
                and patrol_time >= #{beginTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and patrol_time &lt;= #{endTime}
            </if>
        </where>
        order by patrol_time desc
    </select>

    <select id="selectPatrolRecordById" parameterType="Long" resultMap="PatrolRecordResult">
        <include refid="selectPatrolRecordVo"/>
        where id = #{id}
    </select>

    <select id="selectRecentPatrolRecords" parameterType="pd" resultMap="PatrolRecordResult">
        <include refid="selectPatrolRecordVo"/>
        where patrol_time >= date_sub(now(), interval #{recentMinutes} minute)
        order by patrol_time desc
    </select>

    <select id="getTodayPatrolCount" parameterType="pd" resultType="int">
        select count(*) from patrol_record_info
        where date(patrol_time) = curdate()
    </select>

    <select id="getTodayNormalCount" parameterType="pd" resultType="int">
        select count(*) from patrol_record_info
        where date(patrol_time) = curdate() and patrol_status = 1
    </select>

    <select id="getTodayAbnormalCount" parameterType="pd" resultType="int">
        select count(*) from patrol_record_info
        where date(patrol_time) = curdate() and patrol_status = 0
    </select>

    <select id="getTodayTimeoutCount" parameterType="pd" resultType="int">
        select count(*) from patrol_record_info
        where date(patrol_time) = curdate() and patrol_status = 2
    </select>

    <select id="getActivePersonnel" parameterType="pd" resultMap="PatrolRecordResult">
        select distinct patrol_person_id, patrol_person_name, count(*) as patrol_count
        from patrol_record_info
        where date(patrol_time) = curdate()
        group by patrol_person_id, patrol_person_name
        order by patrol_count desc
    </select>

    <select id="getTodayPatrolledPoints" parameterType="pd" resultType="int">
        select count(distinct point_code) from patrol_record_info
        where date(patrol_time) = curdate()
    </select>

    <insert id="insertPatrolRecord" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into patrol_record_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="recordId != null and recordId != ''">record_id,</if>
            <if test="pointCode != null and pointCode != ''">point_code,</if>
            <if test="pointName != null">point_name,</if>
            <if test="patrolPersonId != null">patrol_person_id,</if>
            <if test="patrolPersonName != null">patrol_person_name,</if>
            <if test="patrolTime != null">patrol_time,</if>
            <if test="patrolStatus != null">patrol_status,</if>
            <if test="gpsLongitude != null">gps_longitude,</if>
            <if test="gpsLatitude != null">gps_latitude,</if>
            <if test="deviceSn != null">device_sn,</if>
            <if test="patrolRouteId != null">patrol_route_id,</if>
            <if test="patrolRouteName != null">patrol_route_name,</if>
            <if test="patrolCount != null">patrol_count,</if>
            <if test="exceptionInfo != null">exception_info,</if>
            <if test="images != null">images,</if>
            <if test="remark != null">remark,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="recordId != null and recordId != ''">#{recordId},</if>
            <if test="pointCode != null and pointCode != ''">#{pointCode},</if>
            <if test="pointName != null">#{pointName},</if>
            <if test="patrolPersonId != null">#{patrolPersonId},</if>
            <if test="patrolPersonName != null">#{patrolPersonName},</if>
            <if test="patrolTime != null">#{patrolTime},</if>
            <if test="patrolStatus != null">#{patrolStatus},</if>
            <if test="gpsLongitude != null">#{gpsLongitude},</if>
            <if test="gpsLatitude != null">#{gpsLatitude},</if>
            <if test="deviceSn != null">#{deviceSn},</if>
            <if test="patrolRouteId != null">#{patrolRouteId},</if>
            <if test="patrolRouteName != null">#{patrolRouteName},</if>
            <if test="patrolCount != null">#{patrolCount},</if>
            <if test="exceptionInfo != null">#{exceptionInfo},</if>
            <if test="images != null">#{images},</if>
            <if test="remark != null">#{remark},</if>
            now()
        </trim>
    </insert>

    <update id="updatePatrolRecord" parameterType="pd">
        update patrol_record_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="exceptionInfo != null">exception_info = #{exceptionInfo},</if>
            <if test="images != null">images = #{images},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePatrolRecordById" parameterType="Long">
        delete from patrol_record_info where id = #{id}
    </delete>

</mapper>