SmartWarningController.java 14 KB

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