Yansen vor 2 Jahren
Ursprung
Commit
3a21eea751

+ 24
- 1
pom.xml Datei anzeigen

@@ -76,7 +76,7 @@
76 76
         <dependency>
77 77
             <groupId>com.alibaba</groupId>
78 78
             <artifactId>easyexcel</artifactId>
79
-            <version>3.1.3</version>
79
+            <version>3.3.1</version>
80 80
         </dependency>
81 81
 
82 82
         <!--mybatis-plus start-->
@@ -158,6 +158,29 @@
158 158
                 </configuration>
159 159
             </plugin>
160 160
 
161
+            <plugin>
162
+                <groupId>org.apache.maven.plugins</groupId>
163
+                <artifactId>maven-resources-plugin</artifactId>
164
+                <executions>
165
+                    <execution>
166
+                        <phase>package</phase>
167
+                        <goals>
168
+                            <goal>copy-resources</goal>
169
+                        </goals>
170
+                        <configuration>
171
+                            <outputDirectory>${project.build.directory}/config</outputDirectory>
172
+                            <resources>
173
+                                <resource>
174
+                                    <include>*.xlsx</include>
175
+                                    <include>application.yml</include>
176
+                                    <include>application-${profileActive}.yml</include>
177
+                                </resource>
178
+                            </resources>
179
+                        </configuration>
180
+                    </execution>
181
+                </executions>
182
+            </plugin>
183
+
161 184
             <plugin>
162 185
                 <groupId>org.apache.maven.plugins</groupId>
163 186
                 <artifactId>maven-dependency-plugin</artifactId>

+ 30
- 0
src/main/java/com/example/civilizedcity/common/ExcelUtils.java Datei anzeigen

@@ -6,10 +6,13 @@ import com.alibaba.excel.support.ExcelTypeEnum;
6 6
 import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
7 7
 import com.alibaba.excel.write.handler.WriteHandler;
8 8
 import com.alibaba.excel.write.metadata.WriteSheet;
9
+import com.alibaba.excel.write.metadata.fill.FillConfig;
9 10
 
10 11
 import javax.servlet.http.HttpServletResponse;
11 12
 import java.io.IOException;
13
+import java.io.InputStream;
12 14
 import java.util.List;
15
+import java.util.Map;
13 16
 
