package com.yfd.platform.modules.patroltask.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.platform.modules.patroltask.domain.TaskTodo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 巡视任务执行表 Mapper 接口
 * </p>
 *
 *
 * @since 2023-04-08
 */
public interface TaskTodoMapper extends BaseMapper<TaskTodo> {

    Page<Map<String, Object>> getTaskImplementation(Page<Map<String, Object>> page, String stationCode,
                                                    String currentDate, String startDate, String endDate,String taskState,String taskName);

    @Select("select a.station_code,a.result_id,a.flag,a.task_todo_id,a.bay_name,a.main_device_id,a.main_device_name,a" +
            ".device_id,a.device_name,a.datastatus,a.file_type,a.patroldevice_code,a.patroldevice_code rtsp_device_code,a.patroldevice_channelcode,a.patroldevice_channelcode rtsp_channel_code,a.patroldevice_pos,a.patroldevice_pos rtsp_channel_pos,a.patroldevice_baseimage,a" +
            ".patroldevice_effectiveregion,a.custom1," +
            "b.patroldevice_json,b.recognition_type_name_list,b.identify_material_id,b.outside_angle,b.meter_type,b" +
            ".outside_feature,b.picture_analysis_type_list,b.effective_area "
            + " from iis_task_result a join iis_substation_device b on (a.device_id=b.device_id)"
            + " where a.task_todo_id=#{taskTodoId}"
            + " order by a.order_num"
    )
    List<Map<String, Object>> queryTaskResult(String taskTodoId);

    List<Map<String, Object>> getTaskToDoStatList(String stationCode, String currentDate, String startDate,
                                                  String endDate,String taskName);

    Page<Map<String, Object>> getTaskReportList(Page<Map<String, Object>> page, String stationCode, String areaId,
                                                String taskName, String type, String startDate, String endDate,
                                                String bayId, String patrolType);

    /**********************************
     * 用途说明: 查询主设备所关联的点位
     * 参数说明 ids
     * 参数说明 meterType
     * 参数说明 recognitionType
     * 参数说明 startDate
     * 参数说明 endDate
     * 返回值说明: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     ***********************************/
    List<Map<String, Object>> getDeviceByMainDevice(String mainDeviceId, String meterType, String recognitionType,
                                                    String startDate, String endDate);

    Page<Map<String, Object>> getLinkTaskTodoList(Page<Map<String, Object>> page, String stationCode,
                                                  String linkageDeviceName, String taskName, String linkageType,
                                                  String linkageDeviceType, String startDate, String endDate);

    @Insert("<script> " +
            "insert into iis_task_todo " +
            "(task_todo_id, type, task_id, task_name, task_code, task_type, priority," +
            "task_state, plan_start_time, start_time, task_progress, task_estimated_time, end_time, " +
            "description, device_now, device_sumnum, " +
            "device_unusualnum, device_defore_num, examine_flag, cexamine_user_code, cexamine_user_name, " +
            "examine_date, examine_comment, datastatus, lastmodifier, lastmodifydate, " +
            "custom1, custom2, custom3, task_todo_way, station_code, control_num)"+
            "values " +
            "<foreach collection='taskList' index='index' item='item' separator=','> " +
            "(#{item.taskTodoId}, #{item.type}, #{item.taskId}, #{item.taskName}, #{item.taskCode}, " +
            "#{item.taskType}, #{item.priority}, #{item.taskState},#{item.planStartTime}," +
            "#{item.startTime}, #{item.taskProgress}, #{item.taskEstimatedTime}, #{item.endTime}, #{item.description}, #{item.deviceNow}, " +
            "#{item.deviceSumnum}, #{item.deviceDeforeNum}, #{item.deviceUnusualnum}, #{item.examineFlag}, " +
            "#{item.cexamineUserCode}, #{item.cexamineUserName}, #{item.examineDate}, #{item.examineComment}, " +
            "#{item.datastatus}, " +
            "#{item.lastmodifier}, #{item.lastmodifydate}, #{item.custom1},#{item.custom2}, " +
            "#{item.custom3},#{item.taskTodoWay}, #{item.stationCode}, #{item.controlNum})" +
            "</foreach> " +
            "ON DUPLICATE KEY UPDATE " +
            "type=VALUES(type), " +
            "task_id=VALUES(task_id), " +
            "task_name=VALUES(task_name), " +
            "task_code=VALUES(task_code), " +
            "task_type=VALUES(task_type), " +
            "priority=VALUES(priority), " +
            "task_state=VALUES(task_state), " +
            "plan_start_time=VALUES(plan_start_time), " +
            "start_time=VALUES(start_time), " +
            "task_progress=VALUES(task_progress), " +
            "task_estimated_time=VALUES(task_estimated_time), " +
            "end_time=VALUES(end_time), " +
            "description=VALUES(description), " +
            "device_now=VALUES(device_now), " +
            "device_sumnum=VALUES(device_sumnum), " +
            "device_unusualnum=VALUES(device_unusualnum), " +
            "device_defore_num=VALUES(device_defore_num), " +
            "examine_flag=VALUES(examine_flag), " +
            "cexamine_user_code=VALUES(cexamine_user_code), " +
            "cexamine_user_name=VALUES(cexamine_user_name), " +
            "examine_date=VALUES(examine_date), " +
            "examine_comment=VALUES(examine_comment), " +
            "datastatus=VALUES(datastatus), " +
            "lastmodifier=VALUES(lastmodifier), " +
            "lastmodifydate=VALUES(lastmodifydate), " +
            "custom1=VALUES(custom1), " +
            "custom2=VALUES(custom2), " +
            "custom3=VALUES(custom3), " +
            "task_todo_way=VALUES(task_todo_way), " +
            "station_code=VALUES(station_code), " +
            "control_num=VALUES(control_num)" +
            "</script>")
    void batchAdd(List<TaskTodo> taskList);

    List<TaskTodo> selectTaskTodoList();

    /**********************************
     * 用途说明: 查询联动任务最近十条
     * 参数说明 linkageType 联动类型
     * 返回值说明: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     ***********************************/
    List<Map<String, Object>> getLinkTaskRecordLast(String stationCode, String linkageType,String taskName);

    /**********************************
     * 用途说明: 查询联动控制巡视任务
     * 参数说明 stationCode
     * 参数说明 todoId
     * 参数说明 linkageType
     * 返回值说明: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     ***********************************/
    List<Map<String, Object>> getLinkTaskTodo(String stationCode, String todoId, String linkageType);

    List<Map<String, Object>> getTaskDurationStat(String startFormat, String endFormat, String type);
}
