<?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.inspect.task.mapper.PatrolTaskMapper">
    <resultMap type="PatrolTask" id="PatrolTaskResult">
        <result property="taskId" column="task_id"/>
        <result property="stationName" column="station_name"/>
        <result property="stationCode" column="station_code"/>
        <result property="type" column="type"/>
        <result property="taskCode" column="task_code"/>
        <result property="taskName" column="task_name"/>
        <result property="priority" column="priority"/>
        <result property="deviceLevel" column="device_level"/>
        <result property="deviceList" column="device_list"/>
        <result property="fixedStartTime" column="fixed_start_time"/>
        <result property="cycleMonth" column="cycle_month"/>
        <result property="cycleWeek" column="cycle_week"/>
        <result property="cycleExecuteTime" column="cycle_execute_time"/>
        <result property="cycleStartTime" column="cycle_start_time"/>
        <result property="cycleEndTime" column="cycle_end_time"/>
        <result property="intervalNumber" column="interval_number"/>
        <result property="intervalType" column="interval_type"/>
        <result property="intervalExecuteTime" column="interval_execute_time"/>
        <result property="intervalStartTime" column="interval_start_time"/>
        <result property="intervalEndTime" column="interval_end_time"/>
        <result property="invalidStartTime" column="invalid_start_time"/>
        <result property="invalidEndTime" column="invalid_end_time"/>
        <result property="isEnable" column="is_enable"/>
        <result property="createTime" column="create_time"/>
        <result property="drafter" column="drafter"/>
        <result property="fileStatus" column="file_status"/>
        <result property="executionStatus" column="execution_status"/>
        <result property="taskCycle" column="task_cycle"/>
        <result property="devNo" column="dev_no"/>
        <result property="devType" column="dev_type"/>
        <result property="storage" column="storage"/>
        <result property="areaId" column="area_id"/>
        <result property="areaName" column="area_name"/>
        <result property="executionMode" column="execution_mode"/>
        <result property="linkageState" column="linkage_state"/>
        <result property="isUp" column="is_up"/>
        <result property="areaIdS" column="area_id_s"/>
        <result property="pointType" column="point_type"/>
        <result property="deviceType" column="device_type"/>
        <result property="specifiedAlg" column="specified_alg"/>
    </resultMap>

    <sql id="selectPatrolTaskVo">
        select task_id,
               dev_no,
               is_up,
               linkage_state,
               execution_mode,
               storage,
               area_name,
               area_id,
               dev_type,
               task_cycle,
               station_name,
               file_status,
               execution_status,
               station_code,
               type,
               task_code,
               task_name,
               priority,
               device_level,
               device_list,
               fixed_start_time,
               cycle_month,
               cycle_week,
               cycle_execute_time,
               cycle_start_time,
               cycle_end_time,
               interval_number,
               interval_type,
               interval_execute_time,
               interval_start_time,
               interval_end_time,
               invalid_start_time,
               invalid_end_time,
               is_enable,
               create_time,
               drafter,
               area_id_s,
               point_type,
               device_type,
               specified_alg
        from patrol_task
    </sql>

    <select id="selectPatrolTaskList" parameterType="PatrolTask" resultMap="PatrolTaskResult">
        <include refid="selectPatrolTaskVo"/>
        <where>
            <if test="stationName != null  and stationName != ''">and station_name like concat('%', #{stationName},
                '%')
            </if>
            <if test="stationCode != null  and stationCode != ''">and station_code = #{stationCode}</if>
            <if test="taskId != null  ">and task_id = #{taskId}</if>
            <if test="type != null  and type != ''">and type = #{type}</if>
            <if test="taskCode != null  and taskCode != ''">and task_code = #{taskCode}</if>
            <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
            <if test="priority != null  and priority != ''">and priority = #{priority}</if>
            <if test="deviceLevel != null  and deviceLevel != ''">and device_level = #{deviceLevel}</if>
            <if test="deviceList != null  and deviceList != ''">and device_list like concat ('%',#{deviceList},'%')</if>
            <if test="fixedStartTime != null ">and fixed_start_time = #{fixedStartTime}</if>
            <if test="cycleMonth != null  and cycleMonth != ''">and cycle_month = #{cycleMonth}</if>
            <if test="cycleWeek != null  and cycleWeek != ''">and cycle_week = #{cycleWeek}</if>
            <if test="cycleExecuteTime != null ">and cycle_execute_time = #{cycleExecuteTime}</if>
            <if test="cycleStartTime != null ">
                and date_format(cycle_start_time,'%y%m%d') &gt;= date_format(#{cycleStartTime},'%y%m%d')
            </if>
            <if test="cycleEndTime != null ">
                and date_format(cycle_end_time,'%y%m%d') &lt;= date_format(#{cycleEndTime},'%y%m%d')
            </if>
            <if test="intervalNumber != null  and intervalNumber != ''">and interval_number = #{intervalNumber}</if>
            <if test="intervalType != null  and intervalType != ''">and interval_type = #{intervalType}</if>
            <if test="intervalExecuteTime != null ">and interval_execute_time = #{intervalExecuteTime}</if>
            <if test="intervalStartTime != null ">and interval_start_time = #{intervalStartTime}</if>
            <if test="intervalEndTime != null ">and interval_end_time = #{intervalEndTime}</if>
            <if test="invalidStartTime != null ">and invalid_start_time = #{invalidStartTime}</if>
            <if test="invalidEndTime != null ">and invalid_end_time = #{invalidEndTime}</if>
            <if test="isEnable != null  and isEnable != ''">and is_enable = #{isEnable}</if>
            <if test="drafter != null  and drafter != ''">and drafter = #{drafter}</if>
            <if test="fileStatus != null  and fileStatus != ''">and file_status = #{fileStatus}</if>
            <if test="executionStatus != null  and executionStatus != ''">and execution_status = #{executionStatus}</if>
            <if test="taskCycle != null  and taskCycle != ''">and task_cycle = #{taskCycle}</if>
            <if test="devNo != null  and devNo != ''">and dev_no = #{devNo}</if>
            <if test="devType != null  and devType != ''">and find_in_set(#{devType}, dev_type)</if>
            <if test="storage != null  and storage != ''">and storage = #{storage}</if>
            <if test="areaId != null  and areaId != ''">and area_id = #{areaId}</if>
            <if test="executionMode != null  and executionMode != ''">and execution_mode = #{executionMode}</if>
            <if test="linkageState != null  and linkageState != ''">and linkage_state = #{linkageState}</if>
            <if test="isUp != null  and isUp != ''">and is_up = #{isUp}</if>
            <if test="areaIdS != null  and areaIdS != ''">and area_id_s = #{areaIdS}</if>
            <if test="beginTime != null and beginTime != ''">
                and date_format(create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and date_format(create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            <if test="areaName != null and areaName != ''">
                and CONCAT(',', area_name, ',') LIKE CONCAT('%', #{areaName}, '%')
            </if>
        </where>
        order by task_id desc
    </select>

    <select id="selectPatrolTaskByTaskId" parameterType="Long" resultMap="PatrolTaskResult">
        <include refid="selectPatrolTaskVo"/>
        where task_id = #{taskId}
    </select>
    <select id="selectBasedataBdConfigDeviceConvert" parameterType="DeviceConvert" resultType="java.util.Map">
        select * from basedata_bdconfig_device_convert
        <where>
            <if test="deviceId!=null and deviceId!=''">and device_id=#{deviceId}</if>
            <if test="deviceCode!=null and deviceCode!=''">and device_code=#{deviceCode}</if>
            <if test="originalDevicecode!=null and originalDevicecode!=''">and
                original_device_code=#{originalDevicecode}
            </if>

        </where>
    </select>
    <select id="selectBaseDevice" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_device a
                 inner join basedata_area b on a.area_id = b.area_id
        where b.area_code = #{AreaCode}
    </select>
    <select id="selectBaseDeviceByCode" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_device a
        where a.device_code = #{devcode}
    </select>
    <select id="selectBasePoint" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_patrolpoint a
        where a.main_device_id = #{devId}
    </select>
    <select id="selectBasePointByBj" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_patrolpoint a
        where a.device_id = #{devId}
    </select>
    <select id="selectBasePointByPointId" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_patrolpoint a
        where a.patrol_point_id = #{pointId}
    </select>
    <select id="selectBasePointPre" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_patrolpoint_preset a
        where a.patrol_point_id = #{pointId}
    </select>
    <select id="selectBaseEqBook" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_eqpbook a
        where a.eqp_book_id = #{eqId}
    </select>

    <select id="selectConfigByKey" parameterType="String" resultType="String">
        select config_value
        from sys_config
        where config_key = #{taskId}
    </select>

    <insert id="insertPatrolTask" parameterType="PatrolTask" useGeneratedKeys="true" keyProperty="taskId">
        insert into patrol_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationName != null">station_name,</if>
            <if test="stationCode != null">station_code,</if>
            <if test="type != null">type,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="taskName != null">task_name,</if>
            <if test="priority != null">priority,</if>
            <if test="deviceLevel != null">device_level,</if>
            <if test="deviceList != null">device_list,</if>
            <if test="fixedStartTime != null">fixed_start_time,</if>
            <if test="cycleMonth != null">cycle_month,</if>
            <if test="cycleWeek != null">cycle_week,</if>
            <if test="cycleExecuteTime != null">cycle_execute_time,</if>
            <if test="cycleStartTime != null">cycle_start_time,</if>
            <if test="cycleEndTime != null">cycle_end_time,</if>
            <if test="intervalNumber != null">interval_number,</if>
            <if test="intervalType != null">interval_type,</if>
            <if test="intervalExecuteTime != null">interval_execute_time,</if>
            <if test="intervalStartTime != null">interval_start_time,</if>
            <if test="intervalEndTime != null">interval_end_time,</if>
            <if test="invalidStartTime != null">invalid_start_time,</if>
            <if test="invalidEndTime != null">invalid_end_time,</if>
            <if test="isEnable != null">is_enable,</if>
            <if test="createTime != null">create_time,</if>
            <if test="drafter != null">drafter,</if>
            <if test="fileStatus != null">file_status,</if>
            <if test="executionStatus != null">execution_status,</if>
            <if test="taskCycle != null">task_cycle,</if>
            <if test="devNo != null">dev_no,</if>
            <if test="devType != null">dev_type,</if>
            <if test="storage != null">storage,</if>
            <if test="areaId != null">area_id,</if>
            <if test="areaName != null">area_name,</if>
            <if test="executionMode != null">execution_mode,</if>
            <if test="linkageState != null">linkage_state,</if>
            <if test="isUp != null">is_up,</if>
            <if test="areaIdS != null">area_id_s,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="pointType != null">point_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationName != null">#{stationName},</if>
            <if test="stationCode != null">#{stationCode},</if>
            <if test="type != null">#{type},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="taskName != null">#{taskName},</if>
            <if test="priority != null">#{priority},</if>
            <if test="deviceLevel != null">#{deviceLevel},</if>
            <if test="deviceList != null">#{deviceList},</if>
            <if test="fixedStartTime != null">#{fixedStartTime},</if>
            <if test="cycleMonth != null">#{cycleMonth},</if>
            <if test="cycleWeek != null">#{cycleWeek},</if>
            <if test="cycleExecuteTime != null">#{cycleExecuteTime},</if>
            <if test="cycleStartTime != null">#{cycleStartTime},</if>
            <if test="cycleEndTime != null">#{cycleEndTime},</if>
            <if test="intervalNumber != null">#{intervalNumber},</if>
            <if test="intervalType != null">#{intervalType},</if>
            <if test="intervalExecuteTime != null">#{intervalExecuteTime},</if>
            <if test="intervalStartTime != null">#{intervalStartTime},</if>
            <if test="intervalEndTime != null">#{intervalEndTime},</if>
            <if test="invalidStartTime != null">#{invalidStartTime},</if>
            <if test="invalidEndTime != null">#{invalidEndTime},</if>
            <if test="isEnable != null">#{isEnable},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="drafter != null">#{drafter},</if>
            <if test="fileStatus != null">#{fileStatus},</if>
            <if test="executionStatus != null">#{executionStatus},</if>
            <if test="taskCycle != null">#{taskCycle},</if>
            <if test="devNo != null">#{devNo},</if>
            <if test="devType != null">#{devType},</if>
            <if test="storage != null">#{storage},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="areaName != null">#{areaName},</if>
            <if test="executionMode != null">#{executionMode},</if>
            <if test="linkageState != null">#{linkageState},</if>
            <if test="isUp != null">#{isUp},</if>
            <if test="areaIdS != null">#{areaIdS},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="pointType != null">#{pointType},</if>
        </trim>
    </insert>

    <update id="updatePatrolTask" parameterType="PatrolTask">
        update patrol_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="stationCode != null">station_code = #{stationCode},</if>
            <if test="type != null">type = #{type},</if>
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="priority != null">priority = #{priority},</if>
            <if test="deviceLevel != null">device_level = #{deviceLevel},</if>
            <if test="deviceList != null">device_list = #{deviceList},</if>
            <if test="fixedStartTime != null">fixed_start_time = #{fixedStartTime},</if>
            <if test="cycleMonth != null">cycle_month = #{cycleMonth},</if>
            <if test="cycleWeek != null">cycle_week = #{cycleWeek},</if>
            <if test="cycleExecuteTime != null">cycle_execute_time = #{cycleExecuteTime},</if>
            <if test="cycleStartTime != null">cycle_start_time = #{cycleStartTime},</if>
            <if test="cycleEndTime != null">cycle_end_time = #{cycleEndTime},</if>
            <if test="intervalNumber != null">interval_number = #{intervalNumber},</if>
            <if test="intervalType != null">interval_type = #{intervalType},</if>
            <if test="intervalExecuteTime != null">interval_execute_time = #{intervalExecuteTime},</if>
            <if test="intervalStartTime != null">interval_start_time = #{intervalStartTime},</if>
            <if test="intervalEndTime != null">interval_end_time = #{intervalEndTime},</if>
            <if test="invalidStartTime != null">invalid_start_time = #{invalidStartTime},</if>
            <if test="invalidEndTime != null">invalid_end_time = #{invalidEndTime},</if>
            <if test="isEnable != null">is_enable = #{isEnable},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="drafter != null">drafter = #{drafter},</if>
            <if test="fileStatus != null">file_status = #{fileStatus},</if>
            <if test="executionStatus != null">execution_status = #{executionStatus},</if>
            <if test="taskCycle != null">task_cycle = #{taskCycle},</if>
            <if test="devNo != null">dev_no = #{devNo},</if>
            <if test="devType != null">dev_type = #{devType},</if>
            <if test="storage != null">storage = #{storage},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="areaName != null">area_name = #{areaName},</if>
            <if test="executionMode != null">execution_mode = #{executionMode},</if>
            <if test="linkageState != null">linkage_state = #{linkageState},</if>
            <if test="isUp != null">is_up = #{isUp},</if>
            <if test="areaIdS != null">area_id_s = #{areaIdS},</if>
            <if test="pointType != null">point_type = #{pointType},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
        </trim>
        where task_id = #{taskId}
    </update>

    <update id="updatePatrolTaskTime" parameterType="String">
        update patrol_task
        <trim prefix="SET" suffixOverrides=",">
            fixed_start_time = null
        </trim>
        where task_id = #{taskId}
    </update>

    <delete id="deletePatrolTaskByTaskId" parameterType="Long">
        delete
        from patrol_task
        where task_id = #{taskId}
    </delete>

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

    <select id="selectTaskRationalGroupByType" resultType="java.util.Map">
        select p.type, COUNT(1) num
        from patrol_task p
        group by p.type;
    </select>

    <select id="selectChannelCodeByPatrolPointId" resultType="java.util.Map" parameterType="java.lang.String">
        select d.* from patrol_preset_pos d where d.patrol_point_id = #{patrolPointId} limit 1
    </select>

    <select id="selectPatrolTaskListByTaskAreaName" resultMap="PatrolTaskResult">
        select * from patrol_task where task_name like concat(#{taskName}, '%');
    </select>

    <select id="selectPatrolTaskStatus" resultType="com.alibaba.fastjson.JSONObject">
        select
        s.task_code as taskCode,
        s.task_state as taskState,
        s.task_patrolled_id as taskPatrolledId,
        ptrm.file_status as fileStatus,
        ptrm.line_id as lineId
        from
        patrol_task_status s left join patrol_task_result_main ptrm on s.task_code = ptrm.task_code and
        s.task_patrolled_id = ptrm.task_patrolled_id
        where
        s.task_state = '1' and ptrm.file_status = '0' and date_format(s.create_time,'%Y%m%d') =
        date_format(current_date,'%Y%m%d')
        <if test="taskCodes != null and taskCodes.size() > 0">
            and s.task_code in
            <foreach item="taskCode" collection="taskCodes" open="(" separator="," close=")">
                #{taskCode}
            </foreach>
        </if>
    </select>

    <select id="selectStationNo" resultType="java.lang.String">
        select station_no from basedata_station limit 1
    </select>

    <select id="selectBasedataPatrolPointList" resultType="com.alibaba.fastjson.JSONObject">
        select patrol_point_id          as patrolPointId,
               patrol_point_code        as patrolPointCode,
               patrol_point_name        as patrolPointName,
               device_type_id           as deviceTypeId,
               main_device_id           as mainDeviceId,
               relation_main_device_id  as relationMainDeviceId,
               device_id                as deviceId,
               relation_device_id       as relationDeviceId,
               area_id                  as areaId,
               relation_area_id         as relationAreaId,
               patrol_content           as patrolContent,
               purpose                  as purpose,
               create_by                as createBy,
               create_time              as createTime,
               update_by                as updateBy,
               update_time              as updateTime,
               delete_flag              as deleteFlag,
               std_parol_point_id       as stdPatrolPointId,
               point_impt_level         as pointImptLevel,
               patrol_type_id           as patrolTypeId,
               lower_value              as lowerValue,
               upper_value              as upperValue,
               position_x               as positionX,
               position_y               as positionY,
               position_z               as positionZ,
               alg_subtype_ids          as algSubtypeIds,
               original_alg_subtype_ids as originalAlgSubtypeIds,
               original_device_id       as originalDeviceId,
               dis_ref_picture_path     as disRefPicturePath,
               original_main_device_id  as originalMainDeviceId,
               alarm_threshold          as alarmThreshold,
               meter_type               as meterType
        from basedata_patrolpoint where patrol_point_id in
        <foreach item="patrolPointId" collection="patrolPointIds" open="(" separator="," close=")">
            #{patrolPointId}
        </foreach>

    </select>
    <select id="selectBasedataPatrolPointPresetList" resultType="com.alibaba.fastjson.JSONObject">
        select patrol_point_preset_id as patrolPointPresetId,
               patrol_point_id        as patrolPointId,
               eqp_book_id            as eqpBookId,
               channel_id             as channelId,
               preset_code            as presetCode,
               preset_name            as presetName,
               create_by              as createBy,
               create_time            as createTime,
               update_by              as updateBy,
               update_time            as updateTime,
               delete_flag            as deleteFlag,
               patrol_device_code     as patrolDeviceCode,
               channel_code           as channelCode,
               video_nvr_code         as videoNvrCode
        from basedata_patrolpoint_preset where patrol_point_id in
        <foreach item="patrolPointId" collection="patrolPointIds" open="(" separator="," close=")">
            #{patrolPointId}
        </foreach>
    </select>
    <select id="selectPatrolPresetPosList" resultType="com.alibaba.fastjson.JSONObject">
        select preset_pos_id      as presetPosId,
               preset_pos_code    as presetCode,
               preset_pos_name    as presetPosName,
               eqp_book_id        as eqpBookId,
               patrol_device_code as patrolDeviceCode,
               patrol_point_id    as patrolPointId,
               channel_id         as channelId,
               channel_code       as channelCode,
               channel_type       as channelType,
               coordinate_type    as coordinateType,
               stop_time          as stopTime,
               channel_img        as channelImg,
               description        as description,
               alg_subtype_code   as algSubtypeCode,
               is_enable          as isEnable,
               create_time        as createTime,
               video_nvr_code     as videoNvrCode
        from patrol_preset_pos where patrol_point_id in
        <foreach item="patrolPointId" collection="patrolPointIds" open="(" separator="," close=")">
            #{patrolPointId}
        </foreach>
    </select>
</mapper>
