package com.template.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.template.model.pojo.SmartDataTask;
import com.template.model.pojo.SmartDataTaskDebug;
import com.template.model.pojo.SmartDataTaskLog;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 数据源任务 Mapper 接口
 * </p>
 *
 * @author ceshi
 * @since 2023-12-05
 */
@Repository
public interface SmartDataTaskMapper extends BaseMapper<SmartDataTask> {
    @Select("SELECT " +
            "    COUNT(tk_id) " +
            "FROM " +
            "    smart_data_task  " +
            "WHERE " +
            "    tk_name = #{tkName} " +
            "    AND tk_id != #{tkId} "
    )
    int isRepeatTaskName(SmartDataTask smartDataTask);

    @Update("UPDATE smart_data_task " +
            "SET tk_activation = #{tkActivation} " +
            "    ,tk_next_exe_time = #{tkNextExeTime} " +
            "WHERE " +
            "    tk_id = #{tkId} "
    )
    int markTaskById(SmartDataTask smartDataTask);

    @Insert({
            "<script>",
            "INSERT INTO smart_data_task ",
            "   <trim prefix='(' suffix=')' suffixOverrides=','> ",
            "       <if test='tkName != null'>tk_name,</if> ",
            "       <if test='tkDtId != null'>tk_dt_id,</if> ",
            "       <if test='tkDsIdSource != null'>tk_ds_id_source,</if> ",
            "       <if test='tkSyncPolicy != null'>tk_sync_policy,</if> ",
            "       <if test='tkExchangeType != null'>tk_exchange_type,</if> ",
            "       <if test='tkSql != null'>tk_sql,</if> ",
            "       <if test='tkDsIdDestination != null'>tk_ds_id_destination,</if> ",
            "       <if test='tkDestTable != null'>tk_dest_table,</if> ",
            "       <if test='tkExchangeServer != null'>tk_exchange_server,</if> ",
            "       <if test='tkExchangeServerId != null'>tk_exchange_server_id,</if> ",
            "       <if test='tkOptCfgAutoManual != null'>tk_opt_cfg_auto_manual,</if> ",
            "       <if test='tkOptCfgRsNum != null'>tk_opt_cfg_rs_num,</if> ",
            "       <if test='tkOptCfgThreadsNum != null'>tk_opt_cfg_threads_num,</if> ",
            "       <if test='tkRsIncorrectData != null'>tk_rs_incorrect_data,</if> ",
            "       <if test='tkDsSourceCharset != null'>tk_ds_source_charset,</if> ",
            "       <if test='tkDsDestinationCharset != null'>tk_ds_destination_charset,</if> ",
            "       <if test='tkCron != null'>tk_cron,</if> ",
            "       <if test='tkManualOrAuto != null'>tk_manual_or_auto,</if> ",
            "       <if test='tkExeType != null'>tk_exe_type,</if> ",
            "       <if test='tkRepetTime != null'>tk_repet_time,</if> ",
            "       <if test='tkActivation != null'>tk_activation,</if> ",
            "       <if test='tkDescrition != null'>tk_descrition,</if> ",
            "       <if test='tkDeleted != null'>tk_deleted,</if> ",
            "       <if test='tkNextExeTime != null'>tk_next_exe_time,</if> ",
            "   </trim> ",
            "VALUES ",
            "   <trim prefix='(' suffix=')' suffixOverrides=','>",
            "      <if test='tkName != null'>#{tkName},</if> ",
            "      <if test='tkDtId != null'>#{tkDtId},</if> ",
            "      <if test='tkDsIdSource != null'>#{tkDsIdSource},</if> ",
            "      <if test='tkSyncPolicy != null'>#{tkSyncPolicy},</if> ",
            "      <if test='tkExchangeType != null'>#{tkExchangeType},</if> ",
            "      <if test='tkSql != null'>#{tkSql},</if> ",
            "      <if test='tkDsIdDestination != null'>#{tkDsIdDestination},</if> ",
            "      <if test='tkDestTable != null'>#{tkDestTable},</if> ",
            "      <if test='tkExchangeServer != null'>#{tkExchangeServer},</if> ",
            "      <if test='tkExchangeServerId != null'>#{tkExchangeServerId},</if> ",
            "      <if test='tkOptCfgAutoManual != null'>#{tkOptCfgAutoManual},</if> ",
            "      <if test='tkOptCfgRsNum != null'>#{tkOptCfgRsNum},</if> ",
            "      <if test='tkOptCfgThreadsNum != null'>#{tkOptCfgThreadsNum},</if> ",
            "      <if test='tkRsIncorrectData != null'>#{tkRsIncorrectData},</if> ",
            "      <if test='tkDsSourceCharset != null'>#{tkDsSourceCharset},</if> ",
            "      <if test='tkDsDestinationCharset != null'>#{tkDsDestinationCharset},</if> ",
            "      <if test='tkColRelationship != null'>#{tkColRelationship},</if> ",
            "      <if test='tkCron != null'>#{tkCron},</if> ",
            "      <if test='tkManualOrAuto != null'>#{tkManualOrAuto},</if> ",
            "      <if test='tkExeType != null'>#{tkExeType},</if> ",
            "      <if test='tkRepetTime != null'>#{tkRepetTime},</if> ",
            "      <if test='tkActivation != null'>#{tkActivation},</if> ",
            "      <if test='tkDeleted != null'>#{tkDeleted},</if> ",
            "      <if test='tkNextExeTime != null'>#{tkNextExeTime},</if> ",
            "   </trim>",
            "</script>"
    })
    @Options(useGeneratedKeys = true, keyProperty = "tk_id")
    int insertOne(SmartDataTask smartDataTask);

