<?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.ktg.iscs.mapper.StatisticsMapper">
    <cache/>
    <select id="getCabinetStatistics" resultType="com.ktg.iscs.domain.vo.statistics.CabinetStatisticsVO">
        SELECT
        c.cabinet_id,
        c.cabinet_name,
        w.workstation_name,
        ( CASE WHEN op.open_count IS NULL THEN 0 ELSE op.open_count END ) AS open_count,
        ( CASE WHEN ex.openex_count IS NULL THEN 0 ELSE ex.openex_count END ) AS open_timeout_count,
        ( CASE WHEN lex.loanex_count IS NULL THEN 0 ELSE lex.loanex_count END ) AS misplace_count
        FROM
        is_materials_cabinet c
        LEFT JOIN is_workstation w ON w.workstation_id = c.workstation_id
        LEFT JOIN ( SELECT o.cabinet_id, count( o.record_id ) AS open_count FROM is_cabinet_open_record o
        <where>
            <if test="startTime != null and startTime.trim != ''">
                and o.open_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and o.open_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY o.cabinet_id ) op ON op.cabinet_id = c.cabinet_id
        LEFT JOIN (SELECT e.loan_from_id, count( e.misplace_id ) AS openex_count FROM is_exception_misplace e
        <where>
            e.exception_type = '1'
            <if test="startTime != null and startTime.trim != ''">
                and e.occur_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and e.occur_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY e.loan_from_id ) ex ON ex.loan_from_id = c.cabinet_id
        LEFT JOIN (SELECT e.loan_from_id, count( e.misplace_id ) AS loanex_count FROM is_exception_misplace e
        <where>
            e.exception_type = '0'
            <if test="startTime != null and startTime.trim != ''">
                and e.occur_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and e.occur_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY e.loan_from_id ) lex ON lex.loan_from_id = c.cabinet_id
        order by c.cabinet_id asc
    </select>

    <select id="getMaterialsStatusStatistics"
            resultType="com.ktg.iscs.domain.vo.statistics.MaterialsStatusStatisticsVO">
        SELECT t.materials_type_id,
               t.materials_type_name,
               (CASE WHEN a.all_count IS NULL THEN 0 ELSE a.all_count END)                 AS all_count,
               (CASE WHEN b.will_expire_count IS NULL THEN 0 ELSE b.will_expire_count END) AS will_expire_count,
               (CASE WHEN c.expired_count IS NULL THEN 0 ELSE c.expired_count END)         AS expired_count,
               (CASE WHEN d.bad_count IS NULL THEN 0 ELSE d.bad_count END)                 AS bad_count
        FROM is_materials_type t
                 LEFT JOIN (SELECT m.materials_type_id, count(m.materials_id) AS all_count
                            FROM is_materials m
                            WHERE m.materials_cabinet_id is not null
                            GROUP BY m.materials_type_id) a ON a.materials_type_id = t.materials_type_id
                 LEFT JOIN (SELECT m.materials_type_id, count(m.materials_id) AS will_expire_count
                            FROM is_materials m
                            WHERE m.materials_cabinet_id is not null
                              and DATEADD(DAY, 30, GETDATE()) &gt; m.expiration_date
                              AND GETDATE() &lt; m.expiration_date
                            GROUP BY m.materials_type_id) b ON b.materials_type_id = t.materials_type_id
                 LEFT JOIN (SELECT m.materials_type_id, count(m.materials_id) AS expired_count
                            FROM is_materials m
                            WHERE m.materials_cabinet_id is not null
                              and m.STATUS = '2'
                            GROUP BY m.materials_type_id) c ON c.materials_type_id = t.materials_type_id
                 LEFT JOIN (SELECT m.materials_type_id, count(m.materials_id) AS bad_count
                            FROM is_materials m
                            WHERE m.materials_cabinet_id is not null
                              and m.STATUS = '1'
                            GROUP BY m.materials_type_id) d ON d.materials_type_id = t.materials_type_id
        order by t.materials_type_id asc
    </select>

    <select id="getMaterialsLoanStatistics"
            resultType="com.ktg.iscs.domain.vo.statistics.MaterialsLoanStatisticsVO">
        SELECT
        t.materials_type_id,
        t.materials_type_name,
        ( CASE WHEN a.all_count IS NULL THEN 0 ELSE a.all_count END ) AS all_count,
        ( CASE WHEN b.return_count IS NULL THEN 0 ELSE b.return_count END ) AS return_count,
        ( CASE WHEN c.timeout_count IS NULL THEN 0 ELSE c.timeout_count END ) AS timeout_count,
        ( CASE WHEN d.average_time IS NULL THEN 0 ELSE d.average_time END ) AS average_time
        FROM
        is_materials_type t

        LEFT JOIN ( SELECT ma.materials_type_id, count( m.materials_id ) AS all_count FROM
        is_materials_loan m LEFT JOIN is_materials ma on ma.materials_id = m.materials_id
        <where>
            <if test="startTime != null and startTime.trim != ''">
                and m.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and m.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY ma.materials_type_id ) a ON a.materials_type_id = t.materials_type_id

        LEFT JOIN ( SELECT ma.materials_type_id, count( m.materials_id ) AS return_count FROM
        is_materials_loan m LEFT JOIN is_materials ma on ma.materials_id = m.materials_id
        <where>
            m.actual_restitution_time &lt;= m.reminder_time
            <if test="startTime != null and startTime.trim != ''">
                and m.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and m.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY ma.materials_type_id ) b ON b.materials_type_id = t.materials_type_id

        LEFT JOIN ( SELECT ma.materials_type_id, count( m.materials_id ) AS timeout_count FROM
        is_materials_loan m LEFT JOIN is_materials ma on ma.materials_id = m.materials_id
        <where>
            m.actual_restitution_time &gt; m.reminder_time
            <if test="startTime != null and startTime.trim != ''">
                and m.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and m.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY ma.materials_type_id ) c ON c.materials_type_id = t.materials_type_id

        LEFT JOIN ( SELECT ma.materials_type_id, AVG(DATEDIFF(SECOND, m.loan_time, m.actual_restitution_time)) AS
        average_time FROM
        is_materials_loan m LEFT JOIN is_materials ma on ma.materials_id = m.materials_id
        <where>
            <if test="startTime != null and startTime.trim != ''">
                and m.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and m.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY ma.materials_type_id ) d ON d.materials_type_id = t.materials_type_id
        order by t.materials_type_id asc
    </select>
    <select id="getMaterialsChangeStatistics"
            resultType="com.ktg.iscs.domain.vo.statistics.MaterialsChangeStatisticsVO">
        SELECT
        t.materials_type_id,
        t.materials_type_name,
        ( CASE WHEN a.all_count IS NULL THEN 0 ELSE a.all_count END ) AS all_count,
        ( CASE WHEN b.expire_count IS NULL THEN 0 ELSE b.expire_count END ) AS expire_count,
        ( CASE WHEN c.bad_count IS NULL THEN 0 ELSE c.bad_count END ) AS bad_count
        FROM
        is_materials_type t
        LEFT JOIN ( SELECT ma.materials_type_id, count( m.old_materials_id ) AS all_count FROM
        is_materials_change_record m
        LEFT JOIN is_materials ma on ma.materials_id = m.old_materials_id
        <where>
            (m.check_record_id = null or m.check_record_id = 0)
            <if test="startTime != null and startTime.trim != ''">
                and m.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and m.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY ma.materials_type_id ) a ON a.materials_type_id = t.materials_type_id

        LEFT JOIN (
        SELECT ma.materials_type_id, count( m.old_materials_id ) AS expire_count FROM
        is_materials_change_record m
        LEFT JOIN is_materials ma on ma.materials_id = m.old_materials_id
        LEFT JOIN is_materials_check_record mc on mc.record_id = m.check_record_id
        <where>
            mc.reason = '2'
            <if test="startTime != null and startTime.trim != ''">
                and m.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and m.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY ma.materials_type_id ) b ON b.materials_type_id = t.materials_type_id

        LEFT JOIN (
        SELECT ma.materials_type_id, count( m.old_materials_id ) AS bad_count FROM
        is_materials_change_record m
        LEFT JOIN is_materials ma on ma.materials_id = m.old_materials_id
        LEFT JOIN is_materials_check_record mc on mc.record_id = m.check_record_id
        <where>
            mc.reason = '1'
            <if test="startTime != null and startTime.trim != ''">
                and m.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and m.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY ma.materials_type_id ) c ON c.materials_type_id = t.materials_type_id
        order by t.materials_type_id asc
    </select>
    <select id="getUserLoanStatistics" resultType="com.ktg.iscs.domain.vo.statistics.UserLoanStatisticsVO">
        SELECT a.user_id, a.user_name, a.nick_name,
        ( CASE WHEN b.all_count IS NULL THEN 0 ELSE b.all_count END ) AS all_count,
        ( CASE WHEN c.normal_count IS NULL THEN 0 ELSE c.normal_count END ) AS normal_count,
        ( CASE WHEN d.timeout_count IS NULL THEN 0 ELSE d.timeout_count END ) AS timeout_count
        FROM
        (SELECT
        u.user_id,
        u.user_name,
        u.nick_name
        FROM is_materials_loan l LEFT JOIN sys_user u ON u.user_id = l.loan_user_id GROUP BY u.user_id) a
        LEFT JOIN (SELECT l.loan_user_id, count(l.materials_loan_id) as all_count from is_materials_loan l
        <where>
            <if test="startTime != null and startTime.trim != ''">
                and l.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and l.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY l.loan_user_id) b on b.loan_user_id = a.user_id
        LEFT JOIN (SELECT l.loan_user_id, count(l.materials_loan_id) as normal_count from is_materials_loan l
        <where>
            l.actual_restitution_time &lt;= l.reminder_time
            <if test="startTime != null and startTime.trim != ''">
                and l.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and l.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY l.loan_user_id) c on b.loan_user_id = a.user_id
        LEFT JOIN (SELECT l.loan_user_id, count(l.materials_loan_id) as timeout_count from is_materials_loan l
        <where>
            l.actual_restitution_time &gt; l.reminder_time
            <if test="startTime != null and startTime.trim != ''">
                and l.create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime.trim != ''">
                and l.create_time &lt;= #{endTime}
            </if>
        </where>
        GROUP BY l.loan_user_id) d on b.loan_user_id = a.user_id
    </select>
    <select id="getDayLoanStatistics" resultType="com.ktg.iscs.domain.vo.statistics.DayLoanStatisticsVO">
        select FORMAT(a.date, 'MM/dd') as day,
        ( CASE WHEN b.all_count  IS NULL THEN 0 ELSE b.all_count  END ) AS all_count,
        ( CASE WHEN c.return_count  IS NULL THEN 0 ELSE c.return_count  END ) AS return_count,
        ( CASE WHEN d.timeout_count  IS NULL THEN 0 ELSE d.timeout_count  END ) AS timeout_count
        FROM (
            select a.Date from (
            select DATEADD(DAY, -(a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a)), CURRENT_TIMESTAMP) as Date
            from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
            cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
            cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
            cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as d
            ) a where a.Date between #{startTime} and #{endTime}) a
            left join (select count (l.materials_loan_id) as all_count, FORMAT(l.loan_time, 'yyyy-MM-dd') as date from is_materials_loan l group by FORMAT(l.loan_time, 'yyyy-MM-dd')) b on b.date = FORMAT(a.Date, 'yyyy-MM-dd')
            left join (select count (l.materials_loan_id) as return_count, FORMAT(l.loan_time, 'yyyy-MM-dd') as date from is_materials_loan l where FORMAT(l.loan_time, 'yyyy-MM-dd') between #{startTime} and #{endTime} and l.actual_restitution_time &lt;= l.reminder_time group by FORMAT(l.loan_time, 'yyyy-MM-dd')) c on c.date = FORMAT(a.Date, 'yyyy-MM-dd')
            left join (select count (l.materials_loan_id) as timeout_count, FORMAT(l.loan_time, 'yyyy-MM-dd') as date from is_materials_loan l where FORMAT(l.loan_time, 'yyyy-MM-dd') between #{startTime} and #{endTime} and l.actual_restitution_time &gt; l.reminder_time group by FORMAT(l.loan_time, 'yyyy-MM-dd')) d on d.date = FORMAT(a.Date, 'yyyy-MM-dd')
        ORDER BY a.date DESC
    </select>
    <select id="getOngoingJobList" resultType="com.ktg.iscs.domain.vo.homePage.OngoingJobVO">
        SELECT j.ticket_id,
               j.ticket_name,
               j.workstation_id,
               w.workstation_name,
               j.machinery_id,
               ma.machinery_name,
               j.ticket_type,
               j.ticket_start_time
        FROM is_job_ticket j
                 LEFT JOIN is_workstation w ON w.workstation_id = j.workstation_id
                 LEFT JOIN is_machinery ma ON ma.machinery_id = j.machinery_id
        ORDER BY j.ticket_id DESC
    </select>
    <select id="getLoanList" resultType="com.ktg.iscs.domain.vo.homePage.HomePageLoanVO">
        select FORMAT(a.date, 'MM/dd') as day,
        ( CASE WHEN b.loan_count  IS NULL THEN 0 ELSE b.loan_count  END ) AS loan_count,
        ( CASE WHEN c.return_count  IS NULL THEN 0 ELSE c.return_count  END ) AS return_count
        FROM (
            select a.Date from (
            select DATEADD(DAY, -(a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a)), CURRENT_TIMESTAMP) as Date
            from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
            cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
            cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
            cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as d
            ) a where a.Date between #{startTime} and #{endTime}) a
            left join (select count(l.materials_loan_id) as loan_count, FORMAT(l.loan_time, 'yyyy-MM-dd') as date from is_materials_loan l where FORMAT(l.loan_time, 'yyyy-MM-dd') between #{startTime} and #{endTime} group by FORMAT(l.loan_time, 'yyyy-MM-dd')) b on b.date = FORMAT(a.Date, 'yyyy-MM-dd')
            left join (select count(l.materials_loan_id) as return_count, FORMAT(l.loan_time, 'yyyy-MM-dd') as date from is_materials_loan l where FORMAT(l.loan_time, 'yyyy-MM-dd') between #{startTime} and #{endTime} and l.status = '1' group by FORMAT(l.loan_time, 'yyyy-MM-dd')) c on c.date = FORMAT(a.Date, 'yyyy-MM-dd')
        ORDER BY a.date DESC

    </select>

</mapper>
