<?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.boco.nbd.wios.log.mapper.def.SysLogMapper">
	<resultMap id="SysLog"
		type="com.boco.nbd.wios.log.entity.po.SysLogPO">
		<result column="module_id" property="moduleId" />
		<result column="bussiness_name" property="bussinessName" />
		<result column="use_time" property="useTime" />
		<result column="logcontent" property="logcontent" />
		<result column="region_id" property="regionId" />
		<result column="user_id" property="userId" />
		<result column="log_id" property="logId" />
		<result column="user_name" property="userName" />
		<result column="handle_consuming_time" property="handleConsumingTime" />
		<result column="ip_address" property="ipAddress" />
		<result column="service_name" property="serviceName" />
		<result column="service_ip" property="serviceIp" />
	</resultMap>

	<sql id="Base_Column_List">
		module_id,
		bussiness_name,
		use_time,
		logcontent,
		region_id,
		user_id,
		log_id,
		user_name,
		handle_consuming_time,
		ip_address,
		service_name,
		service_ip
	</sql>

	<!-- 新增一条实体 -->
	<insert id="insert" parameterType="com.boco.nbd.wios.log.entity.qo.SysLogQO">
		INSERT INTO tv3_sys_log
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="moduleId != null">
				module_id ,
			</if>
			<if test="bussinessName != null">
				bussiness_name ,
			</if>
			<if test="useTime != null">
				use_time ,
			</if>
			<if test="logcontent != null">
				logcontent ,
			</if>
			<if test="regionId != null">
				region_id ,
			</if>
			<if test="userId != null">
				user_id ,
			</if>
			<if test="logId != null">
				log_id ,
			</if>
			<if test="userName != null">
				user_name ,
			</if>
			<if test="handleConsumingTime != null">
				handle_consuming_time ,
			</if>
			<if test="ipAddress != null">
				ip_address ,
			</if>
			<if test="serviceName != null">
				service_name ,
			</if>
			<if test="serviceIp != null">
				service_ip
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="moduleId != null">
				#{moduleId},
			</if>
			<if test="bussinessName != null">
				#{bussinessName},
			</if>
			<if test="useTime != null">
				DATE_FORMAT(#{useTime}, '%Y-%m-%d %H:%i:%S'),
			</if>
			<if test="logcontent != null">
				#{logcontent},
			</if>
			<if test="regionId != null">
				#{regionId},
			</if>
			<if test="userId != null">
				#{userId},
			</if>
			<if test="logId != null">
				#{logId},
			</if>
			<if test="userName != null">
				#{userName},
			</if>
			<if test="handleConsumingTime != null">
				#{handleConsumingTime},
			</if>
			<if test="ipAddress != null">
				#{ipAddress},
			</if>
			<if test="serviceName != null">
				#{serviceName},
			</if>
			<if test="serviceIp != null">
				#{serviceIp}
			</if>
		</trim>
	</insert>

	<!-- ############################################登录日志统计############################################ -->


	<!-- 关联了用户模块地市的日志信息详情 -->
	<resultMap id="SysLogInfo"
			   type="com.boco.nbd.wios.log.entity.po.SysLogInfoPO">
		<result column="module_id" property="moduleId" />
		<result column="bussiness_name" property="bussinessName" />
		<result column="use_time" property="useTime" />
		<result column="logcontent" property="logcontent" />
		<result column="user_id" property="userId" />
		<result column="log_id" property="logId" />
		<result column="user_name" property="userName" />
		<result column="handle_consuming_time" property="handleConsumingTime" />
		<result column="ip_address" property="ipAddress" />
		<result column="service_name" property="serviceName" />
		<result column="service_ip" property="serviceIp" />
		<result column="display_name" property="displayName" />
		<result column="parent_id" property="parentId" />
		<result column="role_id" property="roleId" />
		<result column="parent_role_id" property="parentRoleId" />
		<result column="parent_user_name" property="parentUserName" />
	</resultMap>


	<sql id="Include_Login_Where">
		and date_format(USE_TIME,<include refid="framework.framework-datestr" />)
		between #{start} and #{end}
		AND upper(bussiness_name) IN
		(UPPER('login'),UPPER('logout'))
	</sql>

	<sql id="login_counts">
		SUM(CASE WHEN upper(bussiness_name) = UPPER('login') THEN 1 ELSE 0 END ) AS login_Counts,
    	SUM(CASE WHEN upper(bussiness_name) = UPPER('logout') THEN 1 ELSE 0 END ) AS logout_Counts
	</sql>

	<resultMap id="UsersLoginStat"
			   type="com.boco.nbd.wios.log.entity.po.UserLogCountInfoPO">
		<result column="indexs" property="indexs" />
		<result column="id" property="userId" />
		<result column="role_id" property="roleId" />
		<result column="account" property="userName" />
		<result column="name" property="displayName" />
		<result column="login_Counts" property="loginCounts" />
		<result column="logout_Counts" property="logoutCounts" />
	</resultMap>

	<!-- 统计用户级别登录日志 -->
	<select id="getUsersLoginStat" parameterType="java.lang.String"
			resultMap="UsersLoginStat">
		select d.* ,
		-999 as indexs
		from(
			select
				a.id,
				c.role_id,
				a.account,
				a.name,
				IFNULL(login_Counts,0) as login_Counts,
				IFNULL(logout_Counts,0) as logout_Counts
			from
			  wb_account a inner join
			(
			select tv3_sys_log.user_id,
			<include refid="login_counts" />
			from tv3_sys_log
			where 1 = 1
			<include refid="Include_Login_Where" />
			group by tv3_sys_log.user_id
			) b
			on a.id=b.user_id
			INNER JOIN wb_account_role c on a.id=c.account_id
		) d
		order by login_counts desc
	</select>


	<!-- 查询用户的登录日志详细信息 -->
	<select id="getUsersLoginDetails" resultMap="SysLogInfo">
		select a.*,b.name display_name,a.module_id module_name,b.parent_id,c.role_id parent_role_id,d.role_id,e.account parent_user_name
		from
		(
		select * from tv3_sys_log
		where 1=1
		<if test="userId != null and userId != ''">
			and user_id = #{userId}
		</if>
		<include refid="Include_Login_Where" />
		order by use_time desc
		) a
		LEFT JOIN wb_account b ON a.user_id = b.id
		left join wb_account_role c on c.account_id = b.parent_id
		left join wb_account_role d on d.account_id = b.id
		left join wb_account e on b.parent_id = e.id
		ORDER BY USE_TIME DESC
	</select>


	<resultMap id="RolesLoginStat"
			   type="com.boco.nbd.wios.log.entity.po.RoleLogCountInfoPO">
		<result column="indexs" property="indexs" />
		<result column="role_id" property="roleId" />
		<result column="role_name" property="roleName" />
		<result column="login_Counts" property="loginCounts" />
		<result column="logout_Counts" property="logoutCounts" />
	</resultMap>

	<!-- 根据时间角色统计日志 -->
	<select id="getRolesLoginStat" parameterType="java.lang.String"
			resultMap="RolesLoginStat">
		SELECT
			g.*,
			- 999 AS indexs
		FROM
		(
			SELECT
				e.name role_name,
				sum(IFNULL( login_Counts, 0 )) AS login_Counts,
				sum(IFNULL( logout_Counts, 0 )) AS logout_Counts
			FROM
			wb_role e
			LEFT JOIN (
				SELECT
					c.role_id,
					<include refid="login_counts" />
				FROM
				wb_account_role c,
				(
					SELECT
						a.id user_id,
						b.BUSSINESS_NAME,
						b.use_time
						FROM
						wb_account a,
						tv3_sys_log b
					WHERE
						a.id = b.user_id
						<include refid="Include_Login_Where" />
				) d
			WHERE
				c.account_id = d.user_id
				GROUP BY
				c.role_id
				) f ON e.id = f.role_id
				GROUP BY
				e.name
		) g
		ORDER BY
		login_counts DESC
	</select>


	<!-- 查询角色的登录日志详细信息 -->
	<select id="getRolesLoginDetails" parameterType="java.lang.String"
			resultMap="SysLogInfo">
		select
		a.*,b.name display_name,a.module_id module_name,b.parent_id,c.role_id parent_role_id,d.role_id,w.account parent_user_name
		from
		(
			select e.* from
			(
				SELECT * FROM
				tv3_sys_log
				WHERE 1 = 1
				<include refid="Include_Login_Where" />
				order by use_time desc
			) e join
			(
				select account_id
				from wb_account_role g left join wb_role h  on g.role_id=h.id
				where 1=1
				<if test="roleName != null and roleName != ''">
					and h.name = #{roleName}
				</if>
			) f on e.user_id=f.account_id
		) a
		left join wb_account b on a.user_id=b.id
		left join wb_account_role c on c.account_id = b.parent_id
		left join wb_account_role d on d.account_id = b.id
		left join wb_account w on b.parent_id = w.id
		ORDER BY USE_TIME DESC
	</select>


	<!-- ######################统计信息和从统计信息钻取详情##################### -->

	<sql id="Exclude_Login_Where">
		and date_format(USE_TIME,<include refid="framework.framework-datestr" />)
		between #{start} and #{end}
		AND upper(bussiness_name) NOT IN
		(UPPER('login'),UPPER('logout'))
	</sql>

	<resultMap id="UserLogCountInfoPO"
			   type="com.boco.nbd.wios.log.entity.po.UserLogCountInfoPO">
		<result column="indexs" property="indexs" />
		<result column="id" property="userId" />
		<result column="account" property="userName" />
		<result column="name" property="displayName" />
		<result column="counts" property="counts" />
	</resultMap>

	<!-- 根据时间用户统计日志-->
	<select id="countByUserDate" parameterType="java.lang.String"
			resultMap="UserLogCountInfoPO">
		select c.* ,
		-999 indexs
		from(
			select
			a.id,a.account,a.name,
			IFNULL(b.counts,0) as counts
			from
			wb_account a inner join
			(
				select tv3_sys_log.user_id,count(0) as counts
				from tv3_sys_log
				where 1 = 1
				<include refid="Exclude_Login_Where" />
				group by tv3_sys_log.user_id
			) b
			on a.id=b.user_id
		) c
		order by counts desc
	</select>



	<!-- 根据用户ID查询访问日志详细信息 -->
	<select id="selectLogInfoByUserIdDate" resultMap="SysLogInfo">
		select a.*,b.name display_name,a.module_id module_name,b.parent_id,c.role_id parent_role_id,d.role_id,e.account parent_user_name
		from
		(
		select * from tv3_sys_log
		where 1=1
		<if test="userId != null and userId != ''">
			and user_id = #{userId}
		</if>
		<include refid="Exclude_Login_Where" />
		order by use_time desc
		) a
		LEFT JOIN wb_account b ON a.user_id = b.id
		left join wb_account_role c on c.account_id = b.parent_id
		left join wb_account_role d on d.account_id = b.id
		left join wb_account e on b.parent_id = e.id
		ORDER BY USE_TIME DESC
	</select>



	<resultMap id="RoleLogCountInfoPO"
			   type="com.boco.nbd.wios.log.entity.po.RoleLogCountInfoPO">
		<result column="indexs" property="indexs" />
		<result column="role_id" property="roleId" />
		<result column="role_name" property="roleName" />
		<result column="counts" property="counts" />
	</resultMap>

	<!-- 根据时间角色统计日志 -->
	<select id="countByRoleDate" parameterType="java.lang.String"
			resultMap="RoleLogCountInfoPO">
		SELECT
			g.*,
			- 999 AS indexs
		FROM
		(
			SELECT
				e.name role_name,
				sum(IFNULL(f.counts,0)) AS counts
			FROM
			wb_role e
			LEFT JOIN (
				SELECT
					c.role_id,
					count(*)as counts
				FROM
				wb_account_role c,
				(
					SELECT
						a.id user_id,
						b.BUSSINESS_NAME,
						b.use_time
						FROM
						wb_account a,
						tv3_sys_log b
					WHERE
						a.id = b.user_id
						<include refid="Exclude_Login_Where" />
				) d
			WHERE
				c.account_id = d.user_id
				GROUP BY
				c.role_id
				) f ON e.id = f.role_id
				GROUP BY
				e.name
		) g
		ORDER BY
		counts desc
	</select>


	<!-- 根据角色查询访问日志 -->
	<select id="selectByRoleDate" parameterType="java.lang.String"
			resultMap="SysLogInfo">
		select
		a.*,b.name display_name,a.module_id module_name,b.parent_id,c.role_id parent_role_id,d.role_id,w.account parent_user_name
		from
		(
		select e.* from
		(
		SELECT * FROM
		tv3_sys_log
		WHERE 1 = 1
		<include refid="Exclude_Login_Where" />
		order by use_time desc
		) e join
		(
		select account_id
		from wb_account_role g left join wb_role h  on g.role_id=h.id
		where 1=1
		<if test="roleName != null and roleName != ''">
			and h.name = #{roleName}
		</if>
		) f on e.user_id=f.account_id
		) a
		left join wb_account b on a.user_id=b.id
		left join wb_account_role c on c.account_id = b.parent_id
		left join wb_account_role d on d.account_id = b.id
		left join wb_account w on b.parent_id = w.id
		ORDER BY USE_TIME DESC
	</select>


	<!-- ###################模块访问日志统计分割线######################### */-->
	<resultMap id="ModuleLogCountInfoPO"
			   type="com.boco.nbd.wios.log.entity.po.ModuleLogCountInfoPO">
		<result column="indexs" property="indexs" />
		<result column="module_id" property="moduleId" />
		<result column="module_name" property="moduleName" />
		<result column="counts" property="counts" />
	</resultMap>

	<!-- 根据时间模块统计日志 -->
	<select id="countByModuleDate"
			resultMap="ModuleLogCountInfoPO">
		select g.* ,
		-999 as indexs
		from(
		select module_id,module_id module_name,count(0) as counts
		from tv3_sys_log
		where 1 = 1
		<include refid="Exclude_Login_Where" />
		group by module_id
		) g

		order by counts desc
	</select>


	<!-- 根据模块ID查询日志详细信息 -->
	<select id="selectLogInfoByModuleIdDate" resultMap="SysLogInfo">
		select a.*,b.name display_name,a.module_id module_name,b.parent_id,c.role_id parent_role_id,d.role_id,w.account parent_user_name
		from
		(
		select * from
		tv3_sys_log where 1=1
		<if test="moduleId != null and moduleId != ''">
			and module_id = #{moduleId}
		</if>
		<include refid="Exclude_Login_Where" />
		order by use_time desc
		) a
		left join wb_account b on a.user_id=b.id
		left join wb_account_role c on c.account_id = b.parent_id
		left join wb_account_role d on d.account_id = b.id
		left join wb_account w on b.parent_id = w.id
		ORDER BY USE_TIME DESC
	</select>
</mapper>