<?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.yfd.platform.modules.patroltask.mapper.TaskTodoMapper">
    <select id="getTaskImplementation" resultType="java.util.Map">
        SELECT tt.*,s.is_station_flag FROM `iis_task_todo` tt INNER JOIN `iis_task` t ON tt.task_id=t.task_id INNER JOIN
        `iis_substation`
        s ON tt.station_code=s.station_code
        WHERE 1 = 1
        and t.datastatus = '2'
        and t.station_code = #{stationCode}
        and t.task_type != '4'
        <if test="taskState != null and taskState != ''">
            and tt.task_state = #{taskState}
        </if>
        <if test="taskName != null and taskName != ''">
            and tt.task_name like concat('%',#{taskName},'%')
        </if>
        <if test="startDate != null and startDate != ''">
            and str_to_date(tt.plan_start_time, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and str_to_date(tt.plan_start_time, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}
        </if>
        ORDER BY tt.plan_start_time ASC,tt.priority DESC,tt.task_todo_id ASC
    </select>
    <select id="getTaskToDoStatList" resultType="java.util.Map">
        SELECT tt.* FROM `iis_task_todo` tt INNER JOIN `iis_task` t ON tt.task_id=t.task_id
        WHERE 1 = 1
        and t.datastatus = '2'
        and t.task_type != '4'
        and t.station_code = #{stationCode}
        <if test="taskName != null and taskName != ''">
            and tt.task_name like concat('%',#{taskName},'%')
        </if>
        <if test="startDate != null and startDate != ''">
            and str_to_date(tt.plan_start_time, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and str_to_date(tt.plan_start_time, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}
        </if>
        ORDER BY tt.plan_start_time ASC,tt.priority DESC,tt.task_todo_id ASC
    </select>

    <select id="getTaskReportList" resultType="java.util.Map">
        SELECT DISTINCT tt.* FROM `iis_task_todo` tt inner join `iis_task_result` tr on tt.task_todo_id=tr.task_todo_id
        inner join `iis_substation_patroldevice` sp on sp.internationalId=tr.patroldevice_code WHERE tt.examine_flag =
        '1'
        and tt.datastatus = '1'
        and tt.station_code = #{stationCode}
        <if test="bayId != null and bayId != ''">
            and tr.bay_id = #{bayId}
        </if>
        <if test="areaId != null and areaId != ''">
            and tr.area_id = #{areaId}
        </if>
        <if test="taskName != null and taskName != ''">
            and tt.task_name like concat('%',#{taskName},'%')
        </if>
        <if test="type != null and type != ''">
            and tt.type = #{type}
        </if>
        <if test="patrolType != null and patrolType != ''">
            and sp.type = #{patrolType}
        </if>
        <if test="startDate != null and startDate != ''">
            and str_to_date(tt.examine_date, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and str_to_date(tt.examine_date, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}
        </if>
        ORDER BY tt.examine_date,tt.task_todo_id
    </select>
    <select id="getDeviceByMainDevice" resultType="java.util.Map">
        SELECT DISTINCT
            sd.device_id,
            CONCAT(sd.device_name,'—',sd.main_device_name) device_name
        FROM
            iis_substation_device sd
            INNER JOIN iis_task_result ts ON sd.device_id = ts.device_id
        WHERE
            sd.datastatus='1'
            AND sd.main_device_id IN
        <foreach collection="mainDeviceId.split(',')" item="id"  open="(" close=")" separator=",">
            #{id}
        </foreach>
        <if test="meterType != null and meterType != ''">
            AND sd.meter_type IN
            <foreach collection="meterType.split(',')" item="type"  open="(" close=")" separator=",">
                #{type}
            </foreach>
        </if>
        <if test="recognitionType != null and recognitionType != ''">
            AND sd.recognition_type_list IN
            <foreach collection="recognitionType.split(',')" item="retype" open="(" close=")" separator=",">
                #{retype}
            </foreach>
        </if>
        <if test="startDate != null and startDate != ''">
            and str_to_date(ts.time, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and str_to_date(ts.time, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}
        </if>
    </select>
    <select id="getLinkTaskTodoList" resultType="java.util.Map">
        SELECT
        *
        FROM
        `iis_task_todo` tt
        INNER JOIN `iis_linkage_signal` ls ON tt.control_num = ls.control_num
        WHERE
        tt.station_code=#{stationCode}
        <if test="linkageDeviceName != null and linkageDeviceName != ''">
            AND ls.linkage_device_name LIKE CONCAT("%",#{linkageDeviceName},"%")
        </if>
        <if test="taskName != null and taskName != ''">
            AND tt.task_name LIKE CONCAT("%",#{taskName},"%")
        </if>
        <if test="linkageType != null and linkageType != ''">
            AND ls.linkage_type IN
            <foreach collection="linkageType.split(',')" item="type" open="(" close=")" separator=",">
                #{type}
            </foreach>
        </if>
        <if test="linkageDeviceType != null and linkageDeviceType != ''">
            AND ls.linkage_device_type=#{linkageDeviceType}
        </if>
        <if test="startDate != null and startDate != ''">
            and str_to_date(tt.start_time, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and str_to_date(tt.start_time, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}
        </if>
    </select>
    <select id="selectTaskTodoList" resultType="com.yfd.platform.modules.patroltask.domain.TaskTodo">
        SELECT
            tt.*
        FROM
            iis_task_todo tt
            INNER JOIN iis_substation s ON tt.station_code = s.station_code
        WHERE
            tt.datastatus = '1'
            AND tt.task_state IN ( '0', '3' )
            AND tt.plan_start_time > NOW()
            AND s.is_station_flag != '1'
        ORDER BY
            tt.plan_start_time ASC,
            tt.priority DESC
    </select>
    <select id="getLinkTaskRecordLast" resultType="java.util.Map">
        SELECT
        tt.task_todo_id,
        tt.task_name,
        tt.start_time,
        tt.task_state
        FROM
        iis_task_todo tt
        INNER JOIN iis_linkage_signal ls ON tt.control_num = ls.control_num
        where 1=1
        <if test="stationCode != null and stationCode != ''">
            and tt.station_code=#{stationCode}
        </if>
        <if test="linkageType != null and linkageType != ''">
            and ls.linkage_type=#{linkageType}
        </if>
        <if test="taskName != null and taskName != ''">
            and tt.task_name LIKE CONCAT("%",#{taskName},"%")
        </if>
        ORDER BY
        tt.start_time DESC
        LIMIT 0,10
    </select>
    <select id="getLinkTaskTodo" resultType="java.util.Map">
        SELECT
        tt.task_todo_id,
        tt.task_name,
        tt.start_time,
        tt.task_type,
        tt.end_time,
        ls.id,
        ls.device_id_list,
        ls.linkage_device_name,
        ls.linkage_device_type,
        ls.linkage_type,
        ls.station_num,
        ls.control_num
        FROM
        iis_task_todo tt
        INNER JOIN iis_linkage_signal ls ON tt.control_num = ls.control_num
        where 1=1
        <if test="stationCode != null and stationCode != ''">
            and tt.station_code=#{stationCode}
        </if>
        <if test="todoId != null and todoId != ''">
            and tt.task_todo_id=#{todoId}
        </if>
        <if test="linkageType != null and linkageType != ''">
            and ls.linkage_type=#{linkageType}
        </if>
        ORDER BY
        tt.start_time DESC
    </select>
    <select id="getTaskDurationStat" resultType="java.util.Map">
        SELECT
        t3.stattime,
        t3.num,
        t3.duration,
        t4.count,
        t4.type
        FROM
        ((
        SELECT
        count(*) num,
        SUM(
        TIMESTAMPDIFF( SECOND, start_time, end_time )) duration,
        <if test="type ==1">
            DATE_FORMAT( start_time, '%Y-%m-%d' ) stattime
        </if>
        <if test="type ==2">
            CONCAT( DATE_FORMAT( start_time, '%Y-%m' ), '第', FLOOR(( DAY ( start_time ) - 1 ) / 7 ) + 1, '周' ) stattime
        </if>
        <if test="type ==3">
            DATE_FORMAT( start_time, '%Y-%m月' ) stattime
        </if>

        FROM
        `iis_task_todo`
        WHERE
        task_state = '1'
        <if test="startFormat != null and startFormat != ''">
            AND start_time &gt;= #{startFormat}
        </if>
        <if test="endFormat != null and endFormat != ''">
            AND end_time &lt;= #{endFormat}
        </if>
        GROUP BY
        stattime ORDER BY stattime
        ) t3
        LEFT JOIN (
        SELECT
        SUM( count ) count,
        count(*) type,
        stattime
        FROM
        (
        SELECT
        count(*) count,
        <if test="type ==1">
            DATE_FORMAT( tt.start_time, '%Y-%m-%d' ) stattime,
        </if>
        <if test="type ==2">
            CONCAT( DATE_FORMAT( tt.start_time, '%Y-%m' ), '第', FLOOR(( DAY ( start_time ) - 1 ) / 7 ) + 1, '周' ) stattime,
        </if>
        <if test="type ==3">
            DATE_FORMAT( tt.start_time, '%Y-%m月' ) stattime,
        </if>
        tr.recognition_type
        FROM
        iis_task_result tr
        INNER JOIN iis_task_todo tt ON tr.task_todo_id = tt.task_todo_id
        WHERE
        tr.valid = '2'
        AND tt.task_state = '1'
        <if test="startFormat != null and startFormat != ''">
            AND tt.start_time &gt;= #{startFormat}
        </if>
        <if test="endFormat != null and endFormat != ''">
            AND tt.end_time &lt;= #{endFormat}
        </if>
        GROUP BY
        tr.recognition_type,
        stattime
        ) t1
        GROUP BY
        t1.stattime
        ) t4 ON t3.stattime = t4.stattime)
    </select>
</mapper>
