<?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.pdaaphm.biz.mapper.FileMapper">

    <resultMap type="File" id="FileResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="path"    column="path"    />
        <result property="type"    column="type"    />
        <result property="dataType"    column="data_type"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectFileVo">
        select id, `name`, `path`, `type`, data_type, create_by, create_time, update_by, update_time, remark from t_file
    </sql>

    <select id="selectFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        <where>
            <if test="name != null  and name != ''"> and `name` like concat('%', #{name}, '%')</if>
            <if test="path != null  and path != ''"> and `path` = #{path}</if>
            <if test="type != null  and type != ''"> and `type` = #{type}</if>
            <if test="dataType != null  and dataType != ''"> and `data_type` = #{dataType}</if>
        </where>
        order by id desc
    </select>

    <select id="selectInputFileList" parameterType="File" resultMap="FileResult">
        <include refid="selectFileVo"/>
        <where>
            <if test="name != null  and name != ''"> and `name` like concat('%', #{name}, '%')</if>
            <if test="path != null  and path != ''"> and `path` = #{path}</if>
            <if test="type != null  and type != ''"> and `type` = #{type}</if>
            <if test="dataType != null  and dataType != ''"> and `data_type` = #{dataType}</if>
            <if test="dataTypes != null and dataTypes.size > 0">
                AND data_type in
                <foreach collection="dataTypes" item="type" open="(" separator="," close=")">
                    #{type}
                </foreach>
            </if>
        </where>
        order by id desc
    </select>

    <select id="selectFileById" parameterType="Long" resultMap="FileResult">
        <include refid="selectFileVo"/>
        where id = #{id}
    </select>

    <insert id="insertFile" parameterType="File" useGeneratedKeys="true" keyProperty="id">
        insert into t_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">`name`,</if>
            <if test="path != null">`path`,</if>
            <if test="type != null">`type`,</if>
            <if test="dataType != null">`data_type`,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="path != null">#{path},</if>
            <if test="type != null">#{type},</if>
            <if test="dataType != null">#{dataType},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateFile" parameterType="File">
        update t_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">`name` = #{name},</if>
            <if test="path != null">`path` = #{path},</if>
            <if test="type != null">`type` = #{type},</if>
            <if test="dataType != null">`data_type` = #{dataType},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFileById" parameterType="Long">
        delete from t_file where id = #{id}
    </delete>

    <delete id="deleteFileByIds" parameterType="String">
        delete from t_file where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getOption" resultType="Map">
        select id, `name`, `type`, data_type from t_file
    </select>

    <select id="getInputOption" parameterType="Long" resultType="Map">
        SELECT
            f.id,
            f.`name`,
            f.`type`,
            f.data_type
        FROM
            t_file f
                JOIN t_algorithm_sub_type ast ON ast.data_type = f.data_type
        WHERE
            f.type = 1
          AND ast.id = #{subTypeId}
    </select>

    <select id="selectOutputFile" resultType="Long">
        SELECT
            tf.id
        FROM
            t_algorithm ta
                JOIN t_sub_algorithm tsa ON tsa.algorithm_id = ta.id
                JOIN t_file tf ON tf.id = tsa.upload_id
        WHERE
            ta.id = #{id}
          AND tf.type = 2
    </select>
</mapper>
