HomeController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package org.springblade.ship;
  2. import cn.hutool.json.JSONUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
  5. import io.swagger.annotations.Api;
  6. import io.swagger.annotations.ApiOperation;
  7. import lombok.AllArgsConstructor;
  8. import org.springblade.core.boot.ctrl.BladeController;
  9. import org.springblade.core.log.model.LogUsual;
  10. import org.springblade.core.mp.support.Condition;
  11. import org.springblade.core.secure.BladeUser;
  12. import org.springblade.core.secure.utils.AuthUtil;
  13. import org.springblade.core.tool.api.R;
  14. import org.springblade.core.tool.utils.DateUtil;
  15. import org.springblade.modules.system.entity.User;
  16. import org.springblade.modules.system.service.IDeptService;
  17. import org.springblade.modules.system.service.ILogUsualService;
  18. import org.springblade.modules.system.service.IUserService;
  19. import org.springblade.ship.cable.entity.Cable;
  20. import org.springblade.ship.cable.service.ICableService;
  21. import org.springblade.ship.device.entity.Device;
  22. import org.springblade.ship.device.service.IDeviceService;
  23. import org.springframework.web.bind.annotation.GetMapping;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RestController;
  26. import javax.servlet.http.HttpServletResponse;
  27. import java.io.*;
  28. import java.net.MalformedURLException;
  29. import java.net.URL;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. @RestController
  34. @AllArgsConstructor
  35. @RequestMapping("ship/home")
  36. @Api(value = "", tags = "首页")
  37. public class HomeController extends BladeController {
  38. private final IUserService userService;
  39. private final ICableService cableService;
  40. private final IDeviceService deviceService;
  41. private final ILogUsualService usualService;
  42. /**
  43. *
  44. */
  45. @GetMapping("/getTotalData")
  46. @ApiOperationSupport(order = 1)
  47. @ApiOperation(value = "总数", notes = "")
  48. public R getTotalData() {
  49. Map<String, Object> map = new HashMap<>();
  50. map.put("userCount", userService.count());
  51. map.put("cableCount", cableService.count());
  52. map.put("deviceCount", deviceService.count());
  53. return R.data(map);
  54. }
  55. /**
  56. *
  57. */
  58. @GetMapping("/getTodayData")
  59. @ApiOperationSupport(order = 1)
  60. @ApiOperation(value = "今日新增", notes = "")
  61. public R getTodayData(String theDay) {
  62. Map<String, Object> map = new HashMap<>();
  63. if (theDay == null){
  64. theDay = DateUtil.formatDate(DateUtil.now());
  65. }
  66. map.put("userCount", userService.count(new QueryWrapper<>(new User()).lambda().like(User::getCreateTime, theDay)));
  67. map.put("cableCount", cableService.count(new QueryWrapper<>(new Cable()).lambda().like(Cable::getCreateTime, theDay)));
  68. map.put("deviceCount", deviceService.count(new QueryWrapper<>(new Device()).lambda().like(Device::getCreateTime, theDay)));
  69. return R.data(map);
  70. }
  71. /**
  72. *
  73. */
  74. @GetMapping("/getMonthData")
  75. @ApiOperationSupport(order = 1)
  76. @ApiOperation(value = "今日新增", notes = "")
  77. public R getMonthData(String theMonth) {
  78. Map<String, Object> map = new HashMap<>();
  79. if (theMonth == null){
  80. theMonth = DateUtil.format(DateUtil.now(), "yyyy-MM");
  81. }
  82. map.put("userList", userService.list(new QueryWrapper<>(new User()).lambda().like(User::getCreateTime, theMonth).orderByDesc(User::getCreateTime)));
  83. map.put("cableList", cableService.list(new QueryWrapper<>(new Cable()).lambda().like(Cable::getCreateTime, theMonth).orderByDesc(Cable::getCreateTime)));
  84. map.put("deviceList", deviceService.list(new QueryWrapper<>(new Device()).lambda().like(Device::getCreateTime, theMonth).orderByDesc(Device::getCreateTime)));
  85. map.put("modifyList", usualService.list(new QueryWrapper<>(new LogUsual()).lambda().eq(LogUsual::getRequestUri, "/cable/update").eq(LogUsual::getMethodName, "update")));
  86. return R.data(map);
  87. }
  88. /**
  89. *
  90. */
  91. @GetMapping("/getUpdateData")
  92. @ApiOperationSupport(order = 1)
  93. @ApiOperation(value = "更新信息", notes = "")
  94. public HttpServletResponse getUpdateData(HttpServletResponse response) {
  95. QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(new User());
  96. QueryWrapper<Cable> cableQueryWrapper = new QueryWrapper<>(new Cable());
  97. List<User> userList = userService.list(userQueryWrapper);
  98. List<Cable> cableList = cableService.list(cableQueryWrapper);
  99. Map<String, Object> map = new HashMap<>();
  100. map.put("userList", userList);
  101. map.put("cableList", cableList);
  102. map.put("updateTime", DateUtil.format(DateUtil.now(), "yyyy-MM-dd HH:mm:ss"));
  103. BufferedWriter out = null;
  104. try {
  105. File file = new File("datas.json");
  106. if(!file.exists()){
  107. file.createNewFile();
  108. }
  109. out = new BufferedWriter(new FileWriter(file, false));
  110. out.write(JSONUtil.toJsonStr(map));
  111. out.close();
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. }
  115. // 读到流中
  116. InputStream inStream = null;// 文件的存放路径
  117. try {
  118. File f = new File("datas.json");
  119. if (!f.exists()) {
  120. // response.sendError(404, "File not found!");
  121. return response;
  122. }
  123. BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
  124. byte[] buf = new byte[1024];
  125. int len = 0;
  126. response.reset(); // 非常重要
  127. URL u = new URL("file:///" + f.getAbsolutePath());
  128. response.setContentType(u.openConnection().getContentType());
  129. response.setContentType("application/x-msdownload");
  130. response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
  131. // 文件名应该编码成UTF-8
  132. OutputStream out2 = response.getOutputStream();
  133. while ((len = br.read(buf)) > 0)
  134. out2.write(buf, 0, len);
  135. br.close();
  136. out2.close();
  137. } catch (FileNotFoundException | MalformedURLException e) {
  138. e.printStackTrace();
  139. } catch (IOException e) {
  140. e.printStackTrace();
  141. }
  142. return response;
  143. }
  144. }