EventLogController.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.benyun.controller;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.benyun.boot.core.vo.Result;
  5. import com.benyun.entity.Event;
  6. import com.benyun.vo.*;
  7. import lombok.RequiredArgsConstructor;
  8. import com.benyun.entity.EventLog;
  9. import com.benyun.service.EventLogService;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.DeleteMapping;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.PathVariable;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.PutMapping;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import org.beetl.sql.core.engine.PageQuery;
  21. import javax.servlet.http.HttpServletRequest;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * 事件日志信息表(埋点操作)
  26. *
  27. * @author makejava
  28. * create on 2023-03-22 09:25:07
  29. */
  30. @RestController
  31. @RequestMapping("eventLog")
  32. @RequiredArgsConstructor(onConstructor_ = {@Autowired})
  33. public class EventLogController {
  34. /**
  35. * 服务对象
  36. */
  37. private final EventLogService eventLogService;
  38. /**
  39. * 查询所有数据
  40. *
  41. * @return 所有数据
  42. */
  43. @GetMapping("/queryAll")
  44. public Result<List<EventLog>> queryAll() {
  45. return Result.success(eventLogService.queryAll());
  46. }
  47. /**
  48. * 分页查询数据
  49. *
  50. * @return 所有数据
  51. */
  52. @GetMapping("/queryPage")
  53. public Result<List<EventLog>> queryPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, EventLog eventLog) {
  54. PageQuery<EventLog> pageQuery = new PageQuery<EventLog>();
  55. pageQuery.setPageSize(pageSize);
  56. pageQuery.setPageNumber(pageNum);
  57. pageQuery.setParas(eventLog);
  58. return eventLogService.queryByPage(pageQuery);
  59. }
  60. /**
  61. * 分页查询数据2
  62. *
  63. * @return 所有数据
  64. */
  65. @GetMapping("/queryPage2")
  66. public Result<EventLogPageVo> queryPage2(@RequestParam Integer pageNum,
  67. @RequestParam Integer pageSize,
  68. @RequestParam(required = false) String applyId,
  69. @RequestParam(required = false) String eventId,
  70. @RequestParam(required = false) String applyName,
  71. @RequestParam(required = false) String eventName) {
  72. PageQuery<EventLogVo> pageQuery = new PageQuery<EventLogVo>();
  73. pageQuery.setPageSize(pageSize);
  74. pageQuery.setPageNumber(pageNum);
  75. if (StrUtil.isNotBlank(applyId))
  76. pageQuery.setPara("applyId", applyId);
  77. if (StrUtil.isNotBlank(eventId))
  78. pageQuery.setPara("eventId", eventId);
  79. if (StrUtil.isNotBlank(applyName))
  80. pageQuery.setPara("applyName", "%" + applyName + "%");
  81. if (StrUtil.isNotBlank(eventName))
  82. pageQuery.setPara("eventName", "%" + eventName + "%");
  83. return Result.success(eventLogService.queryByPage2(pageQuery));
  84. }
  85. /**
  86. * 通过主键查询单条数据
  87. *
  88. * @param id 主键
  89. * @return 单条数据
  90. */
  91. @GetMapping("/queryOne")
  92. public Result<EventLogVo> queryOne(@RequestParam("id") Integer id) {
  93. return Result.success(eventLogService.queryById(id));
  94. }
  95. /**
  96. * 新增数据
  97. *
  98. * @param eventLog 实体对象
  99. * @return 新增结果
  100. */
  101. @PostMapping("/create")
  102. public Result<?> create(@RequestBody EventLog eventLog) {
  103. eventLogService.insert(eventLog);
  104. return Result.success(eventLog);
  105. }
  106. /**
  107. * 修改数据
  108. *
  109. * @param eventLog 实体对象
  110. * @return 修改结果
  111. */
  112. @PostMapping("/update")
  113. public Result<?> update(@RequestBody EventLog eventLog) {
  114. return eventLogService.updateById(eventLog) ? Result.success() : Result.failure();
  115. }
  116. /**
  117. * 删除数据
  118. *
  119. * @param id 主键
  120. * @return 删除结果
  121. */
  122. @PostMapping("/delete")
  123. public Result<Boolean> delete(@RequestParam("id") Integer id) {
  124. return Result.success(this.eventLogService.deleteById(id));
  125. }
  126. // /**
  127. // * 批量新增数据
  128. // *
  129. // * @param models 实体对象
  130. // * @return 新增结果
  131. // */
  132. // @PostMapping("/createList")
  133. // public Result<Boolean> createList(@RequestBody Map<String, Object> models) {
  134. // List<EventLog> eventLogList = JSON.parseArray(JSON.toJSONString(models.get("eventLogList")), EventLog.class);
  135. // eventLogService.insertList(eventLogList);
  136. // return Result.success(true);
  137. // }
  138. /**
  139. * 批量新增数据
  140. *
  141. * @param eventLogList 实体对象
  142. * @return 新增结果
  143. */
  144. @PostMapping("/createList")
  145. public Result<Boolean> createList(@RequestBody List<EventLog> eventLogList, HttpServletRequest request) {
  146. String ip = getRealIp(request);
  147. eventLogList.forEach(item -> {
  148. item.setIpAddress(ip);
  149. });
  150. eventLogService.insertList(eventLogList);
  151. return Result.success(true);
  152. }
  153. /**
  154. * 获取id地址
  155. *
  156. * @param request
  157. */
  158. public static String getRealIp(HttpServletRequest request) {
  159. String ip = request.getHeader("x-forwarded-for");
  160. if (ip != null && ip.length() > 15 && ip.indexOf(",") > 0) {
  161. ip = ip.substring(0, ip.indexOf(","));
  162. }
  163. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  164. ip = request.getHeader("Proxy-Client-IP");
  165. }
  166. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  167. ip = request.getHeader("WL-Proxy-Client-IP");
  168. }
  169. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  170. ip = request.getRemoteAddr();
  171. }
  172. return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
  173. }
  174. }