    @Update("UPDATE smart_data_task " +
            "   SET tk_col_relationship = #{colRelationship} " +
            "WHERE " +
            "   tk_id = #{tkId}")
    int insertColRelationship(int tkId, String colRelationship);

    @Select("SELECT " +
            "    COUNT(id) " +
            "FROM " +
            "    smart_department  " +
            "WHERE " +
            "    id = #{tkDtId} ")
    int isHaveDepartmentById(Integer tkDtId);

    @Select("SELECT " +
            "    tk_id " +
            "    ,tk_col_relationship " +
            "FROM " +
            "    smart_data_task  " +
            "WHERE " +
            "    tk_id = #{tkId} "
    )
    SmartDataTask selectColRelationship(int tkId);

    @Select("SELECT " +
            "    tk_activation " +
            "FROM " +
            "    smart_data_task  " +
            "WHERE " +
            "    tk_id = #{tkId} "
    )
    int selectColSwappedPrimaryKeys(int tkId);

    @Select({
            "<script>",
            "   SELECT ",
            "      COUNT(*) ",
            "   FROM smart_data_task ",
            "   <where>",
            "      <if test='tkId != null'>AND tk_id = #{tkId}</if> ",
            "      <if test='tkName != null'>AND tk_name = #{tkName}</if> ",
            "      <if test='tkDtId != null'>AND tk_dt_id = #{tkDtId}</if> ",
            "      <if test='tkDsIdSource != null'>AND tk_ds_id_source = #{tkDsIdSource}</if> ",
            "      <if test='tkSyncPolicy != null'>AND tk_sync_policy = #{tkSyncPolicy}</if> ",
            "      <if test='tkExchangeType != null'>AND tk_exchange_type = #{tkExchangeType}</if> ",
            "      <if test='tkSql != null'>AND tk_sql = #{tkSql}</if> ",
            "      <if test='tkDsIdDestination != null'>AND tk_ds_id_destination = #{tkDsIdDestination}</if> ",
            "      <if test='tkDestTable != null'>AND tk_dest_table = #{tkDestTable}</if> ",
            "      <if test='tkExchangeServer != null'>AND tk_exchange_server = #{tkExchangeServer}</if> ",
            "      <if test='tkExchangeServerId != null'>AND tk_exchange_server_id = #{tkExchangeServerId}</if> ",
            "      <if test='tkOptCfgAutoManual != null'>AND tk_opt_cfg_auto_manual = #{tkOptCfgAutoManual}</if> ",
            "      <if test='tkOptCfgRsNum != null'>AND tk_opt_cfg_rs_num = #{tkOptCfgRsNum}</if> ",
            "      <if test='tkOptCfgThreadsNum != null'>AND tk_opt_cfg_threads_num = #{tkOptCfgThreadsNum}</if> ",
            "      <if test='tkRsIncorrectData != null'>AND tk_rs_incorrect_data = #{tkRsIncorrectData}</if> ",
            "      <if test='tkDsSourceCharset != null'>AND tk_ds_source_charset = #{tkDsSourceCharset}</if> ",
            "      <if test='tkDsDestinationCharset != null'>AND tk_ds_destination_charset = #{tkDsDestinationCharset}</if> ",
            "      <if test='tkColRelationship != null'>AND tk_name = #{tkColRelationship}</if> ",
            "      <if test='tkCron != null'>AND tk_cron = #{tkCron}</if> ",
            "      <if test='tkManualOrAuto != null'>AND tk_manual_or_auto = #{tkManualOrAuto}</if> ",
            "      <if test='tkExeType != null'>AND tk_exe_type = #{tkExeType}</if> ",
            "      <if test='tkRepetTime != null'>AND tk_repet_time = #{tkRepetTime}</if> ",
            "      <if test='tkActivation != null'>AND tk_activation = #{tkActivation}</if> ",
            "      <if test='tkDeleted != null'>AND tk_deleted = #{tkDeleted}</if> ",
            "      <if test='tkNextExeTime != null'>AND tk_next_exe_time = #{tkNextExeTime}</if> ",
            "   </where>",
            "</script>"
    })
    int isRepeatTask(SmartDataTask smartDataTask);

