123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- package com.api.games.controller;
- import cn.jpush.api.schedule.ScheduleResult;
- import com.api.common.UtilFun;
- import com.api.common.execl.ExcelUtil;
- import com.api.common.jpush.JpushService;
- import com.api.config.ConstUser;
- import com.api.core.annotation.PowerEnable;
- import com.api.core.config.AuthUser;
- import com.api.core.controller.Ctrl;
- import com.api.core.response.Result;
- import com.api.core.response.ResultGenerator;
- import com.api.games.model.PushLog;
- import com.api.games.model.UserConfig;
- import com.api.games.service.PushLogService;
- import com.api.games.service.UserConfigService;
- 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.security.core.Authentication;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import static com.api.config.ConstUser.PUSH_NO;
- /**
- * Created by wanghuiwen on 2020/02/27.
- */
- @PowerEnable(name = "推送日志", url = "/push/log")
- @Api(value = "推送日志", tags = {"推送日志"})
- @RestController
- @RequestMapping("/push/log")
- public class PushLogController extends Ctrl {
- private Logger logger = LoggerFactory.getLogger(this.getClass());
- @Resource
- private PushLogService pushLogService;
- @Resource
- private JpushService jpushService;
- @Resource
- private UserConfigService userConfigService;
- @ApiOperation(value = "推送日志添加", tags = {"推送日志"}, notes = "推送日志添加")
- @PostMapping(value = "/add", name = "推送日志添加")
- public Result add(@ApiParam PushLog pushLog) {
- pushLog.setStatus(PUSH_NO);
- pushLogService.save(pushLog);
- return ResultGenerator.genSuccessResult(pushLog.getId());
- }
- @GetMapping(value = "/needPlayGame", name = "是否需要玩游戏")
- public Result needPlayGame(Authentication authentication){
- AuthUser authUser = getAuthUser(authentication);
- return pushLogService.needPlayGame(authUser.getId());
- }
- @PostMapping(value = "/game", name = "推送游戏")
- public Result game(Authentication authentication,String start,String end) {
- AuthUser authUser = getAuthUser(authentication);
- UserConfig config = userConfigService.findBy("userId", authUser.getId());
- Map<String, String> extras = new HashMap<>();
- extras.put("version", config.getVersion());
- extras.put("type", ConstUser.PUSH_GAME);
- extras.put("start", start);
- extras.put("end", end);
- //早上的推送
- jpushService.sendCustomPush("測試遊戲", "請開始測試", extras, authUser.getAlias());
- return ResultGenerator.genSuccessResult();
- }
- @PostMapping(value = "/scale", name = "推送量表")
- public Result scale(Authentication authentication) {
- AuthUser authUser = getAuthUser(authentication);
- Map<String, String> scaleExtras = new HashMap<>();
- scaleExtras.put("type", ConstUser.PUSH_SCALE);
- jpushService.sendCustomPush("量表填寫", "請及時填寫量表信息", scaleExtras, authUser.getAlias());
- return ResultGenerator.genSuccessResult();
- }
- @ApiOperation(value = "推送日志添加", tags = {"推送日志"}, notes = "推送日志添加")
- @PostMapping(value = "/jg/list", name = "推送日志添加")
- public Result jsList(@ApiParam Integer page) {
- return ResultGenerator.genSuccessResult(jpushService.schedules(page));
- }
- @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) {
- pushLogService.deleteById(id);
- return ResultGenerator.genSuccessResult();
- }
- @PostMapping(value = "/delay/game", name = "推迟游戏")
- public Result delay(@RequestParam Long gamePlayTimeId, @RequestParam Long gameConfigId, @RequestParam int delayMin, @RequestParam String deviceId){
- return pushLogService.delayGame(gamePlayTimeId, gameConfigId, delayMin, deviceId);
- }
- @PostMapping(value = "/delay/scale", name = "推迟量表")
- public Result delayScale(@RequestParam Long gamePlayTimeId
- , @RequestParam int delayMin, @RequestParam String deviceId){
- return pushLogService.delayScale(gamePlayTimeId, delayMin, deviceId);
- }
- @PostMapping(value = "/enter", name = "进入推送")
- public Result enter(@RequestParam Long gamePlayTimeId, @RequestParam String deviceId, @RequestParam Integer type){
- return pushLogService.enter(gamePlayTimeId, deviceId, type);
- }
- // @PostMapping(value = "/delay/scale", name = "推迟量表")
- // public Result delay(@RequestParam Long gamePlayTimeId){
- // return gameLogService.delay(gamePlayTimeId);
- // }
- @PostMapping(value = "/delete/ids", name = "推送日志批量删除")
- public Result deleteIds(@RequestParam String ids) {
- pushLogService.deleteByIds(ids);
- return ResultGenerator.genSuccessResult();
- }
- @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 pushLogService.list(search, order, page, size);
- }
- @PostMapping("/download")
- public void downloadFile(@RequestParam(defaultValue = "{}") String search,
- @RequestParam(defaultValue = "{}") String order,
- HttpServletResponse response) {
- List<Map<String, Object>> res = pushLogService.download(search, order);
- int column = 5;
- int rowCount = res.size();
- String fileTitle = UtilFun.DateToString(new Date(), UtilFun.YMD) + ".xlsx";
- String[] title = {"序號", "用戶名", "推送日期", "推送時間", "推送狀態", "推送類型"};
- 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; // 设置单元格的数据类型
- if (j == 0) {
- cell = row.createCell(j, CellType.NUMERIC);
- cell.setCellValue(i + 1);
- } else {
- String text = "";
- switch (j) {
- case 1: {
- text = res.get(i).get("nickname").toString();
- break;
- }
- case 2:{
- text = res.get(i).get("pushTime").toString().split(" ")[0];
- break;
- }
- case 3: {
- text = res.get(i).get("pushTime").toString().split(" ")[1];
- break;
- }
- case 4: {
- text = res.get(i).get("status").toString().equals("1") ? "delay" : res.get(i).get("status").toString().equals("2") ? "expired" : "enter";
- break;
- }
- case 5: {
- text = res.get(i).get("type").toString().equals("1") ? "game" : "scale";
- break;
- }
- default: {
- text = "";
- 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);
- }
- }
- }
- }
|