<?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.xhkjedu.sapi.mapper.appversion.AppMonitorMapper">
    <!--按应用统计列表-->
    <select id="listAll" resultType="com.xhkjedu.sapi.vo.appversion.AppMonitorVo">
        select m.modulecode,count(*)num,sum(m.costtime)costtime
        ,(select m2.modulename from t_module m2 where m2.schoolid=m.schoolid and m2.mabbrevname=m.modulecode limit 1)modulename
        from t_app_monitor m where m.schoolid=#{monitor.schoolid}
        <if test="monitor.starttime!=null and monitor.starttime!=0">
            and UNIX_TIMESTAMP(FROM_UNIXTIME(m.createtime,'%Y%m%d'))>=#{monitor.starttime}
        </if>
        <if test="monitor.stoptime!=null and monitor.stoptime!=0">
            and UNIX_TIMESTAMP(FROM_UNIXTIME(m.createtime,'%Y%m%d'))&lt;=#{monitor.stoptime}
        </if>
        group by m.modulecode order by num desc
    </select>
    <!--按人统计列表-->
    <resultMap id="monitorResult" type="com.xhkjedu.sapi.vo.appversion.AppMonitorPVo">
        <result column="modulecode" property="modulecode"></result>
        <result column="costtime" property="costtime"></result>
        <result column="createtime" property="createtime"></result>
        <result column="username" property="username"></result>
        <result column="loginname" property="loginname"></result>
        <result column="createid" property="createid"></result>
        <result column="device" property="device"></result>
        <collection property="classes" ofType="com.xhkjedu.sapi.vo.gradeclass.ClassVo" javaType="java.util.List"
                    select="listClass" column="createid=createid,year=year">
        </collection>
    </resultMap>
    <select id="listClass" resultType="com.xhkjedu.sapi.vo.gradeclass.ClassVo">
        select c.classid,c.gradeid,c.classname
        from t_class_student cs left join t_class c on cs.classid=c.classid
        where cs.studentid=#{createid} and c.classtype=1 and c.classstate=1 and c.year=#{year}
    </select>
    <select id="listPeople" resultMap="monitorResult">
        select m.modulecode,m.costtime,m.createtime,u.username,u.loginname,m.createid,m.device,#{year} year
        ,(select m2.modulename from t_module m2 where m2.schoolid=m.schoolid and m2.mabbrevname=m.modulecode limit 1)modulename
        from t_app_monitor m left join t_user u on m.createid=u.userid
        <if test="(monitor.gradeid!=null and monitor.gradeid!=0) or (monitor.classid!=null and monitor.classid!=0)">
            left join t_class_student cs on m.createid=cs.studentid
            left join t_class c on cs.classid=c.classid
        </if>
        where m.schoolid=#{monitor.schoolid}
        <if test="monitor.device!=null and monitor.device!=0">
            and m.device=#{monitor.device}
        </if>
        <if test="monitor.modulecode!=null and monitor.modulecode!=''">
            and m.modulecode=#{monitor.modulecode}
        </if>
        <if test="monitor.gradeid!=null and monitor.gradeid!=0">
            and c.gradeid=#{monitor.gradeid} and c.classtype=1 and c.classstate=1 and c.year=#{year}
        </if>
        <if test="monitor.classid!=null and monitor.classid!=0">
            and cs.classid=#{monitor.classid}
        </if>
        <if test="monitor.username!=null and monitor.username!=''">
            and (u.username like '%${monitor.username}%' or u.loginname like '%${monitor.username}%')
        </if>
        <if test="monitor.starttime!=null and monitor.starttime!=0">
            and UNIX_TIMESTAMP(FROM_UNIXTIME(m.createtime,'%Y%m%d'))>=#{monitor.starttime}
        </if>
        <if test="monitor.stoptime!=null and monitor.stoptime!=0">
            and UNIX_TIMESTAMP(FROM_UNIXTIME(m.createtime,'%Y%m%d'))&lt;=#{monitor.stoptime}
        </if>
        group by m.amid order by m.createtime desc
    </select>
</mapper>