    @Update("UPDATE smart_data_task " +
            "SET tk_deleted = 1 " +
            "WHERE " +
            "    tk_id = #{id} "
    )
    int logicDeleteMarkTaskById(int id);

    @Delete("DELETE FROM smart_data_task " +
            "WHERE " +
            "    tk_id = #{id} "
    )
    int physicsDeleteMarkTaskById(int id);

    @Update("UPDATE smart_data_task " +
            "SET tk_deleted = 0 " +
            "WHERE " +
            "    tk_id = #{id} "
    )
    int restoreLogicDeleteMarkTaskById(int id);

    @Update("UPDATE smart_data_task " +
            "SET tk_swapped_primary_keys = #{tkSwappedPrimaryKeys} " +
            "WHERE " +
            "    tk_id = #{tkId} "
    )
    int saveSwappedPrimaryKeys(SmartDataTask smartDataTask);

    @Insert({
            "<script>",
            "INSERT INTO smart_data_task_debug ",
            "   <trim prefix='(' suffix=')' suffixOverrides=','> ",
            "       <if test='eTaskId != null'>e_task_id,</if> ",
            "       <if test='eTaskName != null'>e_task_name,</if> ",
            "       <if test='eDateTime != null'>e_date_time,</if> ",
            "       <if test='eMsg != null'>e_msg,</if> ",
            "   </trim> ",
            "VALUES ",
            "   <trim prefix='(' suffix=')' suffixOverrides=','>",
            "      <if test='eTaskId != null'>#{eTaskId},</if> ",
            "      <if test='eTaskName != null'>#{eTaskName},</if> ",
            "      <if test='eDateTime != null'>#{eDateTime},</if> ",
            "      <if test='eMsg != null'>#{eMsg},</if> ",
            "   </trim>",
            "</script>"
    })
    int insertErrorMsg(SmartDataTaskDebug smartDataTaskDebug);

    @Select({
            "<script>",
            "   SELECT ",
            "      * ",
            "   FROM smart_data_task_debug ",
            "   <where>",
            "      <if test='eTaskId != null'>AND e_task_id = #{eTaskId}</if> ",
            "      <if test='eMsg != null'>AND e_msg = #{eMsg}</if> ",
            "   </where>",
            "</script>"
    })
    SmartDataTaskDebug selectErrorMsg(SmartDataTaskDebug smartDataTaskDebug);

