Examination_system/Examination_system-1/.svn/pristine/4d/4dbd29d0e1eec9c5438510b01df...

281 lines
7.9 KiB
Plaintext
Raw Normal View History

2023-10-30 13:10:40 +08:00
package com.bonus.seat.service;
import java.io.InputStream;
import java.util.List;
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.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;
import com.bonus.score.beans.ScoreBean;
import com.bonus.seat.beans.SeatBean;
import com.bonus.seat.dao.SeatDao;
import com.bonus.sys.AjaxRes;
import com.bonus.sys.BaseServiceImp;
import com.bonus.sys.GlobalConst;
import com.bonus.sys.beans.UserBean;
import com.bonus.sys.dao.UserDao;
@Service("SeatService")
public class SeatServiceImp extends BaseServiceImp<SeatBean> implements SeatService {
@Autowired
SeatDao dao;
@Autowired
private UserDao userDao;
@Override
public List<SeatBean> findRoomInfo() {
return dao.finRoomInfo();
}
@Override
public Integer insertInfo(SeatBean o) {
Integer res = 0;
res = dao.insertInfo(o);
return res;
}
@Override
public List<SeatBean> checkSeat(SeatBean o) {
return dao.checkSeat(o);
}
@Override
public SeatBean findSeatByIp(String ipAddress) {
return dao.findSeatByIp(ipAddress);
}
@Override
public SeatBean findPersonBySeat(SeatBean seat) {
return dao.findPersonBySeat(seat);
}
@Override
public void insertNowIdcard(SeatBean pSeat) {
dao.insertNowIdcard(pSeat);
}
@Override
public void updateIsActive(ScoreBean o) {
dao.updateIsActive(o);
}
@Transactional
public AjaxRes addRegistration(SeatBean o) {
AjaxRes ar = getAjaxRes();
try {
int count = dao.insertInfo(o);
if(count == 1)
ar.setSucceedMsg("新增成功");
else
ar.setFailMsg("新增失败!");
} catch(Exception e) {
e.printStackTrace();
ar.setFailMsg("新增失败");
}
return ar;
}
@Override
public AjaxRes deleteApply(SeatBean o) {
AjaxRes ar = getAjaxRes();
try {
int count = dao.deleteApply(o);
if(count == 1)
ar.setSucceedMsg("删除成功");
else
ar.setFailMsg("删除失败!");
} catch(Exception e) {
e.printStackTrace();
ar.setFailMsg("删除失败");
}
return ar;
}
@Override
@Transactional(rollbackFor=Exception.class)
public AjaxRes export(MultipartFile file) {
AjaxRes ar = getAjaxRes();
Integer result = 0;
int team=1;
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 specialty = "";
// 遍历行rowcontinue只是终止本次循环接着还执行后面的循环
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);
// 获得获得第i行第2列的对象并转为int类型
Cell cell1 = row.getCell((short) 1);
if (row.getCell(4) != null) {
row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
specialty = (row.getCell(4).getStringCellValue());
}
/*
* boolean isRule = isSpecialtyRule(specialty);
*
* if (!isRule) { ar.setSucceedMsg("专业id不符合规则请更改"); result = 3; break; }
*/
}
}
// if (result != 3) {
for (int rownums = 1; rownums <= hssfSheet.getLastRowNum(); rownums++) {
// 获取到每一行
HSSFRow sheetRows = hssfSheet.getRow(rownums);
if (sheetRows != null && !isRowEmpty(sheetRows)) {
team = addSaveData(sheetRows);
}
}
//ar.setSucceedMsg(GlobalConst.IMP_SUCCEED);
// }
if(team ==1 ) {
ar.setSucceedMsg(GlobalConst.IMP_SUCCEED);
}else {
ar.setFailMsg(GlobalConst.IMP_FAIL);
}
}
} catch (Exception e) {
logger.error(e.toString(), e);
ar.setFailMsg(GlobalConst.IMP_FAIL);
}
return ar;
}
private boolean isSpecialtyRule(String specialty) {
boolean tf = true;
try {
int orgId = Integer.parseInt(specialty);
UserBean bean = userDao.findOrgId(orgId);
if(bean == null){
tf = false;
}
} catch (Exception e) {
logger.error(e.toString());
tf = false;
}
return tf;
}
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;
}
}
private int addSaveData(HSSFRow sheetRow) throws Exception {
// 保存数据到数据库
int team=1;
try {
SeatBean bean = new SeatBean();
bean.setRoomId(getValue(sheetRow.getCell(0)));
bean.setName(getValue(sheetRow.getCell(1)));
bean.setIdcard(change(getValue(sheetRow.getCell(2))));
bean.setRoomSession(change(getValue(sheetRow.getCell(3))));
bean.setSeatNum(change(getValue(sheetRow.getCell(4))));
bean.setDepartment(change(getValue(sheetRow.getCell(5))));
bean.setSpecialty(change(getValue(sheetRow.getCell(6))));
bean.setAuthIdentity(getValue(sheetRow.getCell(7)));
int i=dao.insertInfo(bean);
if(i != 1) {
team=0;
throw new RuntimeException("插入座位号失败!");
}
} catch (Exception e) {
team=0;
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
e.printStackTrace();
}
return team;
}
// 判断该行是否为空
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;
}
@Override
public SeatBean findById(SeatBean o) {
// TODO Auto-generated method stub
return dao.findById(o);
}
@Override
public int updateBykc(SeatBean bean) {
// TODO Auto-generated method stub
return dao.updateBykc(bean);
}
@Override
public int findCount() {
// TODO Auto-generated method stub
return dao.findCount();
}
@Override
public void insertlogo(SeatBean bean) {
// TODO Auto-generated method stub
dao.insertlogo(bean);
}
@Override
public void deletebyTime(String currentDateTime) {
// TODO Auto-generated method stub
dao.deletebyTime(currentDateTime);
}
}