gunshi-project-ss/src/main/java/com/gunshi/project/xyt/service/GateValveRealService.java

176 lines
7.2 KiB
Java
Raw Normal View History

2024-07-08 17:47:02 +08:00
package com.gunshi.project.xyt.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
2024-07-08 17:47:02 +08:00
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gunshi.project.xyt.entity.so.GateHisPageSo;
import com.gunshi.project.xyt.entity.vo.AttResBaseVo;
import com.gunshi.project.xyt.entity.vo.GateStautsVo;
import com.gunshi.project.xyt.entity.vo.GateValveOplogVo;
2024-07-08 17:47:02 +08:00
import com.gunshi.project.xyt.mapper.GateValveRealMapper;
import com.gunshi.project.xyt.mapper.StWaterRRealMapper;
import com.gunshi.project.xyt.mapper.TyYearRainfallMapper;
import com.gunshi.project.xyt.model.*;
import com.gunshi.project.xyt.util.DataHandleUtil;
import com.gunshi.project.xyt.util.DateUtil;
import com.gunshi.project.xyt.util.ExcelUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
2024-07-08 17:47:02 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;
2024-07-08 17:47:02 +08:00
/**
* :
* author: xusan
* date: 2024-07-08 17:30:37
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class GateValveRealService extends ServiceImpl<GateValveRealMapper, GateValveReal>
{
@Resource
private com.gunshi.project.xyt.model.GateValveKeyAutoDao gateValveKeyAutoDao;
@Resource
private com.gunshi.project.xyt.model.GateValveOplogAutoDao gateValveOplogAutoDao;
@Resource
private ReservoirWaterService reservoirWaterService;
@Resource
private StWaterRRealMapper stWaterRRealMapper;
@Resource
private TyYearRainfallMapper tyYearRainfallMapper;
2024-07-08 17:47:02 +08:00
public List<GateStautsVo> gateStatusList() {
List<GateStautsVo> list = baseMapper.gateStatusList();
for(GateStautsVo vo : list){
if(vo.getTm() != null && DateUtil.hoursBetweenDate(vo.getTm(), new Date()) > 2){
vo.setFlag(1);
}
}
return list;
}
public BigDecimal realQ(String valveCode) {
return baseMapper.realQ(valveCode);
}
public String control(GateValveKey gateValveKey) {
//先判断密码是否正确
String valveCode = gateValveKey.getValveCode();
String key = gateValveKey.getKey();
GateValveKey valveKey = gateValveKeyAutoDao.getById(valveCode);
if(valveKey == null || !key.equals(valveKey.getKey())){
throw new IllegalArgumentException("密码不正确");
}
//生成闸阀操作日志
GateValveOplog oplog = new GateValveOplog();
oplog.setId(IdWorker.getId());
oplog.setStatus(gateValveKey.getStatus());
oplog.setOpContent("设置闸阀开度为"+gateValveKey.getStatus());
oplog.setValveCode(valveCode);
oplog.setTm(new Date());
oplog.setOpUserId(1L);
oplog.setOpUserName("胡兵");
GateValveReal valveReal = this.getOne(new QueryWrapper<GateValveReal>().eq("valve_code", valveCode));
oplog.setBeforeStatus(valveReal == null ? "-" : valveReal.getStatus());
gateValveOplogAutoDao.save(oplog);
2024-07-24 09:11:20 +08:00
//todo 给闸阀下发调节指令
// GateValveR gateValveR = new GateValveR();
// BeanUtils.copyProperties(gateValveKey,gateValveR);
// gateValveR.setTm(new Date());
// gateValveRMapper.insert(gateValveR);
//
// GateValveReal real = new GateValveReal();
// BeanUtils.copyProperties(gateValveKey,real);
// real.setTm(new Date());
// this.remove(new QueryWrapper<GateValveReal>().eq("valve_code",valveCode));
// this.save(real);
return "调节闸阀成功";
}
public Page<GateValveOplogVo> logPage(GateHisPageSo so) {
return baseMapper.logPage(so.getPageSo().toPage(),so);
}
public void logExport(GateHisPageSo so, HttpServletResponse response) {
List<GateValveOplogVo> logList = baseMapper.logList(so);
ExcelUtil.exportExcel(logList,"闸阀操作日志",GateValveOplogVo.class,response,"闸阀操作日志");
}
public Map<BigDecimal, String> supplyTime(Integer year, Integer month) {
Map<BigDecimal, String> map = new HashMap<>();
/**
* = -
* = ( + )*3600
2024-07-29 13:23:23 +08:00
* = * 10000/
* =
*/
List<AttResBaseVo> list = reservoirWaterService.list();
if(CollectionUtils.isEmpty(list)){
return map;
}
AttResBaseVo attResBaseVo = list.get(0);
BigDecimal nowCap = attResBaseVo.getNowCap() == null ? new BigDecimal(0) : attResBaseVo.getNowCap();
BigDecimal deadCap = attResBaseVo.getDeadCap();
BigDecimal supplyV = nowCap.subtract(deadCap);
if(year != null){
//计算预测来水量
BigDecimal predictV = calcPredictV(year,month,attResBaseVo.getWatShedArea());
supplyV = supplyV.add(predictV);
}
List<StWaterRReal> water = stWaterRRealMapper.listRelated();
//小时水量
BigDecimal hourQ = water.stream().map(StWaterRReal::getQ).reduce(BigDecimal.ZERO, BigDecimal::add).multiply(new BigDecimal(3600));
BigDecimal day = supplyV.multiply(new BigDecimal(10000)).divide(hourQ.multiply(new BigDecimal(24)),1, RoundingMode.HALF_UP);
long l = DataHandleUtil.BigDecimalIntegerPart(day);
String date = DateUtil.getPlusDate(new Date(), l);
map.put(day,date);
return map;
}
private BigDecimal calcPredictV(Integer year, Integer month,BigDecimal watShedArea) {
LocalDate now = LocalDate.now();
Integer nowMonth = now.getMonthValue();
LambdaQueryWrapper<TyYearRainfall> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(TyYearRainfall::getYear,year)
.eq(TyYearRainfall::getType,2)
.ge(TyYearRainfall::getMonth,nowMonth)
.le(TyYearRainfall::getMonth,month);
List<TyYearRainfall> list = tyYearRainfallMapper.selectList(queryWrapper);
//和当前月份相同的降雨量
Optional<TyYearRainfall> first = list.stream().filter(o -> nowMonth == o.getMonth()).findFirst();
BigDecimal drp = first.isPresent() ? first.get().getDrp() : new BigDecimal(0);
int dayOfMonth = now.getDayOfMonth();
int total = now.lengthOfMonth();
BigDecimal nowMonthDrp = new BigDecimal((total - dayOfMonth + 1)).multiply(drp).divide(new BigDecimal(total), 2, RoundingMode.HALF_UP);
//当前月到预测月份的降雨量
BigDecimal value = list.stream().filter(o -> nowMonth != o.getMonth()).map(TyYearRainfall::getDrp).reduce(BigDecimal.ZERO, BigDecimal::add);
//预测来水量 = 月降雨量和 * 水库坝址控制流域面积
BigDecimal sum = nowMonthDrp.add(value);
return sum.multiply(watShedArea);
}
2024-07-08 17:47:02 +08:00
}