    @Update("UPDATE smart_data_task_debug " +
            "SET " +
            "    e_task_id = #{eTaskId} " +
            "    ,e_task_name = #{eTaskName} " +
            "    ,e_msg = #{eMsg} " +
            "    ,e_num = e_num + 1 " +
            "    ,e_date_time = #{eDateTime} " +
            "WHERE " +
            "    e_id = #{eId} "
    )
    int updateErrorMsg(SmartDataTaskDebug smartDataTaskErr);

    @Select({
            "<script>",
            "   SELECT ",
            "      tk_col_relationship ",
            "   FROM smart_data_task ",
            "   <where>",
            "      <if test='tkId != null'>AND tk_id = #{tkId}</if> ",
            "   </where>",
            "</script>"
    })
    String getColRelationship(SmartDataTask smartDataTask);

    @Select({
            "<script>",
            "   SELECT ",
            "      e_id, e_task_id, e_task_name, e_msg, e_num, e_date_time, e_create_time ",
            "   FROM smart_data_task_debug ",
            "   <where>",
            "      <if test='eTaskId != null'>AND e_task_id = #{eTaskId}</if> ",
            "      <if test='startTime != null'>AND e_date_time &gt;= #{startTime}</if> ",
            "      <if test='endTime != null'>AND e_date_time &lt;= #{endTime}</if> ",
            "   </where>",
            "   ORDER BY e_date_time DESC, e_id DESC ",
            "</script>"
    })
    List<SmartDataTaskDebug> selectTaskDebugPageOrderByDatetimeDesc(Integer eTaskId, String startTime, String endTime);

    @Delete({
            "<script>",
            "   DELETE ",
            "   FROM ",
            "      smart_data_task_debug ",
            "   WHERE ",
            "      e_id IN ",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach>",
            "</script>"
    })
    int deleteSmartDataTaskDebugByIds(List<Long> ids);

    @Insert({
            "<script>",
            "   INSERT INTO smart_data_task_log ",
            "   <trim prefix='(' suffix=')' suffixOverrides=','>",
            "      <if test='tkLogTaskId != null'>tk_log_task_id,</if>",
            "      <if test='tkLogTaskName != null'>tk_log_task_name,</if>",
            "      <if test='tkLogDtName != null'>tk_log_dt_name,</if>",
            "      <if test='tkLogDsSourceId != null'>tk_log_ds_source_id,</if>",
            "      <if test='tkLogDsSourceName != null'>tk_log_ds_source_name,</if>",
            "      <if test='tkLogDsDestinationId != null'>tk_log_ds_destination_id,</if>",
            "      <if test='tkLogDsDestinationName != null'>tk_log_ds_destination_name,</if>",
            "      <if test='tkLogDestTable != null'>tk_log_dest_table,</if>",
            "      <if test='tkLogExchangeServer != null'>tk_log_exchange_server,</if>",
            "      <if test='tkLogExchangeType != null'>tk_log_exchange_type,</if>",
            "      <if test='tkLogAutoManual != null'>tk_log_auto_manual,</if>",
            "      <if test='tkLogExeType != null'>tk_log_exe_type,</if>",
            "      <if test='tkLogExeStatus != null'>tk_log_exe_status,</if>",
            "      <if test='tkLogStartTime != null'>tk_log_start_time,</if>",
            "      <if test='tkLogEndTime != null'>tk_log_end_time,</if>",
            "      <if test='tkLogCostTime != null'>tk_log_cost_time,</if>",
            "      <if test='tkLogReadRows != null'>tk_log_read_rows,</if>",
            "      <if test='tkLogInsertRows != null'>tk_log_insert_rows,</if>",
            "      <if test='tkLogUpdateRows != null'>tk_log_update_rows,</if>",
            "      <if test='tkLogErrRows != null'>tk_log_err_rows,</if>",
            "      <if test='tkLogErrException != null'>tk_log_err_exception,</if>",
            "   </trim>",
            "VALUES",
            "   <trim prefix='(' suffix=')' suffixOverrides=','>",
            "      <if test='tkLogTaskId != null'>#{tkLogTaskId},</if>",
            "      <if test='tkLogTaskName != null'>#{tkLogTaskName},</if>",
            "      <if test='tkLogDtName != null'>#{tkLogDtName},</if>",
            "      <if test='tkLogDsSourceId != null'>#{tkLogDsSourceId},</if>",
            "      <if test='tkLogDsSourceName != null'>#{tkLogDsSourceName},</if>",
            "      <if test='tkLogDsDestinationId != null'>#{tkLogDsDestinationId},</if>",
            "      <if test='tkLogDsDestinationName != null'>#{tkLogDsDestinationName},</if>",
            "      <if test='tkLogDestTable != null'>#{tkLogDestTable},</if>",
            "      <if test='tkLogExchangeServer != null'>#{tkLogExchangeServer},</if>",
            "      <if test='tkLogExchangeType != null'>#{tkLogExchangeType},</if>",
            "      <if test='tkLogAutoManual != null'>#{tkLogAutoManual},</if>",
            "      <if test='tkLogExeType != null'>#{tkLogExeType},</if>",
            "      <if test='tkLogExeStatus != null'>#{tkLogExeStatus},</if>",
            "      <if test='tkLogStartTime != null'>#{tkLogStartTime},</if>",
            "      <if test='tkLogEndTime != null'>#{tkLogEndTime},</if>",
            "      <if test='tkLogCostTime != null'>#{tkLogCostTime},</if>",
            "      <if test='tkLogReadRows != null'>#{tkLogReadRows},</if>",
            "      <if test='tkLogInsertRows != null'>#{tkLogInsertRows},</if>",
            "      <if test='tkLogUpdateRows != null'>#{tkLogUpdateRows},</if>",
            "      <if test='tkLogErrRows != null'>#{tkLogErrRows},</if>",
            "      <if test='tkLogErrException != null'>#{tkLogErrException},</if>",
            "   </trim>",
            "</script>"
    })
    @Options(useGeneratedKeys = true, keyProperty = "tkLogId")
    int insertTaskLog(SmartDataTaskLog smartDataTaskLog);

