<?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.ruoyi.system.mapper.SystemReportMapper">

    <resultMap type="UserLastWeekSumModel" id="UserLastWeekSumResult">
        <result property="userCount" column="user_count"/>
        <result property="reportedCount" column="reported_count"/>
        <result property="twoWeeksAgoAmount" column="two_weeks_ago_amount"/>
        <result property="costAmount" column="cost_amount"/>
    </resultMap>

    <resultMap id="ProjectSumResult" type="ProjectSumModel">
        <result property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
        <result property="costAmount" column="cost_amount"/>
        <result property="twoWeeksAgoAmount" column="two_weeks_ago_amount"/>
    </resultMap>

    <resultMap id="statusSumResult" type="IndexProjectStatusSumModel">
        <result property="statusCode" column="status"/>
        <result property="count" column="count"/>
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <id property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="loginName" column="login_name"/>
        <result property="userName" column="user_name"/>
        <result property="userType" column="user_type"/>
        <result property="email" column="email"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="sex" column="sex"/>
        <result property="avatar" column="avatar"/>
        <result property="password" column="password"/>
        <result property="salt" column="salt"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="loginIp" column="login_ip"/>
        <result property="loginDate" column="login_date"/>
        <result property="pwdUpdateDate" column="pwd_update_date"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="mgtUserId" column="mgt_user_id"/>
        <result property="deptLeader" column="dept_leader"/>
        <result property="userLeader" column="user_leader"/>
    </resultMap>

    <select id="selectUserLastWeekSummary" resultMap="UserLastWeekSumResult">
        select count(distinct user)                                                                    reported_count,
               sum(cost_day)                                                                           cost_amount,
               (select count(*) from sys_user sUser where sUser.status = '0' and sUser.del_flag = '0' and sUser.user_id != 1) user_count,
               (select sum(cost_day)
                from tbl_project_cost tcost
                where tcost.start_date <![CDATA[ >= ]]> date_add(date_format(#{startDate}, '%Y-%m-%d'), interval -7 day)
                  and tcost.end_date <![CDATA[ < ]]> date_add(date_format(#{endDate}, '%Y-%m-%d'), interval -7 day)
               )                                                                                       two_weeks_ago_amount
        from tbl_project_cost cost
        where cost.start_date <![CDATA[ >= ]]> date_format(#{startDate}, '%Y-%m-%d')
          and cost.end_date <![CDATA[ < ]]> date_format(#{endDate}, '%Y-%m-%d')
    </select>

    <select id="selectProjectLastWeekSummary" resultMap="ProjectSumResult">
        select * from(
        select
        cost.project_id,
        cost.project_name,
        sum(
        case when cost.start_date <![CDATA[ >= ]]> date_add(date_format(#{startDate},'%Y-%m-%d'),interval -7 day)
        and cost.end_date <![CDATA[ < ]]> date_add(date_format(#{endDate},'%Y-%m-%d'),interval -7 day)
        then
        cost.cost_day
        else 0
        end) two_weeks_ago_amount,
        sum(
        case when cost.start_date <![CDATA[ >= ]]> date_format(#{startDate},'%Y-%m-%d')
        and cost.end_date <![CDATA[ < ]]> date_format(#{endDate},'%Y-%m-%d')
        then
        cost.cost_day
        else 0
        end) cost_amount
        from tbl_project_cost cost
        where cost.start_date <![CDATA[ >= ]]> date_add(date_format(#{startDate},'%Y-%m-%d'),interval -7 day)
        and cost.end_date <![CDATA[ < ]]> date_format(#{endDate},'%Y-%m-%d')
        group by cost.project_id,cost.project_name) summary
        order by summary.cost_amount desc
        <if test="queryTop == true">
            limit 5
        </if>
    </select>

    <select id="selectProjectCostSummary" resultMap="ProjectSumResult">
        select * from
        (select cost.project_id,cost.project_name,sum(cost.cost_day) cost_amount
        from tbl_project_cost cost
        group by cost.project_id,cost.project_name) summary
        order by summary.cost_amount desc
        <if test="queryTop == true">
            limit 5
        </if>
    </select>

    <select id="selectProjectSummaryByStatus" resultMap="statusSumResult">
        select status, count(*) count
        from tbl_project
        where del_flag is null or del_flag <![CDATA[ <> ]]> '2'
        group by status
        order by status
    </select>
    <select id="selectNotReportedUserList" resultMap="SysUserResult">
        select *
        from sys_user u
        where u.status = '0'
          and u.del_flag = '0'
          and u.user_id != 1
          and u.user_id not in
              (
                  select distinct(user)
                  from tbl_project_cost cost
                  where cost.start_date <![CDATA[ >= ]]> date_format(#{startDate}, '%Y-%m-%d')
                    and cost.end_date <![CDATA[ < ]]> date_format(#{endDate}, '%Y-%m-%d')
              )
    </select>
</mapper>