Browse Source

[督导与指控 ] 督导与指控接口开发

dev
飞飞飞 1 year ago
parent
commit
6864904a17

+ 7
- 2
pom.xml View File

@@ -273,12 +273,17 @@
273 273
         <dependency>
274 274
             <groupId>org.apache.poi</groupId>
275 275
             <artifactId>poi</artifactId>
276
-            <version>3.17</version>
276
+            <version>4.0.0</version>
277 277
         </dependency>
278 278
         <dependency>
279 279
             <groupId>org.apache.poi</groupId>
280 280
             <artifactId>poi-ooxml</artifactId>
281
-            <version>3.17</version>
281
+            <version>4.0.0</version>
282
+        </dependency>
283
+        <dependency>
284
+            <groupId>org.apache.poi</groupId>
285
+            <artifactId>poi-scratchpad</artifactId>
286
+            <version>4.0.0</version>
282 287
         </dependency>
283 288
         <dependency>
284 289
             <groupId>xerces</groupId>

+ 38
- 0
src/main/java/com/cirle/scientific/controller/miniapp/SuperviseController.java View File

@@ -0,0 +1,38 @@
1
+package com.cirle.scientific.controller.miniapp;/*
2
+ *24901
3
+ *2023/1/4
4
+ *9:47
5
+ */
6
+
7
+import com.cirle.scientific.pojo.Result;
8
+import com.cirle.scientific.service.SuperviseAppService;
9
+import io.swagger.annotations.Api;
10
+import io.swagger.annotations.ApiParam;
11
+import org.springframework.beans.factory.annotation.Autowired;
12
+import org.springframework.stereotype.Controller;
13
+import org.springframework.web.bind.annotation.*;
14
+
15
+/**
16
+ * @author 24901
17
+ */
18
+@Controller
19
+@RequestMapping("/app/Supervise")
20
+@Api("督导与指控接口")
21
+public class SuperviseController {
22
+
23
+    private SuperviseAppService superviseAppService;
24
+
25
+    @Autowired
26
+    public void setSuperviseAppService(SuperviseAppService superviseAppService) {
27
+        this.superviseAppService = superviseAppService;
28
+    }
29
+
30
+    @GetMapping("/findSupervise")
31
+    @ResponseBody
32
+    public Result findSupervise(@ApiParam(name = "memCard", value = "用户唯一标识")   String memCard
33
+            , @RequestParam(name = "type") Integer type){
34
+        return Result.success(superviseAppService.findSuperviseResult(memCard,type));
35
+    }
36
+
37
+
38
+}

+ 19
- 0
src/main/java/com/cirle/scientific/dao/QkjSuperviseAppDao.java View File

@@ -0,0 +1,19 @@
1
+package com.cirle.scientific.dao;/*
2
+ *24901
3
+ *2023/1/4
4
+ *9:31
5
+ */
6
+
7
+import com.cirle.scientific.po.QkjSuperviseApp;
8
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
9
+import org.springframework.data.jpa.repository.Query;
10
+import org.springframework.data.repository.PagingAndSortingRepository;
11
+
12
+import java.util.LinkedList;
13
+
14
+public interface QkjSuperviseAppDao extends PagingAndSortingRepository<QkjSuperviseApp, Long>, JpaSpecificationExecutor<QkjSuperviseApp> {
15
+
16
+    @Query(value = "select result_file from qkj_supervise_app where mem_card = ?1 and year = ?2 and type = ?3 and status = 0 group by month asc", nativeQuery = true)
17
+    LinkedList<String> findResultFileByYearAndTypeAndMemCard(String memCard, Integer year, Integer type);
18
+
19
+}

+ 53
- 0
src/main/java/com/cirle/scientific/po/QkjSuperviseApp.java View File

