Search in sources :

Example 1 with Book

use of com.bc.pmpheep.back.po.Book in project pmph by BCSquad.

the class BookServiceTest method testDeleteBookById.

@Test
public void testDeleteBookById() {
    String returnSring = "ERROR";
    Book book = this.addBook();
    Assert.assertNotNull("插入内容后返回的Book不应为空", book.getId());
    returnSring = bookService.deleteBookById(book.getId());
    Assert.assertEquals("是否删除成功", "SUCCESS", returnSring);
}
Also used : Book(com.bc.pmpheep.back.po.Book) Test(org.junit.Test) BaseTest(com.bc.pmpheep.test.BaseTest)

Example 2 with Book

use of com.bc.pmpheep.back.po.Book in project pmph by BCSquad.

the class MigrationStageNine method book.

protected void book() {
    // 要迁移的旧库表名
    String tableName = "book_goodsinfo";
    // 增加new_pk字段
    JdbcHelper.addColumn(tableName);
    // JdbcHelper.addColumn("sys_booktypes"); //增加new_pk字段
    // 取得该表中所有数据
    List<Map<String, Object>> maps = JdbcHelper.queryForList(tableName);
    // 迁移成功的条目数
    int count = 0;
    /* booktypesid这张表重复的bookid非常多,因此用group来返回唯一结果 */
    String sql = "SELECT booktypesid FROM book_goodstype WHERE bookid = ? GROUP BY bookid";
    List<Map<String, Object>> excel = new LinkedList<>();
    Map<String, Object> result = new LinkedHashMap<>();
    int correctCount = 0;
    int[] state = { 0, 0, 0, 0, 0, 0 };
    StringBuilder reason = new StringBuilder();
    StringBuilder dealWith = new StringBuilder();
    /* 开始遍历查询结果 */
    for (Map<String, Object> map : maps) {
        /* Get book_goodsinfo properties */
        String bookid = (String) map.get("bookid");
        String name = (String) map.get("name");
        String isbn = (String) map.get("ISBN");
        String titleno = (String) map.get("titleno");
        String author = (String) map.get("author");
        Integer revision = (Integer) map.get("revision");
        String press = (String) map.get("press");
        Integer impression = (Integer) map.get("impression");
        BigDecimal price = (BigDecimal) map.get("price");
        String buyurl = (String) map.get("buyurl");
        String picurl = (String) map.get("picurl");
        String pdfurl = (String) map.get("pdfurl");
        // String tags = (String) map.get("tags");
        String reader = (String) map.get("reader");
        // String createdate = map.get("createdate").toString();
        BigDecimal score = (BigDecimal) map.get("score");
        // Integer isdelete = (Integer) map.get("isdelete");
        // String language = (String) map.get("language");
        Integer isnew = (Integer) map.get("isnew");
        Integer ismajor = (Integer) map.get("ismajor");
        Integer isonshelves = (Integer) map.get("isonshelves");
        String editnumber = (String) map.get("editnumber");
        /* Set book properties */
        Book book = new Book();
        book.setAuthor(author);
        // 抛弃旧平台收藏
        book.setBookmarks(0L);
        book.setBookname(name);
        if (StringUtil.isEmpty(buyurl)) {
            map.put(SQLParameters.EXCEL_EX_HEADER, "购买链接为空");
            excel.add(map);
            logger.error("购买链接(buyurl)为空,本条数据无效,将记录在Excel中");
            if (state[0] == 0) {
                reason.append("找不到购买链接。");
                dealWith.append("放弃迁移。");
                state[0] = 1;
            }
            continue;
        }
        book.setBuyUrl(buyurl);
        // 旧平台没有点击数
        book.setClicks(0L);
        // 抛弃旧平台评论
        book.setComments(0L);
        // 旧平台没有置顶
        book.setIsStick(false);
        /* 判断旧平台图书是否新书和推荐 */
        if (0 == isnew) {
            book.setIsNew(false);
        } else {
            book.setIsNew(true);
            book.setDeadlineNew(deadline);
        }
        if (0 == ismajor) {
            book.setIsPromote(false);
        } else {
            book.setIsPromote(true);
            book.setDeadlinePromote(deadline);
        }
        if (StringUtil.isEmpty(picurl)) {
            book.setImageUrl("DEFAULT");
        } else {
            book.setImageUrl(picurl);
        }
        book.setIsOnSale(isonshelves > 0);
        book.setIsbn(isbn);
        // 旧平台没有点赞
        book.setLikes(0L);
        if (StringUtil.notEmpty(pdfurl)) {
            book.setPdfUrl(pdfurl);
        }
        book.setPrice(price.doubleValue());
        if (null == map.get("publicationdate")) {
            book.setPublishDate(new Date());
            map.put(SQLParameters.EXCEL_EX_HEADER, "旧数据库中出版日期为空,已设为当前日期");
            excel.add(map);
            logger.warn("旧数据库中出版日期为空,已设为当前日期,此结果将被记录在Excel中");
            if (state[1] == 0) {
                reason.append("找不到书籍出版日期。");
                dealWith.append("设为当前日期迁入数据库。");
                state[1] = 1;
            }
        } else {
            try {
                String publicationdate = map.get("publicationdate").toString();
                book.setPublishDate(sdf.parse(publicationdate));
            } catch (ParseException ex) {
                book.setPublishDate(new Date());
                map.put(SQLParameters.EXCEL_EX_HEADER, "出版日期转换失败,已设为当前日期");
                excel.add(map);
                logger.warn("出版日期转换失败,此结果将被记录在Excel中,错误信息:{}", ex.getMessage());
                if (state[2] == 0) {
                    reason.append("出版日期转换失败。");
                    dealWith.append("设为当前日期迁入数据库。");
                    state[2] = 1;
                }
            }
        }
        if (StringUtil.notEmpty(press)) {
            book.setPublisher(press);
        } else {
            book.setPublisher("暂缺");
            map.put(SQLParameters.EXCEL_EX_HEADER, "出版图书没有出版社,已设为'暂缺'");
            excel.add(map);
            logger.warn("出版图书的出版社字段为空,此结果将被记录在Excel中");
            if (state[3] == 0) {
                reason.append("找不到书籍的出版社。");
                dealWith.append("设为暂缺迁入数据库。");
                state[3] = 1;
            }
        }
        if (StringUtil.notEmpty(reader)) {
            book.setReader(reader);
        }
        book.setRevision(revision);
        // 旧平台没有销量
        book.setSales(0L);
        // 评分全是9
        book.setScore(9.0);
        book.setSn(titleno);
        String booktypesid;
        try {
            booktypesid = JdbcHelper.getJdbcTemplate().queryForObject(sql, String.class, bookid);
        } catch (DataAccessException ex) {
            map.put(SQLParameters.EXCEL_EX_HEADER, "查询booktypesid时未返回唯一结果");
            excel.add(map);
            logger.error("查询booktypesid时未返回唯一结果,错误信息{}", ex.getMessage());
            if (state[4] == 0) {
                reason.append("书籍具有多个类型,非唯一分类。");
                dealWith.append("放弃迁移。");
                state[4] = 1;
            }
            continue;
        }
        Long pk = JdbcHelper.getPrimaryKey("sys_booktypes", "BookTypesID", booktypesid);
        if (null == pk) {
            map.put(SQLParameters.EXCEL_EX_HEADER, "获取sys_booktypes表new_pk字段失败,BookTypesID=" + booktypesid);
            excel.add(map);
            logger.error("获取sys_booktypes表new_pk字段失败,此结果将被记录在Excel中");
            if (state[5] == 0) {
                reason.append("找不到书籍对应的分类信息。");
                dealWith.append("放弃迁移。");
                state[5] = 1;
            }
            continue;
        }
        book.setType(pk);
        book = bookService.add(book);
        Long bookId = book.getId();
        JdbcHelper.updateNewPrimaryKey(tableName, bookId, "bookid", bookid);
        /* 以下创建关联的BookDetail对象 */
        String content = (String) map.get("content");
        BookDetail detail = new BookDetail();
        detail.setBookId(bookId);
        if (StringUtil.notEmpty(content)) {
            detail.setDetail(content);
        }
        bookService.add(detail);
        count++;
        if (null == map.get("exception")) {
            correctCount++;
        }
    }
    if (excel.size() > 0) {
        try {
            excelHelper.exportFromMaps(excel, "出版图书表", "book");
        } catch (IOException ex) {
            logger.error("异常数据导出到Excel失败", ex);
        }
    }
    if (correctCount != maps.size()) {
        result.put(SQLParameters.EXCEL_HEADER_TABLENAME, "book");
        result.put(SQLParameters.EXCEL_HEADER_DESCRIPTION, "出版图书表");
        result.put(SQLParameters.EXCEL_HEADER_SUM_DATA, maps.size());
        result.put(SQLParameters.EXCEL_HEADER_MIGRATED_DATA, count);
        result.put(SQLParameters.EXCEL_HEADER_CORECT_DATA, correctCount);
        result.put(SQLParameters.EXCEL_HEADER_TRANSFERED_DATA, count - correctCount);
        result.put(SQLParameters.EXCEL_HEADER_NO_MIGRATED_DATA, maps.size() - count);
        result.put(SQLParameters.EXCEL_HEADER_EXCEPTION_REASON, reason.toString());
        result.put(SQLParameters.EXCEL_HEADER_DEAL_WITH, dealWith.toString());
        SQLParameters.STATISTICS_RESULT.add(result);
    }
    logger.info("'{}'表迁移完成,异常条目数量:{}", tableName, excel.size());
    logger.info("原数据库中共有{}条数据,迁移了{}条数据", maps.size(), count);
    // 记录信息
    Map<String, Object> msg = new HashMap<String, Object>();
    msg.put("result", "" + tableName + "  表迁移完成" + count + "/" + maps.size());
    SQLParameters.STATISTICS.add(msg);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IOException(java.io.IOException) LinkedList(java.util.LinkedList) BigDecimal(java.math.BigDecimal) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Book(com.bc.pmpheep.back.po.Book) ParseException(java.text.ParseException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) BookDetail(com.bc.pmpheep.back.po.BookDetail) DataAccessException(org.springframework.dao.DataAccessException)

Example 3 with Book

use of com.bc.pmpheep.back.po.Book in project pmph by BCSquad.

the class BookVideoServiceImpl method getList.

@Override
public PageResult<PastBookVideoVO> getList(Integer pageSize, Integer pageNumber, String bookName) {
    Map<String, Object> map = new HashMap<String, Object>(3);
    if (!StringUtil.isEmpty(bookName)) {
        bookName = StringUtil.toAllCheck(bookName.trim());
        map.put("bookName", bookName);
    }
    map.put("start", ((pageNumber - 1) * pageSize));
    map.put("pageSize", pageSize);
    // 获取书籍的分页
    PageResult<Book> listBooks = bookService.listBook(pageSize, pageNumber, bookName);
    List<PastBookVideoVO> lst = new ArrayList<PastBookVideoVO>();
    Integer total = 0;
    if (null != listBooks && null != listBooks.getTotal() && listBooks.getTotal().intValue() > 0) {
        total = listBooks.getTotal();
        List<Long> bookIds = new ArrayList<Long>(listBooks.getRows().size());
        for (Book book : listBooks.getRows()) {
            bookIds.add(book.getId());
            PastBookVideoVO pastBookVideoVO = new PastBookVideoVO();
            pastBookVideoVO.setAuthor(book.getAuthor()).setBookId(book.getId()).setBookname(book.getBookname()).setImageUrl(book.getImageUrl()).setRevision(book.getRevision()).setSn(book.getSn());
            lst.add(pastBookVideoVO);
        }
        List<BookVideo> BookVideos = bookVideoDao.getBookVideoByBookIds(bookIds);
        for (PastBookVideoVO pastBookVideoVO : lst) {
            List<BookVideo> bookVideos = new ArrayList<BookVideo>();
            for (BookVideo BookVideo : BookVideos) {
                if (pastBookVideoVO.getBookId().equals(BookVideo.getBookId())) {
                    bookVideos.add(BookVideo);
                }
            }
            pastBookVideoVO.setBookVideos(bookVideos);
        }
    }
    PageResult<PastBookVideoVO> BookVideoVOlst = new PageResult<PastBookVideoVO>();
    BookVideoVOlst.setPageNumber(pageNumber);
    BookVideoVOlst.setPageSize(pageSize);
    BookVideoVOlst.setTotal(total);
    BookVideoVOlst.setRows(lst);
    return BookVideoVOlst;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BookVideo(com.bc.pmpheep.back.po.BookVideo) PageResult(com.bc.pmpheep.back.plugin.PageResult) Book(com.bc.pmpheep.back.po.Book) PastBookVideoVO(com.bc.pmpheep.back.vo.PastBookVideoVO)

Example 4 with Book

use of com.bc.pmpheep.back.po.Book in project pmph by BCSquad.

the class BookServiceImpl method bookExcel.

@Override
public String bookExcel(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, "读取文件失败");
    } catch (OfficeXmlFileException e) {
        throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "此文档不是对应的.xls或.xlsx的Excel文档,请修改为正确的后缀名再进行上传");
    }
    for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
        Sheet sheet = workbook.getSheetAt(numSheet);
        if (ObjectUtil.isNull(sheet)) {
            continue;
        }
        for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            Row row = sheet.getRow(rowNum);
            Long id = 0L;
            List<Long> bookIds = new ArrayList<>();
            if (null == row) {
                continue;
            }
            Cell cell = row.getCell(8);
            if (row.getLastCellNum() > 9 || (ObjectUtil.notNull(cell) && !"".equals(cell.toString()))) {
                throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "提交的Excel格式不正确,请按照模版修改后重试");
            }
            for (int i = 0; i < 7; i += 2) {
                Cell name = row.getCell(i);
                Cell isbn = row.getCell(i + 1);
                Book book = new Book();
                if (!ObjectUtil.isNull(isbn) && !"".equals(isbn.toString())) {
                    String ISBN = isbn.toString();
                    if (!ISBN.contains("ISBN")) {
                        ISBN = "ISBN " + ISBN;
                    }
                    book = bookDao.getBookByIsbn(ISBN);
                    if (ObjectUtil.isNull(book)) {
                        throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "Excel中第" + (rowNum + 1) + "行第" + (i + 1) + "列isbn填写错误请确认后重试。");
                    }
                    if (0 == i) {
                        id = book.getId();
                    } else {
                        bookIds.add(book.getId());
                    }
                } else if (!ObjectUtil.isNull(name) && !"".equals(name.toString())) {
                    try {
                        book = bookDao.getBookByBookname(name.toString());
                    } catch (TooManyResultsException e) {
                        throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "Excel中第" + (rowNum + 1) + "行第" + (i + 1) + "列" + name.toString() + "书籍存在多本同名书籍,填写isbn后重试。");
                    }
                    if (ObjectUtil.isNull(book)) {
                        throw new CheckedServiceException(CheckedExceptionBusiness.EXCEL, CheckedExceptionResult.ILLEGAL_PARAM, "Excel中第" + (rowNum + 1) + "行第" + (i + 1) + "列" + name.toString() + "书籍填写错误,请确认后或者填写isbn后重试。");
                    }
                    if (0 == i) {
                        id = book.getId();
                    } else {
                        bookIds.add(book.getId());
                    }
                }
            }
            addBookExcel(id, bookIds, rowNum);
        }
    }
    return "SECCESS";
}
Also used : InputStream(java.io.InputStream) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) IOException(java.io.IOException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) OfficeXmlFileException(org.apache.poi.poifs.filesystem.OfficeXmlFileException) Book(com.bc.pmpheep.back.po.Book) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 5 with Book

