bc_zhang hace 4 años
padre
commit
98fc0ee310

+ 56 - 28
src/main/java/com/zglc/fm/controller/BookController.java

@@ -4,6 +4,7 @@ package com.zglc.fm.controller;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.zglc.fm.base.Result;
+import com.zglc.fm.entity.CatalogueEntity;
 import com.zglc.fm.entity.DeleteEntity;
 import com.zglc.fm.entity.BookEntity;
 import com.zglc.fm.entity.FaultEntity;
@@ -55,45 +56,57 @@ public class BookController {
         String sourcePath1;
 
         sourcePath1 = "d:\\sourcefm";
-        String filePath = FileTool.fileUp(file, sourcePath1, date.getTime() + file.getOriginalFilename());
+        String extName = ""; // 扩展名格式:
+
+//        String filePath = FileTool.fileUp(file, sourcePath1, date.getTime() + file.getOriginalFilename());
+        extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
+        String filePath = FileTool.fileUp(file, sourcePath1, date.getTime() + extName);
 
-        FileTypeUtil.Type fileType = FileTypeUtil.getFileType(file.getOriginalFilename());
+        Type fileType = FileTypeUtil.getFileType(file.getOriginalFilename());
         if (fileType == FileTypeUtil.Type.DOC || fileType == FileTypeUtil.Type.DOCX ||  fileType == FileTypeUtil.Type.RTF ) {
             DocToPdf.convertDoc2Pdf(filePath, sourcePath1);
         }
         if (fileType == FileTypeUtil.Type.PPT || fileType == FileTypeUtil.Type.PPTX) {
             Ppt2ToPdf.convertPpt2Pdf(filePath, sourcePath1);
         }
-        if (fileType == FileTypeUtil.Type.DOC) {
-            deleteDir(new File(filePath));
-            filePath = filePath.replaceAll( "doc","pdf");
-        }
-        if (fileType == FileTypeUtil.Type.DOCX) {
-            deleteDir(new File(filePath));
-            filePath = filePath.replaceAll( "docx","pdf");
-        }
-        if (fileType == FileTypeUtil.Type.RTF) {
-            deleteDir(new File(filePath));
-            filePath = filePath.replaceAll( "rtf","pdf");
-        }
-        if (fileType == FileTypeUtil.Type.PPT) {
-            deleteDir(new File(filePath));
-            filePath = filePath.replaceAll( "ppt","pdf");
-        }
-        if (fileType == FileTypeUtil.Type.PPTX) {
-            deleteDir(new File(filePath));
-            filePath = filePath.replaceAll( "pptx","pdf");
-        }
+//        if (fileType == FileTypeUtil.Type.DOC) {
+////            deleteDir(new File(filePath));
+//            filePath = filePath.replaceAll( "doc","pdf");
+//        }
+//        if (fileType == FileTypeUtil.Type.DOCX) {
+////            deleteDir(new File(filePath));
+//            filePath = filePath.replaceAll( "docx","pdf");
+//        }
+//        if (fileType == FileTypeUtil.Type.RTF) {
+////            deleteDir(new File(filePath));
+//            filePath = filePath.replaceAll( "rtf","pdf");
+//        }
+//        if (fileType == FileTypeUtil.Type.PPT) {
+////            deleteDir(new File(filePath));
+//            filePath = filePath.replaceAll( "ppt","pdf");
+//        }
+//        if (fileType == FileTypeUtil.Type.PPTX) {
+////            deleteDir(new File(filePath));
+//            filePath = filePath.replaceAll( "pptx","pdf");
+//        }
         filePath = filePath.replaceAll( "d:","");
         return Result.success(filePath);
     }
-    private void deleteDir(File file) {
-        if (file == null) {
-            return;
-        }
-        file.delete();
-    }
+//    private void deleteDir(File file) {
+//        if (file == null) {
+//            return;
+//        }
+//        file.delete();
+//    }
 
