<?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.github.tangyi.user.mapper.AttachmentMapper">
    <resultMap id="attachmentResultMap" type="com.github.tangyi.user.api.module.Attachment">
        <id column="id" property="id"/>
        <result column="attach_name" property="attachName"/>
        <result column="attach_size" property="attachSize"/>
        <result column="group_name" property="groupName"/>
        <result column="fast_file_id" property="fastFileId"/>
        <result column="busi_id" property="busiId"/>
        <result column="busi_module" property="busiModule"/>
        <result column="busi_type" property="busiType"/>
        <result column="creator" property="creator"/>
        <result column="create_date" property="createDate"/>
        <result column="modifier" property="modifier"/>
        <result column="modify_date" property="modifyDate"/>
        <result column="del_flag" property="delFlag"/>
        <result column="application_code" property="applicationCode"/>
        <result column="tenant_code" property="tenantCode"/>
    </resultMap>

    <sql id="attachmentColumns">
        a.id,
        a.attach_name,
        a.attach_size,
        a.group_name,
        a.fast_file_id,
        a.busi_id,
        a.busi_module,
        a.busi_type,
        a.creator,
        a.create_date,
        a.modifier,
        a.modify_date,
        a.del_flag,
        a.application_code,
        a.tenant_code
    </sql>

    <!-- where 条件 -->
    <sql id="whereColumnList">
        <if test="attachName != null and attachName != ''">
            and a.attach_name like #{attachName}
        </if>
        <if test="busiType != null and busiType != ''">
            and a.busi_type like #{busiType}
        </if>
        <if test="busiModule != null and busiModule != ''">
            and a.busi_module like #{busiModule}
        </if>
        <if test="tenantCode != null and tenantCode != ''">
            and a.tenant_code = #{tenantCode}
        </if>
    </sql>

    <select id="get" resultMap="attachmentResultMap">
        SELECT
        <include refid="attachmentColumns"/>
        FROM sys_attachment a
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <select id="findList" resultMap="attachmentResultMap">
        SELECT
        <include refid="attachmentColumns"/>
        FROM sys_attachment a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
        ORDER BY CREATE_DATE DESC
    </select>

    <select id="findListById" resultMap="attachmentResultMap">
        SELECT
        <include refid="attachmentColumns"/>
        FROM sys_attachment a
        WHERE a.id IN
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <insert id="insert">
        INSERT  INTO sys_attachment (
            id,
            attach_name,
            attach_size,
            group_name,
            fast_file_id,
            busi_id,
            busi_module,
            busi_type,
            creator,
            create_date,
            modifier,
            modify_date,
            del_flag,
            application_code,
            tenant_code
        ) VALUES (
            #{id},
            #{attachName},
            #{attachSize},
            #{groupName},
            #{fastFileId},
            #{busiId},
            #{busiModule},
            #{busiType},
            #{creator},
            #{createDate},
            #{modifier},
            #{modifyDate},
            #{delFlag},
            #{applicationCode},
            #{tenantCode}
        )
    </insert>

    <update id="update">
        UPDATE sys_attachment SET
        <if test="attachName != null">
            attach_name = #{attachName},
        </if>
        <if test="attachSize != null">
            attach_size = #{attachSize},
        </if>
        <if test="groupName != null">
            group_name = #{groupName},
        </if>
        <if test="fastFileId != null">
            fast_file_id = #{fastFileId},
        </if>
        <if test="busiId != null">
            busi_id = #{busiId},
        </if>
        <if test="busiModule != null">
            busi_module = #{busiModule},
        </if>
        <if test="busiType != null">
            busi_type = #{busiType},
        </if>
        <if test="delFlag != null">
            del_flag = #{delFlag},
        </if>
        modifier = #{modifier},
        modify_date = #{modifyDate}
        WHERE id = #{id}
    </update>

    <delete id="delete">
        UPDATE sys_attachment SET
        DEL_FLAG = 1
        WHERE ID = #{id}
    </delete>

    <delete id="deleteAll">
        UPDATE sys_attachment SET
        del_flag = 1
        WHERE id in
        <foreach item="item" index="index" collection="array" open="("
                 separator="," close=")"> #{item}
        </foreach>
    </delete>
</mapper>
