Yansen 2 years ago
parent
commit
3a21eea751

+ 24
- 1
pom.xml View File

76
         <dependency>
76
         <dependency>
77
             <groupId>com.alibaba</groupId>
77
             <groupId>com.alibaba</groupId>
78
             <artifactId>easyexcel</artifactId>
78
             <artifactId>easyexcel</artifactId>
79
-            <version>3.1.3</version>
79
+            <version>3.3.1</version>
80
         </dependency>
80
         </dependency>
81
 
81
 
82
         <!--mybatis-plus start-->
82
         <!--mybatis-plus start-->
158
                 </configuration>
158
                 </configuration>
159
             </plugin>
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
             <plugin>
184
             <plugin>
162
                 <groupId>org.apache.maven.plugins</groupId>
185
                 <groupId>org.apache.maven.plugins</groupId>
163
                 <artifactId>maven-dependency-plugin</artifactId>
186
                 <artifactId>maven-dependency-plugin</artifactId>

+ 30
- 0
src/main/java/com/example/civilizedcity/common/ExcelUtils.java View File

6
 import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
6
 import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
7
 import com.alibaba.excel.write.handler.WriteHandler;
7
 import com.alibaba.excel.write.handler.WriteHandler;
8
 import com.alibaba.excel.write.metadata.WriteSheet;
8
 import com.alibaba.excel.write.metadata.WriteSheet;
9
+import com.alibaba.excel.write.metadata.fill.FillConfig;
9
 
10
 
10
 import javax.servlet.http.HttpServletResponse;
11
 import javax.servlet.http.HttpServletResponse;
11
 import java.io.IOException;
12
 import java.io.IOException;
13
+import java.io.InputStream;
12
 import java.util.List;
14
 import java.util.List;
15
+import java.util.Map;
13
 
16
 
14
 public class ExcelUtils {
17
 public class ExcelUtils {
15
 
18
 
30
         EasyExcel.write(response.getOutputStream(), dataClass).sheet("sheet1").doWrite(data);
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
      * @param response
65
      * @param response

+ 65
- 0
src/main/java/com/example/civilizedcity/controller/TaCheckController.java View File

7
 import com.example.civilizedcity.entity.SysUser;
7
 import com.example.civilizedcity.entity.SysUser;
8
 import com.example.civilizedcity.entity.TaCheckItem;
8
 import com.example.civilizedcity.entity.TaCheckItem;
9
 import com.example.civilizedcity.service.TaCheckItemService;
9
 import com.example.civilizedcity.service.TaCheckItemService;
10
+import com.example.civilizedcity.vo.CheckLocExport;
10
 import io.swagger.annotations.Api;
11
 import io.swagger.annotations.Api;
11
 import io.swagger.annotations.ApiOperation;
12
 import io.swagger.annotations.ApiOperation;
12
 import io.swagger.annotations.ApiParam;
13
 import io.swagger.annotations.ApiParam;
13
 import org.springframework.beans.factory.annotation.Autowired;
14
 import org.springframework.beans.factory.annotation.Autowired;
15
+import org.springframework.beans.factory.annotation.Value;
14
 import org.springframework.web.bind.annotation.*;
16
 import org.springframework.web.bind.annotation.*;
15
 import com.example.civilizedcity.entity.TaCheck;
17
 import com.example.civilizedcity.entity.TaCheck;
16
 import com.example.civilizedcity.service.TaCheckService;
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
  * 模拟测评;(ta_check)表控制层
28
  * 模拟测评;(ta_check)表控制层
20
  *
29
  *
32
     @Autowired
41
     @Autowired
33
     private TaCheckItemService taCheckItemService;
42
     private TaCheckItemService taCheckItemService;
34
 
43
 
44
+    @Value("${yz.excel.tpl.check}")
45
+    String tplPath;
46
+
35
     /**
47
     /**
36
      * 通过ID查询单条数据
48
      * 通过ID查询单条数据
37
      *
49
      *
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 View File

4
 import com.baomidou.mybatisplus.core.metadata.IPage;
4
 import com.baomidou.mybatisplus.core.metadata.IPage;
5
 import com.example.civilizedcity.entity.SysUser;
5
 import com.example.civilizedcity.entity.SysUser;
6
 import com.example.civilizedcity.entity.TaCheck;
6
 import com.example.civilizedcity.entity.TaCheck;
7
+import com.example.civilizedcity.vo.CheckLocExport;
7
 import org.apache.ibatis.annotations.Mapper;
8
 import org.apache.ibatis.annotations.Mapper;
8
 import org.apache.ibatis.annotations.Param;
9
 import org.apache.ibatis.annotations.Param;
9
 import com.example.civilizedcity.entity.TaCheckItem;
10
 import com.example.civilizedcity.entity.TaCheckItem;
35
     boolean updateFullScore(@Param("itemId") String itemId);
36
     boolean updateFullScore(@Param("itemId") String itemId);
36
 
37
 
37
     int calcScore(@Param("checkId") String checkId);
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 View File

5
 import com.example.civilizedcity.entity.TaCheck;
5
 import com.example.civilizedcity.entity.TaCheck;
6
 import com.example.civilizedcity.entity.TaCheckAnswer;
6
 import com.example.civilizedcity.entity.TaCheckAnswer;
7
 import com.example.civilizedcity.entity.TaCheckItem;
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
  * 测评点位;(ta_check_item)表服务接口
13
  * 测评点位;(ta_check_item)表服务接口
11
  * @author : http://njyunzhi.com
14
  * @author : http://njyunzhi.com
12
  * @date : 2022-12-13
15
  * @date : 2022-12-13
20
      void answer(TaCheckItem taCheckItem, TaCheckAnswer taCheckAnswer, SysUser sysUser) throws Exception;
23
      void answer(TaCheckItem taCheckItem, TaCheckAnswer taCheckAnswer, SysUser sysUser) throws Exception;
21
 
24
 
22
      boolean updateFullScore(String itemId);
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 View File

6
 import com.example.civilizedcity.common.StringUtils;
6
 import com.example.civilizedcity.common.StringUtils;
7
 import com.example.civilizedcity.entity.*;
7
 import com.example.civilizedcity.entity.*;
8
 import com.example.civilizedcity.mapper.*;
8
 import com.example.civilizedcity.mapper.*;
9
+import com.example.civilizedcity.vo.CheckLocExport;
9
 import org.springframework.beans.factory.annotation.Autowired;
10
 import org.springframework.beans.factory.annotation.Autowired;
10
 import org.springframework.stereotype.Service;
11
 import org.springframework.stereotype.Service;
11
 import com.example.civilizedcity.service.TaCheckItemService;
12
 import com.example.civilizedcity.service.TaCheckItemService;
117
         return baseMapper.updateFullScore(itemId);
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 View File

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 View File

24
   upload:
24
   upload:
25
     path: E:\work\public-upload
25
     path: E:\work\public-upload
26
     prefix: http://127.0.0.1:8000
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
 wx:
31
 wx:
29
   miniapp:
32
   miniapp:

BIN
src/main/resources/check_tpl.xlsx View File


+ 12
- 0
src/main/resources/mapper/TaCheckItemMapper.xml View File

129
           AND t.item_type = #{itemType}
129
           AND t.item_type = #{itemType}
130
           AND t.`status` > - 1;
130
           AND t.`status` > - 1;
131
 </select>
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
 </mapper>
145
 </mapper>