+    @ApiOperation("浏览资料")
+    @ApiResponses({
+            @ApiResponse(code = 0, message = "成功")
+    })
+    @GetMapping("browse")
+    public Result<String> browse(@RequestParam String filepath){
+        return bookService.browse(filepath);
+    }
 
     @ApiOperation("导入教材缩略图")
     @ApiResponses({
@@ -175,6 +188,21 @@ public class BookController {
     @PostMapping("add")
     public Result<String> add(@RequestBody BookEntity data){return  bookService.add(data);}
 
+    @ApiOperation("增添文档目录")
+    @ApiResponses({
+            @ApiResponse(code = 0, message = "成功")
+    })
+    @PostMapping("addDoccatalogue")
+    public Result<String> addDoccatalogue(@RequestParam Integer id) throws Exception {return  bookService.addDoccatalogue(id);}
+
+
+    @ApiOperation("列出文档目录")
+    @ApiResponses({        @ApiResponse(code = 0, message = "成功")})
+    @PostMapping("listAllDoccatalogue")
+    public Result<List<CatalogueEntity>> listAllDoccatalogue(Integer bookId) {
+        return Result.success( bookService.listAllDoccatalogue(bookId));
+    }
+
     @ApiOperation("删除")
     @ApiResponses({
             @ApiResponse(code = 0, message = "成功")

+ 19 - 0
src/main/java/com/zglc/fm/dao/CatalogueDao.java

@@ -0,0 +1,19 @@
+package com.zglc.fm.dao;
+
+import com.zglc.fm.base.Result;
+import com.zglc.fm.entity.CatalogueEntity;
+import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Repository;
+import tk.mybatis.mapper.common.Mapper;
+
+import java.util.List;
+
+@Repository
+public interface CatalogueDao extends Mapper<CatalogueEntity> {
+
+    List<CatalogueEntity> listAllDoccatalogue(@Param("bookId") Integer bookId);
+
+//    Integer getCount();
+
+//    List<BookEntity> findByName(@Param("book_name") String name);
+}

+ 41 - 0
src/main/java/com/zglc/fm/entity/CatalogueEntity.java

@@ -0,0 +1,41 @@
+package com.zglc.fm.entity;
+
+import lombok.Data;
+
+import javax.persistence.*;
+import java.io.Serializable;
+
+@Data
+@Table(name = "t_catalogue")
+public class CatalogueEntity implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * id
+     */
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id")
+    private Integer id;
+
+    /**
+     * book_id
+     */
+    @Column(name = "book_id")
+    private Integer book_id;
+
+    /**
+     * contents
+     */
+    @Column(name = "contents")
+    private String contents;
+
+    /**
+     * page_num
+     */
+    @Column(name = "page_num")
+    private Integer page_num;
+
+
+}
+

+ 52 - 2
src/main/java/com/zglc/fm/service/BookService.java

@@ -2,9 +2,12 @@ package com.zglc.fm.service;
 
 import com.zglc.fm.base.Result;
 import com.zglc.fm.dao.BookDao;
-//import com.zglc.fm.dao.FaultDao;
+import com.zglc.fm.dao.CatalogueDao;
 import com.zglc.fm.entity.BookEntity;
 //import com.zglc.fm.entity.FaultEntity;
+import com.zglc.fm.entity.CatalogueEntity;
+import com.zglc.fm.utils.DocToPdf;
+import com.zglc.fm.utils.FileTypeUtil;
 import org.springframework.stereotype.Service;
 import tk.mybatis.mapper.entity.Example;
 
@@ -17,10 +20,13 @@ import java.util.List;
 public class BookService {
 
     private BookDao bookDao;
+    private CatalogueDao catalogueDao;
+
 //    public FaultDao faultDao;
 
-    public BookService(BookDao bookDao) {
+    public BookService(BookDao bookDao, CatalogueDao catalogueDao) {
         this.bookDao = bookDao;
+        this.catalogueDao = catalogueDao;
     }
 
     public List<BookEntity> listAll() {
@@ -36,6 +42,7 @@ public class BookService {
         boolean flag = true;
         String msg = "";
         int index = bookDao.insertSelective(data);
+
         if (index == 0) {
             flag = false;
             msg = "添加教材信息失败!";
@@ -106,10 +113,53 @@ public class BookService {
         return bookDao.findByName("%" + name + "%");
     }
 
+    public List<CatalogueEntity> listAllDoccatalogue(Integer bookId) {
+//        return catalogueDao.listAllDoccatalogue(bookId);
+        Example example = new Example(CatalogueEntity.class);
+        Example.Criteria criteria = example.createCriteria();
+        criteria.andEqualTo("book_id", bookId);
+        return catalogueDao.selectByExample(example);
+    }
+
     public BookEntity getOutline(Integer id) {
         return bookDao.selectByPrimaryKey(id);
     }
 
+
+    public Result<String> addDoccatalogue(Integer id) throws Exception {
+        String msg = "";
+        boolean flag = true;
+        BookEntity tmpData = bookDao.selectByPrimaryKey(id);
+        List<CatalogueEntity> lst = DocToPdf.GetDoccatalogue(tmpData.getFile_path(),id);
+        for (CatalogueEntity lsttmp : lst) {
+            int index = catalogueDao.insertSelective(lsttmp);
+            if (index == 0) {
+                flag = false;
+                msg = "添加目录失败!";
+            } else {
+                msg = "添加目录成功!";
+            }
+        }
+        return Result.result(flag, msg, msg);
+    }
+
+    public Result<String> browse(String filepath){
+        FileTypeUtil.Type fileType = FileTypeUtil.getFileType(filepath);
+        if (fileType == FileTypeUtil.Type.DOC ) {
+            filepath = filepath.replaceAll( "doc","pdf");
+        }
+        if (fileType == FileTypeUtil.Type.DOCX ) {
+            filepath = filepath.replaceAll( "docx","pdf");
+        }
+        if (fileType == FileTypeUtil.Type.PPT ) {
+            filepath = filepath.replaceAll( "ppt","pdf");
+        }
+        if (fileType == FileTypeUtil.Type.PPTX ) {
+            filepath = filepath.replaceAll( "pptx","pdf");
+        }
+        return Result.success(filepath);
+    }
+
     public List<BookEntity> getAllBook() {
         String msg = "";
         Example example = new Example(BookEntity.class);

+ 49 - 6
src/main/java/com/zglc/fm/utils/DocToPdf.java

@@ -1,18 +1,61 @@
 package com.zglc.fm.utils;
 
-import com.aspose.words.Document;
-import com.aspose.words.License;
+import com.aspose.words.*;
+import com.zglc.fm.entity.CatalogueEntity;
 
 import java.io.File;
 import java.io.InputStream;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 public class DocToPdf {
+//    public static void main(String[] args) throws Exception {
+//        loadLicense();
+//        //convertDoc2Pdf("/Users/wcf/Desktop/123.wps","/Users/wcf/Desktop");
+//        GetDoccatalogue("d:\\test.docx");
+//    }
 
-    /*public static void main(String[] args) {
-        loadLicense();
-        convertDoc2Pdf("/Users/wcf/Desktop/123.wps","/Users/wcf/Desktop");
-    }*/
+    public static List<CatalogueEntity> GetDoccatalogue(String filePath,Integer bookId) throws Exception {
+        List<CatalogueEntity> lst = new ArrayList<>();
+        Document document = new Document(filePath);
+        String tmp,txt,lastTmp;
+        String title = "";
+        tmp = "";
+        lastTmp = "";
+        int index ;
+        for(Object object: (Iterable)document.getChildNodes(NodeType.FIELD_START, true)) {
+            FieldStart field = (FieldStart )object;
+            Paragraph tocItem = (Paragraph) field.getAncestor(NodeType.PARAGRAPH);
+            if (tocItem != null) {
+                txt = tocItem.getText();
+                index = txt.indexOf("_Toc");
+                if (index <=0) continue;
+                tmp = txt.substring(index,txt.indexOf(" ",index));
+                if (tmp .compareTo( lastTmp)==0) continue;
+                lastTmp = tmp;
+                index = txt.indexOf("HYPERLINK");
+                index = txt.indexOf("\u0014",index);
+                title = txt.substring(index + 1,txt.indexOf("\t",index));
+                index = txt.indexOf("PAGEREF");
+                index = txt.indexOf("\u0014",index);
+                tmp = txt.substring(index + 1,txt.indexOf("\u0015",index));
+                CatalogueEntity catalogueEntity = new CatalogueEntity();
+                catalogueEntity.setBook_id(bookId);
+                catalogueEntity.setContents(title);
+                catalogueEntity.setPage_num(Integer.parseInt(tmp));
+                lst.add(catalogueEntity);
+                System.out.println(title + "  :" + tmp);
+               /*for (Run run : tocItem.getRuns()) {
+                    System.out.println(run.getText());
+                }*/
 
+            }
+        }
+        return lst;
+    }
 
     public static void loadLicense() {
         final InputStream is = DocToPdf.class.getClassLoader().getResourceAsStream("license.xml");

+ 20 - 0
src/main/resources/mapping/CatalogueDao.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.zglc.fm.dao.CatalogueDao">
+
+	<!-- 可根据自己的需求,是否要使用 -->
+    <resultMap type="com.zglc.fm.entity.CatalogueEntity" id="catalogueMap">
+        <result property="id" column="id"/>
+        <result property="book_id" column="book_id"/>
+        <result property="contents" column="contents"/>
+        <result property="page_num" column="page_num"/>
+    </resultMap>
+
+
+
+    <select id="listAllDoccatalogue" parameterType="Integer" resultMap="catalogueMap">
+        select * from t_catalogue where book_id = #{bookId}
+    </select>
+
+</mapper>