<?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="org.jeecg.modules.performance.mapper.CalculatePerformanceMapper">

    <select id="queryOperateCostGroupByUser" resultType="com.alibaba.fastjson.JSONObject">
        SELECT res.*,
               (SELECT realname from sys_user user where user.id=res.userId limit 1) as userName,
               (SELECT company_id from user_company company where company.user_id=res.userId limit 1) as 'companyId',
               (SELECT company_name from user_company company where company.user_id=res.userId limit 1) as 'companyName',
               (SELECT role_code from sys_role role LEFT JOIN sys_user_role ur on role.id=ur.role_id where ur.user_id=res.userId limit 1) as 'roleCode',
               (SELECT role_name from sys_role role LEFT JOIN sys_user_role ur on role.id=ur.role_id where ur.user_id=res.userId limit 1) as 'roleName'
        from (
                 SELECT
                     user_id as userId,
                     leader_id as leaderId,
                     sum(cost) as cost
                 FROM
                     ctop_performance_operate_detail
                 WHERE
                     media_type= #{mediaType}
                 AND	stat_date &gt;= #{startDate}
                 AND stat_date &lt;= #{endDate}
                 GROUP BY user_id
             ) res
    </select>

    <select id="queryDesignCostGroupByUser" resultType="com.alibaba.fastjson.JSONObject">
        SELECT res.*,
               (SELECT realname from sys_user user where user.id=res.userId limit 1) as userName,
               (SELECT company_id from user_company company where company.user_id=res.userId limit 1) as 'companyId',
               (SELECT company_name from user_company company where company.user_id=res.userId limit 1) as 'companyName',
               (SELECT role_code from sys_role role LEFT JOIN sys_user_role ur on role.id=ur.role_id where ur.user_id=res.userId limit 1) as 'roleCode',
               (SELECT role_name from sys_role role LEFT JOIN sys_user_role ur on role.id=ur.role_id where ur.user_id=res.userId limit 1) as 'roleName'
        from (
                 SELECT
                     user_id as userId,
                     sum(person_cost) as cost
                 FROM
                     ctop_performance_design_detail
                 WHERE
                     media_type= #{mediaType}
                   and start_date= #{startDate}
                   and end_date=#{endDate}
                 GROUP BY user_id
             ) res
    </select>

    <select id="querySaleCostGroupByUser" resultType="com.alibaba.fastjson.JSONObject">
        SELECT res.*,
               (SELECT realname from sys_user user where user.id=res.userId limit 1) as userName,
               (SELECT company_id from user_company company where company.user_id=res.userId limit 1) as 'companyId',
               (SELECT company_name from user_company company where company.user_id=res.userId limit 1) as 'companyName',
               (SELECT role_code from sys_role role LEFT JOIN sys_user_role ur on role.id=ur.role_id where ur.user_id=res.userId limit 1) as 'roleCode',
               (SELECT role_name from sys_role role LEFT JOIN sys_user_role ur on role.id=ur.role_id where ur.user_id=res.userId limit 1) as 'roleName'
        from (
                 SELECT
                     sale_id as userId,
                     sum(total_cost) as cost
                 FROM
                     ctop_sale_performance_detail
                 WHERE
                     media_type= #{mediaType}
                   and year= #{year}
                   and quarter= #{quarter}
                 GROUP BY sale_id
             ) res
    </select>

</mapper>