<?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.pj.project.sta_data.StaDataMapper">


	<!-- 返回统计数据 -->
	<select id="getStaData" resultType="somap">
		select 
		(select count(*) from sys_user) as sys_user_count,
		(select count(*) from sys_user where to_days(create_time) = to_days(now())) as sys_user_today_count,
		(select count(*) from sys_login_log) as sys_login_log_count,
		(select count(*) from sys_login_log where to_days(create_time) = to_days(now())) as sys_login_log_today_count,
		(select count(*) from sys_client) as sys_client_count,
		(select count(*) from sp_apilog) as sp_apilog_count
	</select>


	<!-- 获取指定年份每月登录量  -->
	<select id="getUserLoginSta" resultType="somap">
		select 
		DATE_FORMAT( create_time, '%m' ) as m, 
		count(*) as count	
		from sys_login_log 
		where DATE_FORMAT( create_time, '%Y' ) = #{year} 
		group by m 	
	</select>
	
	<!-- 指定月份每日用户登录量 -->
	<select id="getUserLoginStaByMonth" resultType="somap">
		select 
		DATE_FORMAT( create_time, '%d' ) as d, 
		count(*) as count	
		from sys_login_log 
		where DATE_FORMAT( create_time, '%Y-%m' ) = #{yearMonth} 
		group by d 
	</select>
	
	<!-- 查询总登录数据，按照url分组 -->
	<select id="getUserLoginStaByUrlGroup" resultType="somap">
		select 
		client_domain as name, 
		count(1) as value 
		from sys_login_log 
		group by client_domain 
	</select>
	
	<!-- 查询总登录数据，按照client分组 -->
	<select id="getUserLoginStaByClientGroup" resultType="somap">
		select 
		concat(
			ifnull( (select name from sys_client where id = sys_login_log.client_id), '未知') , 
			' - ',
			client_domain 
		) as name,
		count(1) as count 
		from sys_login_log 
		group by name  
	</select>
	
	<!-- 获取指定年份每月注册量  -->
	<select id="getUserRegSta" resultType="somap">
		select 
		DATE_FORMAT( create_time, '%m' ) as m, 
		count(*) as count	
		from sys_user
		where DATE_FORMAT( create_time, '%Y' ) = #{year} 
		group by m 	
	</select>
	
	<!-- 指定时间段的用户登录数据  -->
	<select id="getLoginData" resultType="somap">
		select 
			date_format(create_time, #{dateFormat} ) as name, 
			count(*) as count 
		from sys_login_log 
		where create_time > #{startTime} 
		and create_time &lt; #{endTime} 
		group by name 
	</select>
	
	<!-- 指定时间段的用户注册数据  -->
	<select id="getRegData" resultType="somap">
		select 
			date_format(create_time, #{dateFormat} ) as name, 
			count(*) as count 
		from sys_user  
		where create_time > #{startTime} 
		and create_time &lt; #{endTime} 
		group by name 
	</select>
	

</mapper>
