/**
   * 到竞赛页面
   *
   * @param request
   * @param competionId
   * @return
   */
  @RequestMapping("/totcompetion")
  public ModelAndView totcompetion(HttpServletRequest request, int competionId) {
    ModelAndView view = new ModelAndView();

    Competion competion = competionService.get(competionId);
    view.addObject("competion", competion);
    // 竞赛裁判
    List<UserCompetion> uclist = usercompetionService.getCompetionjudgment(competionId);
    for (UserCompetion uc : uclist) {
      User user = userService.getUserById(uc.getUserId());
      uc.setUser(user);
    }
    view.addObject("judgmentlist", uclist);
    // Exam course = examService.get(examId);
    // view.addObject("exam", course);
    // 竞赛试卷
    List<CompetionExam> celist = competionService.getCompetionExam(competionId);
    for (CompetionExam ce : celist) {
      TeacherExam te = teacherExamService.getTeacherExamByExamId(ce.getExam().getId());
      ce.setExamuser(userService.getUserById(te.getUserId()).getUsername());
      if (ce.getSelectexam() == 1) {
        view.addObject("sexam", ce);
      }
    }
    view.addObject("celist", celist);
    // 竞赛学生
    List<UserCompetionData> ucslist = usercompetionService.getCompetionStudentData(competionId);
    view.addObject("studentlist", ucslist);

    view.setViewName("/cms/competion");
    return view;
  }
 /**
  * 删除竞赛学生
  *
  * @param request
  * @param name
  * @return
  */
 @RequestMapping("/delcompetionuser")
 public void delcompetionuser(HttpServletRequest request, HttpServletResponse response, int id) {
   try {
     PrintWriter out = response.getWriter();
     usercompetionService.remove(id);
     // examService.createExam(name, userId, competionId);
     String str = "{'sucess':'sucess'}";
     out.write(str);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  /**
   * 得到可以命题的老师
   *
   * @param request
   * @param response
   */
  @RequestMapping("/getCompetionMjudgment")
  public void getCompetionMjudgment(
      HttpServletRequest request, HttpServletResponse response, int competionId) {

    try {
      PrintWriter out = response.getWriter();
      List<UserCompetion> uclist = usercompetionService.getCompetionMjudgment(competionId);
      String str = getMjudgment(uclist);
      out.write(str);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * 删除竞赛裁判
  *
  * @param request
  * @param response
  * @param competionId
  * @param userId
  */
 @RequestMapping("/delcompetionjudgment")
 public void delcompetionjudgment(
     HttpServletRequest request, HttpServletResponse response, String competionId, String userId) {
   try {
     PrintWriter out = response.getWriter();
     usercompetionService.removeCompetionjudgment(
         Integer.parseInt(competionId), Integer.parseInt(userId));
     String str = "{'sucess':'sucess'}";
     out.write(str);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  /**
   * 到竞赛的评分页面
   *
   * @param request
   * @param competionId
   * @return
   */
  @RequestMapping("/totcompetionpf")
  public ModelAndView totcompetionpf(HttpServletRequest request, int competionId) {
    ModelAndView view = new ModelAndView();

    Competion competion = competionService.get(competionId);
    view.addObject("competion", competion);
    // 竞赛学生
    List<UserCompetionData> ucslist = usercompetionService.getCompetionStudentData(competionId);
    view.addObject("studentlist", ucslist);

    CompetionExam ce = competionService.getCompetionSelectExam(competionId);
    view.addObject("ceexam", ce);
    view.setViewName("/cms/competionpf");
    return view;
  }
 /**
  * 增加竞赛裁判
  *
  * @param request
  * @param name
  * @return
  */
 @RequestMapping("/addcompetionjudgment")
 public void addcompetionjudgment(
     HttpServletRequest request,
     HttpServletResponse response,
     int userId,
     int competionId,
     String job) {
   try {
     PrintWriter out = response.getWriter();
     usercompetionService.save(userId, competionId, job);
     String str = "{'sucess':'sucess'}";
     out.write(str);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
 /**
  * 增加竞赛学生
  *
  * @param request
  * @param name
  * @return
  */
 @RequestMapping("/addcompetionuser")
 public void addcompetionuser(
     HttpServletRequest request, HttpServletResponse response, String users, int competionId) {
   try {
     PrintWriter out = response.getWriter();
     if (users != null) {
       String[] strs = users.split(",");
       // 性能有问题再修改统一提交
       int len = strs.length;
       for (int i = 0; i < len; i++) {
         usercompetionService.save(Integer.parseInt(strs[i]), competionId, CommonConstant.CROLE_5);
       }
     }
     // examService.createExam(name, userId, competionId);
     String str = "{'sucess':'sucess'}";
     out.write(str);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  /**
   * 导出学生竞赛信息
   *
   * @param request
   * @param response
   */
  @RequestMapping("/exportdata")
  public void exportdata(
      HttpServletRequest request, HttpServletResponse response, int competionId) {
    try {

      List<UserCompetionData> list = usercompetionService.getCompetionStudentData(competionId);

      //			 String path = request.getSession().getServletContext().getRealPath("/export/");
      //			 File file = new File(path+"/比赛信息.csv");
      //			 if (!file.exists())
      //			 {
      //				 file.createNewFile();
      //			 }

      //			 FileOutputStream out=null;
      //		        OutputStreamWriter osw=null;
      //		        BufferedWriter bw=null;
      StringBuffer sb = new StringBuffer();
      try {
        //		            out = new FileOutputStream(file);
        //		            osw = new OutputStreamWriter(out);
        //		            bw =new BufferedWriter(osw);
        sb.append("#").append("学生编号,比赛成绩").append("\r");
        if (list != null && !list.isEmpty()) {

          for (UserCompetionData ucd : list) {
            sb.append(ucd.getUsername()).append(",").append(ucd.getScore() + "").append("\r");
          }
        }
      } catch (Exception e) {
      } finally {
        //		            if(bw!=null){
        //		            	   bw.close();
        //		                    bw=null;
        //		            }
        //		            if(osw!=null){
        //		            	 osw.close();
        //		                    osw=null;
        //		            }
        //		            if(out!=null){
        //		            	 out.close();
        //		                    out=null;
        //		            }
      }
      // 以流的形式下载文件。
      //	            InputStream fis = new BufferedInputStream(new FileInputStream(file));
      //	            byte[] buffer = new byte[fis.available()];
      //	            fis.read(buffer);
      //	            fis.close();
      //	            // 清空response
      //	            response.reset();
      //	            // 设置response的Header
      //	            response.addHeader("Content-Disposition", "attachment;filename=" + new
      // String(file.getName().getBytes("UTF-8"),"ISO-8859-1"));
      //	            response.addHeader("Content-Length", "" + file.length());
      //	            response.setContentType("application/octet-stream");
      //	            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
      //	            //加上UTF-8文件的标识字符
      //	            toClient.write(new byte []{(byte) 0xEF ,(byte) 0xBB ,(byte) 0xBF});
      //	            toClient.write(buffer);
      //	            toClient.flush();
      //	            toClient.close();

      OutputStreamWriter outw = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
      // 要输出的内容
      //		         result = (String)contentMap.get(RESPONSE_RESULT);
      response.setHeader(
          "Content-Disposition",
          "attachment;filename=" + new String("比赛信息.csv".getBytes("UTF-8"), "ISO-8859-1"));
      outw.write(new String(new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}));
      outw.write(sb.toString());
      outw.flush();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }