Examination_system/Examination_system-1/.svn/pristine/d2/d20dd004361a01d02c0362da156...

277 lines
9.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.bonus.enroll.controller;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.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.core.StringHelper;
import com.bonus.enroll.beans.EnrollBean;
import com.bonus.enroll.service.EnrollService;
import com.bonus.sys.AjaxRes;
import com.bonus.sys.BaseController;
import com.bonus.sys.GlobalConst;
import com.bonus.sys.Page;
import com.bonus.sys.beans.CheckCard;
import com.bonus.sys.beans.UserBean;
@Controller
@RequestMapping("/backstage/enroll/")
public class EnrollController extends BaseController<EnrollBean>{
@Autowired
private EnrollService service;
private List<EnrollBean> enrolist=new ArrayList<EnrollBean>();
private Map<String,String> map=new HashMap<String, String>();
@RequestMapping("list")
public String launchList(Model model) {
return "/enroll/enrollList";
}
//list
@RequestMapping("findByPage")
@ResponseBody
public AjaxRes findByPage(Page<EnrollBean> page,EnrollBean o){
AjaxRes ar = getAjaxRes();
try {
Page<EnrollBean> result = service.findByPage(o, page);
Map<String,Object> p = new HashMap<String,Object>();
p.put("list",result);
ar.setSucceed(p);
} catch (Exception e) {
logger.error(e.toString(),e);
ar.setFailMsg(GlobalConst.DATA_FAIL);
}
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;
int errorNum = 0;
int status=0;//此状态的作用是区分是否有异常数据
enrolist.clear();
map.clear();
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 idcard = ""; //身份证号
// 遍历行rowcontinue只是终止本次循环接着还执行后面的循环
for (int rownum = 1; rownum <= hssfSheet.getLastRowNum(); rownum++) {
// 获取到每一行
HSSFRow sheetRow = hssfSheet.getRow(rownum);
if (sheetRow != null && !isRowEmpty(sheetRow)) {
// 获取到每一行
errorNum = rownum;
// 获得第i行对象
Row row = hssfSheet.getRow(rownum);
if (sheetRow != null && !isRowEmpty(sheetRow)) {
// 先去判断
ars = compareSaveData(sheetRow, rownum + 1);
if (ars.getRes() == 3) {
ar.setFailMsg(ars.getResMsg());
result = 3;
status=1;
break;
}
if (ars.getRes() == 4) {
ar.setFailMsg(ars.getResMsg());
result = 4;
status=1;
break;
}
if (ars.getRes() == 6) {
ar.setFailMsg(ars.getResMsg());
result = 6;
status=1;
break;
}
}
if (result != 6 && result != 3 && result != 4) {
//for (int rownums = 1; rownums <= hssfSheet.getLastRowNum(); rownums++) {
// 获取到每一行
HSSFRow sheetRows = hssfSheet.getRow(rownum);
if (sheetRows != null && !isRowEmpty(sheetRows)) {
addSaveData(sheetRows);
}
// }
}else {
status=1;
}
}
}
if(status == 0) {
for(int i=0;i<enrolist.size();i++) {
service.add(enrolist.get(i));
}
String zwh = ""; //考试场次(1、2、3...)用于分配座位号
// 获得第i行对象
Row rows = hssfSheet.getRow(1);
// 获得获得第i行第0列的 String类型对象
Cell cells = rows.getCell((short) 3);
zwh = cells.getNumericCellValue()+"";
Double f = Double.valueOf(zwh);
int valueOf = (int)Math.ceil(f);
System.out.println("用于分配座位号:" + zwh);
EnrollBean beans = new EnrollBean();
beans.setTimes(valueOf+"");
List<EnrollBean> lists = service.finds(beans);
Collections.shuffle(lists);
String[] seatArr={"1","3","2","4","5", "6","8","7","9", "10",
"21","22","23","24","25", "26","27","28","29","30",
"11","12","13","14","15", "16","17","20","19","18",
"41","42","43","44","45", "46","47","48","49","50",
"33","32","31","34","35", "36","37","40","39","38"
};
for (int i = 0;i<lists.size();i++) {
beans.setSeatNum(seatArr[i]);
beans.setName(lists.get(i).getName());
beans.setIdcard(lists.get(i).getIdcard());
Integer res = 0;
res = service.insertInfo(beans);
service.updateInfo(beans);
if(res == 0) {
ar.setFailMsg("分配座位失败!!!");
}else{
ar.setSucceedMsg("分配座位成功!!!");
}
}
}
}
} catch (Exception e) {
logger.error(e.toString(), e);
ar.setFailMsg(GlobalConst.IMP_FAIL);
}
return ar;
}
private AjaxRes compareSaveData(HSSFRow sheetRow, int rownum) throws Exception {
AjaxRes ar = getAjaxRes();
try {
String loginName = change(getValue(sheetRow.getCell(2))); // 身份证号
String examCount = change(getValue(sheetRow.getCell(3))); // 考试场次
CheckCard card = new CheckCard();
boolean checkIdcard = card.verify(loginName); // 身份证校验
if(examCount.trim().equals("") || examCount.trim() =="") {
ar.setFailMsg("表格第" + rownum + "行考试场次为空,请填写考试场次");
ar.setRes(4);
return ar;
}
if (checkIdcard == false) {
ar.setFailMsg("表格第" + rownum + "行身份证合法性有误,请详细查看身份证是否有误");
ar.setRes(3);
return ar;
}
UserBean o = null;
int chachong1 = service.findCountByLoginName(loginName);
if(map.get(loginName) ==null) {
map.put(loginName, loginName);
}else {
ar.setFailMsg(loginName+"身份证有重复,导入失败");
ar.setRes(6);
return ar;
}
if (chachong1 > 0) {
ar.setFailMsg(loginName+"身份证有重复,导入失败");
ar.setRes(6);
return ar;
}
} catch (Exception e) {
logger.error(e.toString(), e);
ar.setFailMsg("第" + rownum + "行数据有误,导入失败");
}
return ar;
}
// 判断该行是否为空
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 {
// 保存数据到数据库
EnrollBean bean = new EnrollBean();
bean.setName(getValue(sheetRow.getCell(1)));
bean.setIdcard(change(getValue(sheetRow.getCell(2))));
bean.setTimes(change(getValue(sheetRow.getCell(3))));
bean.setSpecialty(change(getValue(sheetRow.getCell(4))));
enrolist.add(bean);
//service.add(bean);
}
private static String getValue(HSSFCell hssfCell) {
// hssfCell.getCellType() 获取当前列的类型
if(hssfCell ==null) {
return "";
}
else 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;
}
}
public static void main(String[] args) {
String str="123";
Map<String,String> map=new HashMap<String, String>();
map.put(str, str);
map.put(str, str);
System.out.println(map.get("1233"));
}
}