<?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">

<!-- PatrolPointMapper.xml -->
<mapper namespace="com.pm.patrol.mapper.PatrolPointMapper">

    <resultMap type="pd" id="PatrolPointResult">
        <result property="id"    column="id"    />
        <result property="pointCode"    column="point_code"    />
        <result property="pointName"    column="point_name"    />
        <result property="areaId"    column="area_id"    />
        <result property="areaName"    column="area_name"    />
        <result property="buildingId"    column="building_id"    />
        <result property="buildingName"    column="building_name"    />
        <result property="floorId"    column="floor_id"    />
        <result property="floorName"    column="floor_name"    />
        <result property="positionX"    column="position_x"    />
        <result property="positionY"    column="position_y"    />
        <result property="positionZ"    column="position_z"    />
        <result property="pointType"    column="point_type"    />
        <result property="patrolFrequency"    column="patrol_frequency"    />
        <result property="cameraIds"    column="camera_ids"    />
        <result property="deviceId"    column="device_id"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="isActive"    column="is_active"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectPatrolPointVo">
        select id, point_code, point_name, area_id, area_name, building_id, building_name,
               floor_id, floor_name, position_x, position_y, position_z, point_type,
               patrol_frequency, camera_ids, device_id, qr_code, is_active, remark,
               create_time, update_time
        from patrol_point_info
    </sql>

    <select id="selectPatrolPointList" parameterType="pd" resultMap="PatrolPointResult">
        <include refid="selectPatrolPointVo"/>
        <where>
            <if test="pointCode != null and pointCode != ''"> and point_code like concat('%', #{pointCode}, '%')</if>
            <if test="pointName != null and pointName != ''"> and point_name like concat('%', #{pointName}, '%')</if>
            <if test="buildingId != null and buildingId != ''"> and building_id = #{buildingId}</if>
            <if test="floorId != null and floorId != ''"> and floor_id = #{floorId}</if>
            <if test="pointType != null and pointType != ''"> and point_type = #{pointType}</if>
            <if test="isActive != null"> and is_active = #{isActive}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectPatrolPointById" parameterType="Long" resultMap="PatrolPointResult">
        <include refid="selectPatrolPointVo"/>
        where id = #{id}
    </select>

    <insert id="insertPatrolPoint" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into patrol_point_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pointCode != null and pointCode != ''">point_code,</if>
            <if test="pointName != null and pointName != ''">point_name,</if>
            <if test="areaId != null">area_id,</if>
            <if test="areaName != null">area_name,</if>
            <if test="buildingId != null">building_id,</if>
            <if test="buildingName != null">building_name,</if>
            <if test="floorId != null">floor_id,</if>
            <if test="floorName != null">floor_name,</if>
            <if test="positionX != null">position_x,</if>
            <if test="positionY != null">position_y,</if>
            <if test="positionZ != null">position_z,</if>
            <if test="pointType != null">point_type,</if>
            <if test="patrolFrequency != null">patrol_frequency,</if>
            <if test="cameraIds != null">camera_ids,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="qrCode != null">qr_code,</if>
            <if test="isActive != null">is_active,</if>
            <if test="remark != null">remark,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pointCode != null and pointCode != ''">#{pointCode},</if>
            <if test="pointName != null and pointName != ''">#{pointName},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="areaName != null">#{areaName},</if>
            <if test="buildingId != null">#{buildingId},</if>
            <if test="buildingName != null">#{buildingName},</if>
            <if test="floorId != null">#{floorId},</if>
            <if test="floorName != null">#{floorName},</if>
            <if test="positionX != null">#{positionX},</if>
            <if test="positionY != null">#{positionY},</if>
            <if test="positionZ != null">#{positionZ},</if>
            <if test="pointType != null">#{pointType},</if>
            <if test="patrolFrequency != null">#{patrolFrequency},</if>
            <if test="cameraIds != null">#{cameraIds},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="qrCode != null">#{qrCode},</if>
            <if test="isActive != null">#{isActive},</if>
            <if test="remark != null">#{remark},</if>
            now()
        </trim>
    </insert>

    <update id="updatePatrolPoint" parameterType="pd">
        update patrol_point_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="pointCode != null and pointCode != ''">point_code = #{pointCode},</if>
            <if test="pointName != null and pointName != ''">point_name = #{pointName},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="areaName != null">area_name = #{areaName},</if>
            <if test="buildingId != null">building_id = #{buildingId},</if>
            <if test="buildingName != null">building_name = #{buildingName},</if>
            <if test="floorId != null">floor_id = #{floorId},</if>
            <if test="floorName != null">floor_name = #{floorName},</if>
            <if test="positionX != null">position_x = #{positionX},</if>
            <if test="positionY != null">position_y = #{positionY},</if>
            <if test="positionZ != null">position_z = #{positionZ},</if>
            <if test="pointType != null">point_type = #{pointType},</if>
            <if test="patrolFrequency != null">patrol_frequency = #{patrolFrequency},</if>
            <if test="cameraIds != null">camera_ids = #{cameraIds},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="qrCode != null">qr_code = #{qrCode},</if>
            <if test="isActive != null">is_active = #{isActive},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = now()
        </trim>
        where id = #{id}
    </update>

    <update id="updatePatrolPointStatus" parameterType="pd">
        update patrol_point_info
        set is_active = #{isActive},
            update_time = now()
        where id = #{id}
    </update>

    <delete id="deletePatrolPointById" parameterType="Long">
        delete from patrol_point_info where id = #{id}
    </delete>

    <delete id="deletePatrolPointByIds" parameterType="String">
        delete from patrol_point_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectPointsByRouteId" parameterType="String" resultMap="PatrolPointResult">
        <include refid="selectPatrolPointVo"/>
        where is_active = 1
        <!-- 这里需要关联路线表，暂时模拟 -->
        order by point_code
    </select>

    <select id="getTotalActivePoints" resultType="int">
        select count(*) from patrol_point_info where is_active = 1
    </select>

</mapper>
