<?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.statistics.SelectStatisticsMapper">


    <select id="selectArchiveStatistics" resultType="com.gz.rvo.statistics.ArchiveStatisticsRVO">
        SELECT atr.title, atr.id, atr.`code`, atr.parent_id
             , (
            SELECT count(id)
            FROM tab_archives a
            WHERE atr.`code` = a.ml
              AND a.deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                AND a.create_time BETWEEN #{startTime} AND #{endTime}
            </if>
        ) AS archive_count
             , (
            SELECT count(af.id)
            FROM tab_archives a
                     LEFT JOIN tab_archive_file af ON a.id = af.archive_id
            WHERE atr.`code` = a.ml
              AND a.deleted = 0
              AND af.deleted = 0
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                AND af.create_time BETWEEN #{startTime} AND #{endTime}
            </if>
        ) AS file_count
        FROM tab_archives_tree atr
        WHERE atr.deleted = 0
        <if test="mlCodes != null and mlCodes.size > 0">
            AND atr.code in
            <foreach collection="mlCodes" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectArchiveYearStatistics" resultType="com.gz.rvo.statistics.ArchiveYearStatisticsRVO">
        SELECT a1.gdnd
             , (
            SELECT count(a.id)
            FROM tab_archives a
            WHERE a1.`gdnd` = a.gdnd
              AND a.deleted = 0
        ) AS archive_count
             , (
            SELECT count(af.id)
            FROM tab_archives a
                     LEFT JOIN tab_archive_file af ON a.id = af.archive_id
            WHERE a1.`gdnd` = a.gdnd
              AND a.deleted = 0
              AND af.deleted = 0
        ) AS file_count
        FROM tab_archives a1
        <where>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                AND a1.gdnd BETWEEN #{startTime} AND #{endTime}
            </if>
            <if test="mlCodes != null and mlCodes.size > 0">
                AND a1.ml in
                <foreach collection="mlCodes" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY a1.gdnd
    </select>

    <select id="selectBorrowStatistics" resultType="com.gz.rvo.statistics.BorrowStatisticsRVO">
        SELECT (
            SELECT max(ab1.create_time)
            FROM tab_archive_borrow ab1
            WHERE ab.archive_id = ab1.archive_id
              AND ab1.deleted = 0
        ) AS create_time, ab.return_time, ab.estimate_return_time, a.tm, a.dh
             , IF(ata.parent_id = -1, ata.title, (
            SELECT title
            FROM tab_archives_tree ata1
            WHERE ata1.id = ata.parent_id
              AND ata1.deleted = 0
        )) AS ml
             , count(ab.id) AS borrow_count
        FROM tab_archive_borrow ab
                 LEFT JOIN tab_archives a ON ab.archive_id = a.id
                 LEFT JOIN tab_archives_tree ata ON a.ml = ata.CODE
        WHERE ab.deleted = 0
          AND a.deleted = 0
          AND ata.deleted = 0
          <if test="gjz != null and gjz != ''">
              and (a.dh like concat(concat('%',#{gjz}),'%') or a.tm like concat(concat('%',#{gjz}),'%'))
          </if>
          <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
              AND ab.create_time BETWEEN #{startTime} AND #{endTime}
          </if>
          <if test="mlCodes != null and mlCodes.size > 0">
              AND a.ml in
              <foreach collection="mlCodes" item="item" index="index" open="(" close=")" separator=",">
                  #{item}
              </foreach>
          </if>
        GROUP BY ab.archive_id
    </select>

</mapper>
