<?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.ruoyi.system.mapper.SysExportTaskMapper">

    <resultMap type="com.ruoyi.system.domain.SysExportTask" id="SysExportTaskResult">
        <id     property="taskId"          column="task_id"          />
        <result property="taskName"        column="task_name"        />
        <result property="taskType"        column="task_type"        />
        <result property="corpId"          column="corp_id"          />
        <result property="queryParams"   column="query_params"     />
        <result property="status"          column="status"           />
        <result property="progress"        column="progress"         />
        <result property="totalCount"      column="total_count"      />
        <result property="processedCount"  column="processed_count"  />
        <result property="filePath"        column="file_path"        />
        <result property="fileName"        column="file_name"        />
        <result property="fileSize"        column="file_size"        />
        <result property="errorMsg"        column="error_msg"        />
        <result property="createBy"        column="create_by"        />
        <result property="createTime"      column="create_time"      />
        <result property="finishTime"      column="finish_time"      />
    </resultMap>

    <sql id="selectExportTaskVo">
        select task_id, task_name, task_type, corp_id, query_params, status, progress, total_count, processed_count,
               file_path, file_name, file_size, error_msg, create_by, create_time, finish_time
        from sys_export_task
    </sql>

    <select id="selectExportTaskById" parameterType="Long" resultMap="SysExportTaskResult">
        <include refid="selectExportTaskVo"/>
        where task_id = #{taskId}
    </select>

    <select id="selectExportTaskList" parameterType="com.ruoyi.system.domain.SysExportTask" resultMap="SysExportTaskResult">
        <include refid="selectExportTaskVo"/>
        <where>
            <if test="taskName != null and taskName != ''">
                AND task_name like concat('%', #{taskName}, '%')
            </if>
            <if test="taskType != null and taskType != ''">
                AND task_type = #{taskType}
            </if>
            <if test="corpId != null and corpId != ''">
                AND corp_id = #{corpId}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="createBy != null and createBy != ''">
                AND create_by = #{createBy}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectExpiredExportTasks" parameterType="int" resultMap="SysExportTaskResult">
        <include refid="selectExportTaskVo"/>
        where status in ('2', '3')
        and create_time &lt; date_sub(now(), interval #{expireDays} day)
    </select>

    <insert id="insertExportTask" parameterType="com.ruoyi.system.domain.SysExportTask" useGeneratedKeys="true" keyProperty="taskId">
        insert into sys_export_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="taskType != null and taskType != ''">task_type,</if>
            <if test="corpId != null and corpId != ''">corp_id,</if>
            <if test="queryParams != null">query_params,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="progress != null">progress,</if>
            <if test="totalCount != null">total_count,</if>
            <if test="processedCount != null">processed_count,</if>
            <if test="filePath != null and filePath != ''">file_path,</if>
            <if test="fileName != null and fileName != ''">file_name,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="errorMsg != null">error_msg,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="finishTime != null">finish_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="taskType != null and taskType != ''">#{taskType},</if>
            <if test="corpId != null and corpId != ''">#{corpId},</if>
            <if test="queryParams != null">#{queryParams},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="progress != null">#{progress},</if>
            <if test="totalCount != null">#{totalCount},</if>
            <if test="processedCount != null">#{processedCount},</if>
            <if test="filePath != null and filePath != ''">#{filePath},</if>
            <if test="fileName != null and fileName != ''">#{fileName},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="errorMsg != null">#{errorMsg},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="finishTime != null">#{finishTime},</if>
        </trim>
    </insert>

    <update id="updateExportTask" parameterType="com.ruoyi.system.domain.SysExportTask">
        update sys_export_task
        <set>
            <if test="taskName != null and taskName != ''">task_name = #{taskName},</if>
            <if test="taskType != null and taskType != ''">task_type = #{taskType},</if>
            <if test="corpId != null and corpId != ''">corp_id = #{corpId},</if>
            <if test="queryParams != null">query_params = #{queryParams},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="progress != null">progress = #{progress},</if>
            <if test="totalCount != null">total_count = #{totalCount},</if>
            <if test="processedCount != null">processed_count = #{processedCount},</if>
            <if test="filePath != null">file_path = #{filePath},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="errorMsg != null">error_msg = #{errorMsg},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
        </set>
        where task_id = #{taskId}
    </update>

    <delete id="deleteExportTaskById" parameterType="Long">
        delete from sys_export_task where task_id = #{taskId}
    </delete>

    <delete id="deleteExportTaskByIds" parameterType="Long">
        delete from sys_export_task where task_id in
        <foreach item="taskId" collection="array" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>

</mapper>
