SmartWarningController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. package com.template.controller;
  2. import cn.hutool.poi.excel.ExcelUtil;
  3. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.metadata.IPage;
  7. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  8. import com.template.annotation.DESRespondSecret;
  9. import com.template.api.SmartWarningControllerAPI;
  10. import com.template.common.result.ResponseStatusEnum;
  11. import com.template.common.utils.*;
  12. import com.template.model.dto.OperationWarningDto;
  13. import com.template.model.dto.WarningDeletePushDto;
  14. import com.template.model.dto.WarningSavePushDto;
  15. import com.template.model.dto.WarningUserDto;
  16. import com.template.model.pojo.SmartDepartment;
  17. import com.template.model.pojo.SmartUser;
  18. import com.template.model.pojo.SmartWarning;
  19. import com.template.model.result.CommonResult;
  20. import com.template.model.result.PageUtils;
  21. import com.template.model.vo.BehaviourListVo;
  22. import com.template.model.vo.DepartmentTreeVo;
  23. import com.template.model.vo.UserVo;
  24. import com.template.services.SmartDepartmentService;
  25. import com.template.services.SmartUserService;
  26. import com.template.services.SmartWarningService;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.validation.BindingResult;
  29. import org.springframework.web.bind.annotation.RequestBody;
  30. import org.springframework.web.bind.annotation.RequestMapping;
  31. import org.springframework.web.bind.annotation.RequestParam;
  32. import org.springframework.web.bind.annotation.RestController;
  33. import org.yaml.snakeyaml.events.Event;
  34. import javax.servlet.http.HttpServletResponse;
  35. import java.io.IOException;
  36. import java.text.SimpleDateFormat;
  37. import java.util.ArrayList;
  38. import java.util.Arrays;
  39. import java.util.List;
  40. import java.util.stream.Collectors;
  41. /**
  42. * <p>
  43. * 前端控制器
  44. * </p>
  45. *
  46. * @author ceshi
  47. * @since 2023-12-04
  48. */
  49. @RestController
  50. //返回参数加密注解
  51. @DESRespondSecret
  52. public class SmartWarningController implements SmartWarningControllerAPI {
  53. @Autowired
  54. private SmartWarningService smartWarningService;
  55. @Autowired
  56. private SmartDepartmentService smartDepartmentService;
  57. @Autowired
  58. private SmartUserService smartUserService;
  59. /**
  60. * 新增预警信息
  61. *
  62. * @param smartApply 预警信息数据
  63. * @param bindingResult
  64. * @return
  65. */
  66. @Override
  67. @DESRespondSecret(validated = true)
  68. public CommonResult insertSmartWarning(SmartWarning smartApply, BindingResult bindingResult) {
  69. if (bindingResult.hasErrors()) {
  70. String st = paramUtils.getParamError(bindingResult);
  71. return CommonResult.fail(st);
  72. }
  73. int result = smartWarningService.insertSmartWarning(smartApply);
  74. return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
  75. }
  76. /**
  77. * 更新预警信息
  78. *
  79. * @param sa 预警信息数据
  80. * @param bindingResult
  81. * @return
  82. */
  83. @Override
  84. @DESRespondSecret(validated = true)
  85. public CommonResult updateSmartWarningById(SmartWarning sa, BindingResult bindingResult) {
  86. if (bindingResult.hasErrors()) {
  87. String st = paramUtils.getParamError(bindingResult);
  88. return CommonResult.fail(st);
  89. }
  90. int result = smartWarningService.updateSmartWarning(sa);
  91. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  92. }
  93. /**
  94. * 预警信息分页数据查询
  95. *
  96. * @param currentPage 当前页数
  97. * @param pageCount 一页数据条数
  98. * @param name 查询名称
  99. * @return
  100. */
  101. @Override
  102. @DESRespondSecret(validated = true)
  103. public CommonResult queryPageSmartWarning(int currentPage, int pageCount,String startTime,String endTime, String name, String state) {
  104. PageUtils<SmartWarning> result = smartWarningService.queryPageSmartWarningMange(currentPage, pageCount,startTime,endTime, name, state);
  105. return CommonResult.ok(result);
  106. }
  107. @Override
  108. @DESRespondSecret(validated = true)
  109. public CommonResult deleteSmartWarningById(int id) {
  110. SmartWarning data = smartWarningService.getSmartById(id);
  111. if (data == null) {
  112. return CommonResult.fail("当前数据不存在,删除失败!");
  113. }
  114. int result = smartWarningService.deleteSmartWarningById(id);
  115. return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
  116. }
  117. @Override
  118. @DESRespondSecret(validated = true)
  119. public CommonResult operation(OperationWarningDto operationWarningDto) {
  120. Integer id = operationWarningDto.getId();
  121. if (ObjectUtils.isEmpty(id)) {
  122. return CommonResult.fail("缺少id");
  123. }
  124. String remark = operationWarningDto.getRemark();
  125. SmartWarning smartById = smartWarningService.getSmartById(id);
  126. smartById.setStatu(1);
  127. smartById.setRemark(remark);
  128. int result = smartWarningService.updateSmartWarning(smartById);
  129. return result > 0 ? CommonResult.ok("操作成功") : CommonResult.fail("操作失败");
  130. }
  131. @Override
  132. @DESRespondSecret(validated = false)
  133. public void excelSmartWarning(HttpServletResponse response,String startTime,String endTime,String location, String state) {
  134. // 表头数据
  135. // List<Object> head = Arrays.asList("姓名","事件","地点","时间","备注","状态","图片");
  136. //
  137. // List<List<Object>> sheetDataList = new ArrayList<>();
  138. // sheetDataList.add(head);
  139. String fileName = "预警.xlsx";
  140. List<WarningEntity> warningEntities = new ArrayList<>();
  141. // 获取数据
  142. LambdaQueryWrapper<SmartWarning> queryWrapper = new LambdaQueryWrapper<>();
  143. queryWrapper.like(ObjectUtils.isNotEmpty(location), SmartWarning::getLocation, location)
  144. .eq(ObjectUtils.isNotEmpty(state), SmartWarning::getStatu, state)
  145. .between(SmartWarning::getDateTime,startTime,endTime);
  146. List<SmartWarning> list = smartWarningService.list(queryWrapper);
  147. for (SmartWarning smartWarning : list) {
  148. WarningEntity warning = new WarningEntity();
  149. // if (ObjectUtils.isNotEmpty(smartWarning.getWarningName())) {
  150. warning.setName(smartWarning.getWarningName());
  151. // }else {
  152. // user.add("无");
  153. // }
  154. warning.setType(smartWarning.getType());
  155. warning.setLocation(smartWarning.getLocation());
  156. warning.setDateTime(smartWarning.getDateTime());
  157. warning.setRemark(smartWarning.getRemark());
  158. Integer statu = smartWarning.getStatu();
  159. String status = "";
  160. if (statu == 0) {
  161. status = "未处理";
  162. } else if (statu == 1) {
  163. status = "已处理";
  164. }
  165. warning.setState(status);
  166. warning.setImage(smartWarning.getImage());
  167. // 将数据汇总
  168. warningEntities.add(warning);
  169. }
  170. try {
  171. ExcelUtil3.process(fileName, warningEntities, WarningEntity.class, response);
  172. } catch (IOException e) {
  173. e.printStackTrace();
  174. }
  175. }
  176. @Override
  177. @DESRespondSecret(validated = true)
  178. public CommonResult warningType() {
  179. List<String> typeList = smartWarningService.warningType();
  180. typeList.add("全部");
  181. return CommonResult.ok(typeList);
  182. }
  183. @Override
  184. @DESRespondSecret(validated = true)
  185. public CommonResult pageWarning(int currentPage, int pageCount, String type, String startTime,String endTime) {
  186. PageUtils<SmartWarning> result = smartWarningService.pageWarning(currentPage, pageCount, type, startTime,endTime);
  187. return CommonResult.ok(result);
  188. }
  189. @Override
  190. @DESRespondSecret(validated = true)
  191. public CommonResult warningDepartmentTree() {
  192. List<SmartDepartment> result = smartDepartmentService.list(new QueryWrapper<>());
  193. List<SmartDepartment> zeroResult = result.stream().filter(e -> e.getParentId().intValue() == 0).collect(Collectors.toList());
  194. List<DepartmentTreeVo> departments = new ArrayList<>();
  195. for (SmartDepartment data : zeroResult) {
  196. DepartmentTreeVo newData = DepartmentTreeVo.builder()
  197. .id(data.getId())
  198. .name(data.getName())
  199. .parentId(data.getParentId())
  200. .build();
  201. List<DepartmentTreeVo> departmentTrees = QueryDepartmentTreeRecords(newData.getId(), result);
  202. newData.setChildren(departmentTrees);
  203. departments.add(newData);
  204. }
  205. return CommonResult.ok(departments);
  206. }
  207. @Override
  208. @DESRespondSecret(validated = true)
  209. public CommonResult warningUserList(int currentPage, int pageCount, String name) {
  210. //获取该部门下的所有子级部门ID
  211. // List<Integer> childDepartmentIds = new ArrayList<>();
  212. // List<SmartDepartment> departments = smartDepartmentService.list(null);
  213. // childDepartmentIds.add(departmentId);
  214. // QueryDepartmentTreeRecords(departmentId, departments, childDepartmentIds);
  215. //
  216. // if (departmentId == null) {
  217. // childDepartmentIds = null;
  218. // }
  219. PageUtils<WarningUserDto> result = smartUserService.warningUserList(currentPage, pageCount, name);
  220. return CommonResult.ok(result);
  221. }
  222. @Override
  223. @DESRespondSecret(validated = true)
  224. public CommonResult warningPushList() {
  225. List<WarningUserDto> names = smartUserService.warningPushList();
  226. return CommonResult.ok(names);
  227. }
  228. @Override
  229. @DESRespondSecret(validated = true)
  230. public CommonResult warningSavePush(WarningSavePushDto warningSavePushDto) {
  231. List<Integer> ids = warningSavePushDto.getIds();
  232. if (ObjectUtils.isEmpty(ids) && ids.size() <= 0) {
  233. return CommonResult.fail(ResponseStatusEnum.PARAM_ERROR);
  234. }
  235. // 获取所有可推送的人
  236. LambdaQueryWrapper<SmartUser> wrapper = new LambdaQueryWrapper<>();
  237. wrapper.eq(SmartUser::getIsCancel, 0)
  238. .eq(SmartUser::getIsPush, 1);
  239. List<SmartUser> list = smartUserService.list(wrapper);
  240. // 需要删除的
  241. ArrayList<Integer> pushDeleteIds = new ArrayList<>();
  242. // 不用修改的
  243. ArrayList<Integer> pushIds = new ArrayList<>();
  244. for (SmartUser smartUser : list) {
  245. Integer id = smartUser.getId();
  246. if (!ids.contains(id)) {
  247. pushDeleteIds.add(id);
  248. } else {
  249. pushIds.add(id);
  250. }
  251. }
  252. // 获取需要添加的人
  253. ArrayList<Integer> savePushIds = new ArrayList<>();
  254. for (Integer id : ids) {
  255. if (!pushIds.contains(id)) {
  256. savePushIds.add(id);
  257. }
  258. }
  259. // 添加
  260. if (ObjectUtils.isNotEmpty(savePushIds) && savePushIds.size() > 0) {
  261. List<SmartUser> smartUsers = smartUserService.getSmartUserByIds(savePushIds);
  262. for (SmartUser smartUser : smartUsers) {
  263. String gzhOpenId = smartUser.getGzhOpenId();
  264. if (ObjectUtils.isEmpty(gzhOpenId)) {
  265. return CommonResult.fail(smartUser.getName() + "未关联公众号");
  266. }
  267. smartUser.setIsPush(1);
  268. smartUserService.updateSmartUser(smartUser);
  269. }
  270. }
  271. // 删除
  272. if (ObjectUtils.isNotEmpty(pushDeleteIds) && pushDeleteIds.size() > 0) {
  273. List<SmartUser> smartUsersDelete = smartUserService.getSmartUserByIds(pushDeleteIds);
  274. ArrayList<SmartUser> smartUsersUpdate = new ArrayList<>();
  275. for (SmartUser smartUser : smartUsersDelete) {
  276. smartUser.setIsPush(0);
  277. smartUsersUpdate.add(smartUser);
  278. }
  279. smartUserService.updateBatchById(smartUsersUpdate);
  280. }
  281. return CommonResult.ok();
  282. }
  283. @Override
  284. @DESRespondSecret(validated = true)
  285. public CommonResult warningDeletePush(WarningDeletePushDto warningDeletePushDto) {
  286. List<Integer> ids = warningDeletePushDto.getIds();
  287. if (ObjectUtils.isEmpty(ids) && ids.size() == 0) {
  288. return CommonResult.fail();
  289. }
  290. List<SmartUser> smartUserList = smartUserService.getSmartUserList(ids);
  291. if (ObjectUtils.isEmpty(smartUserList) && smartUserList.size() == 0) {
  292. return CommonResult.fail("无该用户");
  293. }
  294. ArrayList<SmartUser> smartUsers = new ArrayList<>();
  295. for (SmartUser smartUser : smartUserList) {
  296. smartUser.setIsPush(0);
  297. smartUsers.add(smartUser);
  298. }
  299. boolean b = smartUserService.updateBatchById(smartUsers);
  300. if (b) {
  301. return CommonResult.ok("删除成功");
  302. }
  303. return CommonResult.fail("删除失败");
  304. }
  305. @Override
  306. @DESRespondSecret(validated = true)
  307. public CommonResult behaviourList() {
  308. List<BehaviourListVo> behaviourList = smartWarningService.behaviourList();
  309. return CommonResult.ok(behaviourList);
  310. }
  311. /**
  312. * 根据父级ID获取树形数据
  313. *
  314. * @param parentID 父级ID
  315. * @param lists 数据集合
  316. * @return
  317. */
  318. private List<DepartmentTreeVo> QueryDepartmentTreeRecords(Integer parentID, List<SmartDepartment> lists) {
  319. List<DepartmentTreeVo> newTrees = new ArrayList<>();
  320. List<SmartDepartment> datas = lists.stream().filter(e -> e.getParentId().equals(parentID)).collect(Collectors.toList());
  321. for (SmartDepartment data : datas) {
  322. DepartmentTreeVo item = DepartmentTreeVo.builder()
  323. .id(data.getId())
  324. .parentId(parentID)
  325. .name(data.getName())
  326. .build();
  327. List<DepartmentTreeVo> news = QueryDepartmentTreeRecords(item.getId(), lists);
  328. if (news == null || news.size() == 0) {
  329. newTrees.add(item);
  330. continue;
  331. } else {
  332. item.setChildren(news);
  333. newTrees.add(item);
  334. }
  335. }
  336. return newTrees;
  337. }
  338. /**
  339. * 根据父级ID获取树形数据
  340. *
  341. * @param parentID 父级ID
  342. * @param lists 数据集合
  343. * @return
  344. */
  345. private List<DepartmentTreeVo> QueryDepartmentTreeRecords(Integer parentID, List<SmartDepartment> lists, List<Integer> departmentIds) {
  346. List<DepartmentTreeVo> newTrees = new ArrayList<>();
  347. List<SmartDepartment> datas = lists.stream().filter(e -> e.getParentId().equals(parentID)).collect(Collectors.toList());
  348. for (SmartDepartment data : datas) {
  349. departmentIds.add(data.getId());
  350. QueryDepartmentTreeRecords(data.getId(), lists, departmentIds);
  351. }
  352. return newTrees;
  353. }
  354. }