<?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.taskinfo.mapper.PatrolTaskInfoMapper">

    <resultMap type="PatrolTaskInfo" id="PatrolTaskInfoResult">
        <result property="lineId" column="line_id"/>
        <result property="taskMajorId" column="task_major_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="deviceName" column="device_name"/>
        <result property="componentId" column="component_id"/>
        <result property="componentName" column="component_name"/>
        <result property="bayId" column="bay_id"/>
        <result property="bayName" column="bay_name"/>
        <result property="mainDeviceId" column="main_device_id"/>
        <result property="mainDeviceName" column="main_device_name"/>
        <result property="phyAssetId" column="phy_asset_id"/>
        <result property="pointStatus" column="point_status"/>
        <result property="devType" column="dev_type"/>
        <result property="presetCode" column="preset_code"/>
        <result property="parentId" column="parent_id"/>
        <result property="ebookId" column="ebook_id"/>
        <result property="ebookName" column="ebook_name"/>
        <result property="devNo" column="dev_no"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectPatrolTaskInfoVo">
        select line_id,
               preset_code,
               dev_no,
               create_time,
               task_major_id,
               dev_type,
               device_id,
               phy_asset_id,
               device_name,
               component_id,
               component_name,
               bay_id,
               bay_name,
               main_device_id,
               main_device_name,
               parent_id,
               ebook_id,
               ebook_name
        from patrol_task_info
    </sql>

    <select id="selectPatrolTaskInfoList" parameterType="PatrolTaskInfo" resultMap="PatrolTaskInfoResult">
        <include refid="selectPatrolTaskInfoVo"/>
        <where>
            <if test="taskMajorId != null  and taskMajorId != ''">and task_major_id = #{taskMajorId}</if>
            <if test="deviceId != null ">and device_id = #{deviceId}</if>
            <if test="deviceName != null  and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="componentId != null ">and component_id = #{componentId}</if>
            <if test="componentName != null  and componentName != ''">and component_name like concat('%',
                #{componentName}, '%')
            </if>
            <if test="bayId != null ">and bay_id = #{bayId}</if>
            <if test="bayName != null  and bayName != ''">and bay_name like concat('%', #{bayName}, '%')</if>
            <if test="mainDeviceId != null ">and main_device_id = #{mainDeviceId}</if>
            <if test="phyAssetId != null ">and phy_asset_id = #{phyAssetId}</if>
            <if test="devType != null ">and dev_type = #{devType}</if>
            <if test="presetCode != null ">and preset_code = #{presetCode}</if>
            <if test="mainDeviceName != null  and mainDeviceName != ''">and main_device_name like concat('%',
                #{mainDeviceName}, '%')
            </if>
            <if test="parentId != null  and parentId != ''">and parent_id = #{parentId}</if>
            <if test="ebookId != null  and ebookId != ''">and ebook_id = #{ebookId}</if>
            <if test="devNo != null  and devNo != ''">and dev_no = #{devNo}</if>
            <if test="ebookName != null  and ebookName != ''">and ebook_name like concat('%', #{ebookName}, '%')</if>
        </where>
    </select>
    <select id="selectPatrolTask" parameterType="PatrolTaskInfo" resultMap="PatrolTaskInfoResult">
        select a.*,b.device_name as devName,
        b.device_code as deviceCode,
        c.area_name as areName,
        c.area_id as areaId,
        d.device_type_name as devType,
        d.phase as phase

        from patrol_task_info a
        LEFT JOIN basedata_device b on a.main_device_id=b.device_id
        LEFT JOIN basedata_area c on b.area_id =c.area_id
        LEFT JOIN basedata_devicetype d on b.device_type_id=d.device_type_id
        <where>
            <if test="taskMajorId != null  and taskMajorId != ''">and task_major_id = #{taskMajorId}</if>
            <if test="deviceId != null ">and device_id = #{deviceId}</if>
            <if test="deviceName != null  and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="componentId != null ">and component_id = #{componentId}</if>
            <if test="componentName != null  and componentName != ''">and component_name like concat('%',
                #{componentName}, '%')
            </if>
            <if test="bayId != null ">and bay_id = #{bayId}</if>
            <if test="bayName != null  and bayName != ''">and bay_name like concat('%', #{bayName}, '%')</if>
            <if test="mainDeviceId != null ">and main_device_id = #{mainDeviceId}</if>
            <if test="phyAssetId != null ">and phy_asset_id = #{phyAssetId}</if>
            <if test="mainDeviceName != null  and mainDeviceName != ''">and main_device_name like concat('%',
                #{mainDeviceName}, '%')
            </if>
        </where>
    </select>
    <select id="selectPatrolTaskInfoListByMajorId" parameterType="String" resultType="String">
        select substring_index(patrol_point_code, '|', -1)
        from basedata_patrolpoint
        where patrol_point_id in (select DISTINCT(device_id) from patrol_task_info where task_major_id = #{taskId})
    </select>

    <select id="selectPatrolTaskInfoListByMajorIdEither" parameterType="String" resultType="String">
        select substring_index(patrol_point_code, '_', -1)
        from basedata_patrolpoint
        where patrol_point_id in (select DISTINCT(device_id) from patrol_task_info where task_major_id = #{taskId}) order by patrol_point_id
    </select>

    <select id="selectPatrolTaskInfoListByMajorIdFull" parameterType="String" resultType="String">
        select patrol_point_code
        from basedata_patrolpoint
        where patrol_point_id in (select DISTINCT(device_id) from patrol_task_info where task_major_id = #{taskId}) order by patrol_point_id
    </select>

    <select id="selectPatrolAbnormalTaskList" parameterType="PatrolTaskInfo" resultMap="PatrolTaskInfoResult">
        select a.*,b.point_status,c.task_name as taskName,c.type as taskType, e.area_name as areaName from
        patrol_task_info a
        INNER JOIN patrol_result d on d.device_id=a.device_id
        INNER JOIN result_analysis b on d.line_id=b.business_id
        INNER JOIN patrol_task c on c.task_id=a.task_major_id and c.task_code=d.task_code
        left JOIN basedata_area e on c.area_id=e.area_id
        where b.point_status in('0','2')
        <if test="pointStatus != null ">and b.point_status = #{pointStatus}</if>
        ORDER BY a.line_id desc
    </select>

    <select id="selectPatrolTaskInfoByLineId" parameterType="Long" resultMap="PatrolTaskInfoResult">
        <include refid="selectPatrolTaskInfoVo"/>
        where line_id = #{lineId}
    </select>

    <select id="selectTaskInfoByBaseId" parameterType="String" resultMap="PatrolTaskInfoResult">
        select a.patrol_point_name as device_name,
               a.device_id         as component_id,
               a.main_device_id    as main_device_id

        from basedata_patrolpoint a
        where a.patrol_point_id = #{lineId}
    </select>

    <select id="selectTaskInfoByBaseIds" resultMap="PatrolTaskInfoResult">
        select a.patrol_point_name as device_name,
               a.device_id         as component_id,
               a.main_device_id    as main_device_id,
               a.patrol_point_id   as device_id
        from basedata_patrolpoint a
        where a.patrol_point_id in
        <foreach item="pointId" collection="baseIds" open="(" separator="," close=")">
            #{pointId}
        </foreach>
    </select>

    <select id="selectAlgSubtypeIdsByPatrolPointId" parameterType="String" resultType="String">
        select a.alg_subtype_ids as algSubtypeIds
        from basedata_patrolpoint a
        where a.patrol_point_id = #{patrolPointId}
    </select>

    <select id="selectVideoPosByPointId" parameterType="String" resultType="java.util.Map">
        select a.*
        from basedata_eqpbook a
        where a.eqp_book_id = #{lineId}
    </select>
    <select id="selectPoseInfo" parameterType="String" resultType="java.util.Map">
        select a.*, b.preset_code
        from basedata_patrolpoint a
                 left JOIN basedata_patrolpoint_preset b on a.patrol_point_id = b.patrol_point_id
        where a.device_id = #{devId}
          and b.eqp_book_id = #{eqbookId}
    </select>

    <select id="selectPatrolTaskInfoCount" resultType="Map">
        select task_major_id as taskMajorId,count(1) as num from  patrol_task_info
        where task_major_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        group by taskMajorId
    </select>
    <select id="selectVideoPosByPointIdByEqpbookIds" resultType="java.util.Map">
        select a.*
        from basedata_eqpbook a
        where a.eqp_book_id in
        <foreach item="pointId" collection="list" open="(" separator="," close=")">
            #{pointId}
        </foreach>
    </select>

    <select id="countPatrolTaskInfoByMajorId" resultType="java.lang.Integer">
        select count(1) from patrol_task_info where task_major_id = #{taskMajorId}
    </select>

    <insert id="insertPatrolTaskInfo" parameterType="PatrolTaskInfo" useGeneratedKeys="true" keyProperty="lineId">
        insert into patrol_task_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskMajorId != null">task_major_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="componentId != null">component_id,</if>
            <if test="componentName != null">component_name,</if>
            <if test="bayId != null">bay_id,</if>
            <if test="bayName != null">bay_name,</if>
            <if test="mainDeviceId != null">main_device_id,</if>
            <if test="mainDeviceName != null">main_device_name,</if>
            <if test="phyAssetId != null">phy_asset_id,</if>
            <if test="devType != null">dev_type,</if>
            <if test="presetCode != null">preset_code,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="ebookId != null">ebook_id,</if>
            <if test="ebookName != null">ebook_name,</if>
            <if test="devNo != null">dev_no,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskMajorId != null">#{taskMajorId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="componentId != null">#{componentId},</if>
            <if test="componentName != null">#{componentName},</if>
            <if test="bayId != null">#{bayId},</if>
            <if test="bayName != null">#{bayName},</if>
            <if test="mainDeviceId != null">#{mainDeviceId},</if>
            <if test="mainDeviceName != null">#{mainDeviceName},</if>
            <if test="phyAssetId != null">#{phyAssetId},</if>
            <if test="devType != null">#{devType},</if>
            <if test="presetCode != null">#{presetCode},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="ebookId != null">#{ebookId},</if>
            <if test="ebookName != null">#{ebookName},</if>
            <if test="devNo != null">#{devNo},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>
    <insert id="batchInsertPatrolTaskInfo">
        INSERT INTO patrol_task_info (
        task_major_id, device_id, device_name, component_id, component_name,
        bay_id, bay_name, main_device_id, main_device_name, phy_asset_id,
        dev_type, preset_code, parent_id, ebook_id, ebook_name, dev_no, create_time
        ) VALUES
        <foreach collection="patrolTaskInfos" item="item" separator=",">
            (
            #{item.taskMajorId}, #{item.deviceId}, #{item.deviceName},
            #{item.componentId}, #{item.componentName}, #{item.bayId},
            #{item.bayName}, #{item.mainDeviceId}, #{item.mainDeviceName},
            #{item.phyAssetId}, #{item.devType}, #{item.presetCode},
            #{item.parentId}, #{item.ebookId}, #{item.ebookName},
            #{item.devNo}, #{item.createTime}
            )
        </foreach>
    </insert>

    <update id="updatePatrolTaskInfo" parameterType="PatrolTaskInfo">
        update patrol_task_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskMajorId != null">task_major_id = #{taskMajorId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="componentId != null">component_id = #{componentId},</if>
            <if test="componentName != null">component_name = #{componentName},</if>
            <if test="bayId != null">bay_id = #{bayId},</if>
            <if test="bayName != null">bay_name = #{bayName},</if>
            <if test="mainDeviceId != null">main_device_id = #{mainDeviceId},</if>
            <if test="mainDeviceName != null">main_device_name = #{mainDeviceName},</if>
            <if test="phyAssetId != null">phy_asset_id = #{phyAssetId},</if>
            <if test="devType != null">dev_type = #{devType},</if>
            <if test="presetCode != null">presetCode = #{presetCode},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ebookId != null">ebook_id = #{ebookId},</if>
            <if test="ebookName != null">ebook_name = #{ebookName},</if>
            <if test="devNo != null">dev_no = #{devNo},</if>
        </trim>
        where line_id = #{lineId}
    </update>

    <delete id="deletePatrolTaskInfoByLineId" parameterType="Long">
        delete
        from patrol_task_info
        where line_id = #{lineId}
    </delete>

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

    <delete id="deletePatrolTaskInfoByMajorIds" parameterType="String">
        delete from patrol_task_info where task_major_id in
        <foreach item="majorId" collection="array" open="(" separator="," close=")">
            #{majorId}
        </foreach>
    </delete>
    <delete id="deletePatrolTaskInfoByMajorId">
        delete from patrol_task_info where task_major_id = #{taskMajorId}
    </delete>
</mapper>
