10
10
import apijson .ExcelUtil ;
11
11
import apijson .RequestMethod ;
12
12
import apijson .StringUtil ;
13
+ import apijson .fastjson2 .JSON ;
14
+ import apijson .JSONResponse ;
13
15
import com .alibaba .fastjson2 .JSONArray ;
16
+ import jakarta .servlet .http .HttpSession ;
14
17
import org .apache .commons .io .FileUtils ;
18
+ import org .springframework .beans .factory .annotation .Autowired ;
15
19
import org .springframework .core .io .InputStreamResource ;
16
20
import org .springframework .http .HttpHeaders ;
21
+ import org .springframework .http .HttpMethod ;
17
22
import org .springframework .http .MediaType ;
18
23
import org .springframework .http .ResponseEntity ;
19
24
import org .springframework .stereotype .Controller ;
28
33
29
34
import apijson .demo .DemoParser ;
30
35
36
+ import javax .imageio .ImageIO ;
37
+
31
38
import static com .google .common .io .Files .getFileExtension ;
32
39
33
40
/**文件相关的控制器,包括上传、下载、浏览等
@@ -77,6 +84,7 @@ public class FileController {
77
84
public static final List <String > VIDEO_SUFFIXES = Arrays .asList ("mp4" );
78
85
public static final List <String > IMG_SUFFIXES = Arrays .asList ("jpg" , "jpeg" , "png" );
79
86
private static List <String > fileNames = null ;
87
+
80
88
@ GetMapping ("/files" )
81
89
@ ResponseBody
82
90
public JSONObject files () {
@@ -126,8 +134,7 @@ public JSONObject upload(@RequestParam("file") MultipartFile file) {
126
134
res .put ("path" , "/download/" + name );
127
135
res .put ("size" , file .getBytes ().length );
128
136
return new DemoParser ().extendSuccessResult (res );
129
- }
130
- catch (Exception e ) {
137
+ } catch (Exception e ) {
131
138
e .printStackTrace ();
132
139
return new DemoParser ().newErrorResult (e );
133
140
}
@@ -191,10 +198,15 @@ private MediaType determineContentType(String fileName) {
191
198
}
192
199
}
193
200
194
- @ GetMapping ("/download/cv/report/{reportId}" )
201
+ @ Autowired
202
+ DemoController demoController ;
203
+
204
+ @ GetMapping ("/download/cv/report/{id}" )
195
205
@ ResponseBody
196
- public ResponseEntity <Object > downloadCVReport (@ PathVariable (name = "reportId" ) String reportId ) throws FileNotFoundException , IOException {
197
- String name = "CVAuto_report_" + reportId + ".xlsx" ;
206
+ public ResponseEntity <Object > downloadCVReport (@ PathVariable (name = "id" ) String idStr , HttpSession session ) throws FileNotFoundException , IOException {
207
+ long reportId = Long .parseLong (idStr );
208
+ boolean isLast = reportId <= 0 ;
209
+ String name = "CVAuto_report_" + (isLast ? "last" : reportId ) + ".xlsx" ;
198
210
String path = fileUploadRootDir + name ;
199
211
File file = new File (path );
200
212
long size = file .exists () ? file .length () : 0 ;
@@ -207,15 +219,22 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "reportId")
207
219
208
220
{ // TestRecord <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
209
221
JSONObject testRecord = new JSONObject ();
210
- testRecord .put ("reportId" , reportId );
211
- testRecord .put ("@column" , "documentId" );
222
+ if (isLast ) {
223
+ testRecord .put ("@order" , "reportId-" );
224
+ } else {
225
+ testRecord .put ("reportId" , reportId );
226
+ }
227
+ testRecord .put ("@column" , "reportId,documentId,randomId,sameIds" );
228
+
212
229
request .put ("TestRecord" , testRecord );
213
230
} // TestRecord >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
214
231
215
232
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
216
233
JSONObject item = new JSONObject ();
217
234
item .put ("count" , 3 );
235
+ //item.put("count", 0);
218
236
item .put ("join" , "&/TestRecord" );
237
+ //item.put("join", "@/TestRecord");
219
238
220
239
{ // Random <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
221
240
JSONObject random = new JSONObject ();
@@ -225,27 +244,83 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "reportId")
225
244
random .put ("@combine" , "file[>,img[>" );
226
245
random .put ("file[>" , 0 );
227
246
random .put ("img[>" , 0 );
247
+
228
248
item .put ("Random" , random );
229
249
} // Random >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
230
250
231
251
{ // TestRecord <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
232
252
JSONObject testRecord = new JSONObject ();
233
- testRecord .put ("randomId@" , "/Random/id" );
234
253
testRecord .put ("@column" , "id,total,correct,wrong,compare,response" );
235
- testRecord .put ("reportId" , reportId );
254
+ testRecord .put ("randomId@" , "/Random/id" );
255
+ testRecord .put ("documentId@" , "TestRecord/documentId" );
256
+
257
+ //testRecord.put("idx{}@", "TestRecord/sameIds");
258
+ if (isLast ) {
259
+ testRecord .put ("reportId@" , "TestRecord/reportId" );
260
+ //testRecord.put("@combine", "idx | reportId@");
261
+ } else {
262
+ testRecord .put ("reportId" , reportId );
263
+ //testRecord.put("@combine", "idx | reportId");
264
+ }
265
+ //testRecord.put("@key", "idx:(id)");
266
+ //testRecord.put("@combine", "idx | reportId");
267
+
236
268
testRecord .put ("total>=" , 0 );
237
269
testRecord .put ("correct>=" , 0 );
238
270
testRecord .put ("wrong>=" , 0 );
239
271
testRecord .put ("@order" , "date-" );
272
+
240
273
item .put ("TestRecord" , testRecord );
241
274
} // TestRecord >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
242
275
243
276
request .put ("[]" , item );
244
277
} // [] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
245
278
279
+ { // TestRecord[] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
280
+ JSONObject item = new JSONObject ();
281
+
282
+ { // TestRecord <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
283
+ JSONObject testRecord = new JSONObject ();
284
+ testRecord .put ("@column" , "id,randomId,total,correct,wrong,compare,response" );
285
+ testRecord .put ("id{}@" , "TestRecord/sameIds" );
286
+ testRecord .put ("documentId@" , "TestRecord/documentId" );
287
+ if (isLast ) {
288
+ testRecord .put ("reportId@" , "TestRecord/reportId" );
289
+ } else {
290
+ testRecord .put ("reportId" , reportId );
291
+ }
292
+ testRecord .put ("total>=" , 0 );
293
+ testRecord .put ("correct>=" , 0 );
294
+ testRecord .put ("wrong>=" , 0 );
295
+ testRecord .put ("@order" , "date-" );
296
+
297
+ item .put ("TestRecord" , testRecord );
298
+ } // TestRecord >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
299
+
300
+ request .put ("TestRecord[]" , item );
301
+
302
+ } // TestRecord[] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
303
+
246
304
DemoParser parser = new DemoParser (RequestMethod .GET , false );
247
305
JSONObject response = parser .parseResponse (request );
306
+ if (! JSONResponse .isSuccess (response )) {
307
+ throw new IOException (JSONResponse .getMsg (response ));
308
+ }
309
+
310
+ // JSONObject lastTr = response.getJSONObject("TestRecord");
311
+ // long documentId = lastTr == null ? 0 : lastTr.getLongValue("documentId");
312
+ // long randomId = lastTr == null ? 0 : lastTr.getLongValue("randomId");
313
+ if (isLast ) {
314
+ JSONObject lastTr = response .getJSONObject ("TestRecord" );
315
+ reportId = lastTr == null ? 0 : lastTr .getLongValue ("reportId" );
316
+ if (reportId > 0 ) {
317
+ name = "CVAuto_report_" + reportId + ".xlsx" ;
318
+ path = fileUploadRootDir + name ;
319
+ }
320
+ }
321
+
248
322
JSONArray array = response .getJSONArray ("[]" );
323
+ JSONArray trArr = response .getJSONArray ("TestRecord[]" );
249
324
250
325
List <ExcelUtil .DetailItem > list = new ArrayList <>();
251
326
if (array != null ) {
@@ -258,19 +333,75 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "reportId")
258
333
}
259
334
if (testRecord == null ) {
260
335
testRecord = new JSONObject ();
336
+ if (trArr != null ) {
337
+ int found = -1 ;
338
+ for (int j = 0 ; j < trArr .size (); j ++) {
339
+ JSONObject tr = trArr .getJSONObject (j );
340
+ long randomId = tr == null ? 0 : tr .getLongValue ("randomId" );
341
+ if (randomId <= 0 || ! Objects .equals (randomId , random .getLongValue ("id" ))) {
342
+ continue ;
343
+ }
344
+
345
+ testRecord = tr ;
346
+ found = j ;
347
+ break ;
348
+ }
349
+
350
+ if (found >= 0 ) {
351
+ trArr .remove (found );
352
+ }
353
+ }
261
354
}
262
355
263
356
String fn = random .getString ("file" );
264
357
int ind = fn .lastIndexOf ("." );
265
358
String nfn = fn .substring (0 , ind ) + "_render" + fn .substring (ind );
359
+
360
+ String resStr = testRecord .getString ("response" );
361
+
362
+ String img = random .getString ("img" );
363
+ JSONObject resObj = StringUtil .isEmpty (img ) ? null : JSON .parseObject (resStr );
364
+
365
+ int l = resStr == null ? 0 : resStr .length ();
366
+ if (l > 32767 ) { // EasyExcel 单元格最长字符数限制
367
+ resStr = resStr .substring (0 , 20000 ) + " ... " + resStr .substring (l - 12760 );
368
+ }
369
+
370
+ boolean hasResult = resObj != null && ! resObj .isEmpty ();
371
+ if (hasResult ) {
372
+ try {
373
+ JSONObject renderReq = new JSONObject ();
374
+ renderReq .put ("img" , img );
375
+ renderReq .put ("data" , resObj );
376
+ String body = renderReq .toJSONString ();
377
+ HttpHeaders headers = new HttpHeaders ();
378
+ String renderStr = demoController .sendRequest (session , HttpMethod .POST , "http://localhost:3003/cv/render" , body , headers );
379
+ renderStr = StringUtil .trim (renderStr );
380
+ if (renderStr .length () > 100 ) {
381
+ File renderFile = new File (fileUploadRootDir + nfn );
382
+ try (FileOutputStream fos = new FileOutputStream (renderFile )) {
383
+ int commaInd = renderStr .startsWith ("data:image/" ) ? -1 : renderStr .indexOf ("base64," );
384
+ String base64 = commaInd < 0 ? renderStr : renderStr .substring (commaInd + "base64," .length ());
385
+ byte [] bytes = Base64 .getDecoder ().decode (base64 );
386
+ fos .write (bytes );
387
+ fos .flush ();
388
+ } catch (Throwable e ) {
389
+ e .printStackTrace ();
390
+ }
391
+ }
392
+ } catch (Throwable e ) {
393
+ e .printStackTrace ();
394
+ }
395
+ }
396
+
266
397
list .add (new ExcelUtil .DetailItem (
267
- fn ,
268
- nfn , // TODO 调用 JSONResponse.js 来渲染
398
+ fileUploadRootDir + fn ,
399
+ fileUploadRootDir + nfn , // TODO 调用 JSONResponse.js 来渲染
269
400
testRecord .getIntValue ("total" ),
270
401
testRecord .getIntValue ("correct" ),
271
402
testRecord .getIntValue ("wrong" ),
272
- testRecord . getString ( "response" ) ,
273
- "✅ " ,
403
+ resStr ,
404
+ hasResult ? "✅" : "❌ " ,
274
405
testRecord .getString ("compare" )
275
406
));
276
407
}
0 commit comments