<?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.shzg.project.worn.mapper.WornDeclareFileMapper">
    
    <resultMap type="WornDeclareFile" id="WornDeclareFileResult">
        <result property="id"    column="id"    />
        <result property="billId"    column="bill_id"    />
        <result property="billNo"    column="bill_no"    />
        <result property="fileName"    column="file_name"    />
        <result property="fileUrl"    column="file_url"    />
        <result property="fileType"    column="file_type"    />
        <result property="sortNum"    column="sort_num"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="isDelete"    column="is_delete"    />
    </resultMap>

    <sql id="selectWornDeclareFileVo">
        select id, bill_id, bill_no, file_name, file_url, file_type, sort_num, remark, create_by, create_time, is_delete from worn_declare_file
    </sql>

    <select id="selectWornDeclareFileList" parameterType="WornDeclareFile" resultMap="WornDeclareFileResult">
        <include refid="selectWornDeclareFileVo"/>
        <where>  
            <if test="billId != null "> and bill_id = #{billId}</if>
            <if test="billNo != null  and billNo != ''"> and bill_no = #{billNo}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
            <if test="fileUrl != null  and fileUrl != ''"> and file_url = #{fileUrl}</if>
            <if test="fileType != null  and fileType != ''"> and file_type = #{fileType}</if>
            <if test="sortNum != null "> and sort_num = #{sortNum}</if>
            <if test="isDelete != null  and isDelete != ''"> and is_delete = #{isDelete}</if>
        </where>
    </select>
    
    <select id="selectWornDeclareFileById" parameterType="Long" resultMap="WornDeclareFileResult">
        <include refid="selectWornDeclareFileVo"/>
        where id = #{id}
    </select>

    <insert id="insertWornDeclareFile" parameterType="WornDeclareFile" useGeneratedKeys="true" keyProperty="id">
        insert into worn_declare_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="billId != null">bill_id,</if>
            <if test="billNo != null and billNo != ''">bill_no,</if>
            <if test="fileName != null and fileName != ''">file_name,</if>
            <if test="fileUrl != null and fileUrl != ''">file_url,</if>
            <if test="fileType != null and fileType != ''">file_type,</if>
            <if test="sortNum != null">sort_num,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDelete != null and isDelete != ''">is_delete,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="billId != null">#{billId},</if>
            <if test="billNo != null and billNo != ''">#{billNo},</if>
            <if test="fileName != null and fileName != ''">#{fileName},</if>
            <if test="fileUrl != null and fileUrl != ''">#{fileUrl},</if>
            <if test="fileType != null and fileType != ''">#{fileType},</if>
            <if test="sortNum != null">#{sortNum},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDelete != null and isDelete != ''">#{isDelete},</if>
        </trim>
    </insert>


    <update id="updateWornDeclareFile" parameterType="WornDeclareFile">
        update worn_declare_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="billId != null">bill_id = #{billId},</if>
            <if test="billNo != null and billNo != ''">bill_no = #{billNo},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="fileType != null">file_type = #{fileType},</if>
            <if test="sortNum != null">sort_num = #{sortNum},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWornDeclareFileById" parameterType="Long">
        delete from worn_declare_file where id = #{id}
    </delete>

    <delete id="deleteWornDeclareFileByIds" parameterType="String">
        delete from worn_declare_file where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectWornDeclareFileByBillId" parameterType="Long" resultMap="WornDeclareFileResult">
        select id,
               bill_id,
               bill_no,
               file_name,
               file_url,
               file_type,
               sort_num,
               remark,
               create_by,
               create_time,
               is_delete
        from worn_declare_file
        where bill_id = #{billId}
          and is_delete = '0'
        order by sort_num asc, id asc
    </select>
    <update id="deleteWornDeclareFileByBillId" parameterType="Long">
        update worn_declare_file
        set is_delete = '1'
        where bill_id = #{billId}
          and is_delete = '0'
    </update>
</mapper>