package com.nationalelectric.greenH5; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.nationalelectirc.Constant.Constant; import com.nationalelectirc.utils.RestResult; import com.nationalelectric.greenH5.po.GreenUserInfo; @Controller @RequestMapping("/exam") public class GreenExamController extends GreenBaseController { @RequestMapping(value = "/getExamList", method = RequestMethod.POST) @ResponseBody public RestResult getExamList(@RequestBody Map data) { String userId = data.get("userId") == null || "".equals(data.get("userId")) ? null : data.get("userId").toString(); GreenUserInfo info = getUserInfo(userId); if (info == null) { return new RestResult(Constant.FAILED, "非法用户"); } HashMap rMap = new HashMap(); try { String sql = " SELECT e.exam_name examName, DATE_FORMAT(e.exam_time, '%Y-%m-%d') examTime,e.uuid, e.exam_switch " + " FROM green_exam e LEFT JOIN green_exam_fraction ef ON e.uuid = ef.exam_id and ef.is_deleted = 'N' and ef.user_id =?" + " WHERE e.is_deleted = 'N' AND exam_switch = '1' AND ef.fraction is NULL "; List> examList = hibernateDao.queryForListWithSql(sql.toString(),new Object[]{userId}); rMap.put("list", examList); // rMap.put("list", productionList); return new RestResult(Constant.SUCCESS, "请求成功",rMap); } catch (Exception e) { e.printStackTrace(); return new RestResult(Constant.FAILED, "请求失败"); } } @RequestMapping(value = "/getExamDetail", method = RequestMethod.POST) @ResponseBody public RestResult getExamDetail(@RequestBody Map data) { String userId = data.get("userId") == null || "".equals(data.get("userId")) ? null : data.get("userId").toString(); GreenUserInfo info = getUserInfo(userId); if (info == null) { return new RestResult(Constant.FAILED, "非法用户"); } String uuid = data.get("uuid").toString(); HashMap rMap = new HashMap(); try { String sql2 = "select * from green_exam_fraction where 1=1 AND exam_id = ? AND user_id = ? AND is_deleted ='N'"; List> examList2 = hibernateDao.queryForListWithSql(sql2.toString(),new Object[]{uuid,userId}); if(examList2.size() > 0){ return new RestResult(Constant.FAILED, "该场考试您已经考过了。"); } String sql = "SELECT ed.exam_id examId,es.uuid projectId,es.subject ,es.optionA,es.optionB,es.optionC,es.optionD,es.optionE," + " es.type FROM green_exam_detail ed LEFT JOIN green_exam_subject es on es.is_deleted = 'N' and " + " ed.exam_project_id = es.uuid where 1 =1 and ed.exam_id = ? ORDER BY ed.sort ASC "; List> examList = hibernateDao.queryForListWithSql(sql.toString(),new Object[]{uuid}); rMap.put("list", examList); // rMap.put("list", productionList); return new RestResult(Constant.SUCCESS, "请求成功",rMap); } catch (Exception e) { e.printStackTrace(); return new RestResult(Constant.FAILED, "请求失败"); } } @RequestMapping(value = "/getMyExamAnswerList", method = RequestMethod.POST) @ResponseBody public RestResult getMyExamAnswerList(@RequestBody Map data) { String userId = data.get("userId") == null || "".equals(data.get("userId")) ? null : data.get("userId").toString(); GreenUserInfo info = getUserInfo(userId); if (info == null) { return new RestResult(Constant.FAILED, "非法用户"); } HashMap rMap = new HashMap(); try { String sql = "SELECT ef.fraction,e.exam_name examName,DATE_FORMAT(e.exam_time, '%Y-%m-%d') examTime FROM green_exam_fraction ef INNER JOIN green_exam e on e.uuid =ef.exam_id and " + " e.is_deleted = 'N' where ef.is_deleted = 'N' AND user_id = ?"; List> productionList = hibernateDao.queryForListWithSql(sql.toString(), new Object[]{ userId }); rMap.put("list", productionList); return new RestResult(Constant.SUCCESS, "请求成功",rMap); } catch (Exception e) { e.printStackTrace(); return new RestResult(Constant.FAILED, "请求失败"); } } @RequestMapping(value = "/saveExam", method = RequestMethod.POST) @ResponseBody public RestResult saveExam(@RequestBody Map data) { String userId = data.get("userId") == null || "".equals(data.get("userId")) ? null : data.get("userId").toString(); // {answer=A&B&A,B,C, examId=1, userId=13dffaf11c8f4ca398ee3bba8009ce7a} GreenUserInfo info = getUserInfo(userId); if (info == null) { return new RestResult(Constant.FAILED, "非法用户"); } HashMap rMap = new HashMap(); try { String examId = data.get("examId").toString(); String answer = data.get("answer").toString(); String sql2 = "select * from green_exam_fraction where 1=1 AND exam_id = ? AND user_id = ? AND is_deleted ='N'"; List> examList2 = hibernateDao.queryForListWithSql(sql2.toString(),new Object[]{examId,userId}); if(examList2.size() > 0){ return new RestResult(Constant.FAILED, "该场考试您已经考过了。"); } String[] answers = answer.split("&"); String sql = "SELECT ed.answer,es.uuid,es.solution,es.type FROM green_exam_detail ed LEFT JOIN green_exam_subject es on " + " ed.exam_project_id = es.uuid and es.is_deleted = 'N' WHERE 1= 1 AND ed.is_deleted = 'N' AND ed.exam_id =? " + " ORDER BY sort ASC"; List> productionList = hibernateDao.queryForListWithSql(sql.toString(), new Object[]{ examId }); int fraction = 0; for (int i = 0; i < productionList.size(); i++) { if(answers[i].equals(productionList.get(i).get("solution").toString())){ // System.err.println("正确"); fraction += Integer.valueOf(productionList.get(i).get("answer").toString()); }else{ // System.err.println("错误"+answers[i]); } } String insertUserSql = "INSERT INTO green_exam_fraction (uuid, exam_id, exam_answer, fraction, user_id, user_name, " + "exam_time, creator, modifier, is_deleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'N');"; String id = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase(); hibernateDao.executeSqlUpdate(insertUserSql, new Object[] { id, examId, answer, fraction, userId ,info.getRealName(),new Date(),userId,userId}); // rMap.put("list", productionList); return new RestResult(Constant.SUCCESS, "请求成功",rMap); } catch (Exception e) { e.printStackTrace(); return new RestResult(Constant.FAILED, "请求失败"); } } }