package com.bonus.registration.controller; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.bonus.registration.beans.RegistrationBean; import com.bonus.registration.service.RegistrationService; import com.bonus.sys.AjaxRes; import com.bonus.sys.BaseController; import com.bonus.sys.GlobalConst; import com.bonus.sys.Page; import com.bonus.sys.UserShiroHelper; import com.bonus.sys.beans.UserBean; import com.bonus.sys.service.UserService; @Controller @RequestMapping("/backstage/registration/") public class RegistrationController extends BaseController { @Autowired RegistrationService service; @Autowired UserService userService; @RequestMapping("list") public String List(Model model) { return "/registration/registrationList"; } @RequestMapping("findByPage") public String findByPage(@RequestBody Page page, RegistrationBean o, Model model) { try { o = page.getObj(); page = service.findByPage(o, page); if (page.getResults() != null && page.getResults().size() != 0) { SimpleDateFormat df = new SimpleDateFormat("yyyy");// 设置日期格式 String date = df.format(new Date()); for (int i = 0; i < page.getResults().size(); i++) { RegistrationBean bean = page.getResults().get(i); String temp = date + "年年度考试 第" + bean.getTimes() + "场"; bean.setTimesName(temp); page.getResults().set(i, bean); } } model.addAttribute("page", page); } catch (Exception e) { e.printStackTrace(); } return "/registration/registrationDetails"; } @RequestMapping("registrationFormPage") public String questionFormPage(Model model) { return "/registration/registrationForm"; } @RequestMapping("findRegistrationForm") public String questionFormPage(RegistrationBean o, Model model) { try { String type = o.getType(); o = service.findById(o); o.setType(type); model.addAttribute("registration", o); } catch (Exception e) { e.printStackTrace(); } return "/registration/registrationForm"; } @RequestMapping("addRegistration") @ResponseBody public AjaxRes addQuestion(@RequestBody RegistrationBean o) { AjaxRes ar = getAjaxRes(); try { RegistrationBean b = service.findByidcard(o.getIdcard()); UserBean ubean = userService.findByidcard(o.getIdcard()); if (b == null && ubean != null) { ar = service.addRegistration(o); ar.setSucceedMsg("添加考试人员成功"); } else if (b != null) { ar.setFailMsg("该人员已参加其他场次考试,请勿重复添加"); } else if (ubean == null) { ar.setFailMsg("该人员身份未录入系统,请联系管理员"); } } catch (Exception e) { e.printStackTrace(); ar.setFailMsg("新增考试人员失败,异常处理!"); } return ar; } @RequestMapping("judgeExamPerson") @ResponseBody public AjaxRes judgeExamPerson(RegistrationBean o,HttpServletRequest request) { AjaxRes ar = getAjaxRes(); try { UserBean user = UserShiroHelper.getRealCurrentUser(); // UserBean user = (UserBean) request.getSession().getAttribute("currentUser"); System.out.println("你妹的"); Integer res = service.judgeExamPerson(user); if (res == 0) { ar.setSucceedMsg("您被管理员拒绝参加本场考试请现在离开考场!!!"); } } catch (Exception e) { e.printStackTrace(); ar.setFailMsg("参加考试失败!"); } return ar; } @RequestMapping("updateRegistration") @ResponseBody public AjaxRes updateRegistration(@RequestBody RegistrationBean o) { AjaxRes ar = getAjaxRes(); try { ar = service.updateRegistration(o); } catch (Exception e) { e.printStackTrace(); ar.setFailMsg("修改失败!"); } return ar; } @RequestMapping("deleteApply") @ResponseBody public AjaxRes deleteApply(RegistrationBean o) { AjaxRes ar = getAjaxRes(); try { ar = service.deleteApply(o); } catch (Exception e) { logger.error(e.toString(), e); ar.setFailMsg("删除失败!"); } return ar; } @RequestMapping("toUpdateTimes") @ResponseBody public AjaxRes toUpdateTimes(RegistrationBean o) { AjaxRes ar = getAjaxRes(); try { Integer res = service.toUpdateTimes(o); RegistrationBean bean = service.findExamProfess(o); Integer re = 0; if (bean == null) { // exam_profess表插入新专业 re = service.insertExamProfess(o); } else { // 本身就有这个专业 re = 1; } if (res == 1 && re == 1) { ar.setSucceedMsg("修改成功," + o.getName() + "可参加本场考试!"); } else { ar.setSucceedMsg("修改失败" + o.getName() + "不可参加本场考试!"); } } catch (Exception e) { logger.error(e.toString(), e); ar.setFailMsg("修改失败!"); } return ar; } @RequestMapping("toUpdateIsActive") @ResponseBody public AjaxRes toUpdateIsActive(RegistrationBean o) { AjaxRes ar = getAjaxRes(); try { Integer res = service.toUpdateIsActive(o); if (res == 1) { ar.setSucceedMsg("拒绝成功,请您驱赶" + o.getName() + "离开考场!"); } else { ar.setSucceedMsg("修改失败"); } } catch (Exception e) { logger.error(e.toString(), e); ar.setFailMsg("修改失败!"); } return ar; } @RequestMapping("changeIsActive") @ResponseBody public AjaxRes changeIsActive() { AjaxRes ar = getAjaxRes(); UserBean user = UserShiroHelper.getRealCurrentUser(); try { ar = service.changeIsActive(user); } catch (Exception e) { logger.error(e.toString(), e); ar.setFailMsg("删除失败!"); } return ar; } @RequestMapping(value = "import", method = RequestMethod.POST) @ResponseBody public AjaxRes export(@RequestParam("file") MultipartFile file) { AjaxRes ar = getAjaxRes(); AjaxRes ars = getAjaxRes(); Integer result = 0; try { // @RequestParam("file") MultipartFile file 是用来接收前端传递过来的文件 // 1.创建workbook对象,读取整个文档 InputStream inputStream = file.getInputStream(); POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream); HSSFWorkbook wb = new HSSFWorkbook(poifsFileSystem); // 2.读取页脚sheet HSSFSheet hssfSheet = wb.getSheetAt(0); // 判断Sheet是否为空 if (hssfSheet != null) { System.out.println(hssfSheet.getLastRowNum()); // 获得数据的总行数 int totalRowNum = hssfSheet.getLastRowNum(); // 要获得属性 String name = ""; String latitude = ""; String specialty = ""; String times = ""; // 获得所有数据 for (int i = 1; i <= totalRowNum; i++) { // 获得第i行对象 Row row = hssfSheet.getRow(i); // 获得获得第i行第0列的 String类型对象 Cell cell = row.getCell((short) 0); name = cell.getStringCellValue().toString(); System.out.println("名字:" + name); } // 遍历行row,continue只是终止本次循环,接着还执行后面的循环 for (int rownum = 1; rownum <= hssfSheet.getLastRowNum(); rownum++) { // 获取到每一行 HSSFRow sheetRow = hssfSheet.getRow(rownum); if (sheetRow != null && !isRowEmpty(sheetRow)) { // 保存数据到数据库 System.out.println(sheetRow); // 获得第i行对象 Row row = hssfSheet.getRow(rownum); // 获得获得第i行第0列的 String类型对象 Cell cell = row.getCell((short) 0); name = cell.getStringCellValue().toString(); // 获得获得第i行第2列的对象并转为int类型 Cell cell1 = row.getCell((short) 1); latitude = cell1.getStringCellValue().toString(); // 获取考试场次 int类型的值转换为string类型再由对象接收转换后的值 if (row.getCell(2) != null) { row.getCell(2).setCellType(Cell.CELL_TYPE_STRING); times = (row.getCell(2).getStringCellValue()); } // 获取专业ID int类型的值转换为string类型再由对象接收转换后的值 if (row.getCell(3) != null) { row.getCell(3).setCellType(Cell.CELL_TYPE_STRING); specialty = (row.getCell(3).getStringCellValue()); } ars = compareSaveData(sheetRow); if (name.isEmpty()) { ar.setSucceedMsg("姓名为空,请更改"); result = 3; break; } if (specialty.isEmpty()) { ar.setSucceedMsg("专业id为空,请更改"); result = 3; break; } if (times.isEmpty()) { ar.setSucceedMsg("考试场次为空,请更改"); result = 3; break; } boolean isRule = isSpecialtyRule(specialty); if (!isRule) { ar.setSucceedMsg("专业id不符合规则,请更改"); result = 3; break; } if (ars.getRes() == 2 || ars.getRes() == 3) { ar.setFailMsg(ars.getResMsg()); result = ars.getRes(); break; } } } if (result != 2 && result != 3) { for (int rownums = 1; rownums <= hssfSheet.getLastRowNum(); rownums++) { // 获取到每一行 HSSFRow sheetRows = hssfSheet.getRow(rownums); if (sheetRows != null && !isRowEmpty(sheetRows)) { addSaveData(sheetRows); } } ar.setSucceedMsg(GlobalConst.IMP_SUCCEED); } } } catch (Exception e) { logger.error(e.toString(), e); ar.setFailMsg(GlobalConst.IMP_FAIL); } return ar; } /** * 判断专业id是否符合规则 * * @param specialty * @return */ private boolean isSpecialtyRule(String specialty) { boolean tf = true; try { int orgId = Integer.parseInt(specialty); UserBean bean = userService.findOrgId(orgId); if(bean == null){ tf = false; } } catch (Exception e) { logger.error(e.toString()); tf = false; } return tf; } // 判断该行是否为空 public static boolean isRowEmpty(Row row) { for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { return false; } } return true; } private void addSaveData(HSSFRow sheetRow) throws Exception { // 保存数据到数据库 RegistrationBean bean = new RegistrationBean(); bean.setName(getValue(sheetRow.getCell(0))); bean.setIdcard(change(getValue(sheetRow.getCell(1)))); bean.setTimes(change(getValue(sheetRow.getCell(2)))); bean.setSpecialty(change(getValue(sheetRow.getCell(3)))); RegistrationBean b = service.findByidcard(change(getValue(sheetRow.getCell(1)))); UserBean ubean = userService.findByidcard(change(getValue(sheetRow.getCell(1)))); if (b == null && ubean != null) { service.addRegistration(bean); } } private AjaxRes compareSaveData(HSSFRow sheetRow) throws Exception { // 保存数据到数据库 AjaxRes ar = getAjaxRes(); RegistrationBean bean = new RegistrationBean(); bean.setName(getValue(sheetRow.getCell(0))); bean.setIdcard(change(getValue(sheetRow.getCell(1)))); bean.setTimes(change(getValue(sheetRow.getCell(2)))); bean.setSpecialty(change(getValue(sheetRow.getCell(3)))); ar = service.findUserByIdAndTimes(change(getValue(sheetRow.getCell(1)))); return ar; } private static String getValue(HSSFCell hssfCell) { // hssfCell.getCellType() 获取当前列的类型 if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { return String.valueOf(hssfCell.getNumericCellValue()); } else { return String.valueOf(hssfCell.getStringCellValue()); } } // 将double类型字符串转化为整型 private static String change(String str) { if (str.indexOf('.') != -1) { return str.substring(0, str.indexOf('.')); } else { return str; } } }