<?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.hz.pm.api.todocenter.mapper.StatisticsMapper">
    <select id="getStatistics" parameterType="java.lang.String"
            resultType="com.hz.pm.api.todocenter.model.po.TodoCenterStatisticsPO">
        SELECT
            sum(CASE WHEN tol.todoNum IS NOT NULL AND tol.todoNum > 0 THEN 1 ELSE 0 end) todoNum,
            sum(CASE WHEN tol.idoNum IS NOT NULL AND tol.idoNum > 0 THEN 1 ELSE 0 end) idoNum,
            sum(CASE WHEN tol.mysubmitNum IS NOT NULL AND tol.mysubmitNum > 0 THEN 1 ELSE 0 end) mysubmitNum,
            sum(CASE WHEN tol.ccmeNum IS NOT NULL AND tol.ccmeNum > 0 THEN 1 ELSE 0 end) ccmeNum
        FROM (
                 SELECT
                     sum(CASE WHEN ht.assignee_ = #{employeeCode} AND ht.end_time_ is NULL THEN 1 end) todoNum,
                     sum(CASE WHEN ht.assignee_ = #{employeeCode} AND ht.end_time_ IS not NULL THEN 1 end) idoNum,
                     sum(CASE WHEN hp.start_user_id_ = #{employeeCode} THEN 1 end) mysubmitNum,
                     sum(CASE WHEN cc.user_id = #{employeeCode} THEN 1 end) ccmeNum
                 FROM
                     act_hi_procinst hp
                         LEFT JOIN
                     act_hi_taskinst ht ON hp.proc_inst_id_ = ht.proc_inst_id_
                         LEFT JOIN
                     wflow_cc_tasks cc ON cc.instance_id = hp.proc_inst_id_
                 GROUP BY hp.id_
             ) tol
    </select>

    <select id="getTodoOrIdo" parameterType="java.lang.String"
            resultType="com.hz.pm.api.todocenter.model.po.TodoCenterStatisticsPO">
                 SELECT
                     count(0) todoNum
                 FROM(SELECT
                          hp.ID_
                      FROM
                          act_hi_procinst hp
                              LEFT JOIN
                          act_hi_taskinst ht ON hp.PROC_INST_ID_  = ht.PROC_INST_ID_
                      WHERE (ht.assignee_ = #{employeeCode} AND ht.end_time_ is NULL
                          AND hp.PROC_INST_ID_ NOT IN (SELECT PROC_INST_ID_ FROM act_hi_procinst hp WHERE hp.business_status_ = 'back-end'))
                         OR (hp.start_user_id_ = #{employeeCode} and hp.business_status_ = 'back-end')
                      GROUP BY hp.ID_ )
    </select>

    <select id="getIdo" parameterType="java.lang.String"
            resultType="com.hz.pm.api.todocenter.model.po.TodoCenterStatisticsPO">
        SELECT count(0) idoNum
        FROM act_hi_taskinst ht WHERE
        ht.end_time_ IN (SELECT max(end_time_)  FROM act_hi_taskinst
        WHERE assignee_ = #{employeeCode} and end_time_ is not NULL GROUP BY PROC_INST_ID_)
    </select>

    <select id="mysubmitNum" parameterType="java.lang.String"
            resultType="com.hz.pm.api.todocenter.model.po.TodoCenterStatisticsPO">
        SELECT
            sum(CASE WHEN tol.mysubmitNum IS NOT NULL AND tol.mysubmitNum > 0 THEN 1 ELSE 0 end) mysubmitNum
        from
        (SELECT
            count(0) mysubmitNum
        FROM
            act_hi_procinst hp
        LEFT JOIN
            act_hi_taskinst ht ON hp.proc_inst_id_ = ht.proc_inst_id_
        where start_user_id_ = #{employeeCode}
        GROUP BY hp.id_) tol
    </select>


    <select id="ccNums" parameterType="java.lang.String"
            resultType="com.hz.pm.api.todocenter.model.po.TodoCenterStatisticsPO">
        SELECT
            count(0) ccmeNum
        FROM
            wflow_cc_tasks cc
        where cc.user_id = #{employeeCode}
    </select>
</mapper>
