ProtThresholdServiceImpl.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.huimv.admin.service.impl;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  7. import com.huimv.admin.common.token.TokenSign;
  8. import com.huimv.admin.common.utils.Result;
  9. import com.huimv.admin.common.utils.ResultCode;
  10. import com.huimv.admin.entity.EnvWarningThreshold;
  11. import com.huimv.admin.entity.GasThreshold;
  12. import com.huimv.admin.entity.ProtThreshold;
  13. import com.huimv.admin.mapper.ProtThresholdMapper;
  14. import com.huimv.admin.mapper.SysAccountMultilevelMapper;
  15. import com.huimv.admin.service.IProtThresholdService;
  16. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import javax.servlet.http.HttpServletRequest;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.stream.Collectors;
  25. /**
  26. * <p>
  27. * 环保阈值 服务实现类
  28. * </p>
  29. *
  30. * @author author
  31. * @since 2023-02-14
  32. */
  33. @Service
  34. public class ProtThresholdServiceImpl extends ServiceImpl<ProtThresholdMapper, ProtThreshold> implements IProtThresholdService {
  35. @Autowired
  36. private ProtThresholdMapper protThresholdMapper;
  37. @Autowired
  38. SysAccountMultilevelMapper sysAccountMultilevelMapper;
  39. @Override
  40. public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  41. String farmId = paramsMap.get("farmId");
  42. String protType = paramsMap.get("protType");
  43. QueryWrapper<ProtThreshold> queryWrapper = new QueryWrapper<>();
  44. queryWrapper.eq("farm_id", farmId);
  45. List<ProtThreshold> protThresholdList = protThresholdMapper.selectList(queryWrapper);
  46. Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
  47. List<Integer> userIds =sysAccountMultilevelMapper.getLowerLevel( userId,farmId);
  48. List<Integer> collect =new ArrayList<>();
  49. if (ObjectUtil.isNotEmpty(protThresholdList) &&StringUtils.isNotBlank(protThresholdList.get(0).getUserIds())){
  50. collect = Arrays.stream(protThresholdList.get(0).getUserIds().split(",")).map(Integer::valueOf).filter(userIds::contains).collect(Collectors.toList());
  51. }
  52. if (protThresholdList.size() == 0) {
  53. JSONObject jsonObject = new JSONObject();
  54. jsonObject.put("COD1", 0);
  55. jsonObject.put("COD2", 0);
  56. jsonObject.put("PH1", 0);
  57. jsonObject.put("PH2", 0);
  58. jsonObject.put("NH3N1", 0);
  59. jsonObject.put("NH3N2", 0);
  60. jsonObject.put("TP1", 0);
  61. jsonObject.put("TP2", 0);
  62. jsonObject.put("TN1", 0);
  63. jsonObject.put("TN2", 0);
  64. jsonObject.put("FLOW1", 0);
  65. jsonObject.put("FLOW2", 0);
  66. jsonObject.put("userIds", 0);
  67. jsonObject.put("dataType", 0);
  68. return new Result(ResultCode.SUCCESS, jsonObject);
  69. } else {
  70. JSONObject jsonObject = new JSONObject();
  71. for (int i = 0; i < protThresholdList.size(); i++) {
  72. if (protThresholdList.get(i).getProtType()==1) {
  73. jsonObject.put("in", protThresholdList.get(i));
  74. } else if (protThresholdList.get(i).getProtType()==2) {
  75. jsonObject.put("deal", protThresholdList.get(i));
  76. } else if (protThresholdList.get(i).getProtType()==3) {
  77. jsonObject.put("out", protThresholdList.get(i));
  78. }
  79. }
  80. jsonObject.put("userIds",collect);
  81. return new Result(ResultCode.SUCCESS,jsonObject);
  82. }
  83. }
  84. @Override
  85. public Result add(HttpServletRequest httpServletRequest, List<ProtThreshold> protThresholds) {
  86. for (ProtThreshold protThreshold : protThresholds) {
  87. if (ObjectUtil.isNotEmpty(protThreshold)) {
  88. Integer farmId = protThreshold.getFarmId();
  89. String userIds = protThreshold.getUserIds();
  90. QueryWrapper<ProtThreshold> queryWrapper = new QueryWrapper<>();
  91. queryWrapper.eq("farm_id", protThreshold.getFarmId()).eq("prot_type",protThreshold.getProtType());
  92. ProtThreshold threshold = protThresholdMapper.selectOne(queryWrapper);
  93. //去掉属于该人物的id
  94. Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
  95. //下属id
  96. List<Integer> lowerLevelIds = sysAccountMultilevelMapper.getLowerLevel( userId,farmId+"");
  97. //上传id
  98. List<Integer> uoloadIds = new ArrayList<>();
  99. if (StringUtils.isNotBlank(userIds)){
  100. uoloadIds = Arrays.stream(userIds.split(",")).map(Integer::valueOf).collect(Collectors.toList());
  101. }
  102. //现有id
  103. String oldUserIds = threshold.getUserIds();
  104. List<Integer> oldUsersIds = new ArrayList<>();
  105. if (StringUtils.isNotBlank(oldUserIds)){
  106. oldUsersIds = Arrays.stream(oldUserIds.split(",")).map(Integer::valueOf).collect(Collectors.toList());
  107. }
  108. // 现有 -下属 + 上传
  109. oldUsersIds.removeAll(lowerLevelIds);
  110. oldUsersIds.addAll(uoloadIds);
  111. ProtThreshold protThreshold1 = new ProtThreshold();
  112. protThreshold1.setProtType(protThreshold.getProtType());
  113. protThreshold1.setFarmId(protThreshold.getFarmId());
  114. protThreshold1.setCod1(protThreshold.getCod1());
  115. protThreshold1.setCod2(protThreshold.getCod2());
  116. protThreshold1.setPh1(protThreshold.getPh1());
  117. protThreshold1.setPh2(protThreshold.getPh2());
  118. protThreshold1.setNh3n1(protThreshold.getNh3n1());
  119. protThreshold1.setNh3n2(protThreshold.getNh3n2());
  120. protThreshold1.setTp1(protThreshold.getTp1());
  121. protThreshold1.setTp2(protThreshold.getTp2());
  122. protThreshold1.setTn1(protThreshold.getTn1());
  123. protThreshold1.setTn2(protThreshold.getTn2());
  124. protThreshold1.setFlow1(protThreshold.getFlow1());
  125. protThreshold1.setFlow2(protThreshold.getFlow2());
  126. protThreshold1.setUserIds(oldUsersIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
  127. protThreshold1.setDataType(protThreshold.getDataType());
  128. if (Integer.parseInt(protThreshold.getCod2()) <= Integer.parseInt(protThreshold.getCod1())) {
  129. return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
  130. } else if (Integer.parseInt(protThreshold.getPh2()) <= Integer.parseInt(protThreshold.getPh1())) {
  131. return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
  132. } else if (Integer.parseInt(protThreshold.getNh3n2()) <= Integer.parseInt(protThreshold.getNh3n1())) {
  133. return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
  134. } else if (Integer.parseInt(protThreshold.getTp2()) <= Integer.parseInt(protThreshold.getTp1())) {
  135. return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
  136. } else if (Integer.parseInt(protThreshold.getTn2()) <= Integer.parseInt(protThreshold.getTn1())) {
  137. return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
  138. } else if (Integer.parseInt(protThreshold.getFlow2()) <= Integer.parseInt(protThreshold.getFlow1())) {
  139. return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
  140. } else {
  141. if (ObjectUtil.isEmpty(threshold)) {
  142. protThresholdMapper.insert(protThreshold1);
  143. } else {
  144. protThreshold1.setId(protThreshold.getId());
  145. protThresholdMapper.updateById(protThreshold1);
  146. }
  147. }
  148. }
  149. }
  150. return new Result(ResultCode.SUCCESS, "修改成功");
  151. }
  152. }