<?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.web.work.mapper.CommonMapper">

    <!--后台企业端首页统计-->
    <select id="company" resultType="java.util.Map">
        SELECT
                ( SELECT COUNT( user_id ) FROM sys_user WHERE company_id =#{companyId} ) AS users,
                ( SELECT COUNT( id ) FROM tb_project WHERE company_id =#{companyId} ) AS project,
                ( SELECT COUNT( id ) FROM tb_relate WHERE company_id =#{companyId} ) AS relate,
                ( SELECT COUNT( id ) FROM tb_packages WHERE company_id =#{companyId} ) AS packages,
                ( SELECT SUM( money ) FROM tb_statement WHERE give = 2 AND company_id =#{companyId} ) AS money,
                ( SELECT SUM( real_money ) FROM tb_statement WHERE give = 2 AND company_id =#{companyId} ) AS realMoney,
                ( SELECT SUM( service_money ) FROM tb_statement WHERE give = 2 AND company_id =#{companyId} ) AS serviceMoney,
                ( SELECT SUM( peoples ) FROM tb_statement WHERE give = 2 AND company_id =#{companyId} ) AS peoples
    </select>
    <!--后台管理员端首页统计-->
    <select id="admin" resultType="java.util.Map">
        SELECT
                ( SELECT SUM( money ) FROM tb_company ) AS currentMoney,
                ( SELECT COUNT( id ) FROM tb_company ) AS companys,
                ( SELECT SUM( money ) FROM tb_statement WHERE give = 2 ) AS money,
                ( SELECT SUM( peoples ) FROM tb_statement WHERE give = 2 ) AS peoples,
                ( SELECT SUM( service_money ) FROM tb_statement WHERE give = 2 ) AS serviceMoney,
                ( SELECT SUM( real_money ) FROM tb_statement WHERE give = 2 ) AS realMoney,
                ( SELECT COUNT( user_id ) FROM sys_user WHERE type = 1 ) AS users,
                ( SELECT COUNT( id ) FROM tb_recharge WHERE state = 0 AND type=0 ) AS recharge,
                ( SELECT COUNT( id ) FROM tb_money_out WHERE state = 0 ) AS moneyOut,
                ( SELECT COUNT( id ) FROM tb_statement WHERE audit = 2 AND give = 1 ) AS gives,
                ( SELECT COUNT( id ) FROM tb_packages) AS packages,
                ( SELECT COUNT( id ) FROM tb_user WHERE is_contract=1) AS contracts,
                ( SELECT COUNT( id ) FROM tb_enterprise WHERE state=0) AS enterprise,
                ( SELECT COUNT( id ) FROM tb_position WHERE audit=0) AS position,
                ( SELECT COUNT( id ) FROM tb_pay WHERE state=0 and type=2) AS cash
    </select>
    <!--后台企业端每月交易金额统计-->
    <select id="record" resultType="java.util.Map">
        SELECT
            months.month AS name,
            SUM(tb_record.money) AS value
        FROM
            (
            SELECT 1 AS month UNION ALL
            SELECT 2 AS month UNION ALL
            SELECT 3 AS month UNION ALL
            SELECT 4 AS month UNION ALL
            SELECT 5 AS month UNION ALL
            SELECT 6 AS month UNION ALL
            SELECT 7 AS month UNION ALL
            SELECT 8 AS month UNION ALL
            SELECT 9 AS month UNION ALL
            SELECT 10 AS month UNION ALL
            SELECT 11 AS month UNION ALL
            SELECT 12 AS month
            ) AS months
            LEFT JOIN
            tb_record ON
            MONTH(tb_record.create_time) = months.month AND YEAR(tb_record.create_time) =#{year}
            <if test="companyId != null ">AND tb_record.company_id=#{companyId}</if>
            AND tb_record.type=#{type}
        GROUP BY
            months.month
        ORDER BY
            months.month
    </select>


</mapper>