14 17
 public class ExcelUtils {
15 18
 
@@ -30,6 +33,33 @@ public class ExcelUtils {
30 33
         EasyExcel.write(response.getOutputStream(), dataClass).sheet("sheet1").doWrite(data);
31 34
     }
32 35
 
36
+
37
+
38
+    /**
39
+     * 发送 excel 到客户端
40
+     * 暂时只支持单 sheet 页
41
+     * @param response
42
+     * @param data
43
+     * @param fileName
44
+     * @throws IOException
45
+     */
46
+    public static void flushFile(HttpServletResponse response, InputStream tplFile, Class dataClass, List data, Map<String, Object> otherData, String fileName) throws IOException {
47
+        response.setContentType("application/vnd.ms-excel");
48
+        response.setCharacterEncoding("utf-8");
49
+        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
50
+        response.setHeader("Content-Disposition", "attachment;filename="+StringUtils.urlEncode(fileName)+".xlsx");
51
+
52
+
53
+        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), dataClass).withTemplate(tplFile).build();
54
+        WriteSheet writeSheet = EasyExcel.writerSheet().build();
55
+        FillConfig fillConfig1 = FillConfig.builder().forceNewRow(true).build();
56
+//        FillConfig fillConfig2 = FillConfig.builder().autoStyle(true).build();
57
+
58
+        excelWriter.fill(data, fillConfig1, writeSheet);
59
+        excelWriter.fill(otherData, writeSheet);
60
+        excelWriter.finish();
61
+    }
62
+
33 63
     /**
34 64
      * 导出, 支持复杂一点的自定义
35 65
      * @param response

+ 65
- 0
src/main/java/com/example/civilizedcity/controller/TaCheckController.java Datei anzeigen

@@ -7,14 +7,23 @@ import com.example.civilizedcity.common.*;
7 7
 import com.example.civilizedcity.entity.SysUser;
8 8
 import com.example.civilizedcity.entity.TaCheckItem;
9 9
 import com.example.civilizedcity.service.TaCheckItemService;
10
+import com.example.civilizedcity.vo.CheckLocExport;
10 11
 import io.swagger.annotations.Api;
11 12
 import io.swagger.annotations.ApiOperation;
12 13
 import io.swagger.annotations.ApiParam;
13 14
 import org.springframework.beans.factory.annotation.Autowired;
15
+import org.springframework.beans.factory.annotation.Value;
14 16
 import org.springframework.web.bind.annotation.*;
15 17
 import com.example.civilizedcity.entity.TaCheck;
16 18
 import com.example.civilizedcity.service.TaCheckService;
17 19
 
20
+import javax.servlet.http.HttpServletResponse;
21
+import java.io.FileInputStream;
22
+import java.io.InputStream;
23
+import java.util.HashMap;
24
+import java.util.List;
25
+import java.util.Map;
26
+
18 27
 /**
19 28
  * 模拟测评;(ta_check)表控制层
20 29
  *
@@ -32,6 +41,9 @@ public class TaCheckController extends BaseController {
32 41
     @Autowired
33 42
     private TaCheckItemService taCheckItemService;
34 43
 
44
+    @Value("${yz.excel.tpl.check}")
45
+    String tplPath;
46
+
35 47
     /**
36 48
      * 通过ID查询单条数据
37 49
      *
@@ -112,6 +124,59 @@ public class TaCheckController extends BaseController {
112 124
     }
113 125
 
114 126
 
127
+    /**
128
+     * 导出实地测评
129
+     *
130
+     * @param taCheck 实例对象
131
+     * @return 实例对象
132
+     */
133
+    @ApiOperation("导出实地测评")
134
+    @PostMapping("/taCheck/{id}/loc/export")
135
+    public ResponseBean exportLoc(@ApiParam("对象ID") @PathVariable String id,
136
+                                  HttpServletResponse response) throws Exception {
137
+        TaCheck taCheck = taCheckService.getById(id);
138
+        List<CheckLocExport> list = taCheckItemService.getExportByCheck(id, Constants.CHECK_OF_LOC);
139
+        if (null == list) {
140
+            return ResponseBean.error("当前测评数据不正确");
141
+        }
142
+
143
+        Double totalFullScore = 0.0;
144
+        Integer totalAnswerNum = 0;
145
+        Integer totalNum = 0;
146
+        Double totalSubtotal = 0.0;
147
+        Double totalScore = 0.0;
148
+
149
+        for (CheckLocExport item : list) {
150
+            totalFullScore += getVal(item.getFullScore());
151
+            totalAnswerNum += getVal(item.getAnswerNum());
152
+            totalNum += getVal(item.getNum());
153
+            totalSubtotal += getVal(item.getSubtotal());
154
+            totalScore += getVal(item.getScore());
155
+        }
156
+
157
+        String title = taCheck.getTitle() + " 实地测评";
158
+        Map<String, Object> map = new HashMap<>();
159
+        map.put("totalFullScore", totalFullScore);
160
+        map.put("totalAnswerNum", totalAnswerNum);
161
+        map.put("totalNum", totalNum);
162
+        map.put("totalSubtotal", totalSubtotal);
163
+        map.put("totalScore", totalScore);
164
+        map.put("title", title);
165
+
166
+        InputStream tpl = new FileInputStream(tplPath);
167
+        ExcelUtils.flushFile(response, tpl, CheckLocExport.class, list, map, title);
168
+
169
+        return null;
170
+    }
171
+
172
+    private Double getVal(Double d) {
173
+        return null == d ? 0.0 : d;
174
+    }
175
+    private Integer getVal(Integer d) {
176
+        return null == d ? 0 : d;
177
+    }
178
+
179
+
115 180
     /**
116 181
      * 复制
117 182
      *

+ 3
- 0
src/main/java/com/example/civilizedcity/mapper/TaCheckItemMapper.java Datei anzeigen

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 4
 import com.baomidou.mybatisplus.core.metadata.IPage;
5 5
 import com.example.civilizedcity.entity.SysUser;
6 6
 import com.example.civilizedcity.entity.TaCheck;
7
+import com.example.civilizedcity.vo.CheckLocExport;
7 8
 import org.apache.ibatis.annotations.Mapper;
8 9
 import org.apache.ibatis.annotations.Param;
9 10
 import com.example.civilizedcity.entity.TaCheckItem;
@@ -35,4 +36,6 @@ public interface TaCheckItemMapper  extends BaseMapper<TaCheckItem>{
35 36
     boolean updateFullScore(@Param("itemId") String itemId);
36 37
 
37 38
     int calcScore(@Param("checkId") String checkId);
39
+
40
+    List<CheckLocExport> getExportByCheck(@Param("checkId") String checkId, @Param("itemType") String itemType);
38 41
 }

+ 6
- 1
src/main/java/com/example/civilizedcity/service/TaCheckItemService.java Datei anzeigen

@@ -5,8 +5,11 @@ import com.example.civilizedcity.entity.SysUser;
5 5
 import com.example.civilizedcity.entity.TaCheck;
6 6
 import com.example.civilizedcity.entity.TaCheckAnswer;
7 7
 import com.example.civilizedcity.entity.TaCheckItem;
8
+import com.example.civilizedcity.vo.CheckLocExport;
8 9
 
9
- /**
10
+import java.util.List;
11
+
12
+/**
10 13
  * 测评点位;(ta_check_item)表服务接口
11 14
  * @author : http://njyunzhi.com
12 15
  * @date : 2022-12-13
@@ -20,4 +23,6 @@ public interface TaCheckItemService extends IBaseService<TaCheckItem> {
20 23
      void answer(TaCheckItem taCheckItem, TaCheckAnswer taCheckAnswer, SysUser sysUser) throws Exception;
21 24
 
22 25
      boolean updateFullScore(String itemId);
26
+
27
+     List<CheckLocExport> getExportByCheck(String checkId, String itemType);
23 28
  }

+ 6
- 0
src/main/java/com/example/civilizedcity/service/impl/TaCheckItemServiceImpl.java Datei anzeigen

@@ -6,6 +6,7 @@ import com.example.civilizedcity.common.NumberUtil;
6 6
 import com.example.civilizedcity.common.StringUtils;
7 7
 import com.example.civilizedcity.entity.*;
8 8
 import com.example.civilizedcity.mapper.*;
9
+import com.example.civilizedcity.vo.CheckLocExport;
9 10
 import org.springframework.beans.factory.annotation.Autowired;
10 11
 import org.springframework.stereotype.Service;
11 12
 import com.example.civilizedcity.service.TaCheckItemService;
@@ -117,6 +118,11 @@ public class TaCheckItemServiceImpl extends BaseServiceImpl<TaCheckItemMapper, T
117 118
         return baseMapper.updateFullScore(itemId);
118 119
     }
119 120
 
121
+    @Override
122
+    public List<CheckLocExport> getExportByCheck(String checkId, String itemType) {
123
+        return baseMapper.getExportByCheck(checkId, itemType);
124
+    }
125
+
120 126
     /**
121 127
      * 作答计算分数
122 128
      * 试卷总分 = 每一题分数求和

+ 9
- 0
src/main/java/com/example/civilizedcity/vo/CheckLocExport.java Datei anzeigen

@@ -0,0 +1,9 @@
1
+package com.example.civilizedcity.vo;
2
+
3
+import com.example.civilizedcity.entity.TaCheckItem;
4
+import lombok.Data;
5
+
6
+@Data
7
+public class CheckLocExport extends TaCheckItem {
8
+    Double subtotal;
9
+}

+ 3
- 0
src/main/resources/application-dev.yml Datei anzeigen

@@ -24,6 +24,9 @@ yz:
24 24
   upload:
25 25
     path: E:\work\public-upload
26 26
     prefix: http://127.0.0.1:8000
27
+  excel:
28
+    tpl:
29
+      check: E:\work\civilized_city\service\src\main\resources\check_tpl.xlsx
27 30
 
28 31
 wx:
29 32
   miniapp:

BIN
src/main/resources/check_tpl.xlsx Datei anzeigen


+ 12
- 0
src/main/resources/mapper/TaCheckItemMapper.xml Datei anzeigen

@@ -129,5 +129,17 @@
129 129
           AND t.item_type = #{itemType}
130 130
           AND t.`status` > - 1;
131 131
 </select>
132
+    <select id="getExportByCheck" resultType="com.example.civilizedcity.vo.CheckLocExport">
133
+
134
+        SELECT
135
+            t.*,
136
+            t.full_score * t.num AS subtotal
137
+        FROM
138
+            ta_check_item t
139
+        WHERE
140
+            t.check_id = #{checkId}
141
+          AND t.item_type = #{itemType}
142
+          AND t.`status` &gt; -1
143
+    </select>
132 144
 
133 145
 </mapper>