use of com.bc.pmpheep.back.po.Textbook in project pmph by BCSquad.
the class TextbookServiceImpl method importTopicExcel.
@SuppressWarnings("resource")
@Override
public List<Textbook> importTopicExcel(MultipartFile file) throws CheckedServiceException {
String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
Workbook workbook = null;
InputStream in = null;
try {
in = file.getInputStream();
} catch (FileNotFoundException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.NULL_PARAM, "未获取到文件");
} catch (IOException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "读取文件失败");
}
try {
if ((".xls").equals(fileType)) {
workbook = new HSSFWorkbook(in);
} else if ((".xlsx").equals(fileType)) {
workbook = new XSSFWorkbook(in);
} else {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "读取的不是Excel文件");
}
} catch (IOException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "读取文件失败");
}
List<Textbook> textbookList = new ArrayList<>();
for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
Sheet sheet = workbook.getSheetAt(numSheet);
if (null == sheet) {
continue;
}
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
Textbook textbook = new Textbook();
Row row = sheet.getRow(rowNum);
if (null == row) {
break;
}
Cell first = row.getCell(0);
Cell second = row.getCell(1);
Cell third = row.getCell(2);
Cell fourth = row.getCell(3);
if ((ObjectUtil.isNull(first) || "".equals(first.toString())) && (ObjectUtil.isNull(second) || "".equals(second.toString())) && (ObjectUtil.isNull(third) || "".equals(third.toString()))) {
break;
}
String bookName = StringUtil.getCellValue(second);
if (StringUtil.strLength(bookName) > 25) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "图书名称不能超过25个字数,请修改后再上传");
}
String topicNumber = StringUtil.getCellValue(fourth);
if (StringUtil.notEmpty(topicNumber)) {
if (topicNumber.indexOf(".0") > -1) {
topicNumber = topicNumber.substring(0, topicNumber.indexOf(".0"));
}
}
if (StringUtil.strLength(topicNumber) > 30) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "选题号不能超过30个字数,请修改后再上传");
}
Integer sort = 0;
Integer round = 0;
try {
sort = ObjectUtil.getCellValue(first);
} catch (NumberFormatException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "图书序号格式错误,请按照模板格式修改后" + "再上传");
}
try {
round = ObjectUtil.getCellValue(third);
} catch (NumberFormatException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "书籍版次格式错误,请按照模板格式修改后" + "再上传");
}
textbook.setSort(sort);
textbook.setTextbookName(bookName);
textbook.setTextbookRound(round);
textbook.setTopicNumber(topicNumber);
textbookList.add(textbook);
}
}
return textbookList;
}
use of com.bc.pmpheep.back.po.Textbook in project pmph by BCSquad.
the class TextbookServiceImpl method addOrUpdateTextBookList.
@SuppressWarnings("unchecked")
@Override
public List<Textbook> addOrUpdateTextBookList(Long materialId, Boolean isPublic, String textbooks, String sessionId) throws CheckedServiceException {
if (ObjectUtil.isNull(materialId)) {
throw new CheckedServiceException(CheckedExceptionBusiness.MATERIAL, CheckedExceptionResult.NULL_PARAM, "教材id不能为空");
}
if (ObjectUtil.isNull(isPublic)) {
throw new CheckedServiceException(CheckedExceptionBusiness.MATERIAL, CheckedExceptionResult.NULL_PARAM, "可见性区别不能为空");
}
if (StringUtil.isEmpty(textbooks)) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "书籍信息不能为空");
}
/*
* 检测同一教材下书名和版次都相同的数据
*/
List<Map<String, Object>> list = new ArrayList<>();
Gson gson = new GsonBuilder().serializeNulls().create();
List<Textbook> bookList = gson.fromJson(textbooks, new TypeToken<ArrayList<Textbook>>() {
}.getType());
/*
* 对数据进行排序
*/
ComparatorChain comparatorChain = new ComparatorChain();
comparatorChain.addComparator(new BeanComparator<Textbook>("sort"));
Collections.sort(bookList, comparatorChain);
/*
* 查询此教材下现有的书籍
*/
List<Textbook> textbookList = textbookDao.getTextbookByMaterialId(materialId);
List<Long> ids = new ArrayList<>();
for (Textbook textbook : textbookList) {
ids.add(textbook.getId());
}
// 装数据库中本来已经有的书籍id
List<Long> delBook = new ArrayList<>();
// 判断书序号的连续性计数器
int count = 1;
for (Textbook book : bookList) {
if (ObjectUtil.isNull(book.getMaterialId())) {
book.setMaterialId(materialId);
}
if (StringUtil.isEmpty(book.getTextbookName())) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "书籍名称不能为空");
}
if (StringUtil.strLength(book.getTextbookName()) > 50) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "书籍名称字数不能超过25个,请修改后再提交");
}
if (ObjectUtil.isNull(book.getTextbookRound())) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "书籍轮次不能为空");
}
if (book.getTextbookRound().intValue() > 2147483647) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "图书轮次过大,请修改后再提交");
}
if (ObjectUtil.isNull(book.getSort())) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "图书序号不能为空");
}
if (book.getSort().intValue() > 2147483647) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "图书序号过大,请修改后再提交");
}
if (ObjectUtil.isNull(book.getFounderId())) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "创建人id不能为空");
}
if (count != book.getSort()) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "书籍序号必须从1开始且连续");
}
Map<String, Object> map = new HashMap<>();
map.put(book.getTextbookName(), book.getTextbookRound());
if (list.contains(map)) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "同一教材下书名和版次不能都相同");
}
/*
* 设置书目录时保存操作传回来的对象里可能有已经存在的书籍,已经存在的修改 相关信息,没有的进行新增操作
*/
if (ObjectUtil.notNull(textbookDao.getTextbookById(book.getId()))) {
textbookDao.updateTextbook(book);
delBook.add(book.getId());
} else {
textbookDao.addTextbook(book);
}
list.add(map);
count++;
}
/* 设置书目录时若删除了部分书籍,找出这些书籍的id并将表中的相关数据删除掉 */
ids.removeAll(delBook);
if (CollectionUtil.isNotEmpty(ids)) {
for (Long id : ids) {
if (CollectionUtil.isNotEmpty(decPositionService.listDecPositionsByTextbookId(id)) && decPositionService.listDecPositionsByTextbookId(id).size() > 0) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "被申报的书籍不允许被删除");
} else {
textbookDao.deleteTextbookById(id);
}
}
}
/* 修改对应的教材的可见性区别 */
Material material = new Material();
material.setId(materialId);
material.setIsPublic(isPublic);
materialService.updateMaterial(material, sessionId);
return textbookDao.getTextbookByMaterialId(materialId);
}
use of com.bc.pmpheep.back.po.Textbook in project pmph by BCSquad.
the class TextbookServiceImpl method importExcel.
@SuppressWarnings({ "resource" })
@Override
public List<Textbook> importExcel(MultipartFile file, Long materialId) throws CheckedServiceException {
if (ObjectUtil.isNull(materialId)) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "教材id不能为空");
}
String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
Workbook workbook = null;
InputStream in = null;
try {
in = file.getInputStream();
} catch (FileNotFoundException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.NULL_PARAM, "未获取到文件");
} catch (IOException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "读取文件失败");
}
try {
if ((".xls").equals(fileType)) {
workbook = new HSSFWorkbook(in);
} else if ((".xlsx").equals(fileType)) {
workbook = new XSSFWorkbook(in);
} else {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "读取的不是Excel文件");
}
} catch (IOException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "读取文件失败");
} catch (OfficeXmlFileException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "此文档不是对应的.xls或.xlsx的Excel文档,请修改为正确的后缀名再进行上传");
}
List<Textbook> bookList = new ArrayList<>();
List<Textbook> books = textbookDao.getTextbookByMaterialId(materialId);
// 声明一个有人申报的书籍的集合
List<Textbook> havePeople = new ArrayList<>();
// 声明一个没有人申报的书籍集合
List<Textbook> noPeople = new ArrayList<>();
for (Textbook book : books) {
if (CollectionUtil.isNotEmpty(decPositionService.listDecPositionsByTextbookId(book.getId())) && decPositionService.listDecPositionsByTextbookId(book.getId()).size() > 0) {
havePeople.add(book);
} else {
noPeople.add(book);
}
}
/* 有人申报的教材,以数据库里查询出来的数据为准 */
if (null != havePeople || !havePeople.isEmpty()) {
for (Textbook book : havePeople) {
bookList.add(book);
}
}
for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
Sheet sheet = workbook.getSheetAt(numSheet);
if (null == sheet) {
continue;
}
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
Textbook textbook = new Textbook();
Row row = sheet.getRow(rowNum);
if (null == row) {
break;
}
Cell first = row.getCell(0);
Cell second = row.getCell(1);
Cell third = row.getCell(2);
if ((ObjectUtil.isNull(first) || "".equals(first.toString())) && (ObjectUtil.isNull(second) || "".equals(second.toString())) && (ObjectUtil.isNull(third) || "".equals(third.toString()))) {
break;
}
String bookName = StringUtil.getCellValue(second);
if (StringUtil.strLength(bookName) > 50) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "图书名称不能超过25个字数,请修改后再上传");
}
Integer sort = 0;
Integer round = 0;
try {
sort = ObjectUtil.getCellValue(first);
} catch (NumberFormatException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "图书序号格式错误,请按照模板格式修改后" + "再上传");
}
try {
round = ObjectUtil.getCellValue(third);
} catch (NumberFormatException e) {
throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "书籍版次格式错误,请按照模板格式修改后" + "再上传");
}
/* 无人申报的教材,如果与Excel文档相同的书籍,保留数据库里的数据,否则保存Excel文档里的书籍 */
if (null == noPeople || noPeople.isEmpty()) {
textbook.setSort(sort);
textbook.setTextbookName(bookName);
textbook.setTextbookRound(round);
bookList.add(textbook);
} else {
textbook.setSort(sort);
textbook.setTextbookName(bookName);
textbook.setTextbookRound(round);
for (Textbook book : noPeople) {
if (sort.intValue() == book.getSort() && round.intValue() == book.getTextbookRound() && bookName.trim().equals(book.getTextbookName().trim())) {
textbook = book;
}
}
bookList.add(textbook);
}
}
}
return bookList;
}
use of com.bc.pmpheep.back.po.Textbook in project pmph by BCSquad.
the class TextbookServiceImpl method addTopicNumber.
@Override
public List<Textbook> addTopicNumber(String topicTextbooks) throws CheckedServiceException {
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<Textbook>>() {
}.getType();
List<Textbook> textbooks = gson.fromJson(topicTextbooks, type);
if (CollectionUtil.isEmpty(textbooks)) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "参数不能为空");
}
for (Textbook textbook : textbooks) {
if (StringUtil.notEmpty(textbook.getTopicNumber()) && StringUtil.strLength(textbook.getTopicNumber()) > 30) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "选题号不能超过30个字符");
}
textbookDao.updateTextbook(textbook);
}
return textbooks;
}
use of com.bc.pmpheep.back.po.Textbook in project pmph by BCSquad.
the class TextbookServiceTest method testAddTextbook.
@Test
@Rollback(Const.ISROLLBACK)
public void testAddTextbook() {
Textbook textbook = new Textbook();
textbook.setMaterialId(23L);
textbook.setTextbookName("社会心理学");
textbook.setTextbookRound(5);
textbook.setSort(12);
textbook.setFounderId(77L);
textbook = textbookService.addTextbook(textbook);
Assert.assertTrue("数据添加失败", textbook.getId() > 0);
}
Aggregations