TLocationCheckController.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package com.szwl.controller;
  2. import cn.hutool.core.util.IdUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.szwl.model.bo.R;
  6. import com.szwl.model.bo.ResponseModel;
  7. import com.szwl.model.entity.TLocationCheck;
  8. import com.szwl.service.TAdminService;
  9. import com.szwl.service.TLocationCheckService;
  10. import com.szwl.util.WhoIsUtil;
  11. import io.swagger.annotations.ApiOperation;
  12. import org.apache.commons.lang.StringUtils;
  13. import org.springframework.web.bind.annotation.*;
  14. import java.util.*;
  15. /**
  16. * <p>
  17. * 前端控制器
  18. * </p>
  19. *
  20. * @author wuhs
  21. * @since 2023-12-11
  22. */
  23. @RestController
  24. @RequestMapping("/tLocationCheck")
  25. public class TLocationCheckController {
  26. TLocationCheckService locationCheckService;
  27. TAdminService adminService;
  28. public TLocationCheckController(TLocationCheckService locationCheckService, TAdminService adminService) {
  29. this.locationCheckService = locationCheckService;
  30. this.adminService = adminService;
  31. }
  32. @ApiOperation(value = "获取定位信息")
  33. @GetMapping("/getLocInfo")
  34. public ResponseModel<?> getLocInfo(String clientId) {
  35. if (StringUtils.isEmpty(clientId)) {
  36. return R.fail("设备id不能为空");
  37. }
  38. // 根据设备id查询位置信息
  39. LambdaQueryWrapper<TLocationCheck> lqw = Wrappers.lambdaQuery();
  40. lqw.eq(TLocationCheck::getClientId, clientId);
  41. TLocationCheck locationCheck = locationCheckService.getOne(lqw);
  42. if (Objects.nonNull(locationCheck)) {
  43. HashMap<String, String> locationMap = new HashMap<>();
  44. String country = locationCheck.getCountry();
  45. String username = locationCheck.getUsername();
  46. String name = locationCheck.getName();
  47. String phone = locationCheck.getPhone();
  48. String longitude = locationCheck.getLongitude();
  49. String latitude = locationCheck.getLatitude();
  50. String addr = locationCheck.getAddr();
  51. String modUsername = locationCheck.getModUsername();
  52. String modName = locationCheck.getModName();
  53. String modPhone = locationCheck.getModPhone();
  54. String message = locationCheck.getMessage();
  55. locationMap.put("country", country); // 录入国家
  56. locationMap.put("username", username); // 账号用户名
  57. locationMap.put("name", name); // 录入人姓名
  58. locationMap.put("phone", phone); // 录入人电话
  59. locationMap.put("longitude", longitude); // 经度
  60. locationMap.put("latitude", latitude); // 纬度
  61. locationMap.put("address", addr); // 反编译的地址
  62. locationMap.put("modUsername", modUsername); // 修改人账号
  63. locationMap.put("modName", modName); // 修改人姓名
  64. locationMap.put("modPhone", modPhone); // 修改人电话
  65. locationMap.put("message", message); // 修改原因
  66. // if (country.equals("中国")) {
  67. // String location = locationCheck.getLocation();
  68. // return R.ok(country + location);
  69. // } else {
  70. // return R.ok(country);
  71. // }
  72. return R.ok(locationMap);
  73. }
  74. return R.fail("地区信息未录入");
  75. }
  76. @ApiOperation(value = "根据ip校验clientId设备定位")
  77. @PostMapping("/ipCheck")
  78. public ResponseModel<?> ipCheck(@RequestParam String clientId, @RequestParam String ip) {
  79. if (StringUtils.isBlank(clientId) || StringUtils.isBlank(ip)) {
  80. return R.fail("参数不能为空");
  81. }
  82. // 根据 clientId 查设备的 location,country
  83. LambdaQueryWrapper<TLocationCheck> lqw = Wrappers.lambdaQuery();
  84. lqw.eq(TLocationCheck::getClientId, clientId);
  85. TLocationCheck locationCheck = locationCheckService.getOne(lqw);
  86. if (Objects.isNull(locationCheck)) {
  87. return R.fail("生产部未录入改设备的位置信息");
  88. }
  89. locationCheck.setIp(ip);
  90. locationCheck.setModifyTime(new Date());
  91. String addr = WhoIsUtil.getLocByIp(ip);
  92. // String result = locationCheckService.locCheckMsg(locationCheck, clientId, addr);
  93. String result = locationCheckService.locCheckNoMsg(locationCheck, clientId, addr);
  94. if ("fail".equals(result)) {
  95. locationCheck.setErr("不匹配");
  96. }
  97. locationCheckService.updateById(locationCheck);
  98. return R.ok(result);
  99. }
  100. @ApiOperation(value = "修改定位信息")
  101. @PostMapping("/updateLocInfo")
  102. public ResponseModel<?> updateLocInfo(@RequestBody TLocationCheck locationCheck) {
  103. if (Objects.isNull(locationCheck)) {
  104. return R.fail("参数不能为空");
  105. }
  106. // 售后部同事修改的信息:姓名,电话,username,clientId,国家
  107. String modUsername = locationCheck.getModUsername();
  108. // if (!("shouhoumi".equals(modUsername)) && !("admin".equals(modUsername)) && !("chenfanghao".equals(modUsername)) && !("ethan".equals(modUsername))) {
  109. // return R.fail("该账号无权修改");
  110. // }
  111. Set<String> allowedUsernames = new HashSet<>(Arrays.asList("shouhoumi", "admin", "jiang123", "ethan"));
  112. if (!allowedUsernames.contains(modUsername)) {
  113. return R.fail("该账号无权修改");
  114. }
  115. String modName = locationCheck.getModName();
  116. String modPhone = locationCheck.getModPhone();
  117. String clientId = locationCheck.getClientId();
  118. // String location = locationCheck.getLocation();
  119. String country = locationCheck.getCountry();
  120. String message = locationCheck.getMessage();
  121. if (Objects.isNull(clientId)) {
  122. return R.fail("设备唯一码不能为空");
  123. }
  124. LambdaQueryWrapper<TLocationCheck> wrapper = Wrappers.lambdaQuery();
  125. wrapper.eq(TLocationCheck::getClientId, clientId);
  126. List<TLocationCheck> checks = locationCheckService.list(wrapper);
  127. TLocationCheck tLocationCheck = checks.get(0);
  128. tLocationCheck.setModifyTime(new Date());
  129. tLocationCheck.setModName(modName);
  130. // tLocationCheck.setModAdminId(modAdminId);
  131. tLocationCheck.setModPhone(modPhone);
  132. tLocationCheck.setModUsername(modUsername);
  133. // tLocationCheck.setLocation(location);
  134. tLocationCheck.setClientId(clientId);
  135. tLocationCheck.setCountry(country);
  136. tLocationCheck.setMessage(message);
  137. locationCheckService.updateById(tLocationCheck);
  138. return R.ok("修改成功");
  139. }
  140. @ApiOperation(value = "录入定位信息")
  141. @PostMapping("/inputLocInfo")
  142. public ResponseModel<?> inputLocInfo(@RequestBody TLocationCheck locationCheck) {
  143. if (Objects.isNull(locationCheck)) {
  144. return R.fail("参数不能为空");
  145. }
  146. String name = locationCheck.getName();
  147. String phone = locationCheck.getPhone();
  148. String username = locationCheck.getUsername();
  149. String clientId = locationCheck.getClientId();
  150. String country = locationCheck.getCountry(); // 国家
  151. // String location = locationCheck.getLocation(); // 地区
  152. if (Objects.isNull(clientId)) {
  153. return R.fail("设备唯一码不能为空");
  154. }
  155. LambdaQueryWrapper<TLocationCheck> wrapper = Wrappers.lambdaQuery();
  156. wrapper.eq(TLocationCheck::getClientId, clientId);
  157. List<TLocationCheck> checks = locationCheckService.list(wrapper);
  158. if (checks.size() > 0) {
  159. return R.fail("此设备信息已录入");
  160. }
  161. Set<String> allowedUsers = new HashSet<>(Arrays.asList("admin", "jiang123", "shouhou121", "shouhou369", "shouhou397", "shouhoumi", "zhl123", "ethan"));
  162. if (!allowedUsers.contains(username)) {
  163. return R.fail("该账号无权操作");
  164. }
  165. TLocationCheck tLocationCheck = new TLocationCheck();
  166. String uuid = IdUtil.simpleUUID();
  167. tLocationCheck.setId(uuid);
  168. tLocationCheck.setCreateTime(new Date());
  169. tLocationCheck.setName(name);
  170. tLocationCheck.setPhone(phone);
  171. tLocationCheck.setUsername(username);
  172. tLocationCheck.setClientId(clientId);
  173. tLocationCheck.setCountry(country);
  174. // tLocationCheck.setLocation(location);
  175. locationCheckService.save(tLocationCheck);
  176. return R.ok("设备出厂信息录入成功");
  177. }
  178. }