package com.api.games.controller; import com.api.common.UtilFun; import com.api.common.execl.ExcelUtil; import com.api.core.annotation.PowerEnable; import com.api.core.controller.Ctrl; import com.api.core.response.Result; import com.api.core.response.ResultGenerator; import com.api.games.model.GameAnswer; import com.api.games.model.GameLog; import com.api.games.model.GameLogDownload; import com.api.games.model.GameLogDownloadV2; import com.api.games.service.GameLogService; import io.swagger.annotations.*; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.*; /** * Created by wanghuiwen on 2020/02/23. */ @PowerEnable(name = "游戏测试记录", url = "/game/log") @Api(value = "游戏测试记录", tags = {"游戏测试记录"}) @RestController @RequestMapping("/game/log") public class GameLogController extends Ctrl { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource private GameLogService gameLogService; @PostMapping(value = "/game_time/list", name = "游戏测试记录添加") public Result add( @RequestParam("userId") Long userId ) { return gameLogService.listGameTime(userId); } @ApiOperation(value = "游戏测试记录添加", tags = {"游戏测试记录"}, notes = "游戏测试记录添加") @PostMapping(value = "/add", name = "游戏测试记录添加") public Result add(@ApiParam GameLog gameLog) { gameLogService.save(gameLog); return ResultGenerator.genSuccessResult(gameLog.getId()); } @ApiOperation(value = "游戏测试记录删除", tags = {"游戏测试记录"}, notes = "游戏测试记录删除") @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "游戏测试记录id", dataType = "Long", paramType = "query") }) @PostMapping(value = "/delete", name = "游戏测试记录删除") public Result delete(@RequestParam Long id) { gameLogService.deleteById(id); return ResultGenerator.genSuccessResult(); } @PostMapping(value = "/delete/ids", name = "游戏测试记录删除") public Result deleteIds(@RequestParam String ids) { gameLogService.deleteByIds(ids); return ResultGenerator.genSuccessResult(); } @ApiOperation(value = "游戏测试记录修改", tags = {"游戏测试记录"}, notes = "游戏测试记录修改,对象主键必填") @PostMapping(value = "/update", name = "游戏测试记录修改") public Result update(@ApiParam GameLog gameLog) { gameLogService.update(gameLog); return ResultGenerator.genSuccessResult(); } @ApiOperation(value = "游戏测试记录详细信息", tags = {"游戏测试记录"}, notes = "游戏测试记录详细信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", required = true, value = "游戏测试记录id", dataType = "Long", paramType = "query") }) @PostMapping(value = "/detail", name = "游戏测试记录详细信息") public Result detail(@RequestParam Integer id) { return gameLogService.detail(id); } @ApiOperation(value = "游戏测试记录列表信息", tags = {"游戏测试记录"}, notes = "游戏测试记录列表信息") @ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10") }) @PostMapping(value = "/list", name = "用户配置列表信息") public Result list(@RequestParam(required = false) String search, @RequestParam(required = false) String order, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size) { return gameLogService.list(search, order, page, size); } @PostMapping("/download") public void downloadFile(@RequestParam(defaultValue = "{}") String search, @RequestParam(defaultValue = "{}") String order, HttpServletResponse response) { // List> res = gameLogService.download(search, order); // if (res == null || res.size() < 1) return ; // Map stat = gameLogService.download(search); List res = gameLogService.newDownload(search, order); int column = 15; int columnPlus = 0; for (GameLogDownloadV2 item : res){ if (item.gameAnswers.size() > columnPlus) columnPlus = item.gameAnswers.size(); } column += columnPlus * 4; int rowCount = res.size(); String fileTitle = UtilFun.DateToString(new Date(), UtilFun.YMD) + ".xlsx"; String[] title = getTitles(new String[]{"ID","用戶名", "日期", "序號", "版本", "正確率", "錯誤率", "錯失率", "反應時間" , "主觀正確率", "主觀信度", "開始時間", "完成時間", "完成度", "設備碼", "抽取的數字"}, columnPlus); File file = new File(fileTitle); BufferedInputStream bis = null; OutputStream os = null; // 设置强制下载不打开 response.setContentType("application/octet-stream"); // 设置文件名 response.addHeader("Content-Disposition", "attachment;fileName=" + fileTitle); response.addHeader("filename", fileTitle); response.setHeader("Access-Control-Expose-Headers","filename,Content-Disposition"); try { OutputStream out = new FileOutputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作簿对象 XSSFSheet sheet = workbook.createSheet(UtilFun.DateToString(new Date(), UtilFun.YMD)); // 创建工作表 XSSFRow titleRow = sheet.createRow(0); // 产生表格标题行 XSSFCell cellTitle = titleRow.createCell(0); //创建表格标题列 XSSFCellStyle columnTopStyle = ExcelUtil.getColumnTopStyle(workbook);// 获取列头样式对象 XSSFCellStyle style = ExcelUtil.getStyle(workbook); // 获取单元格样式对象 // 合并表格标题行,合并列数为列名的长度,第一个0为起始行号,第二个1为终止行号,第三个0为起始列好,第四个参数为终止列号 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, column)); cellTitle.setCellStyle(columnTopStyle); //设置标题行样式 cellTitle.setCellValue(fileTitle); //设置标题行值 XSSFRow rowRowName = sheet.createRow(1); // 在索引2的位置创建行(最顶端的行开始的第二行) // 将列头设置到sheet的单元格中 for (int n = 0; n < title.length; n++) { XSSFCell cellRowName = rowRowName.createCell(n); // 创建列头对应个数的单元格 cellRowName.setCellType(CellType.STRING); // 设置列头单元格的数据类型 XSSFRichTextString text = new XSSFRichTextString(title[n]); cellRowName.setCellValue(text); // 设置列头单元格的值 cellRowName.setCellStyle(columnTopStyle); // 设置列头单元格样式 } // 将查询出的数据设置到sheet对应的单元格中 for (int i = 0; i < rowCount; i++) { XSSFRow row = sheet.createRow(i + 2); // 创建所需的行数 for (int j = 0; j <= column; j++) { XSSFCell cell = null; // 设置单元格的数据类型 if (j == 0) { cell = row.createCell(j, CellType.NUMERIC); cell.setCellValue(i + 1); } else { String text = ""; switch (j) { case 1: { text = res.get(i).nickName; break; } case 2:{ text = res.get(i).date; break; } case 3:{ text = res.get(i).sign; break; } case 4:{ text = res.get(i).version; break; } case 5:{ text = res.get(i).rightRate; break; } case 6:{ text = res.get(i).wrongRate; break; } case 7:{ text = res.get(i).missRate; break; } case 8:{ text = res.get(i).reaction; break; } case 9:{ text = res.get(i).right; break; } case 10:{ text = res.get(i).trust; break; } case 11:{ text = res.get(i).startTime; break; } case 12:{ text = res.get(i).finishTime; break; } case 13:{ text = res.get(i).finish; break; } case 14:{ text = res.get(i).deviceId; break; } case 15:{ text = res.get(i).number; break; } default:{ int size = (j - 16) / 4; if (res.get(i).gameAnswers.size() > size){ GameAnswer gameAnswer = res.get(i).gameAnswers.get(size); int itemSize = (j - 16) % 4; if (itemSize == 0){ text = gameAnswer.getShowNum().toString(); }else if(itemSize == 1){ text = gameAnswer.getAnswer() == 1 ? "按下" : "沒按下"; }else if(itemSize == 2){ text = gameAnswer.getCorrect() == 1 ? "正確" : gameAnswer.getCorrect() == 2 ? "錯誤" : "錯失"; }else if(itemSize == 3){ text = gameAnswer.getReaction().floatValue() + "ms"; } } break; } } cell = row.createCell(j, CellType.STRING); cell.setCellValue(text); // 设置单元格的值 } cell.setCellStyle(style); // 设置单元格样式 } } // 让列宽随着导出的列长自动适应 for (int colNum = 0; colNum <= column; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { XSSFRow currentRow; // 当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { XSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == CellType.STRING) { int length = currentCell.getStringCellValue() .getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if (colNum == 0) { sheet.setColumnWidth(colNum, (columnWidth - 2) * 256); } else { sheet.setColumnWidth(colNum, (columnWidth + 4) * 256); } } workbook.write(out); response.addHeader("Content-Length", String.valueOf(file.length())); byte[] buff = new byte[1024]; os = response.getOutputStream(); bis = new BufferedInputStream(new FileInputStream(file)); int i = bis.read(buff); while (i != -1) { os.write(buff, 0, buff.length); os.flush(); i = bis.read(buff); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis != null) bis.close(); if (os != null) os.close(); } catch (IOException e) { logger.error("close exception", e); } } } private String[] getTitles(String[] titles, int count){ List temp = Arrays.asList(titles); List result = new ArrayList<>(temp); for (int i = 0; i < count; i ++){ result.add("出現的數字" + (i + 1)); result.add("用戶回答" + (i + 1)); result.add("正誤" + (i + 1)); result.add("反應時間" + (i + 1)); } return result.toArray(new String[0]); } }