ScaleLogController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package com.api.games.controller;
  2. import com.api.common.UtilFun;
  3. import com.api.common.execl.ExcelUtil;
  4. import com.api.core.annotation.PowerEnable;
  5. import com.api.core.config.AuthUser;
  6. import com.api.core.controller.Ctrl;
  7. import com.api.core.response.Result;
  8. import com.api.core.response.ResultGenerator;
  9. import com.api.games.dao.GamePlayTimeMapper;
  10. import com.api.games.model.GamePlayTime;
  11. import com.api.games.model.ScaleLog;
  12. import com.api.games.model.UserConfig;
  13. import com.api.games.service.ScaleLogService;
  14. import com.api.games.service.UserConfigService;
  15. import io.swagger.annotations.*;
  16. import org.apache.poi.ss.usermodel.CellType;
  17. import org.apache.poi.ss.util.CellRangeAddress;
  18. import org.apache.poi.xssf.usermodel.*;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.security.core.Authentication;
  22. import org.springframework.web.bind.annotation.*;
  23. import tk.mybatis.mapper.entity.Condition;
  24. import javax.annotation.Resource;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.io.*;
  27. import java.lang.reflect.Field;
  28. import java.math.BigDecimal;
  29. import java.util.Date;
  30. import java.util.List;
  31. import java.util.Map;
  32. /**
  33. * Created by wanghuiwen on 2020/02/23.
  34. */
  35. @PowerEnable(name = "量表", url = "/scale/log")
  36. @Api(value = "量表", tags = {"量表"})
  37. @RestController
  38. @RequestMapping("/scale/log")
  39. public class ScaleLogController extends Ctrl {
  40. private Logger logger = LoggerFactory.getLogger(this.getClass());
  41. @Resource
  42. private ScaleLogService scaleLogService;
  43. @Resource
  44. private UserConfigService userConfigService;
  45. @GetMapping(value = "/list/over_date", name = "获取逾期量表")
  46. public Result listOverDate(
  47. Authentication authentication
  48. ){
  49. AuthUser authUser = (AuthUser) authentication.getPrincipal();
  50. return scaleLogService.listOverDate(authUser.getId());
  51. }
  52. @PostMapping(value = "/add/new", name = "新量表添加")
  53. public Result add(Authentication authentication,
  54. @RequestParam Long gamePlayId,
  55. @RequestParam String startDate,
  56. @RequestParam String endDate,
  57. @RequestParam String eventDate,
  58. @RequestParam String contact,
  59. @RequestParam String dinner,
  60. @RequestParam String getUp,
  61. @RequestParam String sleep,
  62. @RequestParam String work,
  63. @RequestParam String deviceId
  64. ){
  65. AuthUser authUser = (AuthUser) authentication.getPrincipal();
  66. ScaleLog scaleLog = new ScaleLog();
  67. scaleLog.setUserId(authUser.getId());
  68. int success = 0;
  69. if(contact != null && !contact.equals("")){
  70. success += 1;
  71. scaleLog.setContact(contact);
  72. } else
  73. scaleLog.setContact("沒做");
  74. if(dinner != null && !dinner.equals("")){
  75. success += 1;
  76. scaleLog.setDinner(dinner);
  77. } else
  78. scaleLog.setDinner("沒做");
  79. if(getUp != null && !getUp.equals("")){
  80. success += 1;
  81. scaleLog.setGetUp(getUp);
  82. } else
  83. scaleLog.setGetUp("沒做");
  84. if(sleep != null && !sleep.equals("")){
  85. success += 1;
  86. scaleLog.setSleep(sleep);
  87. } else
  88. scaleLog.setSleep("沒做");
  89. if(work !=null && !work.equals("")){
  90. success += 1;
  91. scaleLog.setWork(work);
  92. } else
  93. scaleLog.setWork("沒做");
  94. scaleLog.setSchedule(BigDecimal.valueOf(success == 0 ? 1 : success / 5));
  95. scaleLog.setStartDate(UtilFun.StringToDate(startDate, UtilFun.YYYYMMDDHHMMSS));
  96. scaleLog.setCreateDate(UtilFun.StringToDate(endDate, UtilFun.YYYYMMDDHHMMSS));
  97. scaleLog.setEventDate(UtilFun.StringToDate(eventDate, UtilFun.YMD));
  98. scaleLog.setSign(scaleLogService.sign(authUser.getId()));
  99. scaleLog.setDeviceId(deviceId);
  100. scaleLogService.save(scaleLog);
  101. scaleLogService.deletePlayGameId(gamePlayId);
  102. return ResultGenerator.genSuccessResult();
  103. }
  104. @ApiOperation(value = "量表添加", tags = {"量表"}, notes = "量表添加")
  105. @PostMapping(value = "/add", name = "量表添加")
  106. public Result add(Authentication authentication, @ApiParam ScaleLog scaleLog) {
  107. AuthUser authUser = (AuthUser) authentication.getPrincipal();
  108. scaleLog.setCreateDate(new Date());
  109. UserConfig config = userConfigService.findBy("userId", authUser.getId());
  110. int days = (int) ((new Date().getTime() - UtilFun.StringToDate(config.getScaleStart(), UtilFun.YMD).getTime()) / (1000 * 3600 * 24));
  111. scaleLog.setSign(String.valueOf(days));
  112. //记录用户填写的属性
  113. float success = 0;
  114. if(scaleLog.getContact()!=null && !scaleLog.getContact().equals("沒做")){
  115. success += 1;
  116. if (scaleLog.getContact().split(":").length > 1){
  117. scaleLog.setContact(addZero(scaleLog.getContact()));
  118. }
  119. }
  120. if(scaleLog.getDinner() !=null && !scaleLog.getDinner().equals("沒做")){
  121. success += 1;
  122. if (scaleLog.getDinner().split(":").length > 1){
  123. scaleLog.setDinner(addZero(scaleLog.getDinner()));
  124. }
  125. }
  126. if(scaleLog.getGetUp() != null && !scaleLog.getGetUp().equals("沒做")){
  127. success += 1;
  128. if (scaleLog.getGetUp().split(":").length > 1){
  129. scaleLog.setGetUp(addZero(scaleLog.getGetUp()));
  130. }
  131. }
  132. if(scaleLog.getSleep()!=null && !scaleLog.getSleep().equals("沒做")){
  133. success += 1;
  134. if (scaleLog.getSleep().split(":").length > 1){
  135. scaleLog.setSleep(addZero(scaleLog.getSleep()));
  136. }
  137. }
  138. if(scaleLog.getWork() !=null && !scaleLog.getWork().equals("沒做")){
  139. success += 1;
  140. if (scaleLog.getWork().split(":").length > 1){
  141. scaleLog.setWork(addZero(scaleLog.getWork()));
  142. }
  143. }
  144. //计算完成度
  145. scaleLog.setSchedule(BigDecimal.valueOf( success / 5));
  146. scaleLogService.save(scaleLog);
  147. return ResultGenerator.genSuccessResult();
  148. }
  149. private String addZero(String value){
  150. String result = "";
  151. String temp = value.split(":")[0];
  152. if (temp.length() < 2)
  153. temp = "0" + temp;
  154. result += temp + ":";
  155. temp = value.split(":")[1];
  156. if (temp.length() < 2)
  157. temp = "0" + temp;
  158. result += temp;
  159. return result;
  160. }
  161. @ApiOperation(value = "量表删除", tags = {"量表"}, notes = "量表删除")
  162. @ApiImplicitParams({
  163. @ApiImplicitParam(name = "id", required = true, value = "量表id", dataType = "Long", paramType = "query")
  164. })
  165. @PostMapping(value = "/delete", name = "量表删除")
  166. public Result delete(@RequestParam Long id) {
  167. scaleLogService.deleteById(id);
  168. return ResultGenerator.genSuccessResult();
  169. }
  170. @PostMapping(value = "/delete/ids", name = "游戏测试记录删除")
  171. public Result deleteIds(@RequestParam String ids) {
  172. scaleLogService.deleteByIds(ids);
  173. return ResultGenerator.genSuccessResult();
  174. }
  175. @ApiOperation(value = "量表修改", tags = {"量表"}, notes = "量表修改,对象主键必填")
  176. @PostMapping(value = "/update", name = "量表修改")
  177. public Result update(@ApiParam ScaleLog scaleLog) {
  178. scaleLogService.update(scaleLog);
  179. return ResultGenerator.genSuccessResult();
  180. }
  181. @ApiOperation(value = "量表详细信息", tags = {"量表"}, notes = "量表详细信息")
  182. @ApiImplicitParams({
  183. @ApiImplicitParam(name = "id", required = true, value = "量表id", dataType = "Long", paramType = "query")
  184. })
  185. @PostMapping(value = "/detail", name = "量表详细信息")
  186. public Result detail(@RequestParam Integer id) {
  187. ScaleLog scaleLog = scaleLogService.findById(id);
  188. return ResultGenerator.genSuccessResult(scaleLog);
  189. }
  190. @ApiOperation(value = "量表未填日期", tags = {"量表"}, notes = "量表未填日期")
  191. @ApiImplicitParams({
  192. @ApiImplicitParam(name = "id", required = true, value = "量表id", dataType = "Long", paramType = "query")
  193. })
  194. @PostMapping(value = "/date", name = "量表未填日期")
  195. public Result date(@ApiParam(hidden = true) Authentication authentication,
  196. @RequestParam(required = false) Long msgId) {
  197. AuthUser authUser = (AuthUser) authentication.getPrincipal();
  198. return scaleLogService.date(authUser.getId(), msgId);
  199. }
  200. @ApiOperation(value = "量表列表信息", tags = {"量表"}, notes = "量表列表信息")
  201. @ApiImplicitParams({
  202. @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
  203. @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
  204. })
  205. @PostMapping(value = "/list", name = "用户配置列表信息")
  206. public Result list(@RequestParam(required = false) String search,
  207. @RequestParam(required = false) String order,
  208. @RequestParam(defaultValue = "0") Integer page,
  209. @RequestParam(defaultValue = "10") Integer size) {
  210. return scaleLogService.list(search, order, page, size);
  211. }
  212. @PostMapping("/download")
  213. public void downloadFile(@RequestParam(defaultValue = "{}") String search,
  214. @RequestParam(defaultValue = "{}") String order,
  215. HttpServletResponse response) {
  216. List<Map<String, Object>> res = scaleLogService.download(search, order);
  217. int column = 13;
  218. int rowCount = res.size();
  219. String fileTitle = UtilFun.DateToString(new Date(), UtilFun.YMD) + ".xlsx";
  220. String[] title = {"ID", "用戶名", "序號", "量表日期", "填寫日期", "開始時間", "完成時間", "完成度", "起床", "和人接觸", "工作", "晚飯", "睡覺", "設備碼"};
  221. File file = new File(fileTitle);
  222. BufferedInputStream bis = null;
  223. OutputStream os = null;
  224. // 设置强制下载不打开
  225. response.setContentType("application/octet-stream");
  226. // 设置文件名
  227. response.addHeader("Content-Disposition", "attachment;fileName=" + fileTitle);
  228. response.addHeader("filename", fileTitle);
  229. response.setHeader("Access-Control-Expose-Headers", "filename,Content-Disposition");
  230. try {
  231. OutputStream out = new FileOutputStream(file);
  232. XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作簿对象
  233. XSSFSheet sheet = workbook.createSheet(UtilFun.DateToString(new Date(), UtilFun.YMD)); // 创建工作表
  234. XSSFRow titleRow = sheet.createRow(0); // 产生表格标题行
  235. XSSFCell cellTitle = titleRow.createCell(0); //创建表格标题列
  236. XSSFCellStyle columnTopStyle = ExcelUtil.getColumnTopStyle(workbook);// 获取列头样式对象
  237. XSSFCellStyle style = ExcelUtil.getStyle(workbook); // 获取单元格样式对象
  238. // 合并表格标题行,合并列数为列名的长度,第一个0为起始行号,第二个1为终止行号,第三个0为起始列好,第四个参数为终止列号
  239. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, column));
  240. cellTitle.setCellStyle(columnTopStyle); //设置标题行样式
  241. cellTitle.setCellValue(fileTitle); //设置标题行值
  242. XSSFRow rowRowName = sheet.createRow(1); // 在索引2的位置创建行(最顶端的行开始的第二行)
  243. // 将列头设置到sheet的单元格中
  244. for (int n = 0; n < title.length; n++) {
  245. XSSFCell cellRowName = rowRowName.createCell(n); // 创建列头对应个数的单元格
  246. cellRowName.setCellType(CellType.STRING); // 设置列头单元格的数据类型
  247. XSSFRichTextString text = new XSSFRichTextString(title[n]);
  248. cellRowName.setCellValue(text); // 设置列头单元格的值
  249. cellRowName.setCellStyle(columnTopStyle); // 设置列头单元格样式
  250. }
  251. // 将查询出的数据设置到sheet对应的单元格中
  252. for (int i = 0; i < rowCount; i++) {
  253. XSSFRow row = sheet.createRow(i + 2); // 创建所需的行数
  254. for (int j = 0; j <= column; j++) {
  255. XSSFCell cell; // 设置单元格的数据类型
  256. if (j == 0) {
  257. cell = row.createCell(j, CellType.NUMERIC);
  258. cell.setCellValue(i + 1);
  259. } else {
  260. String text = "";
  261. switch (j) {
  262. case 1: {
  263. text = res.get(i).get("nickname").toString();
  264. break;
  265. }
  266. case 2: {
  267. text = res.get(i).get("sign").toString();
  268. break;
  269. }
  270. case 3: {
  271. text = res.get(i).get("eventDate").toString().substring(0, 10);
  272. break;
  273. }
  274. case 4:{
  275. text = res.get(i).get("startDate").toString().split(" ")[0];
  276. break;
  277. }
  278. case 5: {
  279. text = res.get(i).get("startDate").toString().split(" ")[1];
  280. break;
  281. }
  282. case 6: {
  283. text = res.get(i).get("createDate").toString().split(" ")[1];
  284. break;
  285. }
  286. case 7: {
  287. text = res.get(i).get("schedule").toString();
  288. break;
  289. }
  290. case 8: {
  291. text = res.get(i).get("getUp").toString();
  292. break;
  293. }
  294. case 9: {
  295. text = res.get(i).get("contact").toString();
  296. break;
  297. }
  298. case 10: {
  299. text = res.get(i).get("work").toString();
  300. break;
  301. }
  302. case 11: {
  303. text = res.get(i).get("dinner").toString();
  304. break;
  305. }
  306. case 12: {
  307. text = res.get(i).get("sleep").toString();
  308. break;
  309. }
  310. case 13:{
  311. text = res.get(i).get("deviceId").toString();
  312. break;
  313. }
  314. default: {
  315. text = "";
  316. break;
  317. }
  318. }
  319. cell = row.createCell(j, CellType.STRING);
  320. cell.setCellValue(text); // 设置单元格的值
  321. }
  322. cell.setCellStyle(style); // 设置单元格样式
  323. }
  324. }
  325. // 让列宽随着导出的列长自动适应
  326. for (int colNum = 0; colNum <= column; colNum++) {
  327. int columnWidth = sheet.getColumnWidth(colNum) / 256;
  328. for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
  329. XSSFRow currentRow;
  330. // 当前行未被使用过
  331. if (sheet.getRow(rowNum) == null) {
  332. currentRow = sheet.createRow(rowNum);
  333. } else {
  334. currentRow = sheet.getRow(rowNum);
  335. }
  336. if (currentRow.getCell(colNum) != null) {
  337. XSSFCell currentCell = currentRow.getCell(colNum);
  338. if (currentCell.getCellType() == CellType.STRING) {
  339. int length = currentCell.getStringCellValue()
  340. .getBytes().length;
  341. if (columnWidth < length) {
  342. columnWidth = length;
  343. }
  344. }
  345. }
  346. }
  347. if (colNum == 0) {
  348. sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
  349. } else {
  350. sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
  351. }
  352. }
  353. workbook.write(out);
  354. response.addHeader("Content-Length", String.valueOf(file.length()));
  355. byte[] buff = new byte[1024];
  356. os = response.getOutputStream();
  357. bis = new BufferedInputStream(new FileInputStream(file));
  358. int i = bis.read(buff);
  359. while (i != -1) {
  360. os.write(buff, 0, buff.length);
  361. os.flush();
  362. i = bis.read(buff);
  363. }
  364. } catch (IOException e) {
  365. e.printStackTrace();
  366. } finally {
  367. try {
  368. if (bis != null)
  369. bis.close();
  370. if (os != null)
  371. os.close();
  372. } catch (IOException e) {
  373. logger.error("close exception", e);
  374. }
  375. }
  376. }
  377. }