package com.gunshi.project.hsz.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; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gunshi.project.hsz.entity.so.GateHisPageSo; import com.gunshi.project.hsz.entity.vo.AttResBaseVo; import com.gunshi.project.hsz.entity.vo.GateStautsVo; import com.gunshi.project.hsz.entity.vo.GateValveOplogVo; import com.gunshi.project.hsz.mapper.GateValveRealMapper; import com.gunshi.project.hsz.mapper.StWaterRRealMapper; import com.gunshi.project.hsz.mapper.TyYearRainfallMapper; import com.gunshi.project.hsz.model.*; import com.gunshi.project.hsz.util.DataHandleUtil; import com.gunshi.project.hsz.util.DateUtil; import com.gunshi.project.hsz.util.ExcelUtil; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; 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.*; /** * 描述: 闸阀开关表 * author: xusan * date: 2024-07-08 17:30:37 */ @Service @Slf4j @Transactional(rollbackFor = Exception.class) public class GateValveRealService extends ServiceImpl { @Resource private com.gunshi.project.hsz.model.GateValveKeyAutoDao gateValveKeyAutoDao; @Resource private com.gunshi.project.hsz.model.GateValveOplogAutoDao gateValveOplogAutoDao; @Resource private ReservoirWaterService reservoirWaterService; @Resource private StWaterRRealMapper stWaterRRealMapper; @Resource private TyYearRainfallMapper tyYearRainfallMapper; public List gateStatusList() { List 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().eq("valve_code", valveCode)); oplog.setBeforeStatus(valveReal == null ? "-" : valveReal.getStatus()); gateValveOplogAutoDao.save(oplog); //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().eq("valve_code",valveCode)); // this.save(real); return "调节闸阀成功"; } public Page logPage(GateHisPageSo so) { return baseMapper.logPage(so.getPageSo().toPage(),so); } public GateValveOplog loginfo(String code) { if(StringUtils.isBlank(code)){ return null; } LambdaQueryWrapper qw = new LambdaQueryWrapper(); qw.eq(GateValveOplogVo::getValveCode,code); qw.orderByDesc(GateValveOplogVo::getTm).last("LIMIT 1"); List list = gateValveOplogAutoDao.list(); return list.isEmpty() ? null : list.get(0); } public void logExport(GateHisPageSo so, HttpServletResponse response) { List logList = baseMapper.logList(so); ExcelUtil.exportExcel(logList,"闸阀操作日志",GateValveOplogVo.class,response,"闸阀操作日志"); } public Map supplyTime(Integer year, Integer month) { Map map = new HashMap<>(); /** * 可供水量= 实时库容 - 死库容 * 小时水量= (输水管流量 + 放水管流量)*3600 * 可供水小时数 = 可供水量 * 10000/ 小时水量 * 可供水天数 = 可供水小时数换算为天数 */ List 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() == null ? new BigDecimal(0) : attResBaseVo.getDeadCap(); BigDecimal supplyV = nowCap.subtract(deadCap); if(year != null){ //计算预测来水量 BigDecimal predictV = calcPredictV(year,month,attResBaseVo.getWatShedArea()); supplyV = supplyV.add(predictV); } List 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 queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(TyYearRainfall::getYear,year) .eq(TyYearRainfall::getType,2) .ge(TyYearRainfall::getMonth,nowMonth) .le(TyYearRainfall::getMonth,month); List list = tyYearRainfallMapper.selectList(queryWrapper); //和当前月份相同的降雨量 Optional 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).divide(new BigDecimal(1000),2,RoundingMode.HALF_UP); return sum.multiply(watShedArea); } public BigDecimal predictWater(Integer year, Integer month) { List list = reservoirWaterService.list(); if(CollectionUtils.isEmpty(list)){ return new BigDecimal(0); } return calcPredictV(year,month,list.get(0).getWatShedArea()); } }