@@ -0,0 +1,53 @@
1
+package com.cirle.scientific.po;/*
2
+ *24901
3
+ *2023/1/4
4
+ *9:23
5
+ */
6
+
7
+import io.swagger.annotations.ApiModel;
8
+import io.swagger.annotations.ApiModelProperty;
9
+import lombok.Data;
10
+import org.springframework.data.annotation.CreatedDate;
11
+import org.springframework.data.annotation.LastModifiedDate;
12
+
13
+import javax.persistence.*;
14
+import java.time.LocalDateTime;
15
+
16
+@Entity
17
+@Table(name = "qkj_supervise_app")
18
+@Data
19
+@ApiModel
20
+public class QkjSuperviseApp {
21
+
22
+    @Id
23
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
24
+    @ApiModelProperty(value = "督导表id")
25
+    private Long id;
26
+
27
+    @ApiModelProperty(value = "用户唯一标识")
28
+    private String memCard;
29
+
30
+    @ApiModelProperty(value = "上传的结果文件url")
31
+    private String resultFile;
32
+
33
+    @ApiModelProperty(value = "0: 线上督导结果, 1:线下督导结果, 2:质控结果")
34
+    private Integer type;
35
+
36
+    @ApiModelProperty(value = "上传当前的年")
37
+    private Integer year;
38
+
39
+    @ApiModelProperty(value = "上传的月份")
40
+    private Integer month;
41
+
42
+    @ApiModelProperty(value = "状态 0:未删除 1删除")
43
+    private Integer status;
44
+
45
+    @CreatedDate
46
+    @Column(nullable = false, updatable = false)
47
+    private LocalDateTime createTime;
48
+
49
+    @LastModifiedDate
50
+    @Column()
51
+    private LocalDateTime updateTime;
52
+
53
+}

+ 15
- 0
src/main/java/com/cirle/scientific/service/SuperviseAppService.java View File

@@ -0,0 +1,15 @@
1
+package com.cirle.scientific.service;/*
2
+ *24901
3
+ *2023/1/4
4
+ *9:58
5
+ */
6
+
7
+import java.util.LinkedList;
8
+
9
+/**
10
+ * @author 24901
11
+ */
12
+public interface SuperviseAppService {
13
+
14
+    LinkedList<String> findSuperviseResult(String memCard , Integer type);
15
+}

+ 58
- 0
src/main/java/com/cirle/scientific/service/impl/SuperviseAppServiceImpl.java View File

@@ -0,0 +1,58 @@
1
+package com.cirle.scientific.service.impl;/*
2
+ *24901
3
+ *2023/1/4
4
+ *10:01
5
+ */
6
+
7
+import com.cirle.scientific.dao.QkjSuperviseAppDao;
8
+import com.cirle.scientific.service.SuperviseAppService;
9
+import com.cirle.scientific.utils.ReadWordUtil;
10
+import lombok.extern.slf4j.Slf4j;
11
+import org.springframework.beans.factory.annotation.Autowired;
12
+import org.springframework.stereotype.Service;
13
+
14
+import java.io.File;
15
+import java.io.FileInputStream;
16
+import java.io.InputStream;
17
+import java.net.URI;
18
+import java.util.Calendar;
19
+import java.util.LinkedList;
20
+
21
+@Service
22
+@Slf4j
23
+public class SuperviseAppServiceImpl implements SuperviseAppService {
24
+
25
+    private QkjSuperviseAppDao qkjSuperviseAppDao;
26
+
27
+    @Autowired
28
+    public void setQkjSuperviseAppDao(QkjSuperviseAppDao qkjSuperviseAppDao) {
29
+        this.qkjSuperviseAppDao = qkjSuperviseAppDao;
30
+    }
31
+
32
+    @Override
33
+    public LinkedList<String> findSuperviseResult(String memCard, Integer type) {
34
+        LinkedList<String> data = new LinkedList<>();
35
+        Calendar instance = Calendar.getInstance();
36
+        //获取当前的年
37
+        int year = instance.get(Calendar.YEAR);
38
+        LinkedList<String> resultFile = qkjSuperviseAppDao.findResultFileByYearAndTypeAndMemCard(memCard, year, type);
39
+        for (String url : resultFile) {
40
+            File file = null;
41
+            try {
42
+                file = ReadWordUtil.getNetUrlHttp(url);
43
+                String test = ReadWordUtil.readWord(file.getPath());
44
+                data.add(test);
45
+            }catch (Exception e){
46
+                log.info("接口/app/Supervise/findSupervise出错 e->{}", e);
47
+            }finally {
48
+                //立即删除文件
49
+                if(null != file){
50
+                  file.delete();
51
+                  //在JVM退出时删除文件
52
+                  file.deleteOnExit();
53
+                }
54
+            }
55
+        }
56
+        return data;
57
+    }
58
+}

+ 125
- 0
src/main/java/com/cirle/scientific/utils/ReadWordUtil.java View File