    @Update({
            "<script>",
            "   UPDATE smart_data_task_log ",
            "   SET",
            "   <trim suffixOverrides=','>",
            "      <if test='tkLogTaskId != null'>tk_log_task_id = #{tkLogTaskId},</if>",
            "      <if test='tkLogTaskName != null'>tk_log_task_name = #{tkLogTaskName},</if>",
            "      <if test='tkLogDtName != null'>tk_log_dt_name = #{tkLogDtName},</if>",
            "      <if test='tkLogDsSourceName != null'>tk_log_ds_source_name = #{tkLogDsSourceName},</if>",
            "      <if test='tkLogDsSourceId != null'>tk_log_ds_source_id = #{tkLogDsSourceId},</if>",
            "      <if test='tkLogDsDestinationName != null'>tk_log_ds_destination_name = #{tkLogDsDestinationName},</if>",
            "      <if test='tkLogDsDestinationId != null'>tk_log_ds_destination_id = #{tkLogDsDestinationId},</if>",
            "      <if test='tkLogDestTable != null'>tk_log_dest_table = #{tkLogDestTable},</if>",
            "      <if test='tkLogExchangeServer != null'>tk_log_exchange_server = #{tkLogExchangeServer},</if>",
            "      <if test='tkLogExchangeType != null'>tk_log_exchange_type = #{tkLogExchangeType},</if>",
            "      <if test='tkLogAutoManual != null'>tk_log_auto_manual = #{tkLogAutoManual},</if>",
            "      <if test='tkLogExeType != null'>tk_log_exe_type = #{tkLogExeType},</if>",
            "      <if test='tkLogExeStatus != null'>tk_log_exe_status = #{tkLogExeStatus},</if>",
            "      <if test='tkLogStartTime != null'>tk_log_start_time = #{tkLogStartTime},</if>",
            "      <if test='tkLogEndTime != null'>tk_log_end_time = #{tkLogEndTime},</if>",
            "      <if test='tkLogCostTime != null'>tk_log_cost_time = #{tkLogCostTime},</if>",
            "      <if test='tkLogReadRows != null'>tk_log_read_rows = #{tkLogReadRows},</if>",
            "      <if test='tkLogInsertRows != null'>tk_log_insert_rows = #{tkLogInsertRows},</if>",
            "      <if test='tkLogUpdateRows != null'>tk_log_update_rows = #{tkLogUpdateRows},</if>",
            "      <if test='tkLogErrRows != null'>tk_log_err_rows = #{tkLogErrRows},</if>",
            "      <if test='tkLogErrException != null'>tk_log_err_exception = #{tkLogErrException},</if>",
            "   </trim>",
            "   WHERE ",
            "      tk_log_id = #{tkLogId} ",
            "</script>"
    })
    int updateTaskLog(SmartDataTaskLog smartDataTaskLog);

