首页 > 代码库 > 课时统计

课时统计

项目是是应用springmvc 框架

java,类,控制层

package com.school.web.controller.nini;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.school.business.StatisticsDurationCuManager;
import com.school.domain.StatisticsDurationCourse;
import com.school.util.PageRequest;
import com.school.util.Pageable;
import com.school.util.Paginator;

@Controller
@RequestMapping("/secure/course/statistics")
public class StatisticsDurationCuController extends AbstractSecureController {

    @Autowired
    private StatisticsDurationCuManager statisticsDurationCuManager;

    /**
     * 根据一门课程学员分组去统计时长。
     */
    @RequestMapping("/{courseId}/duration")
    public ModelAndView getStuOneCourseDur(@PathVariable String courseId, HttpServletRequest request) {
        String schoolId = this.getSchoolId(request); // 网校ID
        int page = ServletRequestUtils.getIntParameter(request, "page", 1); // 第几页
        // 分页查询该课程的所有学员
        Pageable pageable = new PageRequest(page, PAGE_SIZE);
        Paginator<StatisticsDurationCourse> paginator = statisticsDurationCuManager.getDurationByCourse(schoolId,
                courseId, pageable);
        // 模版视图
        ModelAndView mav = this.createCourseModelAndView(courseId, request);
        mav.setViewName(getSecureViewDir(request) + "/courseStatistics/students");

        String queryString = null;
        mav.addObject("paginator", paginator);
        mav.addObject("paginationHtml", paginator.toPaginationHtml(queryString));

        return mav;
    }

    /**
     * 根据一门课程学员分组去统计时长的详情。(分页)
     */
    @RequestMapping("/{courseId}/duration/{userId}/detailed")
    public ModelAndView getStuOneCourseDurDetail(@PathVariable String courseId, @PathVariable String userId,
            HttpServletRequest request) {
        String schoolId = this.getSchoolId(request); // 网校ID
        String nickname = ServletRequestUtils.getStringParameter(request, "nickname", "");
        String duration = ServletRequestUtils.getStringParameter(request, "duration", "");
        String count = ServletRequestUtils.getStringParameter(request, "count", "");
        int page = ServletRequestUtils.getIntParameter(request, "page", 1); // 第几页
        // 分页查询
        Pageable pageable = new PageRequest(page, PAGE_SIZE);
        Paginator<StatisticsDurationCourse> paginator = statisticsDurationCuManager.getDurByCourseDetailPage(schoolId,
                courseId, userId, pageable);
        // 模版视图
        ModelAndView mav = this.createCourseModelAndView(courseId, request);
        mav.setViewName(getSecureViewDir(request) + "/courseStatistics/detailed");

        String url = "/secure/course/statistics/" + courseId + "/duration/" + userId + "/detailed";
        String queryString = null;
        mav.addObject("paginator", paginator);
        mav.addObject("paginationHtml", paginator.toPaginationHtml(url, queryString));

        mav.addObject("nickname", nickname);
        mav.addObject("courseId", courseId);
        mav.addObject("userId", courseId);
        mav.addObject("duration", duration);
        mav.addObject("count", count);
        return mav;
    }

    /**
     * 根据一门课程学员分组去统计时长的详情。(不分页)
     */
    @RequestMapping("/{courseId}/duration/{userId}/detailed/list")
    public ModelAndView getStuOneCoDurDetailList(@PathVariable String courseId, @PathVariable String userId,
            HttpServletRequest request) {
        String schoolId = this.getSchoolId(request); // 网校ID
        String nickname = ServletRequestUtils.getStringParameter(request, "nickname", "");

        List<StatisticsDurationCourse> durationList = statisticsDurationCuManager.getDurByCourseDetailed(schoolId,
                courseId, userId);
        // 模版视图
        ModelAndView mav = this.createCourseModelAndView(courseId, request);
        mav.setViewName(getSecureViewDir(request) + "/courseStatistics/detailed");

        mav.addObject("durationList", durationList);
        mav.addObject("nickname", nickname);
        return mav;
    }

