<?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.archives.mapper.ArchivesMapper">

    <select id="getTypeList" resultType="com.bonus.common.biz.domain.TreeNode">
        select info_id as id, archives_name as label, parent_id as parentId,
        level as level
        from archives_record_info
        where del_flag = '0'
        <if test="keyWord != null and keyWord != ''">
            and archives_name like concat('%', #{keyWord}, '%')
        </if>
    </select>

    <select id="selectByName" resultType="com.bonus.material.archives.domain.ArchivesInfo">
        select info_id       as infoId,
               archives_name as archivesName,
               parent_id     as parentId,
               level         as level
        from archives_record_info
        where archives_name = #{archivesName}
          and parent_id = #{parentId}
          and del_flag = '0'
    </select>

    <select id="getListById" resultType="com.bonus.material.archives.domain.ArchivesInfo">
        SELECT
            info_id as infoId,
            archives_name AS label,
            parent_id AS parentId,
            level AS level
        FROM
            archives_record_info
        WHERE
            del_flag = '0' and parent_id = #{infoId}
    </select>

    <select id="selectDetailsList" resultType="com.bonus.material.archives.domain.ArchivesDetails">
        select
        details_id as detailsId,
        info_id as infoId,
        parent_id as parentId,
        level as level,
        doc_name as docName,
        doc_type as docType,
        doc_url as docUrl,
        doc_size as docSize,
        type_id as typeId,
        YEAR(create_time) as year,
        create_by as createBy,
        create_time as createTime,
        update_by as updateBy,
        update_time as updateTime
        FROM
        archives_record_details
        WHERE
        del_flag = '0'
        <if test="infoId != null">
            and (info_id = #{infoId} and level = '1')
        </if>
        <if test="detailsId != null">
            and parent_id = #{detailsId}
        </if>
        <if test="keyWord != null  and keyWord != ''">
            and (
                doc_name like concat('%', #{keyWord}, '%')
                or doc_type like concat('%', #{keyWord}, '%')
                or create_by like concat('%', #{keyWord}, '%')
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( update_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
    </select>

    <select id="selectDetailsByName" resultType="com.bonus.material.archives.domain.ArchivesDetails">
        select
        details_id as detailsId,
        info_id as infoId,
        doc_name as docName,
        doc_type as docType,
        doc_url as docUrl,
        doc_size as docSize,
        type_id as typeId,
        YEAR(create_time) as year,
        create_by as createBy,
        create_time as createTime,
        update_by as updateBy,
        update_time as updateTime
        FROM
        archives_record_details
       WHERE
         del_flag = '0'
         and doc_name = #{docName}
        <if test="infoId != null">
            and info_id = #{infoId}
        </if>
        <if test="level != null">
            and level = #{level}
        </if>
        <if test="detailsId != null">
            and parent_id = #{detailsId}
        </if>
    </select>

    <select id="selectDetails" resultType="com.bonus.material.archives.domain.ArchivesDetails">
        select
        details_id as detailsId,
        info_id as infoId,
        parent_id as parentId,
        level as level,
        doc_name as docName,
        doc_type as docType,
        doc_url as docUrl,
        doc_size as docSize,
        type_id as typeId,
        YEAR(create_time) as year,
        create_by as createBy,
        create_time as createTime,
        update_by as updateBy,
        update_time as updateTime
        FROM
        archives_record_details
        WHERE
        del_flag = '0'
        and details_id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectDetailsUpdateName" resultType="com.bonus.material.archives.domain.ArchivesDetails">
        select
        details_id as detailsId,
        info_id as infoId,
        doc_name as docName,
        doc_type as docType,
        doc_url as docUrl,
        doc_size as docSize,
        type_id as typeId,
        YEAR(create_time) as year,
        create_by as createBy,
        create_time as createTime,
        update_by as updateBy,
        update_time as updateTime
        FROM
        archives_record_details
        WHERE
        del_flag = '0'
        and doc_name = #{docName}
        <if test="parentId != null">
            and parent_id = #{parentId}
        </if>
    </select>

    <select id="getSign" resultType="com.bonus.material.archives.domain.ElcSignatureInfo">
        select
        sign_url as signUrl,
        sign_type as signType,
        user_id as userId
        from sys_user
        where user_id = #{userId}
    </select>

    <insert id="insertInfo" useGeneratedKeys="true" keyProperty="infoId">
        insert into archives_record_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="archivesName != null and archivesName != ''">archives_name,</if>
            <if test="archivesValue != null and archivesValue != ''">archives_value,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="level != null and level != ''">level,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            del_flag
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="archivesName != null and archivesName != ''">#{archivesName},</if>
            <if test="archivesValue != null and archivesValue != ''">#{archivesValue},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="level != null and level != ''">#{level},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            0
        </trim>
    </insert>

    <insert id="insertDetails">
        insert into archives_record_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="infoId != null">info_id,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="level != null and level != ''">level,</if>
            <if test="docName != null and docName != ''">doc_name,</if>
            <if test="docType != null and docType != ''">doc_type,</if>
            <if test="docUrl != null and docUrl != ''">doc_url,</if>
            <if test="docSize != null and docSize != ''">doc_size,</if>
            <if test="typeId != null">type_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createTime != null">update_time,</if>
            del_flag
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="infoId != null">#{infoId},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="level != null and level != ''">#{level},</if>
            <if test="docName != null and docName != ''">#{docName},</if>
            <if test="docType != null and docType != ''">#{docType},</if>
            <if test="docUrl != null and docUrl != ''">#{docUrl},</if>
            <if test="docSize != null and docSize != ''">#{docSize},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createTime != null">#{createTime},</if>
            0
        </trim>
    </insert>

    <update id="updateInfo">
        update archives_record_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="archivesName != null and archivesName != ''">archives_name = #{archivesName},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="level != null and level != ''">level = #{level},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where info_id = #{infoId}
    </update>

    <update id="updateDetails">
        update archives_record_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="docName != null and docName != ''">doc_name = #{docName},</if>
            <if test="docType != null and docType != ''">doc_type = #{docType},</if>
            <if test="docUrl != null and docUrl != ''">doc_url = #{docUrl},</if>
            <if test="docSize != null and docSize != ''">doc_size = #{docSize},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where details_id = #{detailsId}
    </update>

    <update id="updateSign">
        update sys_user
        set sign_url = #{signUrl},
            sign_type = #{signType}
        where user_id = #{userId}
    </update>

    <delete id="deleteInfo">
        update archives_record_info
        set del_flag = '1'
        where info_id = #{infoId}
    </delete>

    <delete id="deleteDetails">
        update archives_record_details
        set del_flag = '1'
        where details_id = #{detailsId}
    </delete>

    <select id="selectInfoListByNameAndLevel" resultType="com.bonus.material.archives.domain.ArchivesInfo">
        select
            info_id       as infoId,
            archives_name as archivesName,
            parent_id     as parentId,
            level         as level
        from
            archives_record_info
        where
            archives_name = #{name}
            and level = #{level}
            and del_flag = '0'
        limit 1
    </select>

    <select id="selectDetailsNodeIdByDocNameAndInfoValue" resultType="java.lang.Integer">
        SELECT
            ard.details_id
        FROM
            archives_record_info ari
        LEFT JOIN
            archives_record_details ard ON ari.info_id = ard.info_id AND ard.`level` = '1'
        WHERE
            ari.`level` = #{level}
          AND ari.archives_value = #{value}
          AND ard.doc_name = #{name}
          AND ard.doc_type = '文件夹'
    </select>
</mapper>