<?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="cn.edu.whu.boot.statistics.mapper.CollectorMapper">

    <select id="getTaskId" resultType="Integer">
        SELECT id FROM task WHERE task_name = #{tskName}
    </select>

    <resultMap id="collectorMap" type="cn.edu.whu.boot.statistics.bean.Collector">
        <result column="task_id" property="taskId"></result>
        <result column="user_id" property="userId"></result>
        <result column="username" property="name"></result>
    </resultMap>

    <select id="getTaskWorkers" resultMap="collectorMap">
        SELECT a.task_id, a.user_id, b.username, b.phone, b.email
        FROM task_user_role a
        INNER JOIN user_staff b
        on a.user_id = b.ID
        WHERE task_id = #{taskId}
    </select>

    <select id="getCollectorRoute" resultType="cn.edu.whu.boot.statistics.bean.RouteLine">
        SELECT collector, route, begin_time, stop_time FROM ${tblName} WHERE collector = #{collector}
    </select>

    <select id="getCollectorWorkLoad" resultType="Integer">
        SELECT count(1) FROM ${tblName} WHERE collector = #{collector}
    </select>

    <select id="getCollectorWorkLoadDaily" resultType="HashMap">
        SELECT to_char("create_time", 'YYYY-MM-DD') as "date", "count"(1)
        FROM ${tblName} WHERE collector = #{collector}
        group by "date" order by "date"
    </select>

    <select id="statDaily" resultType="HashMap">
        SELECT to_char("create_time", 'YYYY-MM-DD') as "date", "count"(1) FROM ${tblName}
        where status = 1 or status = 2
        group by "date" order by "date"
    </select>

    <select id="statMonthly" resultType="HashMap">
        SELECT to_char("create_time", 'YYYY-MM') as "date", "count"(1) FROM ${tblName}
        where status = 1 or status = 2
        group by "date" order by "date"
    </select>

    <select id="statDailyRatio" resultType="HashMap">
        SELECT to_char("create_time", 'YYYY-MM-DD') as "date", (round("count"(1)  / (SELECT "count"(1) FROM ${tblName}) :: NUMERIC, 3)) ratio
        FROM ${tblName}
        group by "date" order by "date"
    </select>

    <select id="statMonthlyRatio" resultType="HashMap">
        SELECT to_char("create_time", 'YYYY-MM') as "date", (round("count"(1)  / (SELECT "count"(1) FROM ${tblName}) :: NUMERIC, 3)) ratio
        FROM ${tblName}
        group by "date" order by "date"
    </select>

    <select id="countWorkloadByReviewer" resultType="cn.edu.whu.boot.statistics.bean.ReviewerWorkload">
        SELECT reviewer                                     AS user,
               COUNT(*)                                     AS total_workload,
               SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END)  AS to_be_reviewed_data_count,
               SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END)  AS valid_data_count,
               SUM(CASE WHEN status = -1 THEN 1 ELSE 0 END) AS invalid_data_count
        FROM ${table}
        WHERE reviewer != -1
        GROUP BY reviewer
    </select>

    <select id="countWorkloadByCollector" resultType="cn.edu.whu.boot.statistics.bean.CollectorWorkload">
        SELECT collector AS user,
               COUNT(*) AS new_data_count
        FROM ${table}
        WHERE collector != -1
        GROUP BY collector
    </select>
</mapper>