<?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.keyware.shandan.bianmu.mapper.AnalysisMapper">
    <!-- 近12个月数据资源关联情况统计 -->
    <select id="countsBy12Months" resultType="java.util.Map">
        SELECT "month_array"."month" AS "months",
               count("dir"."ID")      AS "counts"
        FROM (SELECT "ID"                              AS "ID",
                     TO_CHAR("CREATE_TIME", 'YYYY-MM') AS "create_month"
              FROM "B_DIRECTORY"
              WHERE "CREATE_TIME" >= ADD_MONTHS(SYSDATE(), -11)
                AND "DIRECTORY_TYPE" IN ('FILE', 'METADATA')) AS "dir"
         RIGHT JOIN (SELECT COLUMN_VALUE AS "month" FROM ARRAY NEW VARCHAR2[12] {
                TO_CHAR(ADD_MONTHS(SYSDATE(), 0), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -1), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -2), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -3), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -4), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -5), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -6), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -7), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -8), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -9), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -10), 'YYYY-MM'),
                TO_CHAR(ADD_MONTHS(SYSDATE(), -11), 'YYYY-MM')
        }) AS "month_array"
        ON "month_array"."month" = "dir"."create_month"
        GROUP BY "month_array"."month"
        ORDER BY "month_array"."month" ASC
    </select>

    <!-- 统计指定部门下关联的各类型资源数量 -->
    <select id="countsByResourceType" resultType="java.util.Map">
        SELECT
            CASE WHEN F.FILE_TYPE IS NULL THEN '数据库表'
                 WHEN F.FILE_TYPE = '' THEN '其他文件'
                 ELSE F.FILE_TYPE END AS "type",
            COUNT(F.ID) AS "counts"
        FROM (SELECT *
              FROM B_DIRECTORY AS D
              WHERE D.DIRECTORY_TYPE IN ('METADATA', 'FILE')
                AND D.RESOURCE_ID IS NOT NULL
                AND EXISTS(SELECT * FROM SYS_ORG AS O WHERE O.ID = D.ORG_ID AND INSTR(O.ORG_PATH, #{orgId}) > 0)) AS D2
         LEFT JOIN SYS_FILE AS F ON F.ID = D2.RESOURCE_ID
        GROUP BY F.FILE_TYPE
        ORDER BY "counts" DESC
    </select>

    <!-- 统计各类软连接数量 -->
    <select id="countsByDirectoryLink" resultType="java.util.Map">
        SELECT D.DIRECTORY_TYPE AS "type", COUNT(ID) AS "counts"
        FROM B_DIRECTORY AS D
        WHERE DIRECTORY_TYPE IN ('LINK_DIR', 'LINK_FILE', 'LINK_METADATA')
          AND EXISTS(SELECT * FROM SYS_ORG AS O WHERE O.ID = D.ORG_ID AND INSTR(O.ORG_PATH, #{orgId}) > 0)
        GROUP BY DIRECTORY_TYPE;
    </select>

    <!-- 统计部门关联资源的排行情况 -->
    <select id="countsByOrgResourceNumber" resultType="java.util.Map">
        SELECT O3.PNAME AS "name", COUNT(D.ID) AS "counts"
        FROM (SELECT P.ID AS PID, P.ORG_NAME AS PNAME, O2.ID
              FROM (SELECT O.ID, O.ORG_NAME, O.ORG_PARENT_ID, O.ORG_PATH || '|' AS ORG_PATH
                    FROM SYS_ORG AS O
                    WHERE O.ORG_PARENT_ID = #{orgId}) P
                       LEFT JOIN SYS_ORG AS O2 ON O2.ORG_PATH || '|' LIKE P.ORG_PATH || '%') AS O3
         LEFT JOIN (SELECT * FROM B_DIRECTORY WHERE DIRECTORY_TYPE IN ('FILE', 'METADATA')) AS D ON O3.ID = D.ORG_ID
        GROUP BY O3.PNAME
        ORDER BY "counts" DESC;
    </select>

    <select id="countsByUserResourceNumber" resultType="java.util.Map">
        SELECT U2.USER_NAME AS "name", COUNT(D.ID) AS "counts"
        FROM (SELECT DISTINCT U.USER_ID, U.USER_NAME, U.ORG_ID
              FROM SYS_USER AS U
               RIGHT JOIN (SELECT P.ID AS PID, P.ORG_NAME AS PNAME, O2.ID
                           FROM (SELECT O.ID, O.ORG_NAME, O.ORG_PARENT_ID, O.ORG_PATH || '|' AS ORG_PATH
                                 FROM SYS_ORG AS O
                                 WHERE O.ORG_PARENT_ID = #{orgId} OR O.ID = #{orgId}) P
                                    LEFT JOIN SYS_ORG AS O2 ON O2.ORG_PATH || '|' LIKE P.ORG_PATH || '%') AS O3
              ON O3.ID = U.ORG_ID
              WHERE U.IS_DELETE = 0) AS U2
         LEFT JOIN (SELECT * FROM B_DIRECTORY WHERE DIRECTORY_TYPE IN ('FILE', 'METADATA')) AS D
        ON U2.USER_ID = D.CREATE_USER
        GROUP BY U2.USER_NAME
        ORDER BY counts DESC LIMIT 10
    </select>

</mapper>
