<?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.erp.dataimport.repository.ImportTaskRepository">

    <resultMap id="BaseResultMap" type="com.erp.dataimport.entity.ImportTask">
        <id column="id" property="id"/>
        <result column="task_name" property="taskName"/>
        <result column="biz_type" property="bizType"/>
        <result column="file_format" property="fileFormat"/>
        <result column="original_file_name" property="originalFileName"/>
        <result column="file_path" property="filePath"/>
        <result column="file_size" property="fileSize"/>
        <result column="total_rows" property="totalRows"/>
        <result column="success_rows" property="successRows"/>
        <result column="fail_rows" property="failRows"/>
        <result column="skip_rows" property="skipRows"/>
        <result column="status" property="status"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="creator_id" property="creatorId"/>
        <result column="creator_name" property="creatorName"/>
        <result column="finish_time" property="finishTime"/>
        <result column="error_summary" property="errorSummary"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
        <result column="deleted_at" property="deletedAt"/>
    </resultMap>

    <select id="selectById" resultMap="BaseResultMap">
        SELECT * FROM import_task
        WHERE id = #{id} AND deleted_at IS NULL
    </select>

    <select id="selectByIdAndTenant" resultMap="BaseResultMap">
        SELECT * FROM import_task
        WHERE id = #{id} AND tenant_id = #{tenantId} AND deleted_at IS NULL
    </select>

    <select id="selectPage" resultMap="BaseResultMap">
        SELECT * FROM import_task
        WHERE deleted_at IS NULL
        <if test="tenantId != null">
            AND tenant_id = #{tenantId}
        </if>
        <if test="bizType != null">
            AND biz_type = #{bizType}
        </if>
        <if test="status != null">
            AND status = #{status}
        </if>
        <if test="keyword != null">
            AND task_name LIKE CONCAT('%', #{keyword}, '%')
        </if>
        ORDER BY created_at DESC
    </select>

</mapper>
