GasThresholdServiceImpl.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.huimv.admin.service.impl;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  6. import com.huimv.admin.common.token.TokenSign;
  7. import com.huimv.admin.common.utils.ResultCode;
  8. import com.huimv.admin.entity.EnvWarningThreshold;
  9. import com.huimv.admin.entity.GasThreshold;
  10. import com.huimv.admin.mapper.GasThresholdMapper;
  11. import com.huimv.admin.mapper.SysAccountMultilevelMapper;
  12. import com.huimv.admin.service.IGasThresholdService;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import javax.servlet.http.HttpServletRequest;
  17. import com.huimv.admin.common.utils.Result;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.stream.Collectors;
  23. /**
  24. * <p>
  25. * 气体阈值 服务实现类
  26. * </p>
  27. *
  28. * @author author
  29. * @since 2023-02-21
  30. */
  31. @Service
  32. public class GasThresholdServiceImpl extends ServiceImpl<GasThresholdMapper, GasThreshold> implements IGasThresholdService {
  33. @Autowired
  34. private GasThresholdMapper gasThresholdMapper;
  35. @Autowired
  36. SysAccountMultilevelMapper sysAccountMultilevelMapper;
  37. @Override
  38. public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
  39. String farmId = paramsMap.get("farmId");
  40. Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
  41. List<Integer> userIds =sysAccountMultilevelMapper.getLowerLevel( userId,farmId);
  42. List<Integer> collect =new ArrayList<>();
  43. QueryWrapper<GasThreshold> queryWrapper = new QueryWrapper<>();
  44. queryWrapper.eq("farm_id", farmId);
  45. List<GasThreshold> gasThresholds = gasThresholdMapper.selectList(queryWrapper);
  46. String userIds1 = "";
  47. if (ObjectUtil.isNotEmpty(gasThresholds)){
  48. userIds1 = gasThresholds.get(0).getUserIds();
  49. }
  50. if (StringUtils.isNotBlank(userIds1)){
  51. collect = Arrays.stream(userIds1.split(",")).map(Integer::valueOf).filter(userIds::contains).collect(Collectors.toList());
  52. }
  53. JSONObject jsonObject = new JSONObject();
  54. if (gasThresholds.size() == 0) {
  55. jsonObject.put("NH3-N", 0);
  56. jsonObject.put("JLM", 0);
  57. jsonObject.put("ELHT", 0);
  58. jsonObject.put("EJEL", 0);
  59. jsonObject.put("H2S", 0);
  60. jsonObject.put("BYX", 0);
  61. jsonObject.put("CH3SH", 0);
  62. jsonObject.put("SJA", 0);
  63. jsonObject.put("CQ", 0);
  64. } else {
  65. for (GasThreshold gasThreshold : gasThresholds) {
  66. if (gasThreshold.getGasType() == 1) {
  67. jsonObject.put("pigpen", gasThreshold);
  68. }
  69. else if (gasThreshold.getGasType() == 2) {
  70. jsonObject.put("above", gasThreshold);
  71. }
  72. else if (gasThreshold.getGasType() == 3) {
  73. jsonObject.put("under", gasThreshold);
  74. }
  75. else if (gasThreshold.getGasType() == 4) {
  76. jsonObject.put("people", gasThreshold);
  77. }
  78. }
  79. jsonObject.put("userIds", collect);
  80. }
  81. return new Result(ResultCode.SUCCESS,jsonObject);
  82. }
  83. @Override
  84. public Result edit(HttpServletRequest httpServletRequest, List<GasThreshold> gasThresholds) {
  85. for (GasThreshold gasThreshold : gasThresholds) {
  86. if (ObjectUtil.isNotEmpty(gasThreshold)) {
  87. Integer farmId = gasThreshold.getFarmId();
  88. String userIds = gasThreshold.getUserIds();
  89. QueryWrapper<GasThreshold> queryWrapper = new QueryWrapper<>();
  90. queryWrapper.eq("farm_id", gasThreshold.getFarmId()).eq("gas_type", gasThreshold.getGasType());
  91. GasThreshold gasThreshold2 = gasThresholdMapper.selectOne(queryWrapper);
  92. //去掉属于该人物的id
  93. Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
  94. //下属id
  95. List<Integer> lowerLevelIds = sysAccountMultilevelMapper.getLowerLevel( userId,farmId+"");
  96. //上传id
  97. List<Integer> uoloadIds = new ArrayList<>();
  98. if (StringUtils.isNotBlank(userIds)){
  99. uoloadIds = Arrays.stream(userIds.split(",")).map(Integer::valueOf).collect(Collectors.toList());
  100. }
  101. //现有id
  102. String oldUserIds = gasThreshold2.getUserIds();
  103. List<Integer> oldUsersIds = new ArrayList<>();
  104. if (StringUtils.isNotBlank(oldUserIds)){
  105. oldUsersIds = Arrays.stream(oldUserIds.split(",")).map(Integer::valueOf).collect(Collectors.toList());
  106. }
  107. // 现有 -下属 + 上传
  108. oldUsersIds.removeAll(lowerLevelIds);
  109. oldUsersIds.addAll(uoloadIds);
  110. GasThreshold gasThreshold1 = new GasThreshold();
  111. gasThreshold1.setGasType(gasThreshold.getGasType());
  112. gasThreshold1.setNh3N(gasThreshold.getNh3N());
  113. gasThreshold1.setJlm(gasThreshold.getJlm());
  114. gasThreshold1.setElht(gasThreshold.getElht());
  115. gasThreshold1.setH2s(gasThreshold.getH2s());
  116. gasThreshold1.setByx(gasThreshold.getByx());
  117. gasThreshold1.setCh3sh(gasThreshold.getCh3sh());
  118. gasThreshold1.setSja(gasThreshold.getSja());
  119. gasThreshold1.setCq(gasThreshold.getCq());
  120. gasThreshold1.setUserIds(oldUsersIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
  121. gasThreshold1.setFarmId(farmId);
  122. if (ObjectUtil.isEmpty(gasThreshold2)) {
  123. gasThresholdMapper.insert(gasThreshold1);
  124. } else {
  125. gasThreshold1.setId(gasThreshold.getId());
  126. gasThresholdMapper.updateById(gasThreshold1);
  127. }
  128. }
  129. }
  130. return new Result(ResultCode.SUCCESS, "修改成功");
  131. }
  132. }