<?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.ozs.da.dao.TaskDao">

    <insert id="addTask" parameterType="com.ozs.da.entity.task.Task">
        insert into da_info_data_collection_task
        (id, task_name, period_type, task_status, schedule_offset, run_status, task_cron,
        <if test="nextRunDate != null and nextRunDate != ''">
            next_run_date,
        </if>
        ddl_sql, source_database_id, source_table_id, source_table_name, target_database_id,
        target_table_id, target_table_name, user_id, create_time, update_time)
        values
        (#{id}, #{taskName}, #{periodType}, #{taskStatus}, #{scheduleOffset}, #{runStatus}, #{taskCron},
        <if test="nextRunDate != null and nextRunDate != ''">
            next_run_date,
        </if>
        #{ddlSql}, #{sourceDatabaseId}, #{sourceTableId}, #{sourceTableName}, #{targetDatabaseId},
        #{targetTableId}, #{targetTableName}, #{userId}, now(), now())
    </insert>

    <select id="listTask" parameterType="com.ozs.da.entity.PageParams" resultType="com.ozs.da.entity.task.Task">
        select
            didct.id as id,
            didct.task_name as taskName,
            didct.task_status as taskStatus,
            ts.sdt_name as taskStatusDesc,
            didct.task_cron as taskCron,
            didct.next_run_date as nextRunDate,
            diu.nickname as userName,
            date_format(didct.create_time, '%Y-%m-%d %T') as createTime,
            date_format(didct.update_time, '%Y-%m-%d %T') as updateTime
        from da_info_data_collection_task didct
        left join dc_info_user diu on diu.id=didct.user_id
        left join (select sdt_code,sdt_name from da_dim_sdt_code
          where sdt_type_code=(select id from da_dim_sdt_type where sdt_type_code='task_status')) ts on ts.sdt_code=didct.task_status
        where 1=1
        <if test="data.taskName != null and data.taskName != ''">
            and didct.task_name like concat('%',#{data.taskName},'%')
        </if>
    </select>

    <select id="getTaskInfoById" resultType="com.ozs.da.entity.task.Task">
        select
            didct.id as id,
            didct.task_name as taskName,
            didct.task_cron as taskCron,
            didct.period_type as periodType,
            didct.run_status as runStatus,
            didct.task_status as taskStatus,
            didct.schedule_offset as scheduleOffset,
            didct.next_run_date as nextRunDate,
            pt.sdt_name as periodTypeDesc,
            didct.ddl_sql as ddlSql,
            didct.source_database_id as sourceDatabaseId,
            didct.source_table_id as sourceTableId,
            didct.source_table_name as sourceTableName,
            didct.target_database_id as targetDatabaseId,
            didct.target_table_id as targetTableId,
            didct.target_table_name as targetTableName
        from da_info_data_collection_task didct
        left join dc_info_user diu on diu.id=didct.user_id
        left join (select sdt_code,sdt_name from da_dim_sdt_code
          where sdt_type_code=(select id from da_dim_sdt_type where sdt_type_code='period_type')) pt on pt.sdt_code=didct.period_type
        where didct.id=#{taskId}
    </select>

    <update id="updateTaskById" parameterType="com.ozs.da.entity.task.Task">
        update da_info_data_collection_task set
        <trim suffixOverrides=",">
            update_time = now(),
            <if test="taskName!=null and taskName!='' ">
                task_name = #{taskName},
            </if>
            period_type = #{periodType},
            <if test="taskStatus!=null and taskStatus!='' ">
                task_status = #{taskStatus},
            </if>
            schedule_offset = #{scheduleOffset},
            <if test="runStatus!=null and runStatus!='' ">
                run_status = #{runStatus},
            </if>
            <if test="taskCron!=null and taskCron!='' ">
                task_cron = #{taskCron},
            </if>
            <if test="nextRunDate!=null and nextRunDate!='' ">
                next_run_date = #{nextRunDate},
            </if>
            <if test="ddlSql!=null and ddlSql!='' ">
                ddl_sql = #{ddlSql},
            </if>
            <if test="sourceDatabaseId!=null and sourceDatabaseId!='' ">
                source_database_id = #{sourceDatabaseId},
            </if>
            <if test="sourceTableId!=null and sourceTableId!='' ">
                source_table_id = #{sourceTableId},
            </if>
            <if test="sourceTableName!=null and sourceTableName!='' ">
                source_table_name = #{sourceTableName},
            </if>
            <if test="targetDatabaseId!=null and targetDatabaseId!='' ">
                target_database_id = #{targetDatabaseId},
            </if>
            <if test="targetTableId!=null and targetTableId!='' ">
                target_table_id = #{targetTableId},
            </if>
            <if test="targetTableName!=null and targetTableName!='' ">
                target_table_name = #{targetTableName},
            </if>
            <if test="userId!=null and userId!='' ">
                user_id = #{userId},
            </if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTaskStatusById">
        update da_info_data_collection_task set
        <trim suffixOverrides=",">
            update_time = now(),
            <if test="taskStatus!=null and taskStatus!='' ">
                task_status = #{taskStatus},
            </if>
            <if test="nextRunDate!=null and nextRunDate!='' ">
                next_run_date = #{nextRunDate},
            </if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTaskRunStatusById">
        update da_info_data_collection_task set
        <trim suffixOverrides=",">
            update_time = now(),
            <if test="runStatus!=null and runStatus!='' ">
                run_status = #{runStatus},
            </if>
            <if test="nextRunDate!=null and nextRunDate!='' ">
                next_run_date = #{nextRunDate},
            </if>
        </trim>
        where id = #{id}
    </update>
	
	<delete id="deleteTaskById">
		delete from da_info_data_collection_task where id=#{taskId}
	</delete>

    <select id="getAllInitJob" resultType="com.ozs.da.entity.task.Task">
        select
            didct.id as id,
            didct.task_name as taskName,
            didct.task_cron as taskCron,
            didct.period_type as periodType,
            didct.run_status as runStatus,
            didct.task_status as taskStatus,
            didct.schedule_offset as scheduleOffset,
            didct.next_run_date as nextRunDate,
            didct.ddl_sql as ddlSql,
            didct.source_database_id as sourceDatabaseId,
            didct.source_table_id as sourceTableId,
            didct.source_table_name as sourceTableName,
            didct.target_database_id as targetDatabaseId,
            didct.target_table_id as targetTableId,
            didct.target_table_name as targetTableName
        from da_info_data_collection_task didct
    </select>

    <insert id="insertTaskErrorInfo" parameterType="com.ozs.da.entity.mail.ErrorJobInfo">
        insert into da_info_task_error_record (id, task_id, task_name, error_desc, send_status, create_time, update_time)
        values (uuid(), #{taskId}, #{taskName}, #{errorDesc}, #{sendStatus}, now(), now())
    </insert>

    <delete id="deleteTaskErrorInfo">
        delete from da_info_task_error_record where task_id=#{taskId}
    </delete>

    <update id="updateSendedStatusById">
        update da_info_task_error_record set send_status=#{sendStatus},update_time=now() where id=#{id}
    </update>

    <select id="getTaskSendStatusById" resultType="String">
        select send_status as sendStatus from da_info_task_error_record where task_id=#{taskId}
        order by update_time desc limit 1
    </select>

    <select id="getErrorJobInfoById" resultType="com.ozs.da.entity.mail.ErrorJobInfo">
        select
            id as id,
            task_id as taskId,
            task_name as taskName,
            error_desc as errorDesc,
            send_status as sendStatus,
            date_format(create_time, '%Y-%m-%d %T') as createTime,
            date_format(update_time, '%Y-%m-%d %T') as updateTime
        from da_info_task_error_record
        where task_id=#{taskId}
        order by update_time desc limit 1
    </select>

    <select id="getSendMailsConfig" resultType="String">
        select send_address from da_info_send_mails_config order by update_time desc
    </select>
</mapper>