    /**
     * 一门课程最近30天的时长统计
     * @throws IOException
     * @throws ParseException
     */
    @RequestMapping("/{courseId}/duration/day")
    public ModelAndView getCourseDurationDay(@PathVariable String courseId, HttpServletRequest request)
            throws IOException, ParseException {
        String schoolId = this.getSchoolId(request); // 网校ID
        String type = ServletRequestUtils.getStringParameter(request, "type", "");
        String startDate = ServletRequestUtils.getStringParameter(request, "startDate", "");
        String endDate = ServletRequestUtils.getStringParameter(request, "endDate", "");
        ModelAndView mav = this.createCourseModelAndView(courseId, request);
        
        boolean selectmark = false;// 页面传过来的不合法的自定义的查询条件

        if (StringUtils.isNotBlank(type)) {

            if ("week".equals(type.trim())) {
                // 按照时间的顺序,最近7天
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String currentTime = sdf.format(new Date());
                String sevenDay = getPassDate(currentTime, 7);
                
                List<StatisticsDurationCourse> durationList = statisticsDurationCuManager.getDurationByTime(
                        schoolId, courseId, sevenDay, currentTime);
                
                //补数据,没有记录的数据,一天没有产生数据,补一条0的数据
                List<StatisticsDurationCourse> durationListData = http://www.mamicode.com/addNullDayData(durationList,7);>
页面是使用freemarker

<#-- 页头 -->
<#assign pageTitle="课程统计">
<#include "/default/commons/header.ftl">
  <style>
     .searchDate{
        margin:10px auto -10px;
     }
     .inputLabel{
        line-height:30px;
     }
     .form-radio{
        margin-top:-2px;
     }
     .name-font{
        font-size:18px;
        text-indent:2em;
        margin-top:20px;
     }
     .terminal{
        margin-top:10px;
     }
     #draw{
        width:95%;
        height:400px;
        margin:0 auto;
     }
     .table-th{
        background-color: #4B4B4B;
        color:#e2e2e2;
     }
     .warnning{
        padding:5px 15px;
     }
  </style>
  <div id="content-container" class="container">
  
    <#-- 课程管理相关页面的顶部模版 -->
    <#include "../course/manage/topSection.ftl">  
    
     <div class="row">
     
	  <div class="col-md-3">
	    <#-- 左侧导航 -->
	    <#assign sidebarNav="duration">
	    <#include "../course/manage/sidebar.ftl">
	  </div>
	  
	<div class="col-md-9">
	   <div class="panel panel-default panel-col">
	   <!--右侧内容-->
         
           <div class="well well-sm" style="width:90%; margin-top:20px;margin-left:auto;margin-right:auto;text-align:center;">
			    <form id="statistics-search" class="form-inline" action="/secure/course/statistics/${course.courseId}/duration/day" method="get"> 
				      <div class="form-group">
				        <input class="form-control" type="text" id="startDate" name="startDate" value="" placeholder="起始时间">
				      </div>
				
				      <span>--</span>
				      
				      <div class="form-group">
				        <input class="form-control" type="text" id="endDate" name="endDate" value="" placeholder="结束时间">
				      </div>
				      <span>--</span>
				
				      <div class="form-group">
				          <select class="form-control" name="type" id="markType">
					         <option value="week">最近7天</option>
					         <option value="month">最近30天</option>
					         <option value="time">自定义时间</option>
					      </select>
				      </div>
				
				      <button class="btn btn-primary">统计</button>
			    </form>
  		  </div>
         
           <!--统计日期选择-->
           <div class="name-font">
              « <span id="courseName">${(course.title)!''}</span>» 统计   
              <span id="markText" style="float:right;margin-right:40px;"></span>
           </div>
           <div id="draw">
           </div>
           <!--折线图-->
      
           <div class="table-view" style="width:90%;margin:auto;">
               <table class="table table-hover" id="tongji" style="text-align:center;">
                  <thead class="table-th">
                     <th style="text-align:center;">日期</th>
                     <th style="text-align:center;">观看次数(次)</th>
                     <th style="text-align:center;">观看时长(分钟)</th>
                  </thead>
                  <tbody>
                    <#if durationList?has_content>
                      	<#list durationList as item>
		                    <tr>
		                       <td>${(item.date)?string("yyyy-MM-dd")}</td>
		                       <td>${(item.number)!''}</td>
		                       <td>${((item.totalDuration)*60)?string("0.##")}</td>
		                    </tr>
	                     </#list>
	                <#else>
	                   <span> 暂时还没数据! </span>    
                    </#if>
                  </tbody>
               </table>
           </div>
           <!--数据列表-->
           <input type="hidden" id="timeMark" value="${(timeMark)!''}" />
       </div>
   </div>
   </div>


 
 <#--页脚开始 -->
<#include "/default/commons/footerBegin.ftl">
<script type="text/javascript" src="/resources/echart/esl.js"></script> 
<script type="text/javascript" src="/resources/datetimepicker/js/bootstrap-datetimepicker.min.js"></script> 


  <script type="text/javascript">  
            // 路径配置  
            require.config({  
                paths:{   
                    'echarts' : '/resources/echart/echarts',  
                    'echarts/chart/bar' : '/resources/echart/echarts'  
                }  
            });  
              
            // 使用  
            require(  
                [  
                    'echarts',  
                    'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载  
                ],  
                function (ec) {  
                    // 基于准备好的dom,初始化echarts图表  
                    var myChart = ec.init(document.getElementById('draw'));   
                      
                     var jsonNum=${numberList};
					  var jsonTime=${totalTimes};
					  var jsonDay=${dayList};
                      
                   option = {
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					        data:['观看次数','观看时长']
					    },
					    toolbox: {
					        show : true,
					        feature : {
					            magicType : {show: false, type: ['line']}
					           
					            
					        }
					    },
					    calculable : true,
					    xAxis : [
					        {
					            type : 'category',
					            boundaryGap : true,
					            name: '月份',
					            data : jsonDay
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value',
					            name: '分钟/次数'
					        }
					    ],
					    series : [
					        {
					            name:'观看次数',
					            type:'line',
					            stack: '次',
					            data:jsonNum
					        },
					        {
					            name:'观看时长',
					            type:'line',
					            stack: '分钟',
					            data:jsonTime
					        }
					    ]
					}; 
              
                    // 为echarts对象加载数据   
                    myChart.setOption(option);   
                }  
            );  
  </script> 
  
  
  <script>
  
      $(document).ready(function(){
          var timeMark="${timeMark}";
           $("#markType").val(timeMark);
           
           if("time"==timeMark){
              var startDate="${startDate}";
              var endDate="${endDate}";
              var textStr=startDate+"----"+endDate+"的观看情况";
              $("#markText").text(textStr);
           }
      });
  
       $("#startDate").datetimepicker({
		    weekStart: 1,
	        todayBtn:  1,
			autoclose: 1,
			todayHighlight: 1,
			startView: 2,
			minView: 2,
			forceParse: 0,
     	  format:"yyyy-mm-dd"
       });
            	   
	   $("#endDate").datetimepicker({
	        weekStart: 1,
            todayBtn:  1,
		    autoclose: 1,
		    todayHighlight: 1,
		    startView: 2,
		    minView: 2,
		    forceParse: 0,
 	        format:"yyyy-mm-dd"
       });
  </script>


  
<#--页脚页脚页脚-->
<#include "/default/commons/footerEnd.ftl">

运行效果图:






课时统计