use of com.bc.pmpheep.back.po.Book in project pmph by BCSquad.

the class BookServiceImpl method updateBookById.

@Override
public String updateBookById(Long[] ids, Long type, Boolean isOnSale, Boolean isNew, Boolean isPromote, Long materialId, Boolean isKey) throws CheckedServiceException {
    if (ArrayUtil.isEmpty(ids)) {
        throw new CheckedServiceException(CheckedExceptionBusiness.BOOK, CheckedExceptionResult.NULL_PARAM, "id为空");
    }
    if (ObjectUtil.isNull(type)) {
        throw new CheckedServiceException(CheckedExceptionBusiness.BOOK, CheckedExceptionResult.NULL_PARAM, "书籍类型为空");
    }
    if (ObjectUtil.isNull(isOnSale)) {
        throw new CheckedServiceException(CheckedExceptionBusiness.BOOK, CheckedExceptionResult.NULL_PARAM, "是否上架为空");
    }
    if (ObjectUtil.isNull(isNew)) {
        throw new CheckedServiceException(CheckedExceptionBusiness.BOOK, CheckedExceptionResult.NULL_PARAM, "是否新书为空");
    }
    if (ObjectUtil.isNull(isPromote)) {
        throw new CheckedServiceException(CheckedExceptionBusiness.BOOK, CheckedExceptionResult.NULL_PARAM, "是否推荐为空");
    }
    if (ObjectUtil.isNull(isKey)) {
        throw new CheckedServiceException(CheckedExceptionBusiness.BOOK, CheckedExceptionResult.NULL_PARAM, "是否重点学科为空");
    }
    String result = "FAIL";
    for (Long id : ids) {
        Book book = new Book();
        book.setId(id);
        book.setType(type);
        book.setIsNew(isNew);
        book.setIsKey(isKey);
        book.setIsOnSale(isOnSale);
        book.setIsPromote(isPromote);
        book.setMaterialId(materialId);
        bookDao.updateBook(book);
    }
    result = "SUCCESS";
    return result;
}
Also used : Book(com.bc.pmpheep.back.po.Book) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException)

Aggregations

Book (com.bc.pmpheep.back.po.Book)10 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)4 IOException (java.io.IOException)4 BaseTest (com.bc.pmpheep.test.BaseTest)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 BookDetail (com.bc.pmpheep.back.po.BookDetail)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 JSONArray (net.sf.json.JSONArray)2 JSONObject (net.sf.json.JSONObject)2 TooManyResultsException (org.apache.ibatis.exceptions.TooManyResultsException)2 OfficeXmlFileException (org.apache.poi.poifs.filesystem.OfficeXmlFileException)2 Cell (org.apache.poi.ss.usermodel.Cell)2 Row (org.apache.poi.ss.usermodel.Row)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2