package com.huimv.admin.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
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.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.EnvWarningThreshold;
import com.huimv.admin.entity.GasThreshold;
import com.huimv.admin.entity.ProtThreshold;
import com.huimv.admin.mapper.ProtThresholdMapper;
import com.huimv.admin.mapper.SysAccountMultilevelMapper;
import com.huimv.admin.service.IProtThresholdService;
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 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-14
*/
@Service
public class ProtThresholdServiceImpl extends ServiceImpl implements IProtThresholdService {
@Autowired
private ProtThresholdMapper protThresholdMapper;
@Autowired
SysAccountMultilevelMapper sysAccountMultilevelMapper;
@Override
public Result list(HttpServletRequest httpServletRequest, Map paramsMap) {
String farmId = paramsMap.get("farmId");
String protType = paramsMap.get("protType");
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("farm_id", farmId);
List protThresholdList = protThresholdMapper.selectList(queryWrapper);
Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
List userIds =sysAccountMultilevelMapper.getLowerLevel( userId,farmId);
List collect =new ArrayList<>();
if (ObjectUtil.isNotEmpty(protThresholdList) &&StringUtils.isNotBlank(protThresholdList.get(0).getUserIds())){
collect = Arrays.stream(protThresholdList.get(0).getUserIds().split(",")).map(Integer::valueOf).filter(userIds::contains).collect(Collectors.toList());
}
if (protThresholdList.size() == 0) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("COD1", 0);
jsonObject.put("COD2", 0);
jsonObject.put("PH1", 0);
jsonObject.put("PH2", 0);
jsonObject.put("NH3N1", 0);
jsonObject.put("NH3N2", 0);
jsonObject.put("TP1", 0);
jsonObject.put("TP2", 0);
jsonObject.put("TN1", 0);
jsonObject.put("TN2", 0);
jsonObject.put("FLOW1", 0);
jsonObject.put("FLOW2", 0);
jsonObject.put("userIds", 0);
jsonObject.put("dataType", 0);
return new Result(ResultCode.SUCCESS, jsonObject);
} else {
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < protThresholdList.size(); i++) {
if (protThresholdList.get(i).getProtType()==1) {
jsonObject.put("in", protThresholdList.get(i));
} else if (protThresholdList.get(i).getProtType()==2) {
jsonObject.put("deal", protThresholdList.get(i));
} else if (protThresholdList.get(i).getProtType()==3) {
jsonObject.put("out", protThresholdList.get(i));
}
}
jsonObject.put("userIds",collect);
return new Result(ResultCode.SUCCESS,jsonObject);
}
}
@Override
public Result add(HttpServletRequest httpServletRequest, List protThresholds) {
for (ProtThreshold protThreshold : protThresholds) {
if (ObjectUtil.isNotEmpty(protThreshold)) {
Integer farmId = protThreshold.getFarmId();
String userIds = protThreshold.getUserIds();
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("farm_id", protThreshold.getFarmId()).eq("prot_type",protThreshold.getProtType());
ProtThreshold threshold = protThresholdMapper.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 = threshold.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);
ProtThreshold protThreshold1 = new ProtThreshold();
protThreshold1.setProtType(protThreshold.getProtType());
protThreshold1.setFarmId(protThreshold.getFarmId());
protThreshold1.setCod1(protThreshold.getCod1());
protThreshold1.setCod2(protThreshold.getCod2());
protThreshold1.setPh1(protThreshold.getPh1());
protThreshold1.setPh2(protThreshold.getPh2());
protThreshold1.setNh3n1(protThreshold.getNh3n1());
protThreshold1.setNh3n2(protThreshold.getNh3n2());
protThreshold1.setTp1(protThreshold.getTp1());
protThreshold1.setTp2(protThreshold.getTp2());
protThreshold1.setTn1(protThreshold.getTn1());
protThreshold1.setTn2(protThreshold.getTn2());
protThreshold1.setFlow1(protThreshold.getFlow1());
protThreshold1.setFlow2(protThreshold.getFlow2());
protThreshold1.setUserIds(oldUsersIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
protThreshold1.setDataType(protThreshold.getDataType());
if (Integer.parseInt(protThreshold.getCod2()) <= Integer.parseInt(protThreshold.getCod1())) {
return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
} else if (Integer.parseInt(protThreshold.getPh2()) <= Integer.parseInt(protThreshold.getPh1())) {
return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
} else if (Integer.parseInt(protThreshold.getNh3n2()) <= Integer.parseInt(protThreshold.getNh3n1())) {
return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
} else if (Integer.parseInt(protThreshold.getTp2()) <= Integer.parseInt(protThreshold.getTp1())) {
return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
} else if (Integer.parseInt(protThreshold.getTn2()) <= Integer.parseInt(protThreshold.getTn1())) {
return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
} else if (Integer.parseInt(protThreshold.getFlow2()) <= Integer.parseInt(protThreshold.getFlow1())) {
return new Result(ResultCode.FAIL, "红色预警浓度不能低于橙色预警浓度");
} else {
if (ObjectUtil.isEmpty(threshold)) {
protThresholdMapper.insert(protThreshold1);
} else {
protThreshold1.setId(protThreshold.getId());
protThresholdMapper.updateById(protThreshold1);
}
}
}
}
return new Result(ResultCode.SUCCESS, "修改成功");
}
}