    @Select({
            "<script>",
            "   SELECT ",
            "      * ",
            "   FROM smart_data_task_log ",
            "   <where>",
            "      <if test='eTaskId != null'>AND tk_log_task_id = #{eTaskId}</if> ",
            "      <if test='tkLogAutoManual != null'>AND tk_log_auto_manual = #{tkLogAutoManual}</if> ",
            "      <if test='tkLogDsSourceId != null'>AND tk_log_ds_source_id = #{tkLogDsSourceId}</if> ",
            "      <if test='tkLogDsDestinationId != null'>AND tk_log_ds_destination_id = #{tkLogDsDestinationId}</if> ",
            "      <if test='tkLogCostTime != null'> ",
            "         <if test='tkLogCostTime == 1'>AND tk_log_cost_time &lt; 60000</if> ",
            "         <if test='tkLogCostTime == 2'>AND tk_log_cost_time BETWEEN 60000 AND 300000</if> ",
            "         <if test='tkLogCostTime == 3'>AND tk_log_cost_time BETWEEN 300000 AND 600000</if> ",
            "         <if test='tkLogCostTime == 4'>AND tk_log_cost_time BETWEEN 300000 AND 600000</if> ",
            "         <if test='tkLogCostTime == 5'>AND tk_log_cost_time BETWEEN 600000 AND 1800000</if> ",
            "         <if test='tkLogCostTime == 6'>AND tk_log_cost_time &gt; 1800000</if> ",
            "      </if> ",
            "      <if test='tkLogExeStatus != null'> ",
            "         <if test='tkLogExeStatus == 1'>AND tk_log_exe_status = '执行中'</if> ",
            "         <if test='tkLogExeStatus == 2'>AND tk_log_exe_status = '执行成功'</if> ",
            "         <if test='tkLogExeStatus == 3'>AND tk_log_exe_status = '部分数据异常'</if> ",
            "         <if test='tkLogExeStatus == 4'>AND tk_log_exe_status = '执行中断'</if> ",
            "         <if test='tkLogExeStatus == 5'>AND tk_log_exe_status = '执行跳过'</if> ",
            "         <if test='tkLogExeStatus == 6'>AND tk_log_exe_status = '执行失败'</if> ",
            "      </if> ",
            "      <if test='startTime != null'>AND tk_log_start_time &gt;= #{startTime}</if> ",
            "      <if test='endTime != null'>AND tk_log_end_time &lt;= #{endTime}</if> ",
            "   </where>",
            "   ORDER BY tk_log_update_time DESC, tk_log_task_id DESC ",
            "</script>"
    })
    List<SmartDataTaskLog> selectTaskLogPageOrderByDatetimeDesc(Integer eTaskId, String tkLogTaskName, Integer tkLogAutoManual,
                                                                Integer tkLogDsSourceId, Integer tkLogDsDestinationId,
                                                                Integer tkLogCostTime, Integer tkLogExeStatus,
                                                                String startTime, String endTime);

    @Select({
            "<script>",
            "   SELECT ",
            "      * ",
            "   FROM smart_data_task_log ",
            "   <where>",
            "      <if test='tkLogTaskId != null'>AND tk_log_task_id = #{tkLogTaskId}</if> ",
            "      <if test='tkLogExeStatus != null'> ",
            "         <if test='tkLogExeStatus == 1'>AND tk_log_exe_status = '执行中'</if> ",
            "      </if> ",
            "   </where>",
            "</script>"
    })
    List<SmartDataTaskLog> queryTaskExecuting(Integer tkLogTaskId, Integer tkLogExeStatus);
}
