wukai 2 năm trước cách đây
mục cha
commit
43cc30d41b

+ 6 - 0
lzga-common/lzga-common-core/pom.xml

@@ -16,6 +16,12 @@
     </description>
 
     <dependencies>
+        <!-- POI-word文件处理需要 -->
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-scratchpad</artifactId>
+            <version>4.1.2</version>
+        </dependency>
         <!-- mybatis-plus -->
         <dependency>
             <groupId>com.baomidou</groupId>

+ 34 - 46
lzga-common/lzga-common-core/src/main/java/com/jjt/common/core/utils/DateUtils.java

@@ -9,6 +9,7 @@ import java.time.LocalTime;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
 import java.util.Date;
+
 import org.apache.commons.lang3.time.DateFormatUtils;
 
 /**
@@ -16,8 +17,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
  *
  * @author ruoyi
  */
-public class DateUtils extends org.apache.commons.lang3.time.DateUtils
-{
+public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
     public static String YYYY = "yyyy";
 
     public static String YYYY_MM = "yyyy-MM";
@@ -38,8 +38,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
      *
      * @return Date() 当前日期
      */
-    public static Date getNowDate()
-    {
+    public static Date getNowDate() {
         return new Date();
     }
 
@@ -48,44 +47,44 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
      *
      * @return String
      */
-    public static String getDate()
-    {
+    public static String getDate() {
         return dateTimeNow(YYYY_MM_DD);
     }
 
-    public static final String getTime()
-    {
+    /**
+     * 获取当前年份,分表时使用
+     *
+     * @return 年份
+     */
+    public static Long getCurrentYear() {
+        Integer currentYear = LocalDate.now().getYear();
+        return (long) currentYear;
+    }
+
+    public static final String getTime() {
         return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
     }
 
-    public static final String dateTimeNow()
-    {
+    public static final String dateTimeNow() {
         return dateTimeNow(YYYYMMDDHHMMSS);
     }
 
-    public static final String dateTimeNow(final String format)
-    {
+    public static final String dateTimeNow(final String format) {
         return parseDateToStr(format, new Date());
     }
 
-    public static final String dateTime(final Date date)
-    {
+    public static final String dateTime(final Date date) {
         return parseDateToStr(YYYY_MM_DD, date);
     }
 
-    public static final String parseDateToStr(final String format, final Date date)
-    {
+    public static final String parseDateToStr(final String format, final Date date) {
         return new SimpleDateFormat(format).format(date);
     }
 
-    public static final Date dateTime(final String format, final String ts)
-    {
-        try
-        {
+    public static final Date dateTime(final String format, final String ts) {
+        try {
             return new SimpleDateFormat(format).parse(ts);
-        }
-        catch (ParseException e)
-        {
+        } catch (ParseException e) {
             throw new RuntimeException(e);
         }
     }
@@ -93,8 +92,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
     /**
      * 日期路径 即年/月/日 如2018/08/08
      */
-    public static final String datePath()
-    {
+    public static final String datePath() {
         Date now = new Date();
         return DateFormatUtils.format(now, "yyyy/MM/dd");
     }
@@ -102,8 +100,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
     /**
      * 日期路径 即年/月/日 如20180808
      */
-    public static final String dateTime()
-    {
+    public static final String dateTime() {
         Date now = new Date();
         return DateFormatUtils.format(now, "yyyyMMdd");
     }
@@ -111,18 +108,13 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
     /**
      * 日期型字符串转化为日期 格式
      */
-    public static Date parseDate(Object str)
-    {
-        if (str == null)
-        {
+    public static Date parseDate(Object str) {
+        if (str == null) {
             return null;
         }
-        try
-        {
+        try {
             return parseDate(str.toString(), parsePatterns);
-        }
-        catch (ParseException e)
-        {
+        } catch (ParseException e) {
             return null;
         }
     }
@@ -130,8 +122,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
     /**
      * 获取服务器启动时间
      */
-    public static Date getServerStartDate()
-    {
+    public static Date getServerStartDate() {
         long time = ManagementFactory.getRuntimeMXBean().getStartTime();
         return new Date(time);
     }
@@ -139,18 +130,17 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
     /**
      * 计算时间差
      *
-     * @param endTime 最后时间
+     * @param endTime   最后时间
      * @param startTime 开始时间
      * @return 时间差(天/小时/分钟)
      */
-    public static String timeDistance(Date endDate, Date startTime)
-    {
+    public static String timeDistance(Date endTime, Date startTime) {
         long nd = 1000 * 24 * 60 * 60;
         long nh = 1000 * 60 * 60;
         long nm = 1000 * 60;
         // long ns = 1000;
         // 获得两个时间的毫秒时间差异
-        long diff = endDate.getTime() - startTime.getTime();
+        long diff = endTime.getTime() - startTime.getTime();
         // 计算差多少天
         long day = diff / nd;
         // 计算差多少小时
@@ -165,8 +155,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
     /**
      * 增加 LocalDateTime ==> Date
      */
-    public static Date toDate(LocalDateTime temporalAccessor)
-    {
+    public static Date toDate(LocalDateTime temporalAccessor) {
         ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
         return Date.from(zdt.toInstant());
     }
@@ -174,8 +163,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
     /**
      * 增加 LocalDate ==> Date
      */
-    public static Date toDate(LocalDate temporalAccessor)
-    {
+    public static Date toDate(LocalDate temporalAccessor) {
         LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
         ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
         return Date.from(zdt.toInstant());

+ 174 - 0
lzga-common/lzga-common-core/src/main/java/com/jjt/common/core/utils/FileContentUtils.java

@@ -0,0 +1,174 @@
+package com.jjt.common.core.utils;
+
+import org.apache.poi.hwpf.HWPFDocument;
+import org.apache.poi.hwpf.extractor.WordExtractor;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.apache.poi.xwpf.usermodel.XWPFParagraph;
+
+import java.io.*;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Logger;
+
+/**
+ * @author wukai
+ */
+public class FileContentUtils {
+
+    /**
+     * 获取正文文件内容,docx方法
+     *
+     * @param file
+     * @return
+     */
+    public static String getContentDocx(File file) {
+        Map<String, String> map = new HashMap(16);
+        StringBuffer content = new StringBuffer("");
+        String result = "0";
+        // 0表示获取正常,1表示获取异常
+        InputStream is = null;
+        Logger logger = null;
+        try {
+            //根据需求入参也可以改为文件路径,对应的输入流部分改为new File(路径)即可
+            is = new FileInputStream(file);
+            // 2007版本的word
+            XWPFDocument xwpf = new XWPFDocument(is);
+            // 2007版本,仅支持docx文件处理
+            List<XWPFParagraph> paragraphs = xwpf.getParagraphs();
+            if (paragraphs != null && paragraphs.size() > 0) {
+                for (XWPFParagraph paragraph : paragraphs) {
+                    if (!paragraph.getParagraphText().startsWith("    ")) {
+                        content.append(paragraph.getParagraphText().trim()).append("\r\n");
+                    } else {
+                        content.append(paragraph.getParagraphText());
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            result = "1";
+            // 出现异常
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            map.put("result", result);
+            map.put("content", String.valueOf(content));
+        }
+        return map.get("content");
+    }
+
+    /**
+     * 获取正文文件内容,doc方法
+     *
+     * @param file
+     * @return
+     */
+    public static String getContentDoc(File file) {
+        Map<String, String> map = new HashMap(16);
+        StringBuffer content = new StringBuffer("");
+        String result = "0";
+        // 0表示获取正常,1表示获取异常
+        InputStream is = null;
+        Logger logger = null;
+        try {
+            is = new FileInputStream(file);
+            // 2003版本的word
+            WordExtractor wordExtractor = new WordExtractor(is);
+            // 2003版本 仅doc格式文件可处理,docx文件不可处理
+            String[] paragraphText = wordExtractor.getParagraphText();
+            // 获取段落,段落缩进无法获取,可以在前添加空格填充
+            if (paragraphText != null && paragraphText.length > 0) {
+                for (String paragraph : paragraphText) {
+                    if (!paragraph.startsWith("    ")) {
+                        content.append(paragraph.trim()).append("\r\n");
+                    } else {
+                        content.append(paragraph);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            result = "1";
+            // 出现异常
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            map.put("result", result);
+            map.put("content", content.toString());
+        }
+        return map.get("content");
+    }
+
+    /**
+     * 获取正文文件内容,wps方法
+     *
+     * @param file
+     * @return
+     */
+    public static String getContentWps(File file) {
+        Map<String, String> map = new HashMap(16);
+        StringBuffer content = new StringBuffer("");
+        String result = "0";
+        // 0表示获取正常,1表示获取异常
+        InputStream is = null;
+        Logger logger = null;
+        try {
+            is = new FileInputStream(file);
+            // wps版本word
+            HWPFDocument hwpf = new HWPFDocument(is);
+
+            // 文档文本内容
+            String[] paragraphText1 = new WordExtractor(hwpf).getParagraphText();
+            if (paragraphText1 != null && paragraphText1.length > 0) {
+                for (String paragraph : paragraphText1) {
+                    if (!paragraph.startsWith("    ")) {
+                        content.append(paragraph.trim()).append("\r\n");
+                    } else {
+                        content.append(paragraph);
+                    }
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            result = "1";
+            // 出现异常
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            map.put("result", result);
+            map.put("content", content.toString());
+        }
+        return map.get("content");
+    }
+
+    public static String getContentTxt(File file) {
+        StringBuilder sb = new StringBuilder();
+        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
+            String line;
+            while ((line = br.readLine()) != null) {
+                sb.append(line).append(System.lineSeparator());
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        String fileContent = sb.toString();
+        return fileContent;
+    }
+}

+ 39 - 40
lzga-common/lzga-common-core/src/main/java/com/jjt/common/core/web/domain/BaseEntity.java

@@ -4,115 +4,114 @@ import java.io.Serializable;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+
 import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonInclude;
 
 /**
  * Entity基类
- * 
+ *
  * @author ruoyi
  */
-public class BaseEntity implements Serializable
-{
+public class BaseEntity implements Serializable {
     private static final long serialVersionUID = 1L;
 
-    /** 搜索值 */
+    /**
+     * 搜索值
+     */
     @JsonIgnore
     private String searchValue;
 
-    /** 创建者 */
+    /**
+     * 创建者
+     */
     private String createBy;
 
-    /** 创建时间 */
+    /**
+     * 创建时间
+     */
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date createTime;
 
-    /** 更新者 */
+    /**
+     * 更新者
+     */
     private String updateBy;
 
-    /** 更新时间 */
+    /**
+     * 更新时间
+     */
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date updateTime;
 
-    /** 备注 */
+    /**
+     * 备注
+     */
     private String remark;
 
-    /** 请求参数 */
+    /**
+     * 请求参数
+     */
     @JsonInclude(JsonInclude.Include.NON_EMPTY)
     private Map<String, Object> params;
 
-    public String getSearchValue()
-    {
+    public String getSearchValue() {
         return searchValue;
     }
 
-    public void setSearchValue(String searchValue)
-    {
+    public void setSearchValue(String searchValue) {
         this.searchValue = searchValue;
     }
 
-    public String getCreateBy()
-    {
+    public String getCreateBy() {
         return createBy;
     }
 
-    public void setCreateBy(String createBy)
-    {
+    public void setCreateBy(String createBy) {
         this.createBy = createBy;
     }
 
-    public Date getCreateTime()
-    {
+    public Date getCreateTime() {
         return createTime;
     }
 
-    public void setCreateTime(Date createTime)
-    {
+    public void setCreateTime(Date createTime) {
         this.createTime = createTime;
     }
 
-    public String getUpdateBy()
-    {
+    public String getUpdateBy() {
         return updateBy;
     }
 
-    public void setUpdateBy(String updateBy)
-    {
+    public void setUpdateBy(String updateBy) {
         this.updateBy = updateBy;
     }
 
-    public Date getUpdateTime()
-    {
+    public Date getUpdateTime() {
         return updateTime;
     }
 
-    public void setUpdateTime(Date updateTime)
-    {
+    public void setUpdateTime(Date updateTime) {
         this.updateTime = updateTime;
     }
 
-    public String getRemark()
-    {
+    public String getRemark() {
         return remark;
     }
 
-    public void setRemark(String remark)
-    {
+    public void setRemark(String remark) {
         this.remark = remark;
     }
 
-    public Map<String, Object> getParams()
-    {
-        if (params == null)
-        {
+    public Map<String, Object> getParams() {
+        if (params == null) {
             params = new HashMap<>();
         }
         return params;
     }
 
-    public void setParams(Map<String, Object> params)
-    {
+    public void setParams(Map<String, Object> params) {
         this.params = params;
     }
 }

+ 3 - 0
lzga-modules/lzga-doc/src/main/java/com/jjt/doc/config/AfterRunner.java

@@ -9,6 +9,9 @@ import org.springframework.stereotype.Component;
 import javax.annotation.Resource;
 import java.util.List;
 
+/**
+ * @author wukai
+ */
 @Component
 @Order(value = 8)
 public class AfterRunner implements ApplicationRunner {

+ 3 - 0
lzga-modules/lzga-doc/src/main/java/com/jjt/doc/config/InitActualDataNodes.java

@@ -22,6 +22,9 @@ import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 
+/**
+ * @author wukai
+ */
 @Component
 @Slf4j
 /**

+ 5 - 1
lzga-modules/lzga-doc/src/main/java/com/jjt/doc/controller/DocInfoController.java

@@ -5,6 +5,7 @@ import java.io.IOException;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 
+import com.jjt.common.security.utils.SecurityUtils;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
@@ -67,7 +68,6 @@ public class DocInfoController extends BaseController {
         DocInfo docInfo = new DocInfo();
         docInfo.setDocId(docId);
         docInfo.setCreateYear(createYear);
-
         return success(docInfoService.selectDocInfoByDocInfo(docInfo));
     }
 
@@ -78,6 +78,8 @@ public class DocInfoController extends BaseController {
     @Log(title = "文档基本信息", businessType = BusinessType.INSERT)
     @PostMapping
     public AjaxResult add(@RequestBody DocInfo docInfo) {
+        docInfo.setOwner(SecurityUtils.getUserId());
+        docInfo.setCreateBy(SecurityUtils.getUsername());
         return toAjax(docInfoService.insertDocInfo(docInfo));
     }
 
@@ -85,9 +87,11 @@ public class DocInfoController extends BaseController {
      * 修改文档基本信息
      */
     @RequiresPermissions("doc:info:edit")
+
     @Log(title = "文档基本信息", businessType = BusinessType.UPDATE)
     @PutMapping
     public AjaxResult edit(@RequestBody DocInfo docInfo) {
+        docInfo.setUpdateBy(SecurityUtils.getUsername());
         return toAjax(docInfoService.updateDocInfo(docInfo));
     }
 

+ 10 - 3
lzga-modules/lzga-doc/src/main/java/com/jjt/doc/controller/ElasticSearchController.java

@@ -1,8 +1,10 @@
 package com.jjt.doc.controller;
 
 import com.alibaba.fastjson2.JSON;
+import com.jjt.doc.domain.EsDocInfo;
 import com.jjt.doc.domain.HelloEntity;
 import com.jjt.doc.domain.PageDTO;
+import com.jjt.doc.service.EsDocInfoService;
 import com.jjt.doc.service.HelloDao;
 import org.elasticsearch.index.query.MatchQueryBuilder;
 import org.elasticsearch.index.query.QueryBuilders;
@@ -21,6 +23,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 
+/**
+ * @author wukai
+ */
 @RestController
 @RequestMapping("es")
 public class ElasticSearchController {
@@ -29,6 +34,8 @@ public class ElasticSearchController {
     private ElasticsearchRestTemplate elasticsearchRestTemplate;
     @Resource
     private HelloDao helloDao;
+    @Resource
+    private EsDocInfoService esDocInfoService;
 
     /**
      * 创建索引 hello
@@ -52,7 +59,7 @@ public class ElasticSearchController {
      */
     @GetMapping("/createIndexWithMapping")
     public String createMapping() {
-        boolean b = elasticsearchRestTemplate.indexOps(HelloEntity.class).createWithMapping();
+        boolean b = elasticsearchRestTemplate.indexOps(EsDocInfo.class).createWithMapping();
         if (b) {
             return "success";
         }
@@ -106,7 +113,7 @@ public class ElasticSearchController {
      */
     @GetMapping("/{id}")
     public String get(@PathVariable(value = "id") Long id) {
-        Optional<HelloEntity> byId = helloDao.findById(id);
+        Optional<EsDocInfo> byId = esDocInfoService.findById(id);
         boolean present = byId.isPresent();
         if (present) {
             return JSON.toJSONString(byId.get());
@@ -170,7 +177,7 @@ public class ElasticSearchController {
      */
     @GetMapping("/findByContent")
     public String queryContent(@RequestParam(value = "content", required = false) String content) {
-        List<SearchHit<HelloEntity>> all = helloDao.findByContent(content);
+        List<SearchHit<EsDocInfo>> all = esDocInfoService.findByContent(content);
         return JSON.toJSONString(all);
     }
 

+ 3 - 3
lzga-modules/lzga-doc/src/main/java/com/jjt/doc/domain/EsDocInfo.java

@@ -23,10 +23,10 @@ public class EsDocInfo {
     @Field(type = FieldType.Text, analyzer = "ik_max_word")
     private String content;
 
-    @Field(type = FieldType.Integer)
-    private Integer year;
+    @Field(type = FieldType.Long)
+    private Long year;
 
-    public EsDocInfo(Long id, String content, Integer year) {
+    public EsDocInfo(Long id, String content, Long year) {
         this.id = id;
         this.content = content;
         this.year = year;

+ 34 - 1
lzga-modules/lzga-doc/src/main/java/com/jjt/doc/service/impl/DocInfoServiceImpl.java

@@ -1,8 +1,13 @@
 package com.jjt.doc.service.impl;
 
-import java.util.List;
+import java.io.File;
+import java.util.*;
+import java.util.function.Function;
 
 import com.jjt.common.core.utils.DateUtils;
+import com.jjt.common.core.utils.FileContentUtils;
+import com.jjt.doc.domain.EsDocInfo;
+import com.jjt.doc.service.EsDocInfoService;
 import com.jjt.system.api.RemoteFileService;
 import org.springframework.stereotype.Service;
 import com.jjt.doc.mapper.DocInfoMapper;
@@ -24,6 +29,8 @@ public class DocInfoServiceImpl implements IDocInfoService {
     private DocInfoMapper docInfoMapper;
     @Resource
     private RemoteFileService remoteFileService;
+    @Resource
+    private EsDocInfoService esDocInfoService;
 
     /**
      * 查询文档基本信息
@@ -68,6 +75,7 @@ public class DocInfoServiceImpl implements IDocInfoService {
     public int insertDocInfo(DocInfo docInfo) {
         String localFilePath = remoteFileService.localFilePath(docInfo.getDocPath()).getData();
         docInfo.setCreateTime(DateUtils.getNowDate());
+//        docInfo.setCreateYear(DateUtils.getCurrentYear());
         return docInfoMapper.insertDocInfo(docInfo);
     }
 
@@ -79,6 +87,31 @@ public class DocInfoServiceImpl implements IDocInfoService {
      */
     @Override
     public int updateDocInfo(DocInfo docInfo) {
+        String localFilePath = remoteFileService.localFilePath(docInfo.getDocPath()).getData();
+        File file = new File(localFilePath);
+        //文件大小
+        docInfo.setDocSize(file.length());
+        //获取文件扩展名
+        int dot = localFilePath.lastIndexOf(".");
+        String ext = localFilePath.substring(dot + 1);
+        System.err.println(ext);
+        Map<String, Function<File, String>> handlerMap = new HashMap<>();
+        handlerMap.put("docx", FileContentUtils::getContentDocx);
+        handlerMap.put("doc", FileContentUtils::getContentDoc);
+        handlerMap.put("wps", FileContentUtils::getContentWps);
+        handlerMap.put("txt", FileContentUtils::getContentTxt);
+
+        Function<File, String> handler = handlerMap.get(ext);
+        if (handler != null) {
+            String content = handler.apply(file);
+            System.err.println(content);
+            EsDocInfo esDocInfo = new EsDocInfo();
+            esDocInfo.setId(docInfo.getDocId());
+            esDocInfo.setYear(docInfo.getCreateYear());
+            esDocInfo.setContent(content);
+            esDocInfoService.save(esDocInfo);
+        }
+        System.err.println(file.length());
         docInfo.setUpdateTime(DateUtils.getNowDate());
         return docInfoMapper.updateDocInfo(docInfo);
     }

+ 1 - 2
lzga-modules/lzga-doc/src/main/resources/mapper/doc/DocInfoMapper.xml

@@ -134,7 +134,6 @@
             <if test="allowEdit != null">ALLOW_EDIT = #{allowEdit},</if>
             <if test="docOf != null">DOC_OF = #{docOf},</if>
             <if test="owner != null">OWNER = #{owner},</if>
-            <if test="createYear != null">CREATE_YEAR = #{createYear},</if>
             <if test="createBy != null">CREATE_BY = #{createBy},</if>
             <if test="createTime != null">CREATE_TIME = #{createTime},</if>
             <if test="updateBy != null">UPDATE_BY = #{updateBy},</if>
@@ -142,7 +141,7 @@
             <if test="remark != null">REMARK = #{remark},</if>
             <if test="isDel != null">IS_DEL = #{isDel},</if>
         </trim>
-        where DOC_ID = #{docId}
+        where DOC_ID = #{docId} and CREATE_YEAR = #{createYear}
     </update>
 
     <delete id="deleteDocInfoByDocId" parameterType="Long">