| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- 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.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.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;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author ceshi
- * @since 2023-12-04
- */
- @RestController
- public class SmartWarningController implements SmartWarningControllerAPI {
- @Autowired
- private SmartWarningService smartWarningService;
- @Autowired
- private SmartDepartmentService smartDepartmentService;
- @Autowired
- private SmartUserService smartUserService;
- /**
- * 新增预警信息
- *
- * @param smartApply 预警信息数据
- * @param bindingResult
- * @return
- */
- @Override
- 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
- 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
- public CommonResult queryPageSmartWarning(int currentPage, int pageCount, String name, String state) {
- PageUtils<SmartWarning> result = smartWarningService.queryPageSmartWarnings(currentPage, pageCount, name, state);
- return CommonResult.ok(result);
- }
- @Override
- 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
- 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
- public void excelSmartWarning(HttpServletResponse response, String location, String state) {
- // 表头数据
- // List<Object> head = Arrays.asList("姓名","事件","地点","时间","备注","状态","图片");
- //
- // List<List<Object>> sheetDataList = new ArrayList<>();
- // sheetDataList.add(head);
- String fileName = "预警.xlsx";
- List<WarningEntity> warningEntities = new ArrayList<>();
- // 获取数据
- LambdaQueryWrapper<SmartWarning> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.like(ObjectUtils.isNotEmpty(location), SmartWarning::getLocation, location)
- .eq(ObjectUtils.isNotEmpty(state), SmartWarning::getStatu, state);
- List<SmartWarning> 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
- public CommonResult warningType() {
- List<String> typeList = smartWarningService.warningType();
- typeList.add("全部");
- return CommonResult.ok(typeList);
- }
- @Override
- public CommonResult pageWarning(int currentPage, int pageCount, String type, String dateTime) {
- PageUtils<SmartWarning> result = smartWarningService.pageWarning(currentPage, pageCount, type, dateTime);
- return CommonResult.ok(result);
- }
- @Override
- public CommonResult warningDepartmentTree() {
- List<SmartDepartment> result = smartDepartmentService.list(new QueryWrapper<>());
- List<SmartDepartment> zeroResult = result.stream().filter(e -> e.getParentId().intValue() == 0).collect(Collectors.toList());
- List<DepartmentTreeVo> departments = new ArrayList<>();
- for (SmartDepartment data : zeroResult) {
- DepartmentTreeVo newData = DepartmentTreeVo.builder()
- .id(data.getId())
- .name(data.getName())
- .parentId(data.getParentId())
- .build();
- List<DepartmentTreeVo> departmentTrees = QueryDepartmentTreeRecords(newData.getId(), result);
- newData.setChildren(departmentTrees);
- departments.add(newData);
- }
- return CommonResult.ok(departments);
- }
- @Override
- public CommonResult warningUserList(int currentPage, int pageCount, String name) {
- //获取该部门下的所有子级部门ID
- // List<Integer> childDepartmentIds = new ArrayList<>();
- // List<SmartDepartment> departments = smartDepartmentService.list(null);
- // childDepartmentIds.add(departmentId);
- // QueryDepartmentTreeRecords(departmentId, departments, childDepartmentIds);
- //
- // if (departmentId == null) {
- // childDepartmentIds = null;
- // }
- PageUtils<WarningUserDto> result = smartUserService.warningUserList(currentPage, pageCount, name);
- return CommonResult.ok(result);
- }
- @Override
- public CommonResult warningPushList() {
- List<WarningUserDto> names = smartUserService.warningPushList();
- return CommonResult.ok(names);
- }
- @Override
- public CommonResult warningSavePush(WarningSavePushDto warningSavePushDto) {
- List<Integer> ids = warningSavePushDto.getIds();
- if (ObjectUtils.isEmpty(ids) && ids.size() <= 0) {
- return CommonResult.fail(ResponseStatusEnum.PARAM_ERROR);
- }
- // 获取所有可推送的人
- LambdaQueryWrapper<SmartUser> wrapper=new LambdaQueryWrapper<>();
- wrapper.eq(SmartUser::getIsCancel, 0)
- .eq(SmartUser::getIsPush,1);
- List<SmartUser> list = smartUserService.list(wrapper);
- // 需要删除的
- ArrayList<Integer> pushDeleteIds = new ArrayList<>();
- // 不用修改的
- ArrayList<Integer> pushIds = new ArrayList<>();
- for (SmartUser smartUser : list) {
- Integer id = smartUser.getId();
- if (!ids.contains(id)) {
- pushDeleteIds.add(id);
- }else {
- pushIds.add(id);
- }
- }
- // 获取需要添加的人
- ArrayList<Integer> savePushIds = new ArrayList<>();
- for (Integer id : ids) {
- if (!pushIds.contains(id)) {
- savePushIds.add(id);
- }
- }
- // 添加
- if (ObjectUtils.isNotEmpty(savePushIds)&&savePushIds.size()>0) {
- List<SmartUser> 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);
- }
- }
- // 删除
- List<SmartUser> smartUsersDelete = smartUserService.getSmartUserByIds(pushDeleteIds);
- ArrayList<SmartUser> smartUsersUpdate = new ArrayList<>();
- for (SmartUser smartUser : smartUsersDelete) {
- smartUser.setIsPush(0);
- smartUsersUpdate.add(smartUser);
- }
- smartUserService.updateBatchById(smartUsersUpdate);
- return CommonResult.ok();
- }
- @Override
- public CommonResult warningDeletePush(WarningDeletePushDto warningDeletePushDto) {
- List<Integer> ids = warningDeletePushDto.getIds();
- if (ObjectUtils.isEmpty(ids) && ids.size() == 0) {
- return CommonResult.fail();
- }
- List<SmartUser> smartUserList = smartUserService.getSmartUserList(ids);
- if (ObjectUtils.isEmpty(smartUserList)&&smartUserList.size()==0 ) {
- return CommonResult.fail("无该用户");
- }
- ArrayList<SmartUser> 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("删除失败");
- }
- /**
- * 根据父级ID获取树形数据
- *
- * @param parentID 父级ID
- * @param lists 数据集合
- * @return
- */
- private List<DepartmentTreeVo> QueryDepartmentTreeRecords(Integer parentID, List<SmartDepartment> lists) {
- List<DepartmentTreeVo> newTrees = new ArrayList<>();
- List<SmartDepartment> 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<DepartmentTreeVo> 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<DepartmentTreeVo> QueryDepartmentTreeRecords(Integer parentID, List<SmartDepartment> lists, List<Integer> departmentIds) {
- List<DepartmentTreeVo> newTrees = new ArrayList<>();
- List<SmartDepartment> 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;
- }
- }
|