<?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.bonus.material.basic.mapper.BmFileInfoMapper">
    <resultMap type="com.bonus.common.biz.domain.BmFileInfo" id="BmFileInfoResult">
        <result property="id"    column="id"    />
        <result property="taskType"    column="task_type"    />
        <result property="taskId"    column="task_id"    />
        <result property="modelId"    column="model_id"    />
        <result property="name"    column="name"    />
        <result property="url"    column="url"    />
        <result property="fileType"    column="file_type"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectBmFileInfoVo">
        select id, task_type, task_id, model_id, name, url, file_type, create_by, create_time from bm_file_info
    </sql>

    <select id="selectBmFileInfoList" parameterType="com.bonus.common.biz.domain.BmFileInfo" resultMap="BmFileInfoResult">
        <include refid="selectBmFileInfoVo"/>
        <where>
            <if test="taskType != null "> and task_type = #{taskType}</if>
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="modelId != null "> and model_id = #{modelId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="url != null  and url != ''"> and url = #{url}</if>
            <if test="fileType != null "> and file_type = #{fileType}</if>

        </where>
    </select>
    
    <select id="selectBmFileInfoById" parameterType="Long" resultMap="BmFileInfoResult">
        <include refid="selectBmFileInfoVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertBmFileInfo" parameterType="com.bonus.common.biz.domain.BmFileInfo" useGeneratedKeys="true" keyProperty="id">
        insert into bm_file_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskType != null">task_type,</if>
            <if test="taskId != null">task_id,</if>
            <if test="modelId != null">model_id,</if>
            <if test="name != null">name,</if>
            <if test="url != null">url,</if>
            <if test="fileType != null">file_type,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskType != null">#{taskType},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="modelId != null">#{modelId},</if>
            <if test="name != null">#{name},</if>
            <if test="url != null">#{url},</if>
            <if test="fileType != null">#{fileType},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <insert id="insertBmFileInfos" parameterType="com.bonus.common.biz.domain.BmFileInfo">
        INSERT INTO bm_file_info(task_type,task_id,model_id,name,url,file_type,create_by,create_time)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.taskType},
            #{item.taskId},
            #{item.modelId},
            #{item.name},
            #{item.url},
            #{item.fileType},
            #{item.createBy},
            #{item.createTime})
        </foreach>
    </insert>

    <update id="updateBmFileInfo" parameterType="com.bonus.common.biz.domain.BmFileInfo">
        update bm_file_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="modelId != null">model_id = #{modelId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="url != null">url = #{url},</if>
            <if test="fileType != null">file_type = #{fileType},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBmFileInfoById" parameterType="Long">
        delete from bm_file_info where id = #{id}
    </delete>

    <delete id="deleteBmFileInfoByModelId" parameterType="Long">
        delete from bm_file_info where model_id = #{modelId}
    </delete>

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

    <delete id="deleteBmFileInfoByBizInfo" parameterType="com.bonus.common.biz.domain.BmFileInfo">
        delete from bm_file_info
        <where>
            <if test="taskType != null "> and task_type = #{taskType}</if>
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="modelId != null "> and model_id = #{modelId}</if>
            <if test="fileType != null "> and file_type = #{fileType}</if>
        </where>
    </delete>

    <delete id="deleteBmFileList">
        delete from bm_file_info where task_id = #{taskId}
        and task_type = 0
    </delete>
</mapper>