package com.template.controller; import cn.hutool.poi.excel.ExcelUtil; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.template.annotation.DESRespondSecret; import com.template.api.SmartWarningControllerAPI; import com.template.common.result.ResponseStatusEnum; import com.template.common.utils.*; import com.template.model.dto.OperationWarningDto; import com.template.model.dto.WarningDeletePushDto; import com.template.model.dto.WarningSavePushDto; import com.template.model.dto.WarningUserDto; import com.template.model.pojo.SmartDepartment; import com.template.model.pojo.SmartUser; import com.template.model.pojo.SmartWarning; import com.template.model.result.CommonResult; import com.template.model.result.PageUtils; import com.template.model.vo.BehaviourListVo; import com.template.model.vo.DepartmentTreeVo; import com.template.model.vo.UserVo; import com.template.services.SmartDepartmentService; import com.template.services.SmartUserService; import com.template.services.SmartWarningService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.yaml.snakeyaml.events.Event; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; /** *

* 前端控制器 *

* * @author ceshi * @since 2023-12-04 */ @RestController //返回参数加密注解 @DESRespondSecret public class SmartWarningController implements SmartWarningControllerAPI { @Autowired private SmartWarningService smartWarningService; @Autowired private SmartDepartmentService smartDepartmentService; @Autowired private SmartUserService smartUserService; /** * 新增预警信息 * * @param smartApply 预警信息数据 * @param bindingResult * @return */ @Override @DESRespondSecret(validated = true) public CommonResult insertSmartWarning(SmartWarning smartApply, BindingResult bindingResult) { if (bindingResult.hasErrors()) { String st = paramUtils.getParamError(bindingResult); return CommonResult.fail(st); } int result = smartWarningService.insertSmartWarning(smartApply); return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败"); } /** * 更新预警信息 * * @param sa 预警信息数据 * @param bindingResult * @return */ @Override @DESRespondSecret(validated = true) public CommonResult updateSmartWarningById(SmartWarning sa, BindingResult bindingResult) { if (bindingResult.hasErrors()) { String st = paramUtils.getParamError(bindingResult); return CommonResult.fail(st); } int result = smartWarningService.updateSmartWarning(sa); return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败"); } /** * 预警信息分页数据查询 * * @param currentPage 当前页数 * @param pageCount 一页数据条数 * @param name 查询名称 * @return */ @Override @DESRespondSecret(validated = true) public CommonResult queryPageSmartWarning(int currentPage, int pageCount,String startTime,String endTime, String name, String state) { PageUtils result = smartWarningService.queryPageSmartWarningMange(currentPage, pageCount,startTime,endTime, name, state); return CommonResult.ok(result); } @Override @DESRespondSecret(validated = true) public CommonResult deleteSmartWarningById(int id) { SmartWarning data = smartWarningService.getSmartById(id); if (data == null) { return CommonResult.fail("当前数据不存在,删除失败!"); } int result = smartWarningService.deleteSmartWarningById(id); return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败"); } @Override @DESRespondSecret(validated = true) public CommonResult operation(OperationWarningDto operationWarningDto) { Integer id = operationWarningDto.getId(); if (ObjectUtils.isEmpty(id)) { return CommonResult.fail("缺少id"); } String remark = operationWarningDto.getRemark(); SmartWarning smartById = smartWarningService.getSmartById(id); smartById.setStatu(1); smartById.setRemark(remark); int result = smartWarningService.updateSmartWarning(smartById); return result > 0 ? CommonResult.ok("操作成功") : CommonResult.fail("操作失败"); } @Override @DESRespondSecret(validated = false) public void excelSmartWarning(HttpServletResponse response,String startTime,String endTime,String location, String state) { // 表头数据 // List head = Arrays.asList("姓名","事件","地点","时间","备注","状态","图片"); // // List> sheetDataList = new ArrayList<>(); // sheetDataList.add(head); String fileName = "预警.xlsx"; List warningEntities = new ArrayList<>(); // 获取数据 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.like(ObjectUtils.isNotEmpty(location), SmartWarning::getLocation, location) .eq(ObjectUtils.isNotEmpty(state), SmartWarning::getStatu, state) .between(SmartWarning::getDateTime,startTime,endTime); List list = smartWarningService.list(queryWrapper); for (SmartWarning smartWarning : list) { WarningEntity warning = new WarningEntity(); // if (ObjectUtils.isNotEmpty(smartWarning.getWarningName())) { warning.setName(smartWarning.getWarningName()); // }else { // user.add("无"); // } warning.setType(smartWarning.getType()); warning.setLocation(smartWarning.getLocation()); warning.setDateTime(smartWarning.getDateTime()); warning.setRemark(smartWarning.getRemark()); Integer statu = smartWarning.getStatu(); String status = ""; if (statu == 0) { status = "未处理"; } else if (statu == 1) { status = "已处理"; } warning.setState(status); warning.setImage(smartWarning.getImage()); // 将数据汇总 warningEntities.add(warning); } try { ExcelUtil3.process(fileName, warningEntities, WarningEntity.class, response); } catch (IOException e) { e.printStackTrace(); } } @Override @DESRespondSecret(validated = true) public CommonResult warningType() { List typeList = smartWarningService.warningType(); typeList.add("全部"); return CommonResult.ok(typeList); } @Override @DESRespondSecret(validated = true) public CommonResult pageWarning(int currentPage, int pageCount, String type, String startTime,String endTime) { PageUtils result = smartWarningService.pageWarning(currentPage, pageCount, type, startTime,endTime); return CommonResult.ok(result); } @Override @DESRespondSecret(validated = true) public CommonResult warningDepartmentTree() { List result = smartDepartmentService.list(new QueryWrapper<>()); List zeroResult = result.stream().filter(e -> e.getParentId().intValue() == 0).collect(Collectors.toList()); List departments = new ArrayList<>(); for (SmartDepartment data : zeroResult) { DepartmentTreeVo newData = DepartmentTreeVo.builder() .id(data.getId()) .name(data.getName()) .parentId(data.getParentId()) .build(); List departmentTrees = QueryDepartmentTreeRecords(newData.getId(), result); newData.setChildren(departmentTrees); departments.add(newData); } return CommonResult.ok(departments); } @Override @DESRespondSecret(validated = true) public CommonResult warningUserList(int currentPage, int pageCount, String name) { //获取该部门下的所有子级部门ID // List childDepartmentIds = new ArrayList<>(); // List departments = smartDepartmentService.list(null); // childDepartmentIds.add(departmentId); // QueryDepartmentTreeRecords(departmentId, departments, childDepartmentIds); // // if (departmentId == null) { // childDepartmentIds = null; // } PageUtils result = smartUserService.warningUserList(currentPage, pageCount, name); return CommonResult.ok(result); } @Override @DESRespondSecret(validated = true) public CommonResult warningPushList() { List names = smartUserService.warningPushList(); return CommonResult.ok(names); } @Override @DESRespondSecret(validated = true) public CommonResult warningSavePush(WarningSavePushDto warningSavePushDto) { List ids = warningSavePushDto.getIds(); if (ObjectUtils.isEmpty(ids) && ids.size() <= 0) { return CommonResult.fail(ResponseStatusEnum.PARAM_ERROR); } // 获取所有可推送的人 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(SmartUser::getIsCancel, 0) .eq(SmartUser::getIsPush, 1); List list = smartUserService.list(wrapper); // 需要删除的 ArrayList pushDeleteIds = new ArrayList<>(); // 不用修改的 ArrayList pushIds = new ArrayList<>(); for (SmartUser smartUser : list) { Integer id = smartUser.getId(); if (!ids.contains(id)) { pushDeleteIds.add(id); } else { pushIds.add(id); } } // 获取需要添加的人 ArrayList savePushIds = new ArrayList<>(); for (Integer id : ids) { if (!pushIds.contains(id)) { savePushIds.add(id); } } // 添加 if (ObjectUtils.isNotEmpty(savePushIds) && savePushIds.size() > 0) { List smartUsers = smartUserService.getSmartUserByIds(savePushIds); for (SmartUser smartUser : smartUsers) { String gzhOpenId = smartUser.getGzhOpenId(); if (ObjectUtils.isEmpty(gzhOpenId)) { return CommonResult.fail(smartUser.getName() + "未关联公众号"); } smartUser.setIsPush(1); smartUserService.updateSmartUser(smartUser); } } // 删除 if (ObjectUtils.isNotEmpty(pushDeleteIds) && pushDeleteIds.size() > 0) { List smartUsersDelete = smartUserService.getSmartUserByIds(pushDeleteIds); ArrayList smartUsersUpdate = new ArrayList<>(); for (SmartUser smartUser : smartUsersDelete) { smartUser.setIsPush(0); smartUsersUpdate.add(smartUser); } smartUserService.updateBatchById(smartUsersUpdate); } return CommonResult.ok(); } @Override @DESRespondSecret(validated = true) public CommonResult warningDeletePush(WarningDeletePushDto warningDeletePushDto) { List ids = warningDeletePushDto.getIds(); if (ObjectUtils.isEmpty(ids) && ids.size() == 0) { return CommonResult.fail(); } List smartUserList = smartUserService.getSmartUserList(ids); if (ObjectUtils.isEmpty(smartUserList) && smartUserList.size() == 0) { return CommonResult.fail("无该用户"); } ArrayList smartUsers = new ArrayList<>(); for (SmartUser smartUser : smartUserList) { smartUser.setIsPush(0); smartUsers.add(smartUser); } boolean b = smartUserService.updateBatchById(smartUsers); if (b) { return CommonResult.ok("删除成功"); } return CommonResult.fail("删除失败"); } @Override @DESRespondSecret(validated = true) public CommonResult behaviourList() { List behaviourList = smartWarningService.behaviourList(); return CommonResult.ok(behaviourList); } /** * 根据父级ID获取树形数据 * * @param parentID 父级ID * @param lists 数据集合 * @return */ private List QueryDepartmentTreeRecords(Integer parentID, List lists) { List newTrees = new ArrayList<>(); List datas = lists.stream().filter(e -> e.getParentId().equals(parentID)).collect(Collectors.toList()); for (SmartDepartment data : datas) { DepartmentTreeVo item = DepartmentTreeVo.builder() .id(data.getId()) .parentId(parentID) .name(data.getName()) .build(); List news = QueryDepartmentTreeRecords(item.getId(), lists); if (news == null || news.size() == 0) { newTrees.add(item); continue; } else { item.setChildren(news); newTrees.add(item); } } return newTrees; } /** * 根据父级ID获取树形数据 * * @param parentID 父级ID * @param lists 数据集合 * @return */ private List QueryDepartmentTreeRecords(Integer parentID, List lists, List departmentIds) { List newTrees = new ArrayList<>(); List datas = lists.stream().filter(e -> e.getParentId().equals(parentID)).collect(Collectors.toList()); for (SmartDepartment data : datas) { departmentIds.add(data.getId()); QueryDepartmentTreeRecords(data.getId(), lists, departmentIds); } return newTrees; } }