<?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.gz.mapper.welcome.StatisticsMapper">


    <select id="selectWaitHandleRecord" resultType="com.gz.rvo.welcome.BorrowRecordRVO">
        <![CDATA[
            SELECT
                ab.name admin_name,
                ab.create_time,
                a.tm,
                a.dh
            FROM
                `tab_archive_borrow` ab
                    LEFT JOIN tab_archives a
                              ON a.id = ab.archive_id
            WHERE
                audit_status = 0
                AND ab.deleted = 0
                AND a.deleted = 0
        ]]>
    </select>


    <select id="selectWaitReturnRecord" resultType="com.gz.rvo.welcome.BorrowRecordRVO">
        <![CDATA[
        SELECT
            ab.name admin_name,
            a.tm,
            ab.estimate_return_time,
            a.dh
        FROM
            `tab_archive_borrow` ab
                LEFT JOIN tab_archives a ON a.id = ab.archive_id
        WHERE
            ab.audit_status = 2
          AND ab.return_status = 0
          AND ab.borrow_type = 1
          AND ab.deleted = 0
          AND a.deleted = 0
        ]]>
    </select>

    <select id="selectArchiveByYear" resultType="com.gz.rvo.welcome.ArchiveCountRVO">
        select gdnd title,count(id) count from tab_archives where deleted = 0 group by gdnd
    </select>


    <select id="selectArchiveByMl" resultType="com.gz.rvo.welcome.ArchiveCountRVO">
        select ml title,count(id) count from tab_archives where deleted = 0 group by ml
    </select>

    <select id="selectMyWaitReturnRecord" resultType="com.gz.rvo.welcome.BorrowRecordRVO">
        <![CDATA[
        SELECT
            ad.admin_name,
            a.tm,
            ab.estimate_return_time,
            a.dh
        FROM
            `tab_archive_borrow` ab
                LEFT JOIN tab_admin ad ON ab.created = ad.id
                LEFT JOIN tab_archives a ON a.id = ab.archive_id
        WHERE
            ab.audit_status = 2
          AND ab.return_status = 0
          AND ab.borrow_type = 1
          AND ab.deleted = 0
          AND a.deleted = 0
          AND ad.deleted = 0
          AND ab.created = #{userId}
          AND ab.estimate_return_time <= now()
        ]]>
    </select>

    <select id="selectMyDueSoonRecord" resultType="com.gz.rvo.welcome.BorrowRecordRVO">
        <![CDATA[
        SELECT
            ad.admin_name,
            a.tm,
            ab.estimate_return_time,
            a.dh,
            DATEDIFF( estimate_return_time, now( ) ) surplus_date
        FROM
            tab_archive_borrow ab
                LEFT JOIN tab_admin ad ON ab.created = ad.id
                LEFT JOIN tab_archives a ON a.id = ab.archive_id
        WHERE
            estimate_return_time > now()
          AND ab.borrow_type = 1 -- 原件借阅
          AND	ab.audit_status = 2 -- 审核通过
          AND ab.return_status = 0 -- 未归还
          AND DATEDIFF( estimate_return_time, now( ) ) < 3 -- 剩余时间小于3天
          AND ab.deleted = 0
          AND a.deleted = 0
          AND ad.deleted = 0
          AND ab.created = #{userId}
        ]]>
    </select>


    <select id="selectMyHistoryBorrowCount" resultType="string">
        select count(id) from tab_archive_borrow where created = #{userId} and deleted = 0
    </select>


    <select id="selectRuditNotHandleCount" resultType="string">
        select count(id) from tab_archive_borrow where created = #{userId} and deleted = 0 and audit_status = 0
    </select>

    <select id="selectBorrowCountByDate" resultType="com.gz.rvo.borrow.ArchiveBorrowHistoryRVO" >
        SELECT ab.id,
               a.dh,
               a.tm,
               if(art.parent_id = -1,art.code,(select tart.code from tab_archives_tree tart where tart.id = art.parent_id)) code,
               art.parent_id,
               ab.create_time,
               ab.borrow_type,
               ab.return_time,
               ab.return_status,
               ab.estimate_return_time,
               ab.audit_status,
               ad.admin_name
        FROM tab_archive_borrow ab
                 LEFT JOIN
             tab_archives a
             ON ab.archive_id = a.id
                 left join
             tab_admin ad
             on ad.id = ab.admin_id
                 left join tab_archives_tree art
                           on a.ml = art.code
        where ab.deleted = 0
          and a.deleted = 0
          and ad.deleted = 0
          and ab.create_time > DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
        order by ab.create_time asc
    </select>

</mapper>
