Search in sources :

Example 1 with BookDetail

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

the class BookServiceTest method testAddBookDetail.

@Test
public void testAddBookDetail() {
    BookDetail bookDetail = this.addBookDetail();
    logger.info("插入的BookDetail对象=" + bookDetail.toString());
    Assert.assertNotNull("插入内容后返回的BookDetail不应为空", bookDetail.getId());
}
Also used : BookDetail(com.bc.pmpheep.back.po.BookDetail) Test(org.junit.Test) BaseTest(com.bc.pmpheep.test.BaseTest)

Example 2 with BookDetail

use of com.bc.pmpheep.back.po.BookDetail 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 BookDetail

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

the class BookServiceImpl method AbuttingJoint.

@Override
public String AbuttingJoint(String[] vns, Integer type) throws CheckedServiceException {
    String result = "SUCCESS";
    Const.AllSYNCHRONIZATION = 1;
    int num = vns.length / 100;
    for (int i = 0; i < vns.length; i++) {
        Book oldBook = bookDao.getBookByBookVn(vns[i]);
        JSONObject ot = new JSONObject();
        if (type == 0) {
            // 商城发送修改的请求
            if (ObjectUtil.isNull(oldBook)) {
                continue;
            }
        }
        try {
            // System.out.println("第"+(i+1)+"条数据,本版号为"+vns[i]+"  共"+vns.length+"条");
            ot = PostBusyAPI(vns[i]);
            if (null != ot && "1".equals(ot.getJSONObject("RESP").getString("CODE"))) {
                JSONArray array = ot.getJSONObject("RESP").getJSONObject("responseData").getJSONArray("results");
                if (array.size() > 0) {
                    Book book = BusyResJSONToModel(array.getJSONObject(0), null);
                    // 获取到图书详情将其存入到图书详情表中
                    String content = book.getContent();
                    if (ObjectUtil.isNull(oldBook)) {
                        book.setScore(9.0);
                        book.setType(1L);
                        bookDao.addBook(book);
                        BookDetail bookDetail = new BookDetail(book.getId(), content);
                        bookDetailDao.addBookDetail(bookDetail);
                    } else {
                        Book newBook = new Book(book.getBookname(), book.getIsbn(), book.getSn(), book.getAuthor(), book.getPublisher(), book.getLang(), book.getRevision(), book.getType(), book.getPublishDate(), book.getReader(), book.getPrice(), book.getScore(), book.getBuyUrl(), book.getImageUrl(), book.getPdfUrl(), book.getClicks(), book.getComments(), book.getLikes(), book.getBookmarks(), book.getIsStick(), book.getSort(), book.getDeadlineStick(), book.getIsNew(), book.getSortNew(), book.getDeadlineNew(), book.getIsPromote(), book.getSortPromote(), book.getDeadlinePromote(), book.getIsKey(), book.getSortKey(), book.getSales(), book.getIsOnSale(), book.getGmtCreate(), book.getGmtUpdate());
                        newBook.setId(oldBook.getId());
                        bookDao.updateBook(newBook);
                        BookDetail bookDetail = new BookDetail(oldBook.getId(), content);
                        bookDetailDao.updateBookDetailByBookId(bookDetail);
                    }
                }
                if ((i + 1) >= num * (Const.AllSYNCHRONIZATION + 1)) {
                    Const.AllSYNCHRONIZATION++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "FAIL";
        }
    }
    return result;
}
Also used : JSONObject(net.sf.json.JSONObject) Book(com.bc.pmpheep.back.po.Book) JSONArray(net.sf.json.JSONArray) BookDetail(com.bc.pmpheep.back.po.BookDetail) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) OfficeXmlFileException(org.apache.poi.poifs.filesystem.OfficeXmlFileException) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Aggregations

BookDetail (com.bc.pmpheep.back.po.BookDetail)3 Book (com.bc.pmpheep.back.po.Book)2 IOException (java.io.IOException)2 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)1 BaseTest (com.bc.pmpheep.test.BaseTest)1 FileNotFoundException (java.io.FileNotFoundException)1 BigDecimal (java.math.BigDecimal)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 JSONArray (net.sf.json.JSONArray)1 JSONObject (net.sf.json.JSONObject)1 TooManyResultsException (org.apache.ibatis.exceptions.TooManyResultsException)1 OfficeXmlFileException (org.apache.poi.poifs.filesystem.OfficeXmlFileException)1 Test (org.junit.Test)1 DataAccessException (org.springframework.dao.DataAccessException)1