123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- 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.config.AuthUser;
- import com.api.core.controller.Ctrl;
- import com.api.core.response.Result;
- import com.api.core.response.ResultGenerator;
- import com.api.games.dao.GamePlayTimeMapper;
- import com.api.games.model.GamePlayTime;
- import com.api.games.model.ScaleLog;
- import com.api.games.model.UserConfig;
- import com.api.games.service.ScaleLogService;
- 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 tk.mybatis.mapper.entity.Condition;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.lang.reflect.Field;
- import java.math.BigDecimal;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by wanghuiwen on 2020/02/23.
- */
- @PowerEnable(name = "量表", url = "/scale/log")
- @Api(value = "量表", tags = {"量表"})
- @RestController
- @RequestMapping("/scale/log")
- public class ScaleLogController extends Ctrl {
- private Logger logger = LoggerFactory.getLogger(this.getClass());
- @Resource
- private ScaleLogService scaleLogService;
- @Resource
- private UserConfigService userConfigService;
- @GetMapping(value = "/list/over_date", name = "获取逾期量表")
- public Result listOverDate(
- Authentication authentication
- ){
- AuthUser authUser = (AuthUser) authentication.getPrincipal();
- return scaleLogService.listOverDate(authUser.getId());
- }
- @PostMapping(value = "/add/new", name = "新量表添加")
- public Result add(Authentication authentication,
- @RequestParam Long gamePlayId,
- @RequestParam String startDate,
- @RequestParam String endDate,
- @RequestParam String eventDate,
- @RequestParam String contact,
- @RequestParam String dinner,
- @RequestParam String getUp,
- @RequestParam String sleep,
- @RequestParam String work,
- @RequestParam String deviceId
- ){
- AuthUser authUser = (AuthUser) authentication.getPrincipal();
- ScaleLog scaleLog = new ScaleLog();
- scaleLog.setUserId(authUser.getId());
- int success = 0;
- if(contact != null && !contact.equals("")){
- success += 1;
- scaleLog.setContact(contact);
- } else
- scaleLog.setContact("沒做");
- if(dinner != null && !dinner.equals("")){
- success += 1;
- scaleLog.setDinner(dinner);
- } else
- scaleLog.setDinner("沒做");
- if(getUp != null && !getUp.equals("")){
- success += 1;
- scaleLog.setGetUp(getUp);
- } else
- scaleLog.setGetUp("沒做");
- if(sleep != null && !sleep.equals("")){
- success += 1;
- scaleLog.setSleep(sleep);
- } else
- scaleLog.setSleep("沒做");
- if(work !=null && !work.equals("")){
- success += 1;
- scaleLog.setWork(work);
- } else
- scaleLog.setWork("沒做");
- scaleLog.setSchedule(BigDecimal.valueOf(success == 0 ? 1 : success / 5));
- scaleLog.setStartDate(UtilFun.StringToDate(startDate, UtilFun.YYYYMMDDHHMMSS));
- scaleLog.setCreateDate(UtilFun.StringToDate(endDate, UtilFun.YYYYMMDDHHMMSS));
- scaleLog.setEventDate(UtilFun.StringToDate(eventDate, UtilFun.YMD));
- scaleLog.setSign(scaleLogService.sign(authUser.getId()));
- scaleLog.setDeviceId(deviceId);
- scaleLogService.save(scaleLog);
- scaleLogService.deletePlayGameId(gamePlayId);
- return ResultGenerator.genSuccessResult();
- }
- @ApiOperation(value = "量表添加", tags = {"量表"}, notes = "量表添加")
- @PostMapping(value = "/add", name = "量表添加")
- public Result add(Authentication authentication, @ApiParam ScaleLog scaleLog) {
- AuthUser authUser = (AuthUser) authentication.getPrincipal();
- scaleLog.setCreateDate(new Date());
- UserConfig config = userConfigService.findBy("userId", authUser.getId());
- int days = (int) ((new Date().getTime() - UtilFun.StringToDate(config.getScaleStart(), UtilFun.YMD).getTime()) / (1000 * 3600 * 24));
- scaleLog.setSign(String.valueOf(days));
- //记录用户填写的属性
- float success = 0;
- if(scaleLog.getContact()!=null && !scaleLog.getContact().equals("沒做")){
- success += 1;
- if (scaleLog.getContact().split(":").length > 1){
- scaleLog.setContact(addZero(scaleLog.getContact()));
- }
- }
- if(scaleLog.getDinner() !=null && !scaleLog.getDinner().equals("沒做")){
- success += 1;
- if (scaleLog.getDinner().split(":").length > 1){
- scaleLog.setDinner(addZero(scaleLog.getDinner()));
- }
- }
- if(scaleLog.getGetUp() != null && !scaleLog.getGetUp().equals("沒做")){
- success += 1;
- if (scaleLog.getGetUp().split(":").length > 1){
- scaleLog.setGetUp(addZero(scaleLog.getGetUp()));
- }
- }
- if(scaleLog.getSleep()!=null && !scaleLog.getSleep().equals("沒做")){
- success += 1;
- if (scaleLog.getSleep().split(":").length > 1){
- scaleLog.setSleep(addZero(scaleLog.getSleep()));
- }
- }
- if(scaleLog.getWork() !=null && !scaleLog.getWork().equals("沒做")){
- success += 1;
- if (scaleLog.getWork().split(":").length > 1){
- scaleLog.setWork(addZero(scaleLog.getWork()));
- }
- }
- //计算完成度
- scaleLog.setSchedule(BigDecimal.valueOf( success / 5));
- scaleLogService.save(scaleLog);
- return ResultGenerator.genSuccessResult();
- }
- private String addZero(String value){
- String result = "";
- String temp = value.split(":")[0];
- if (temp.length() < 2)
- temp = "0" + temp;
- result += temp + ":";
- temp = value.split(":")[1];
- if (temp.length() < 2)
- temp = "0" + temp;
- result += temp;
- return result;
- }
- @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) {
- scaleLogService.deleteById(id);
- return ResultGenerator.genSuccessResult();
- }
- @PostMapping(value = "/delete/ids", name = "游戏测试记录删除")
- public Result deleteIds(@RequestParam String ids) {
- scaleLogService.deleteByIds(ids);
- return ResultGenerator.genSuccessResult();
- }
- @ApiOperation(value = "量表修改", tags = {"量表"}, notes = "量表修改,对象主键必填")
- @PostMapping(value = "/update", name = "量表修改")
- public Result update(@ApiParam ScaleLog scaleLog) {
- scaleLogService.update(scaleLog);
- 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) {
- ScaleLog scaleLog = scaleLogService.findById(id);
- return ResultGenerator.genSuccessResult(scaleLog);
- }
- @ApiOperation(value = "量表未填日期", tags = {"量表"}, notes = "量表未填日期")
- @ApiImplicitParams({
- @ApiImplicitParam(name = "id", required = true, value = "量表id", dataType = "Long", paramType = "query")
- })
- @PostMapping(value = "/date", name = "量表未填日期")
- public Result date(@ApiParam(hidden = true) Authentication authentication,
- @RequestParam(required = false) Long msgId) {
- AuthUser authUser = (AuthUser) authentication.getPrincipal();
- return scaleLogService.date(authUser.getId(), msgId);
- }
- @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 scaleLogService.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 = scaleLogService.download(search, order);
- int column = 13;
- int rowCount = res.size();
- String fileTitle = UtilFun.DateToString(new Date(), UtilFun.YMD) + ".xlsx";
- String[] title = {"ID", "用戶名", "序號", "量表日期", "填寫日期", "開始時間", "完成時間", "完成度", "起床", "和人接觸", "工作", "晚飯", "睡覺", "設備碼"};
- 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("sign").toString();
- break;
- }
- case 3: {
- text = res.get(i).get("eventDate").toString().substring(0, 10);
- break;
- }
- case 4:{
- text = res.get(i).get("startDate").toString().split(" ")[0];
- break;
- }
- case 5: {
- text = res.get(i).get("startDate").toString().split(" ")[1];
- break;
- }
- case 6: {
- text = res.get(i).get("createDate").toString().split(" ")[1];
- break;
- }
- case 7: {
- text = res.get(i).get("schedule").toString();
- break;
- }
- case 8: {
- text = res.get(i).get("getUp").toString();
- break;
- }
- case 9: {
- text = res.get(i).get("contact").toString();
- break;
- }
- case 10: {
- text = res.get(i).get("work").toString();
- break;
- }
- case 11: {
- text = res.get(i).get("dinner").toString();
- break;
- }
- case 12: {
- text = res.get(i).get("sleep").toString();
- break;
- }
- case 13:{
- text = res.get(i).get("deviceId").toString();
- 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);
- }
- }
- }
- }
|