package com.huimv.admin.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.common.token.TokenSign;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.EnvWarningThreshold;
import com.huimv.admin.entity.GasThreshold;
import com.huimv.admin.mapper.GasThresholdMapper;
import com.huimv.admin.mapper.SysAccountMultilevelMapper;
import com.huimv.admin.service.IGasThresholdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import com.huimv.admin.common.utils.Result;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
* 气体阈值 服务实现类
*
*
* @author author
* @since 2023-02-21
*/
@Service
public class GasThresholdServiceImpl extends ServiceImpl implements IGasThresholdService {
@Autowired
private GasThresholdMapper gasThresholdMapper;
@Autowired
SysAccountMultilevelMapper sysAccountMultilevelMapper;
@Override
public Result list(HttpServletRequest httpServletRequest, Map paramsMap) {
String farmId = paramsMap.get("farmId");
Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
List userIds =sysAccountMultilevelMapper.getLowerLevel( userId,farmId);
List collect =new ArrayList<>();
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("farm_id", farmId);
List gasThresholds = gasThresholdMapper.selectList(queryWrapper);
String userIds1 = "";
if (ObjectUtil.isNotEmpty(gasThresholds)){
userIds1 = gasThresholds.get(0).getUserIds();
}
if (StringUtils.isNotBlank(userIds1)){
collect = Arrays.stream(userIds1.split(",")).map(Integer::valueOf).filter(userIds::contains).collect(Collectors.toList());
}
JSONObject jsonObject = new JSONObject();
if (gasThresholds.size() == 0) {
jsonObject.put("NH3-N", 0);
jsonObject.put("JLM", 0);
jsonObject.put("ELHT", 0);
jsonObject.put("EJEL", 0);
jsonObject.put("H2S", 0);
jsonObject.put("BYX", 0);
jsonObject.put("CH3SH", 0);
jsonObject.put("SJA", 0);
jsonObject.put("CQ", 0);
} else {
for (GasThreshold gasThreshold : gasThresholds) {
if (gasThreshold.getGasType() == 1) {
jsonObject.put("pigpen", gasThreshold);
}
else if (gasThreshold.getGasType() == 2) {
jsonObject.put("above", gasThreshold);
}
else if (gasThreshold.getGasType() == 3) {
jsonObject.put("under", gasThreshold);
}
else if (gasThreshold.getGasType() == 4) {
jsonObject.put("people", gasThreshold);
}
}
jsonObject.put("userIds", collect);
}
return new Result(ResultCode.SUCCESS,jsonObject);
}
@Override
public Result edit(HttpServletRequest httpServletRequest, List gasThresholds) {
for (GasThreshold gasThreshold : gasThresholds) {
if (ObjectUtil.isNotEmpty(gasThreshold)) {
Integer farmId = gasThreshold.getFarmId();
String userIds = gasThreshold.getUserIds();
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("farm_id", gasThreshold.getFarmId()).eq("gas_type", gasThreshold.getGasType());
GasThreshold gasThreshold2 = gasThresholdMapper.selectOne(queryWrapper);
//去掉属于该人物的id
Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
//下属id
List lowerLevelIds = sysAccountMultilevelMapper.getLowerLevel( userId,farmId+"");
//上传id
List uoloadIds = new ArrayList<>();
if (StringUtils.isNotBlank(userIds)){
uoloadIds = Arrays.stream(userIds.split(",")).map(Integer::valueOf).collect(Collectors.toList());
}
//现有id
String oldUserIds = gasThreshold2.getUserIds();
List oldUsersIds = new ArrayList<>();
if (StringUtils.isNotBlank(oldUserIds)){
oldUsersIds = Arrays.stream(oldUserIds.split(",")).map(Integer::valueOf).collect(Collectors.toList());
}
// 现有 -下属 + 上传
oldUsersIds.removeAll(lowerLevelIds);
oldUsersIds.addAll(uoloadIds);
GasThreshold gasThreshold1 = new GasThreshold();
gasThreshold1.setGasType(gasThreshold.getGasType());
gasThreshold1.setNh3N(gasThreshold.getNh3N());
gasThreshold1.setJlm(gasThreshold.getJlm());
gasThreshold1.setElht(gasThreshold.getElht());
gasThreshold1.setH2s(gasThreshold.getH2s());
gasThreshold1.setByx(gasThreshold.getByx());
gasThreshold1.setCh3sh(gasThreshold.getCh3sh());
gasThreshold1.setSja(gasThreshold.getSja());
gasThreshold1.setCq(gasThreshold.getCq());
gasThreshold1.setUserIds(oldUsersIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
gasThreshold1.setFarmId(farmId);
if (ObjectUtil.isEmpty(gasThreshold2)) {
gasThresholdMapper.insert(gasThreshold1);
} else {
gasThreshold1.setId(gasThreshold.getId());
gasThresholdMapper.updateById(gasThreshold1);
}
}
}
return new Result(ResultCode.SUCCESS, "修改成功");
}
}