@@ -0,0 +1,125 @@
1
+package com.cirle.scientific.utils;/*
2
+ *24901
3
+ *2023/1/4
4
+ *14:00
5
+ */
6
+
7
+
8
+
9
+import lombok.extern.slf4j.Slf4j;
10
+import org.apache.commons.io.FileUtils;
11
+import org.apache.poi.hwpf.extractor.WordExtractor;
12
+import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
13
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
14
+import org.springframework.web.multipart.MultipartFile;
15
+
16
+import java.io.*;
17
+import java.net.URL;
18
+
19
+/**
20
+ * @author 24901
21
+ */
22
+@Slf4j
23
+public class ReadWordUtil {
24
+
25
+
26
+    //使用路径读取word文件
27
+    public static String readWord(String path){
28
+        String buffer = "";
29
+        try {
30
+            if (path.endsWith(".doc")){
31
+                InputStream is = new FileInputStream(new File(path));
32
+                WordExtractor ex = new WordExtractor(is);
33
+                buffer = ex.getText();
34
+            }else if (path.endsWith(".docx")){
35
+                FileInputStream fs = new FileInputStream(new File(path));
36
+                XWPFDocument xdoc = new XWPFDocument(fs);
37
+                XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
38
+                buffer = extractor.getText();
39
+            }
40
+        }catch (Exception e){
41
+            log.info("类:ReadWordUtil 更具文件路径提取word文件内容报错 e->{}", e);
42
+            e.printStackTrace();
43
+        }
44
+        return buffer;
45
+    }
46
+
47
+    //使用MultipartFile 接收文件流
48
+    public static void importWord(MultipartFile file) throws IOException {
49
+        InputStream inputStream = file.getInputStream();
50
+        String fileName = file.getOriginalFilename();
51
+        String suff = fileName.substring(fileName.lastIndexOf(".") + 1);
52
+        String content = "";
53
+        if (suff.equals("docx")) {
54
+            XWPFDocument xdoc = new XWPFDocument(inputStream);
55
+            XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
56
+            content = extractor.getText();
57
+            System.out.println(content);
58
+            extractor.close();
59
+        } else if (suff.equals("doc")) {
60
+            WordExtractor ex = new WordExtractor(inputStream);
61
+            content = ex.getText();
62
+            System.out.println(content);
63
+            ex.close();
64
+        } else {
65
+            System.out.println("此文件不是word文件");
66
+        }
67
+    }
68
+
69
+    public static File getNetUrlHttp(String path) throws IOException {
70
+        File file = null;
71
+        if(path.endsWith(".doc")){
72
+            file  = File.createTempFile("wordText",".doc");
73
+        }else if(path.endsWith(".docx")){
74
+            file  = File.createTempFile("wordText",".docx");
75
+        }
76
+        if(file != null){
77
+            FileUtils.touch(file);
78
+        }
79
+        URL urlfile;
80
+        InputStream inputStream = null;
81
+        OutputStream outputStream = null;
82
+        try{
83
+            //判断文件的父级目录是否存在,不存在则创建
84
+            assert file != null;
85
+            mkdirs(file.getParent());
86
+            //下载
87
+            urlfile = new URL(path);
88
+            inputStream = urlfile.openStream();
89
+            outputStream = new FileOutputStream(file);
90
+
91
+            int bytesRead = 0;
92
+            byte[] buffer = new byte[8192];
93
+            while ((bytesRead=inputStream.read(buffer,0,8192))!=-1) {
94
+                outputStream.write(buffer, 0, bytesRead);
95
+            }
96
+        }catch (Exception e) {
97
+            e.printStackTrace();
98
+            log.info("类:ReadWordUtil url地址转文件方法报错 e->{}", e);
99
+        }finally {
100
+            try {
101
+                if (null != outputStream) {
102
+                    outputStream.close();
103
+                }
104
+                if (null != inputStream) {
105
+                    inputStream.close();
106
+                }
107
+
108
+            } catch (Exception e) {
109
+                e.printStackTrace();
110
+            }
111
+        }
112
+        return file;
113
+    }
114
+
115
+    /**
116
+     * 1.5 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir。 (mkdir如果父目录不存在则会抛出异常)
117
+     * @param destPath  存放目录
118
+     */
119
+    public static void mkdirs(String destPath) {
120
+        File file = new File(destPath);
121
+        if (!file.exists() && !file.isDirectory()) {
122
+            file.mkdirs();
123
+        }
124
+    }
125
+}

Loading…
Cancel
Save