|
@@ -1,17 +1,35 @@
|
|
package com.rf.kjb.scale.rest;
|
|
package com.rf.kjb.scale.rest;
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.rf.kjb.base.rest.BaseController;
|
|
import com.rf.kjb.base.rest.BaseController;
|
|
import com.rf.kjb.scale.dao.model.GameRecordEntity;
|
|
import com.rf.kjb.scale.dao.model.GameRecordEntity;
|
|
|
|
+import com.rf.kjb.scale.dao.model.RecordEntity;
|
|
import com.rf.kjb.scale.service.GameRecordService;
|
|
import com.rf.kjb.scale.service.GameRecordService;
|
|
|
|
+import com.rf.kjb.utils.Constant;
|
|
import com.rf.kjb.utils.Result;
|
|
import com.rf.kjb.utils.Result;
|
|
|
|
+import com.rf.kjb.utils.ZipUtils;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Comparator;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.stream.Stream;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @Author:zzf
|
|
* @Author:zzf
|
|
* @Date:2023/6/7:09:55
|
|
* @Date:2023/6/7:09:55
|
|
@@ -28,14 +46,263 @@ public class GameRecordController extends BaseController {
|
|
@PostMapping("/save")
|
|
@PostMapping("/save")
|
|
public Result save(@RequestBody String json){
|
|
public Result save(@RequestBody String json){
|
|
GameRecordEntity gameRecordEntity = JSONObject.parseObject(json).toJavaObject(GameRecordEntity.class);
|
|
GameRecordEntity gameRecordEntity = JSONObject.parseObject(json).toJavaObject(GameRecordEntity.class);
|
|
|
|
+ gameRecordEntity.setCreateTime(new Date());
|
|
this.gameRecordService.save(gameRecordEntity);
|
|
this.gameRecordService.save(gameRecordEntity);
|
|
return success();
|
|
return success();
|
|
}
|
|
}
|
|
|
|
|
|
- @ApiOperation(value = "查询",notes = "id:游戏记录id;identifier:编号;gameScene:游戏场景;equipmentType:设备类型;gameDifficulty:游戏难度;gameTime:游戏时间;fileName:文件名;pageNum:页数,默认1;pageSize:每页记录数,默认10")
|
|
|
|
- @GetMapping("/find")
|
|
|
|
|
|
+ @ApiOperation(value = "查询",notes = "id:游戏记录id;type:1是单人模式 2是双人模式;num1:编号;gameScene:游戏场景;equipmentType:设备类型;gameDifficulty:游戏难度;gameTime:游戏时间;startDate:开始时间;endDate:结束时间;pageNum:页数,默认1;pageSize:每页记录数,默认10")
|
|
|
|
+ @PostMapping("/find")
|
|
public Result find(@RequestBody String json){
|
|
public Result find(@RequestBody String json){
|
|
Page<GameRecordEntity> page = this.gameRecordService.find(json);
|
|
Page<GameRecordEntity> page = this.gameRecordService.find(json);
|
|
return success(page);
|
|
return success(page);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "原始信号单个和批量下载")
|
|
|
|
+ @GetMapping("/download/originalSignal")
|
|
|
|
+ public String downloadOriginalSignal(@RequestParam(value = "ids", required = false) List<String> recordingIds, HttpServletResponse response) throws Exception {
|
|
|
|
+ //原始记录文件
|
|
|
|
+ File file1 = null;
|
|
|
|
+ File file2 = null;
|
|
|
|
+ //原始记录复制后文件
|
|
|
|
+ File destFile1 = null;
|
|
|
|
+ File destFile2 = null;
|
|
|
|
+ //所有记录汇总后文件夹,用来压缩
|
|
|
|
+ String filePath ="./心理检测/生物反馈原始信号";
|
|
|
|
+ //原始记录文件存放路径
|
|
|
|
+ String userHome = System.getProperty("user.home");
|
|
|
|
+ for (int i = 0; i < recordingIds.size(); i++) {
|
|
|
|
+ GameRecordEntity gameRecordEntity = this.gameRecordService.findById(recordingIds.get(i));
|
|
|
|
+ if (gameRecordEntity != null){
|
|
|
|
+ file1 = new File(userHome+"/rzgy/"+gameRecordEntity.getFileName1()+".txt");
|
|
|
|
+ if (file1.exists()){
|
|
|
|
+ destFile1 = new File(filePath+"/"+gameRecordEntity.getNum1()+"/"+gameRecordEntity.getSaveTime()+"/"+gameRecordEntity.getFileName1()+".txt");
|
|
|
|
+ //将原始文件进行复制
|
|
|
|
+ FileUtils.copyFile(file1, destFile1);
|
|
|
|
+ }
|
|
|
|
+ if (gameRecordEntity.getFileName2() != null && !gameRecordEntity.getFileName2().equals("")){
|
|
|
|
+ file2 = new File(userHome+"/rzgy/"+gameRecordEntity.getFileName2()+".txt");
|
|
|
|
+ if (file2.exists()){
|
|
|
|
+ destFile2 = new File(filePath+"/"+gameRecordEntity.getNum1()+"/"+gameRecordEntity.getSaveTime()+"/"+gameRecordEntity.getFileName2()+".txt");
|
|
|
|
+ //将原始文件进行复制
|
|
|
|
+ FileUtils.copyFile(file2, destFile2);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //将复制后的整个文件夹打包
|
|
|
|
+ ZipUtils.createZip(filePath, filePath + ".zip", true);
|
|
|
|
+ File zipFile = new File(filePath + ".zip");
|
|
|
|
+ //下载压缩后文件
|
|
|
|
+ if (zipFile.exists()) {
|
|
|
|
+ return com.rf.kjb.utils.FileUtils.downloadFile(zipFile,filePath,"originalSignal"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+".zip",response);
|
|
|
|
+ }
|
|
|
|
+ return "下载失败";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "原始信号全部下载",notes = "id:游戏记录id;type:1是单人模式 2是双人模式;num1:编号;gameScene:游戏场景;equipmentType:设备类型;gameDifficulty:游戏难度;gameTime:游戏时间;startDate:开始时间;endDate:结束时间;pageNum:页数,默认1;pageSize:每页记录数,默认10")
|
|
|
|
+ @PostMapping("/download/originalSignalAll")
|
|
|
|
+ public String downloadOriginalSignalAll(@RequestBody String json, HttpServletResponse response) throws IOException {
|
|
|
|
+ Page<GameRecordEntity> page = this.gameRecordService.find(json);
|
|
|
|
+ List<GameRecordEntity> gameRecordEntityList = page.getContent();
|
|
|
|
+ //原始记录文件
|
|
|
|
+ File file1 = null;
|
|
|
|
+ File file2 = null;
|
|
|
|
+ //原始记录复制后文件
|
|
|
|
+ File destFile1 = null;
|
|
|
|
+ File destFile2 = null;
|
|
|
|
+ //所有记录汇总后文件夹,用来压缩
|
|
|
|
+ String filePath ="./心理检测/生物反馈原始信号";
|
|
|
|
+ //原始记录文件存放路径
|
|
|
|
+ String userHome = System.getProperty("user.home");
|
|
|
|
+ for (int i = 0; i < gameRecordEntityList.size(); i++) {
|
|
|
|
+ GameRecordEntity gameRecordEntity = gameRecordEntityList.get(i);
|
|
|
|
+ if (gameRecordEntity != null){
|
|
|
|
+ file1 = new File(userHome+"/rzgy/"+gameRecordEntity.getFileName1()+".txt");
|
|
|
|
+ if (file1.exists()){
|
|
|
|
+ destFile1 = new File(filePath+"/"+gameRecordEntity.getNum1()+"/"+gameRecordEntity.getSaveTime()+"/"+gameRecordEntity.getFileName1()+".txt");
|
|
|
|
+ //将原始文件进行复制
|
|
|
|
+ FileUtils.copyFile(file1, destFile1);
|
|
|
|
+ }
|
|
|
|
+ if (gameRecordEntity.getFileName2() != null && !gameRecordEntity.getFileName2().equals("")){
|
|
|
|
+ file2 = new File(userHome+"/rzgy/"+gameRecordEntity.getFileName2()+".txt");
|
|
|
|
+ if (file2.exists()){
|
|
|
|
+ destFile2 = new File(filePath+"/"+gameRecordEntity.getNum1()+"/"+gameRecordEntity.getSaveTime()+"/"+gameRecordEntity.getFileName2()+".txt");
|
|
|
|
+ //将原始文件进行复制
|
|
|
|
+ FileUtils.copyFile(file2, destFile2);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //将复制后的整个文件夹打包
|
|
|
|
+ ZipUtils.createZip(filePath, filePath + ".zip", true);
|
|
|
|
+ File zipFile = new File(filePath + ".zip");
|
|
|
|
+ //下载压缩后文件
|
|
|
|
+ if (zipFile.exists()) {
|
|
|
|
+ return com.rf.kjb.utils.FileUtils.downloadFile(zipFile,filePath,"originalSignal"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+".zip",response);
|
|
|
|
+ }
|
|
|
|
+ return "下载失败";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "生物反馈训练记录单个下载")
|
|
|
|
+ @GetMapping("/download/trainingRecord")
|
|
|
|
+ public String downloadTrainingRecord(String id, HttpServletResponse response) throws IOException {
|
|
|
|
+ if (id != null) {
|
|
|
|
+ GameRecordEntity gameRecordEntity = this.gameRecordService.findById(id);
|
|
|
|
+ if (gameRecordEntity == null){
|
|
|
|
+ return "该记录异常,下载失败";
|
|
|
|
+ }
|
|
|
|
+ String destFileName ="./心理检测/生物反馈记录/"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
|
|
|
|
+ String destFileName2 = destFileName+"/"+gameRecordEntity.getNum1()+"-"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+".xlsx";
|
|
|
|
+ createFile(gameRecordEntity,destFileName2);
|
|
|
|
+ File file = new File(destFileName2);
|
|
|
|
+ return com.rf.kjb.utils.FileUtils.downloadFile(file,destFileName,file.getName(),response);
|
|
|
|
+ }
|
|
|
|
+ return "下载失败";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "生物反馈训练记录批量下载")
|
|
|
|
+ @GetMapping("/download/trainingRecords")
|
|
|
|
+ public String downloadTrainingRecords(@RequestParam(value = "ids", required = false) List<String> recordingIds, HttpServletResponse response) throws Exception {
|
|
|
|
+ String destFileName ="./心理检测/生物反馈记录/"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
|
|
|
|
+ for (int i = 0; i < recordingIds.size(); i++) {
|
|
|
|
+ GameRecordEntity gameRecordEntity = this.gameRecordService.findById(recordingIds.get(i));
|
|
|
|
+ if (gameRecordEntity != null){
|
|
|
|
+ String destFileName2 = destFileName+"/"+gameRecordEntity.getNum1()+"-"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+".xlsx";
|
|
|
|
+ createFile(gameRecordEntity,destFileName2);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //将复制后的整个文件夹打包
|
|
|
|
+ ZipUtils.createZip(destFileName, destFileName + ".zip", true);
|
|
|
|
+ File zipFile = new File(destFileName + ".zip");
|
|
|
|
+ //下载压缩后文件
|
|
|
|
+ if (zipFile.exists()) {
|
|
|
|
+ return com.rf.kjb.utils.FileUtils.downloadFile(zipFile,destFileName,"trainingRecord"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+".zip",response);
|
|
|
|
+ }
|
|
|
|
+ return "下载失败";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "生物反馈训练记录全部下载",notes = "id:游戏记录id;type:1是单人模式 2是双人模式;num1:编号;gameScene:游戏场景;equipmentType:设备类型;gameDifficulty:游戏难度;gameTime:游戏时间;startDate:开始时间;endDate:结束时间;pageNum:页数,默认1;pageSize:每页记录数,默认10")
|
|
|
|
+ @PostMapping("/download/trainingRecordsAll")
|
|
|
|
+ public String downloadTrainingRecordsAll(@RequestBody String json, HttpServletResponse response) throws IOException {
|
|
|
|
+ String destFileName ="./心理检测/生物反馈记录/"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
|
|
|
|
+ Page<GameRecordEntity> page = this.gameRecordService.find(json);
|
|
|
|
+ List<GameRecordEntity> gameRecordEntityList = page.getContent();
|
|
|
|
+ for (int i = 0; i < gameRecordEntityList.size(); i++) {
|
|
|
|
+ GameRecordEntity gameRecordEntity = gameRecordEntityList.get(i);
|
|
|
|
+ if (gameRecordEntity != null){
|
|
|
|
+ String destFileName2 = destFileName+"/"+gameRecordEntity.getNum1()+"-"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+".xlsx";
|
|
|
|
+ createFile(gameRecordEntity,destFileName2);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //将复制后的整个文件夹打包
|
|
|
|
+ ZipUtils.createZip(destFileName, destFileName + ".zip", true);
|
|
|
|
+ File zipFile = new File(destFileName + ".zip");
|
|
|
|
+ //下载压缩后文件
|
|
|
|
+ if (zipFile.exists()) {
|
|
|
|
+ return com.rf.kjb.utils.FileUtils.downloadFile(zipFile,destFileName,"trainingRecord"+new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+".zip",response);
|
|
|
|
+ }
|
|
|
|
+ return "下载失败";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Workbook createExcelFile(Workbook wb,GameRecordEntity gameRecordEntity,String []title,JSONArray jsonData,String type) {
|
|
|
|
+ String num;
|
|
|
|
+ //1:单人获取num1;2双人获取num2
|
|
|
|
+ if (type.equals("1")) {
|
|
|
|
+ num = gameRecordEntity.getNum1();
|
|
|
|
+ }else {
|
|
|
|
+ num = gameRecordEntity.getNum2();
|
|
|
|
+ }
|
|
|
|
+ Sheet downSheet = wb.createSheet(num);
|
|
|
|
+ //获取表头行
|
|
|
|
+ Row titleRow = downSheet.createRow(0);
|
|
|
|
+ //创建单元格,设置style居中,字体,单元格大小等
|
|
|
|
+ CellStyle style = wb.createCellStyle();
|
|
|
|
+ //设置水平对齐的样式为居中对齐;
|
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
+ //设置垂直对齐的样式为居中对齐;
|
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
+ Cell cell;
|
|
|
|
+ //把已经写好的标题行写入excel文件中
|
|
|
|
+ for (int k = 0; k < title.length; k++) {
|
|
|
|
+ cell = titleRow.createCell(k);
|
|
|
|
+ cell.setCellValue(title[k]);
|
|
|
|
+ cell.setCellStyle(style);
|
|
|
|
+ }
|
|
|
|
+ Row row = downSheet.createRow(1);
|
|
|
|
+ row.createCell(0).setCellValue(num);
|
|
|
|
+ row.createCell(1).setCellValue(gameRecordEntity.getGameScene());
|
|
|
|
+ row.createCell(2).setCellValue(gameRecordEntity.getEquipmentType());
|
|
|
|
+ row.createCell(3).setCellValue(gameRecordEntity.getTargetType());
|
|
|
|
+ row.createCell(4).setCellValue(gameRecordEntity.getGameDifficulty());
|
|
|
|
+ row.createCell(5).setCellValue(gameRecordEntity.getGameTime());
|
|
|
|
+ for (int m = 0; m < jsonData.size(); m++) {
|
|
|
|
+ if (m == 0){
|
|
|
|
+ row.createCell(6).setCellValue(jsonData.getDoubleValue(m));
|
|
|
|
+ }else {
|
|
|
|
+ Row row2 = downSheet.createRow(m + 1);
|
|
|
|
+ row2.createCell(6).setCellValue(jsonData.getDoubleValue(m));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return wb;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void createFile(GameRecordEntity gameRecordEntity,String destFileName2) throws IOException {
|
|
|
|
+ Workbook wb = new XSSFWorkbook();
|
|
|
|
+ String []title;
|
|
|
|
+ title = new String[7];
|
|
|
|
+ title[0] = "用户编号";
|
|
|
|
+ String [] temp = {"场景名称","设备类型","所选指标","游戏难度","游戏时长"};
|
|
|
|
+ System.arraycopy(temp,0,title,1,temp.length);
|
|
|
|
+ if(gameRecordEntity.getEquipmentType().equals("心电")){
|
|
|
|
+ title[6] = "heartRate";
|
|
|
|
+ }else{
|
|
|
|
+ if (gameRecordEntity.getTargetType().equals("专注度")){
|
|
|
|
+ title[6] = "attention";
|
|
|
|
+ }else {
|
|
|
|
+ title[6] = "meditation";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONArray jsonData ;
|
|
|
|
+ JSONArray jsonData2 ;
|
|
|
|
+ if(gameRecordEntity.getType().equals("1")){
|
|
|
|
+ if(gameRecordEntity.getEquipmentType().equals("心电")){
|
|
|
|
+ jsonData = JSONObject.parseArray(gameRecordEntity.getAnimationTarget1());
|
|
|
|
+ }else {
|
|
|
|
+ if (gameRecordEntity.getTargetType().equals("专注度")){
|
|
|
|
+ jsonData = JSONObject.parseObject(gameRecordEntity.getAnimationSourceList1()).getJSONArray("attentionDataAll");
|
|
|
|
+ }else {
|
|
|
|
+ jsonData = JSONObject.parseObject(gameRecordEntity.getAnimationSourceList1()).getJSONArray("meditationDataAll");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ wb = createExcelFile(wb,gameRecordEntity,title,jsonData,"1");
|
|
|
|
+ }else {
|
|
|
|
+ if(gameRecordEntity.getEquipmentType().equals("心电")){
|
|
|
|
+ jsonData = JSONObject.parseArray(gameRecordEntity.getAnimationTarget1());
|
|
|
|
+ jsonData2 = JSONObject.parseArray(gameRecordEntity.getAnimationTarget2());
|
|
|
|
+ }else {
|
|
|
|
+ if (gameRecordEntity.getTargetType().equals("专注度")){
|
|
|
|
+ jsonData = JSONObject.parseObject(gameRecordEntity.getAnimationSourceList1()).getJSONArray("attentionDataAll");
|
|
|
|
+ jsonData2 = JSONObject.parseObject(gameRecordEntity.getAnimationSourceList2()).getJSONArray("attentionDataAll");
|
|
|
|
+ }else {
|
|
|
|
+ jsonData = JSONObject.parseObject(gameRecordEntity.getAnimationSourceList1()).getJSONArray("meditationDataAll");
|
|
|
|
+ jsonData2 = JSONObject.parseObject(gameRecordEntity.getAnimationSourceList2()).getJSONArray("meditationDataAll");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ wb = createExcelFile(wb,gameRecordEntity,title,jsonData,"1");
|
|
|
|
+ wb = createExcelFile(wb,gameRecordEntity,title,jsonData2,"2");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ File destFile = new File(destFileName2);
|
|
|
|
+ File fileParent = destFile.getParentFile();
|
|
|
|
+ if(!fileParent.exists()){
|
|
|
|
+ fileParent.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ destFile.createNewFile();
|
|
|
|
+ OutputStream fileOut = Files.newOutputStream(destFile.toPath());
|
|
|
|
+ wb.write(fileOut);
|
|
|
|
+ fileOut.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|