UserConfigController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package com.api.games.controller;
  2. import com.api.base.model.User;
  3. import com.api.base.service.UserService;
  4. import com.api.common.UtilFun;
  5. import com.api.common.execl.ExcelUtil;
  6. import com.api.config.ConstUser;
  7. import com.api.core.annotation.PowerEnable;
  8. import com.api.core.config.AuthUser;
  9. import com.api.core.controller.Ctrl;
  10. import com.api.core.response.Result;
  11. import com.api.core.response.ResultGenerator;
  12. import com.api.games.model.GameConfig;
  13. import com.api.games.model.UserConfig;
  14. import com.api.games.service.GameConfigService;
  15. import com.api.games.service.ScaleLogService;
  16. import com.api.games.service.UserConfigService;
  17. import io.swagger.annotations.*;
  18. import org.apache.poi.ss.usermodel.CellType;
  19. import org.apache.poi.ss.util.CellRangeAddress;
  20. import org.apache.poi.xssf.usermodel.*;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.security.core.Authentication;
  24. import org.springframework.web.bind.annotation.PostMapping;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import org.springframework.web.bind.annotation.RequestParam;
  27. import org.springframework.web.bind.annotation.RestController;
  28. import javax.annotation.Resource;
  29. import javax.servlet.http.HttpServletResponse;
  30. import java.io.*;
  31. import java.util.Calendar;
  32. import java.util.Date;
  33. import java.util.List;
  34. import java.util.Map;
  35. /**
  36. * Created by wanghuiwen on 2020/02/23.
  37. */
  38. @PowerEnable(name = "用户配置", url = "/user/config")
  39. @Api(value = "用户配置", tags = {"用户配置"})
  40. @RestController
  41. @RequestMapping("/user/config")
  42. public class UserConfigController extends Ctrl {
  43. private Logger logger = LoggerFactory.getLogger(this.getClass());
  44. @Resource
  45. ScaleLogService scaleLogService;
  46. @Resource
  47. private UserConfigService userConfigService;
  48. @Resource
  49. private UserService userService;
  50. @Resource
  51. private GameConfigService gameConfigService;
  52. @ApiOperation(value = "用户配置删除", tags = {"用户配置"}, notes = "用户配置删除")
  53. @ApiImplicitParams({
  54. @ApiImplicitParam(name = "id", required = true, value = "用户配置id", dataType = "Long", paramType = "query")
  55. })
  56. @PostMapping(value = "/delete", name = "用户配置删除")
  57. public Result delete(@RequestParam Long id) {
  58. return userConfigService.delete(id);
  59. }
  60. @ApiOperation(value = "用户配置修改", tags = {"用户配置"}, notes = "用户配置修改,对象主键必填")
  61. @PostMapping(value = "/update", name = "用户配置修改")
  62. public Result update(User user, UserConfig config, Long configId, String configs) {
  63. return userConfigService.update(user, config, configId, configs);
  64. }
  65. @ApiOperation(value = "用户配置详细信息", tags = {"用户配置"}, notes = "用户配置详细信息")
  66. @ApiImplicitParams({
  67. @ApiImplicitParam(name = "id", required = true, value = "用户配置id", dataType = "Long", paramType = "query")
  68. })
  69. @PostMapping(value = "/detail", name = "用户配置详细信息")
  70. public Result detail(@RequestParam Integer id) {
  71. UserConfig userConfig = userConfigService.findById(id);
  72. return ResultGenerator.genSuccessResult(userConfig);
  73. }
  74. @ApiOperation(value = "用户配置信息", tags = {"用户配置"}, notes = "用户配置信息")
  75. @ApiImplicitParams({
  76. @ApiImplicitParam(name = "id", required = true, value = "用户id", dataType = "Long", paramType = "query")
  77. })
  78. @PostMapping(value = "/get", name = "用户配置信息")
  79. public Result detail(@RequestParam Long id) {
  80. UserConfig userConfig = userConfigService.findBy("userId", id);
  81. return ResultGenerator.genSuccessResult(userConfig);
  82. }
  83. @ApiOperation(value = "用户配置列表信息", tags = {"用户配置"}, notes = "用户配置列表信息")
  84. @ApiImplicitParams({
  85. @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
  86. @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
  87. })
  88. @PostMapping(value = "/list", name = "用户配置列表信息")
  89. public Result list(@RequestParam(required = false) String search,
  90. @RequestParam(required = false) String order,
  91. @RequestParam(defaultValue = "0") Integer page,
  92. @RequestParam(defaultValue = "10") Integer size) {
  93. return userConfigService.list(search, order, page, size);
  94. }
  95. @PostMapping(value = "/save", name = "添加用户")
  96. public Result list(User user, UserConfig config, String configs) {
  97. return userConfigService.add(user, config, configs);
  98. }
  99. @PostMapping(value = "/bind", name = "綁定設備")
  100. public Result list(Authentication authentication) {
  101. AuthUser user = getAuthUser(authentication);
  102. return userConfigService.bind(user);
  103. }
  104. @PostMapping(value = "/delay", name = "延迟")
  105. public Result delay(@ApiParam(hidden = true) Authentication authentication,
  106. Date date,
  107. String type,
  108. @RequestParam(required = false) Integer num,
  109. @RequestParam(required = false) Long msgId,
  110. @RequestParam(required = false) String start,
  111. @RequestParam(required = false) String end) {
  112. AuthUser user = getAuthUser(authentication);
  113. return userConfigService.delay(user, date, msgId, num, type, start, end);
  114. }
  115. @PostMapping(value = "/version", name = "获取已经执行的游戏版本")
  116. public Result version(@ApiParam(hidden = true) Authentication authentication,
  117. @RequestParam(required = false) Long msgId) {
  118. AuthUser user = getAuthUser(authentication);
  119. return userConfigService.getVersion(user, msgId);
  120. }
  121. @PostMapping(value = "/user/start", name = "测试标志用户状态")
  122. public Result start() {
  123. userConfigService.expired();
  124. return ResultGenerator.genSuccessResult();
  125. }
  126. @PostMapping(value = "/user/scale", name = "测试标志用户状态")
  127. public Result scale() {
  128. scaleLogService.notPerformed();
  129. return ResultGenerator.genSuccessResult();
  130. }
  131. @PostMapping(value = "/logout", name = "推出登录")
  132. public Result logout(Authentication authentication) {
  133. AuthUser user = getAuthUser(authentication);
  134. if (user.getType() == ConstUser.USER_ADMIN) {
  135. return ResultGenerator.genSuccessResult();
  136. }
  137. UserConfig config = userConfigService.findBy("userId", user.getId());
  138. if (config.getLoginOut() == 1) {
  139. return ResultGenerator.genFailResult();
  140. }
  141. return ResultGenerator.genSuccessResult();
  142. }
  143. @PostMapping("/download")
  144. public void downloadFile(@RequestParam(defaultValue = "{}") String search,
  145. @RequestParam(defaultValue = "{}") String order,
  146. HttpServletResponse response) {
  147. List<Map<String, Object>> res = userConfigService.download(search, order);
  148. for(Map<String, Object> item : res){
  149. String scaleStart = item.get("scaleStart") == null ? null : (String) item.get("scaleStart");
  150. String scaleEnd = item.get("scaleEnd") == null ? null : (String) item.get("scaleEnd");
  151. if (scaleStart != null && scaleEnd != null){
  152. Calendar current = Calendar.getInstance();
  153. Calendar calendarStart = Calendar.getInstance();
  154. calendarStart.setTime(UtilFun.StringToDate(scaleStart, UtilFun.YYYYMMDDHHMMSS));
  155. Calendar calendarEnd = Calendar.getInstance();
  156. calendarEnd.setTime(UtilFun.StringToDate(scaleEnd, UtilFun.YYYYMMDDHHMMSS));
  157. if (current.before(calendarStart)){
  158. item.put("status", 0);
  159. } else if (current.after(calendarEnd)){
  160. item.put("status", 2);
  161. } else if(current.after(calendarStart) && current.before(calendarEnd)){
  162. item.put("status", 1);
  163. }
  164. }
  165. }
  166. int column = 15;
  167. int rowCount = res.size();
  168. String fileTitle = UtilFun.DateToString(new Date(), UtilFun.YMD) + ".xlsx";
  169. String[] title = {"ID", "用戶名", "狀態", "創建日期", "開始日期", "結束日期", "量表提示時間", "數字顯示的時長", "數字交換間隔",
  170. "正確數字出現次數", "隨機數字出現次數", "練習結束進入測試的時長", "每天遊戲次數", "遊戲版本", "遊戲時間段", "登出"};
  171. File file = new File(fileTitle);
  172. BufferedInputStream bis = null;
  173. OutputStream os = null;
  174. // 设置强制下载不打开
  175. response.setContentType("application/octet-stream");
  176. // 设置文件名
  177. response.addHeader("Content-Disposition", "attachment;fileName=" + fileTitle);
  178. response.addHeader("filename", fileTitle);
  179. response.setHeader("Access-Control-Expose-Headers", "filename,Content-Disposition");
  180. try {
  181. OutputStream out = new FileOutputStream(file);
  182. XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作簿对象
  183. XSSFSheet sheet = workbook.createSheet(UtilFun.DateToString(new Date(), UtilFun.YMD)); // 创建工作表
  184. XSSFRow titleRow = sheet.createRow(0); // 产生表格标题行
  185. XSSFCell cellTitle = titleRow.createCell(0); //创建表格标题列
  186. XSSFCellStyle columnTopStyle = ExcelUtil.getColumnTopStyle(workbook);// 获取列头样式对象
  187. XSSFCellStyle style = ExcelUtil.getStyle(workbook); // 获取单元格样式对象
  188. // 合并表格标题行,合并列数为列名的长度,第一个0为起始行号,第二个1为终止行号,第三个0为起始列好,第四个参数为终止列号
  189. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, column));
  190. cellTitle.setCellStyle(columnTopStyle); //设置标题行样式
  191. cellTitle.setCellValue(fileTitle); //设置标题行值
  192. XSSFRow rowRowName = sheet.createRow(1); // 在索引2的位置创建行(最顶端的行开始的第二行)
  193. // 将列头设置到sheet的单元格中
  194. for (int n = 0; n < title.length; n++) {
  195. XSSFCell cellRowName = rowRowName.createCell(n); // 创建列头对应个数的单元格
  196. cellRowName.setCellType(CellType.STRING); // 设置列头单元格的数据类型
  197. XSSFRichTextString text = new XSSFRichTextString(title[n]);
  198. cellRowName.setCellValue(text); // 设置列头单元格的值
  199. cellRowName.setCellStyle(columnTopStyle); // 设置列头单元格样式
  200. }
  201. // 将查询出的数据设置到sheet对应的单元格中
  202. for (int i = 0; i < rowCount; i++) {
  203. XSSFRow row = sheet.createRow(i + 2); // 创建所需的行数
  204. for (int j = 0; j <= column; j++) {
  205. XSSFCell cell = null; // 设置单元格的数据类型
  206. if (j == 0) {
  207. cell = row.createCell(j, CellType.NUMERIC);
  208. cell.setCellValue(i + 1);
  209. } else {
  210. String text = "";
  211. switch (j) {
  212. case 1: {
  213. text = res.get(i).get("nickname").toString();
  214. break;
  215. }
  216. case 2: {
  217. String status = res.get(i).get("status").toString();
  218. if (status.equals("0"))
  219. text = "waiting";
  220. else if(status.equals("1"))
  221. text = "processing";
  222. else
  223. text = "end";
  224. break;
  225. }
  226. case 3: {
  227. text = res.get(i).get("createTime").toString();
  228. break;
  229. }
  230. case 4: {
  231. text = res.get(i).get("scaleStart").toString();
  232. break;
  233. }
  234. case 5: {
  235. text = res.get(i).get("scaleEnd").toString();
  236. break;
  237. }
  238. case 6: {
  239. text = res.get(i).get("scaleTime").toString();
  240. break;
  241. }
  242. case 7: {
  243. text = res.get(i).get("showTime").toString();
  244. break;
  245. }
  246. case 8: {
  247. text = res.get(i).get("hideTime").toString();
  248. break;
  249. }
  250. case 9: {
  251. text = res.get(i).get("correct").toString();
  252. break;
  253. }
  254. case 10: {
  255. text = res.get(i).get("supporting").toString();
  256. break;
  257. }
  258. case 11: {
  259. text = res.get(i).get("practice").toString();
  260. break;
  261. }
  262. case 12: {
  263. text = res.get(i).get("gameCount").toString();
  264. break;
  265. }
  266. case 13:{
  267. text = res.get(i).get("version").toString();
  268. break;
  269. }
  270. case 14:{
  271. List<GameConfig> gameConfigList = (List<GameConfig>)gameConfigService.listUid((Long) res.get(i).get("userId")).getData();
  272. text = "";
  273. for (GameConfig item: gameConfigList)
  274. text += item.getStartTime() + "-" + item.getEndTime() + " ";
  275. break;
  276. }
  277. case 15: {
  278. text = res.get(i).get("loginOut").toString().equals("0") ? "yes" : "no";
  279. break;
  280. }
  281. default: {
  282. text = "";
  283. break;
  284. }
  285. }
  286. cell = row.createCell(j, CellType.STRING);
  287. cell.setCellValue(text); // 设置单元格的值
  288. }
  289. cell.setCellStyle(style); // 设置单元格样式
  290. }
  291. }
  292. // 让列宽随着导出的列长自动适应
  293. for (int colNum = 0; colNum <= column; colNum++) {
  294. int columnWidth = sheet.getColumnWidth(colNum) / 256;
  295. for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
  296. XSSFRow currentRow;
  297. // 当前行未被使用过
  298. if (sheet.getRow(rowNum) == null) {
  299. currentRow = sheet.createRow(rowNum);
  300. } else {
  301. currentRow = sheet.getRow(rowNum);
  302. }
  303. if (currentRow.getCell(colNum) != null) {
  304. XSSFCell currentCell = currentRow.getCell(colNum);
  305. if (currentCell.getCellType() == CellType.STRING) {
  306. int length = currentCell.getStringCellValue()
  307. .getBytes().length;
  308. if (columnWidth < length) {
  309. columnWidth = length;
  310. }
  311. }
  312. }
  313. }
  314. if (colNum == 0) {
  315. sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
  316. } else {
  317. sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
  318. }
  319. }
  320. workbook.write(out);
  321. response.addHeader("Content-Length", String.valueOf(file.length()));
  322. byte[] buff = new byte[1024];
  323. os = response.getOutputStream();
  324. bis = new BufferedInputStream(new FileInputStream(file));
  325. int i = bis.read(buff);
  326. while (i != -1) {
  327. os.write(buff, 0, buff.length);
  328. os.flush();
  329. i = bis.read(buff);
  330. }
  331. } catch (IOException e) {
  332. e.printStackTrace();
  333. } finally {
  334. try {
  335. if (bis != null)
  336. bis.close();
  337. if (os != null)
  338. os.close();
  339. } catch (IOException e) {
  340. logger.error("close exception", e);
  341. }
  342. }
  343. }
  344. }