<?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.jeethink.using.mapper.BorrowMapper">
    <insert id="insertBorrow" parameterType="ArchivesBorrow">
        insert into archives_borrow (borrow_id, archives_id, borrow_status, reback_time, create_by, create_time, borrow_reason)
        VALUES (#{borrowId}, #{archivesId}, #{borrowStatus}, #{rebackTime}, #{createBy}, now(), #{borrowReason})
    </insert>
    <update id="removeBorrow">
        update archives_borrow set deleted = '1' where borrow_id = #{0}
    </update>
    <update id="changeBorrowStatus">
        update archives_borrow
        <set>
            borrow_status = #{borrowStatus},
            <if test="borrowStatus=='12'">real_reback_time = now()</if>
        </set>
        where deleted = '0' and borrow_id = #{borrowId}
    </update>
    <sql id="selectBorrow">
        select b.borrow_id, b.archives_id, a.name as archives_name, a.archive_no, a.classified, a.tier, b.borrow_status, b.reback_time, b.real_reback_time, b.borrow_reason,
               b.create_by, u.nick_name as create_by_name, b.create_time, a.name as archives_name
        from archives_borrow b
        left join archives_master a on b.archives_id = a.archives_id
        left join sys_user u on b.create_by = u.user_name
        where a.deleted = '0'
    </sql>
    <select id="selectBorrowingList" parameterType="ArchivesBorrow" resultType="ArchivesBorrow">
        <include refid="selectBorrow"/>
        and b.borrow_status != '12' and b.borrow_status != '9'
        <if test="createBy != null and createBy != ''"> and b.create_by = #{createBy} </if>
        <if test="archivesId != null and archivesId != ''"> and b.archives_id = #{archivesId} </if>
        order by b.create_time desc
    </select>
    <select id="selectBorrowHistory" resultType="ArchivesBorrow">
        <include refid="selectBorrow"/>
        <if test="archivesId != null and archivesId != ''"> and b.archives_id = #{archivesId} </if>
        <if test="borrowStatus != null and borrowStatus != ''"> and b.borrow_status = #{borrowStatus} </if>
        <if test="archivesName != null and archivesName != ''"> and a.name like concat('%', #{archivesName}, '%') </if>
        order by a.create_time desc
    </select>
    <select id="selectById" resultType="ArchivesBorrow">
        <include refid="selectBorrow"/>
        and b.borrow_id = #{0}
    </select>
    <select id="selectMyBorrowList" resultType="ArchivesBorrow">
        <include refid="selectBorrow"/>
        and b.create_by = #{createBy}
        <if test="archivesId != null and archivesId != ''"> and b.archives_id = #{archivesId} </if>
        <if test="borrowStatus != null and borrowStatus != ''"> and b.borrow_status = #{borrowStatus} </if>
        <if test="archivesName != null and archivesName != ''"> and a.name like concat('%', #{archivesName}, '%') </if>
        <if test="archiveNo != null  and archiveNo != ''"> and archive_no like concat('%', #{archiveNo}, '%')</if>
        order by b.create_time desc
    </select>
    <select id="selectStatusBorrowList" resultType="ArchivesBorrow">
        <include refid="selectBorrow"/>
        <if test="statusList != null and statusList.size() > 0">
            and b.borrow_status in <foreach collection="statusList" separator="," open="(" close=")" item="status">#{status}</foreach>
        </if>
        <if test="borrow.archivesId != null and borrow.archivesId != ''"> and b.archives_id = #{borrow.archivesId} </if>
        <if test="borrow.archivesName != null and borrow.archivesName != ''"> and a.name like concat('%', #{borrow.archivesName}, '%') </if>
        order by a.create_time desc
    </select>
    <select id="selectArchivesList" resultType="ArchivesMaster">
        select a.archives_id, a.name, a.archive_no, a.type, a.people_type, a.tier, a.classified, a.duration, a.create_time,
               a.if_paper, a.gd_people_id, a.user_id, u.nick_name, if(b.archives_id is not null, true, false) as is_borrowed
        from archives_master a
        left join sys_user u on a.user_id = u.user_id
        left join (
            select archives_id from archives_borrow where deleted = '0' and borrow_status != '12' and borrow_status != '9' and create_by = #{createBy}
        ) b on a.archives_id = b.archives_id
        where a.deleted = '0' and a.now_status = '10' and a.type='0'
        <if test="name != null  and name != ''"> and a.name like concat('%', #{name}, '%')</if>
        <if test="archiveNo != null  and archiveNo != ''"> and a.archive_no like concat('%', #{archiveNo}, '%')</if>
        <if test="peopleType != null  and peopleType != ''"> and a.people_type = #{peopleType}</if>
    </select>
</mapper>