<?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.AppthirdMonitorMapper">
    <resultMap id="monitorResult" type="com.xhkjedu.sapi.vo.appversion.AppthirdMonitorVo">
        <result column="username" property="username"></result>
        <result column="loginname" property="loginname"></result>
        <result column="studentid" property="studentid"></result>
        <result column="handlenum" property="handlenum"></result>
        <result column="handletime" property="handletime"></result>
        <collection property="classes" ofType="com.xhkjedu.sapi.vo.gradeclass.ClassVo" javaType="java.util.List"
                    select="listClass" column="studentid=studentid,year=year">
        </collection>
        <collection property="apps" ofType="com.xhkjedu.sapi.vo.appversion.AppthirdManagerVo" javaType="java.util.List"
                    select="listApp" column="{studentid=studentid,handlenum=handlenum
                    ,gradeid=gradeid,classid=classid,starttime=starttime,stoptime=stoptime,year=year}">
            <result column="mname" property="mname"></result>
            <result column="num" property="num"></result>
        </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=#{studentid} and c.classtype=1 and c.classstate=1 and c.year=#{year}
    </select>
    <select id="listApp" resultType="com.xhkjedu.sapi.vo.appversion.AppthirdManagerVo">
        <if test="handlenum==0">
            select ifnull(a.mname,m.ampackage)mname,count(distinct m.amid) num
            from t_appthird_monitor m left join t_appthird_manager a on m.ampackage=a.mpackage and (a.belong=2 or a.schoolid=m.schoolid)
        </if>
        <if test="handlenum!=0">
            select ifnull(a.mname,m.amapackage)mname,m.amanum num
            from t_appthird_monitoranalyze m left join t_appthird_manager a on m.amapackage=a.mpackage and (a.belong=2 or a.schoolid=m.schoolid)
        </if>
        <if test="(gradeid!=null and gradeid!=0) or (classid!=null and classid!=0)">
            left join t_class_student cs on m.studentid=cs.studentid
            left join t_class c on cs.classid=c.classid
        </if>
        where m.studentid=#{studentid}
        <if test="handlenum==0">
            and m.amstate=0
        </if>
        <if test="handlenum!=0">
            and m.handlenum=#{handlenum}
        </if>
        <if test="gradeid!=null and gradeid!=0">
            and c.gradeid=#{gradeid} and c.classtype=1 and c.classstate=1 and c.year=#{year}
        </if>
        <if test="classid!=null and classid!=0">
            and cs.classid=#{classid}
        </if>
        <if test="starttime!=null and starttime!=0">
            and
            <if test="handlenum==0">m.createtime</if>
            <if test="handlenum!=0">m.handletime</if>
            >=#{starttime}
        </if>
        <if test="stoptime!=null and stoptime!=0">
            and
            <if test="handlenum==0">m.createtime</if>
            <if test="handlenum!=0">m.handletime</if>
            &lt;=#{stoptime}
        </if>
        <if test="handlenum==0">
            group by m.ampackage
        </if>

        <if test="handlenum!=0">
            group by m.amapackage
        </if>

    </select>

    <!--应用外监控未处理列表-->
    <select id="listAll" resultMap="monitorResult">
        select u.username,u.loginname,m.studentid,0 handlenum,#{year} year
        ,#{monitor.gradeid} gradeid,#{monitor.classid} classid,#{monitor.starttime} starttime,#{monitor.stoptime} stoptime
        from t_appthird_monitor m left join t_user u on m.studentid=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.studentid=cs.studentid
            left join t_class c on cs.classid=c.classid
        </if>
        where m.schoolid=#{monitor.schoolid} and m.amstate=0
        <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.starttime!=null and monitor.starttime!=0">
            and m.createtime>=#{monitor.starttime}
        </if>
        <if test="monitor.stoptime!=null and monitor.stoptime!=0">
            and m.createtime&lt;=#{monitor.stoptime}
        </if>
        group by m.studentid order by m.createtime desc
    </select>
    <!--学生未处理监控详情-->
    <select id="listPeople" resultType="com.xhkjedu.sapi.vo.appversion.AppthirdMonitorPVo">
        select m.createtime,ifnull(a.mname,m.ampackage)appname
        from t_appthird_monitor m left join t_appthird_manager a on m.ampackage=a.mpackage and (a.belong=2 or a.schoolid=m.schoolid)
        <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.studentid=cs.studentid
            left join t_class c on cs.classid=c.classid
        </if>
        where m.studentid=#{monitor.createid} and m.amstate=0
        <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.starttime!=null and monitor.starttime!=0">
            and m.createtime>=#{monitor.starttime}
        </if>
        <if test="monitor.stoptime!=null and monitor.stoptime!=0">
            and m.createtime&lt;=#{monitor.stoptime}
        </if>
        group by m.amid order by m.createtime desc
    </select>
    <!--标记为已处理-->
    <update id="handle">
        update t_appthird_monitor m set m.amstate=1,m.handlenum=#{analyze.handlenum},m.handletime=#{timestamp}
        ,m.handleid=#{analyze.handleid},m.amaid=#{analyze.amaid}
        where
        <foreach collection="amids" item="amid" index="index" separator="or">
            m.amid =#{amid}
        </foreach>
    </update>
    <resultMap id="monitorAResult" type="com.xhkjedu.sapi.vo.appversion.AppthirdMonitoranalyzeVo">
        <result column="studentid" property="studentid"></result>
        <result column="amapackage" property="amapackage"></result>
        <result column="handlenum" property="handlenum"></result>
        <collection property="amids" ofType="java.lang.Integer" javaType="java.util.List">
            <result column="amid" property="amid"></result>
        </collection>
    </resultMap>
    <select id="listByStudentId" resultMap="monitorAResult">
        select m.studentid,m.ampackage amapackage,m.amid
        ,(select max(ifnull(m2.handlenum,0)+1) from t_appthird_monitor m2 where m2.studentid=m.studentid)handlenum
        from t_appthird_monitor m
        <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.studentid=cs.studentid
            left join t_class c on cs.classid=c.classid
        </if>
        where m.amstate=0 and m.studentid in
        <foreach collection="monitor.createids" item="createid" index="index" open="(" separator="," close=")">
            ${createid}
        </foreach>
        <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.starttime!=null and monitor.starttime!=0">
            and m.createtime>=#{monitor.starttime}
        </if>
        <if test="monitor.stoptime!=null and monitor.stoptime!=0">
            and m.createtime&lt;=#{monitor.stoptime}
        </if>
        group by m.amid
    </select>
    <!--应用外监控已处理列表-->
    <select id="listAll2" resultMap="monitorResult">
        select u.username,u.loginname,m.studentid,m.handlenum,m.handletime,#{year} year
        ,#{monitor.gradeid} gradeid,#{monitor.classid} classid,#{monitor.starttime} starttime,#{monitor.stoptime} stoptime
        from t_appthird_monitoranalyze m left join t_user u on m.studentid=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.studentid=cs.studentid
            left join t_class c on cs.classid=c.classid
        </if>
        where m.schoolid=#{monitor.schoolid}
        <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.starttime!=null and monitor.starttime!=0">
            and m.handletime>=#{monitor.starttime}
        </if>
        <if test="monitor.stoptime!=null and monitor.stoptime!=0">
            and m.handletime&lt;=#{monitor.stoptime}
        </if>
        group by m.studentid,m.handlenum order by m.handletime desc
    </select>
    <!--学生已处理监控详情-->
    <select id="listPeople2" resultType="com.xhkjedu.sapi.vo.appversion.AppthirdMonitorPVo">
        select m.createtime,ifnull(a.mname,m.ampackage)appname
        from t_appthird_monitor m left join t_appthird_manager a on m.ampackage=a.mpackage and (a.belong=2 or a.schoolid=m.schoolid)
        where m.studentid=#{monitor.createid} and m.handlenum=#{monitor.handlenum}
        order by m.createtime desc
    </select>
</mapper>
