PushLogController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package com.api.games.controller;
  2. import cn.jpush.api.schedule.ScheduleResult;
  3. import com.api.common.UtilFun;
  4. import com.api.common.execl.ExcelUtil;
  5. import com.api.common.jpush.JpushService;
  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.PushLog;
  13. import com.api.games.model.UserConfig;
  14. import com.api.games.service.PushLogService;
  15. import com.api.games.service.UserConfigService;
  16. import io.swagger.annotations.*;
  17. import org.apache.poi.ss.usermodel.CellType;
  18. import org.apache.poi.ss.util.CellRangeAddress;
  19. import org.apache.poi.xssf.usermodel.*;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.springframework.security.core.Authentication;
  23. import org.springframework.web.bind.annotation.*;
  24. import javax.annotation.Resource;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.io.*;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import static com.api.config.ConstUser.PUSH_NO;
  32. /**
  33. * Created by wanghuiwen on 2020/02/27.
  34. */
  35. @PowerEnable(name = "推送日志", url = "/push/log")
  36. @Api(value = "推送日志", tags = {"推送日志"})
  37. @RestController
  38. @RequestMapping("/push/log")
  39. public class PushLogController extends Ctrl {
  40. private Logger logger = LoggerFactory.getLogger(this.getClass());
  41. @Resource
  42. private PushLogService pushLogService;
  43. @Resource
  44. private JpushService jpushService;
  45. @Resource
  46. private UserConfigService userConfigService;
  47. @ApiOperation(value = "推送日志添加", tags = {"推送日志"}, notes = "推送日志添加")
  48. @PostMapping(value = "/add", name = "推送日志添加")
  49. public Result add(@ApiParam PushLog pushLog) {
  50. pushLog.setStatus(PUSH_NO);
  51. pushLogService.save(pushLog);
  52. return ResultGenerator.genSuccessResult(pushLog.getId());
  53. }
  54. @GetMapping(value = "/needPlayGame", name = "是否需要玩游戏")
  55. public Result needPlayGame(Authentication authentication){
  56. AuthUser authUser = getAuthUser(authentication);
  57. return pushLogService.needPlayGame(authUser.getId());
  58. }
  59. @PostMapping(value = "/game", name = "推送游戏")
  60. public Result game(Authentication authentication,String start,String end) {
  61. AuthUser authUser = getAuthUser(authentication);
  62. UserConfig config = userConfigService.findBy("userId", authUser.getId());
  63. Map<String, String> extras = new HashMap<>();
  64. extras.put("version", config.getVersion());
  65. extras.put("type", ConstUser.PUSH_GAME);
  66. extras.put("start", start);
  67. extras.put("end", end);
  68. //早上的推送
  69. jpushService.sendCustomPush("測試遊戲", "請開始測試", extras, authUser.getAlias());
  70. return ResultGenerator.genSuccessResult();
  71. }
  72. @PostMapping(value = "/scale", name = "推送量表")
  73. public Result scale(Authentication authentication) {
  74. AuthUser authUser = getAuthUser(authentication);
  75. Map<String, String> scaleExtras = new HashMap<>();
  76. scaleExtras.put("type", ConstUser.PUSH_SCALE);
  77. jpushService.sendCustomPush("量表填寫", "請及時填寫量表信息", scaleExtras, authUser.getAlias());
  78. return ResultGenerator.genSuccessResult();
  79. }
  80. @ApiOperation(value = "推送日志添加", tags = {"推送日志"}, notes = "推送日志添加")
  81. @PostMapping(value = "/jg/list", name = "推送日志添加")
  82. public Result jsList(@ApiParam Integer page) {
  83. return ResultGenerator.genSuccessResult(jpushService.schedules(page));
  84. }
  85. @ApiOperation(value = "推送日志删除", tags = {"推送日志"}, notes = "推送日志删除")
  86. @ApiImplicitParams({
  87. @ApiImplicitParam(name = "id", required = true, value = "推送日志id", dataType = "Long", paramType = "query")
  88. })
  89. @PostMapping(value = "/delete", name = "推送日志删除")
  90. public Result delete(@RequestParam Long id) {
  91. pushLogService.deleteById(id);
  92. return ResultGenerator.genSuccessResult();
  93. }
  94. @PostMapping(value = "/delay/game", name = "推迟游戏")
  95. public Result delay(@RequestParam Long gamePlayTimeId, @RequestParam Long gameConfigId, @RequestParam int delayMin, @RequestParam String deviceId){
  96. return pushLogService.delayGame(gamePlayTimeId, gameConfigId, delayMin, deviceId);
  97. }
  98. @PostMapping(value = "/delay/scale", name = "推迟量表")
  99. public Result delayScale(@RequestParam Long gamePlayTimeId
  100. , @RequestParam int delayMin, @RequestParam String deviceId){
  101. return pushLogService.delayScale(gamePlayTimeId, delayMin, deviceId);
  102. }
  103. @PostMapping(value = "/enter", name = "进入推送")
  104. public Result enter(@RequestParam Long gamePlayTimeId, @RequestParam String deviceId, @RequestParam Integer type){
  105. return pushLogService.enter(gamePlayTimeId, deviceId, type);
  106. }
  107. // @PostMapping(value = "/delay/scale", name = "推迟量表")
  108. // public Result delay(@RequestParam Long gamePlayTimeId){
  109. // return gameLogService.delay(gamePlayTimeId);
  110. // }
  111. @PostMapping(value = "/delete/ids", name = "推送日志批量删除")
  112. public Result deleteIds(@RequestParam String ids) {
  113. pushLogService.deleteByIds(ids);
  114. return ResultGenerator.genSuccessResult();
  115. }
  116. @ApiOperation(value = "推送日志列表信息", tags = {"推送日志"}, notes = "推送日志列表信息")
  117. @ApiImplicitParams({
  118. @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
  119. @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
  120. })
  121. @PostMapping(value = "/list", name = "推送日志列表信息")
  122. public Result list(@RequestParam(required = false) String search,
  123. @RequestParam(required = false) String order,
  124. @RequestParam(defaultValue = "0") Integer page,
  125. @RequestParam(defaultValue = "10") Integer size) {
  126. return pushLogService.list(search, order, page, size);
  127. }
  128. @PostMapping("/download")
  129. public void downloadFile(@RequestParam(defaultValue = "{}") String search,
  130. @RequestParam(defaultValue = "{}") String order,
  131. HttpServletResponse response) {
  132. List<Map<String, Object>> res = pushLogService.download(search, order);
  133. int column = 5;
  134. int rowCount = res.size();
  135. String fileTitle = UtilFun.DateToString(new Date(), UtilFun.YMD) + ".xlsx";
  136. String[] title = {"序號", "用戶名", "推送日期", "推送時間", "推送狀態", "推送類型"};
  137. File file = new File(fileTitle);
  138. BufferedInputStream bis = null;
  139. OutputStream os = null;
  140. // 设置强制下载不打开
  141. response.setContentType("application/octet-stream");
  142. // 设置文件名
  143. response.addHeader("Content-Disposition", "attachment;fileName=" + fileTitle);
  144. response.addHeader("filename", fileTitle);
  145. response.setHeader("Access-Control-Expose-Headers", "filename,Content-Disposition");
  146. try {
  147. OutputStream out = new FileOutputStream(file);
  148. XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作簿对象
  149. XSSFSheet sheet = workbook.createSheet(UtilFun.DateToString(new Date(), UtilFun.YMD)); // 创建工作表
  150. XSSFRow titleRow = sheet.createRow(0); // 产生表格标题行
  151. XSSFCell cellTitle = titleRow.createCell(0); //创建表格标题列
  152. XSSFCellStyle columnTopStyle = ExcelUtil.getColumnTopStyle(workbook);// 获取列头样式对象
  153. XSSFCellStyle style = ExcelUtil.getStyle(workbook); // 获取单元格样式对象
  154. // 合并表格标题行,合并列数为列名的长度,第一个0为起始行号,第二个1为终止行号,第三个0为起始列好,第四个参数为终止列号
  155. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, column));
  156. cellTitle.setCellStyle(columnTopStyle); //设置标题行样式
  157. cellTitle.setCellValue(fileTitle); //设置标题行值
  158. XSSFRow rowRowName = sheet.createRow(1); // 在索引2的位置创建行(最顶端的行开始的第二行)
  159. // 将列头设置到sheet的单元格中
  160. for (int n = 0; n < title.length; n++) {
  161. XSSFCell cellRowName = rowRowName.createCell(n); // 创建列头对应个数的单元格
  162. cellRowName.setCellType(CellType.STRING); // 设置列头单元格的数据类型
  163. XSSFRichTextString text = new XSSFRichTextString(title[n]);
  164. cellRowName.setCellValue(text); // 设置列头单元格的值
  165. cellRowName.setCellStyle(columnTopStyle); // 设置列头单元格样式
  166. }
  167. // 将查询出的数据设置到sheet对应的单元格中
  168. for (int i = 0; i < rowCount; i++) {
  169. XSSFRow row = sheet.createRow(i + 2); // 创建所需的行数
  170. for (int j = 0; j <= column; j++) {
  171. XSSFCell cell; // 设置单元格的数据类型
  172. if (j == 0) {
  173. cell = row.createCell(j, CellType.NUMERIC);
  174. cell.setCellValue(i + 1);
  175. } else {
  176. String text = "";
  177. switch (j) {
  178. case 1: {
  179. text = res.get(i).get("nickname").toString();
  180. break;
  181. }
  182. case 2:{
  183. text = res.get(i).get("pushTime").toString().split(" ")[0];
  184. break;
  185. }
  186. case 3: {
  187. text = res.get(i).get("pushTime").toString().split(" ")[1];
  188. break;
  189. }
  190. case 4: {
  191. text = res.get(i).get("status").toString().equals("1") ? "delay" : res.get(i).get("status").toString().equals("2") ? "expired" : "enter";
  192. break;
  193. }
  194. case 5: {
  195. text = res.get(i).get("type").toString().equals("1") ? "game" : "scale";
  196. break;
  197. }
  198. default: {
  199. text = "";
  200. break;
  201. }
  202. }
  203. cell = row.createCell(j, CellType.STRING);
  204. cell.setCellValue(text); // 设置单元格的值
  205. }
  206. cell.setCellStyle(style); // 设置单元格样式
  207. }
  208. }
  209. // 让列宽随着导出的列长自动适应
  210. for (int colNum = 0; colNum <= column; colNum++) {
  211. int columnWidth = sheet.getColumnWidth(colNum) / 256;
  212. for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
  213. XSSFRow currentRow;
  214. // 当前行未被使用过
  215. if (sheet.getRow(rowNum) == null) {
  216. currentRow = sheet.createRow(rowNum);
  217. } else {
  218. currentRow = sheet.getRow(rowNum);
  219. }
  220. if (currentRow.getCell(colNum) != null) {
  221. XSSFCell currentCell = currentRow.getCell(colNum);
  222. if (currentCell.getCellType() == CellType.STRING) {
  223. int length = currentCell.getStringCellValue()
  224. .getBytes().length;
  225. if (columnWidth < length) {
  226. columnWidth = length;
  227. }
  228. }
  229. }
  230. }
  231. if (colNum == 0) {
  232. sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
  233. } else {
  234. sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
  235. }
  236. }
  237. workbook.write(out);
  238. response.addHeader("Content-Length", String.valueOf(file.length()));
  239. byte[] buff = new byte[1024];
  240. os = response.getOutputStream();
  241. bis = new BufferedInputStream(new FileInputStream(file));
  242. int i = bis.read(buff);
  243. while (i != -1) {
  244. os.write(buff, 0, buff.length);
  245. os.flush();
  246. i = bis.read(buff);
  247. }
  248. } catch (IOException e) {
  249. e.printStackTrace();
  250. } finally {
  251. try {
  252. if (bis != null)
  253. bis.close();
  254. if (os != null)
  255. os.close();
  256. } catch (IOException e) {
  257. logger.error("close exception", e);
  258. }
  259. }
  260. }
  261. }