guoshilong
2024-03-05 2f8eaa7c850445fde0cdca0e8ecddcb7e2a936a3
skjcmanager/skjcmanager-service/skjcmanager-nky/src/main/java/cn/gistack/nky/service/impl/HstPredictServiceImpl.java
@@ -1,90 +1,306 @@
package cn.gistack.nky.service.impl;
import cn.gistack.nky.entity.ArimaPredict;
import cn.gistack.nky.entity.HstPredict;
import cn.gistack.nky.mapper.HstPredictMapper;
import cn.gistack.nky.requestpojo.HstPredictReqPo;
import cn.gistack.nky.requestpojo.PredictWatersPo;
import cn.gistack.nky.requestpojo.QueryReqPo;
import cn.gistack.nky.resultpojo.DataResChildrenPo;
import cn.gistack.nky.resultpojo.NewSwFuturePo;
import cn.gistack.nky.service.IHstPredictService;
import cn.gistack.nky.service.IHsybService;
import cn.gistack.nky.service.INkyService;
import cn.gistack.nky.service.IZtApiService;
import cn.gistack.nky.vo.HstPredictVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@AllArgsConstructor
@Service
public class HstPredictServiceImpl extends ServiceImpl<HstPredictMapper, HstPredict> implements IHstPredictService {
   @Autowired
   private INkyService nkyService;
   @Autowired
   private IZtApiService ztApiService;
   @Autowired
   private IHsybService hsybService;
   private final INkyService nkyService;
   private final IZtApiService ztApiService;
   @Override
   public void getData(String type) {
      List<DataResChildrenPo> cdList = getCdList(type);
      List<DataResChildrenPo> collect = cdList.stream().filter(
//         item -> item.getRes_cd().equals("42011640018") || item.getRes_cd().equals("42128140006")
         item -> item.getRes_cd().equals("42011640018") || item.getRes_cd().equals("42130350046")|| item.getRes_cd().equals("42092250024")|| item.getRes_cd().equals("42022250039")|| item.getRes_cd().equals("42132150292")
      cdList.forEach(dataResChildrenPo -> {
      ).collect(Collectors.toList());
      collect.forEach(dataResChildrenPo -> {
         //循环请求预测结果
         QueryReqPo queryReqPo = new QueryReqPo(dataResChildrenPo,type);
         QueryReqPo queryReqPo = new QueryReqPo(dataResChildrenPo, type);
         //请求预测结果
         HstPredict hstPredict = nkyService.hstQuery(queryReqPo);
         if (hstPredict != null){
         if (hstPredict != null) {
            //存储预测结果至数据库
            save(hstPredict);
         }
      });
   }
//   private String resCd = "";
   @Override
   public void predictData(String type) {
      List<DataResChildrenPo> cdList = getCdList(type);
      cdList.forEach(dataResChildrenPo -> {
         HstPredictReqPo arimaPredictReqPo = new HstPredictReqPo(dataResChildrenPo, type);
         //循环预测请求
         nkyService.hstPredict(arimaPredictReqPo);
      //暂时筛选出数据
//      List<DataResChildrenPo> collect = cdList.stream().filter(
//         item -> item.getRes_cd().equals("42011640018") || item.getRes_cd().equals("42130350046")|| item.getRes_cd().equals("42092250024")|| item.getRes_cd().equals("42022250039")|| item.getRes_cd().equals("42132150292")
//      ).collect(Collectors.toList());
//      predict(collect,type);
      newPredict(cdList,type);
   }
   private void newPredict(List<DataResChildrenPo> collect, String type) {
      String resCd = "";
      //无数据直接返回
      if (collect.size() == 0){
         return;
      }
      List<NewSwFuturePo> future = new ArrayList<>();
      if (type.equals("1") || type.equals("2")){
         for (int i = 0; i < collect.size(); i++) {
            DataResChildrenPo dataResChildrenPo = collect.get(i);
            String currentResCd = dataResChildrenPo.getRes_cd();
            //第一个水库
            if (future.size() == 0 && resCd.equals("")){
               future = hsybService.getNewFuture(currentResCd);
               //当前水库
               resCd = currentResCd;
            } else if (!resCd.equals(currentResCd)) {
               //其他水库,获取新的预测数据
               future = hsybService.getNewFuture(currentResCd);
               //更换为新水库
               resCd = currentResCd;
            }
            //预测数据构造请求数据
            if (future != null && future.size() > 0) {
               List<PredictWatersPo> po = new ArrayList<>();
               future.forEach(item -> {
                  PredictWatersPo predictWatersPo = new PredictWatersPo();
                  //设置时间
                  predictWatersPo.setTime(item.getTime());
                  //设置预测水位
                  predictWatersPo.setValue(Double.parseDouble(item.getSw()));
                  po.add(predictWatersPo);
               });
               HstPredictReqPo arimaPredictReqPo = new HstPredictReqPo(dataResChildrenPo, type, po);
               //循环预测请求
               nkyService.hstPredict(arimaPredictReqPo);
            }
         }
      }else if (type.equals("3") || type.equals("4") || type.equals("5")){
         for (int i = 0; i < collect.size(); i++) {
            DataResChildrenPo dataResChildrenPo = collect.get(i);
            String currentResCd = dataResChildrenPo.getRes_cd();
            //第一个水库
            if (future.size() == 0 && resCd.equals("")){
               future = hsybService.getNewFuture(currentResCd);
               //当前水库
               resCd = currentResCd;
            } else if (!resCd.equals(currentResCd)) {
               //其他水库,获取新的预测数据
               future = hsybService.getNewFuture(currentResCd);
               //更换为新水库
               resCd = currentResCd;
            }
            //预测数据构造请求数据
            if (future != null && future.size() > 0) {
               List<PredictWatersPo> po = new ArrayList<>();
               future.forEach(item -> {
                  PredictWatersPo predictWatersPo = new PredictWatersPo();
                  //设置时间
                  predictWatersPo.setTime(item.getTime());
                  //设置预测水位
                  predictWatersPo.setValue(Double.parseDouble(item.getSw()));
                  po.add(predictWatersPo);
               });
               //循环预测请求(位移一起请求减少请求次数)
               HstPredictReqPo arimaPredictReqPoX = new HstPredictReqPo(dataResChildrenPo, "3", po);
               nkyService.hstPredict(arimaPredictReqPoX);
               HstPredictReqPo arimaPredictReqPoY = new HstPredictReqPo(dataResChildrenPo, "4", po);
               nkyService.hstPredict(arimaPredictReqPoY);
               HstPredictReqPo arimaPredictReqPoZ = new HstPredictReqPo(dataResChildrenPo, "5", po);
               nkyService.hstPredict(arimaPredictReqPoZ);
            }
         }
      }
   }
   private void predict(List<DataResChildrenPo> collect, String type) {
      String resCd = "";
      //无数据直接返回
      if (collect.size() == 0){
         return;
      }
      List<List<String>> future = new ArrayList<>();
      if (type.equals("1") || type.equals("2")){
         for (int i = 0; i < collect.size(); i++) {
            DataResChildrenPo dataResChildrenPo = collect.get(i);
            String currentResCd = dataResChildrenPo.getRes_cd();
            //第一个水库
            if (future.size() == 0 && resCd.equals("")){
               future = hsybService.getFuture(currentResCd);
               //当前水库
               resCd = currentResCd;
            } else if (!resCd.equals(currentResCd)) {
               //其他水库,获取新的预测数据
               future = hsybService.getFuture(currentResCd);
               //更换为新水库
               resCd = currentResCd;
            }
            //预测数据构造请求数据
            if (future != null && future.size() > 0) {
               List<PredictWatersPo> po = new ArrayList<>();
               future.forEach(item -> {
                  PredictWatersPo predictWatersPo = new PredictWatersPo();
                  //设置时间
                  predictWatersPo.setTime(item.get(0));
                  //设置预测水位
                  predictWatersPo.setValue(Double.parseDouble(item.get(2)));
                  po.add(predictWatersPo);
               });
               HstPredictReqPo arimaPredictReqPo = new HstPredictReqPo(dataResChildrenPo, type, po);
               //循环预测请求
               nkyService.hstPredict(arimaPredictReqPo);
            }
         }
      }else if (type.equals("3") || type.equals("4") || type.equals("5")){
         for (int i = 0; i < collect.size(); i++) {
            DataResChildrenPo dataResChildrenPo = collect.get(i);
            String currentResCd = dataResChildrenPo.getRes_cd();
            //第一个水库
            if (future.size() == 0 && resCd.equals("")){
               future = hsybService.getFuture(currentResCd);
               //当前水库
               resCd = currentResCd;
            } else if (!resCd.equals(currentResCd)) {
               //其他水库,获取新的预测数据
               future = hsybService.getFuture(currentResCd);
               //更换为新水库
               resCd = currentResCd;
            }
            //预测数据构造请求数据
            if (future != null && future.size() > 0) {
               List<PredictWatersPo> po = new ArrayList<>();
               future.forEach(item -> {
                  PredictWatersPo predictWatersPo = new PredictWatersPo();
                  //设置时间
                  predictWatersPo.setTime(item.get(0));
                  //设置预测水位
                  predictWatersPo.setValue(Double.parseDouble(item.get(2)));
                  po.add(predictWatersPo);
               });
               //循环预测请求(位移一起请求减少请求次数)
               HstPredictReqPo arimaPredictReqPoX = new HstPredictReqPo(dataResChildrenPo, "3", po);
               nkyService.hstPredict(arimaPredictReqPoX);
               HstPredictReqPo arimaPredictReqPoY = new HstPredictReqPo(dataResChildrenPo, "4", po);
               nkyService.hstPredict(arimaPredictReqPoY);
               HstPredictReqPo arimaPredictReqPoZ = new HstPredictReqPo(dataResChildrenPo, "5", po);
               nkyService.hstPredict(arimaPredictReqPoZ);
            }
         }
      }
   }
   @Override
   public List<HstPredictVO> getFutureData(String resCd, String type) {
      List<HstPredictVO> list = new ArrayList<>();
      if (type.equals("3")) {
         list = baseMapper.getFutureDataWy(resCd, type);
      } else {
         list = baseMapper.getFutureData(resCd, type);
      }
      list.forEach(hstPredict -> {
         if (hstPredict.getPredictList() != null) {
            hstPredict.setPredictList(JSON.parse(hstPredict.getPredictList().toString()));
         } else {
            hstPredict.setPredictList("[]");
         }
         hstPredict.setPreWaterList(JSON.parse(hstPredict.getPreWaterList().toString()));
      });
      return list;
   }
   @Override
   public boolean save(HstPredict entity) {
      if (ObjectUtil.isEmpty(entity.getCreateTime())){
      if (ObjectUtil.isEmpty(entity.getCreateTime())) {
         return false;
      }
      HstPredict params = new HstPredict();
      params.setDamId(entity.getDamId());
      params.setPointId(entity.getPointId());
      params.setType(entity.getType());
      params.setCreateTime(entity.getCreateTime());
      List<HstPredict> list = baseMapper.selectTodayData(entity);
      List<HstPredict> list = list(Condition.getQueryWrapper(params));
      if (list.size()>0){
      if (list.size() > 0) {
         //删除该数据,添加最新数据
//         baseMapper.deleteById(list.get(0));
//         return super.save(entity);
         return false;
      }else {
      } else {
         return super.save(entity);
      }
   }
   public  List<DataResChildrenPo> getCdList(String type){
   public List<DataResChildrenPo> getCdList(String type) {
      List<DataResChildrenPo> cdList = new ArrayList<>();
      if (type.equals("1")){
      if (type.equals("1")) {
         //获取渗压测点
         cdList = ztApiService.getSy();
      }else if (type.equals("2")){
      } else if (type.equals("2")) {
         //获取渗流测点
         return null;
      }else if (type.equals("3") || type.equals("4") || type.equals("5")){
         cdList = ztApiService.getSl();
      } else if (type.equals("3") || type.equals("4") || type.equals("5")) {
         //获取位移测点
         cdList = ztApiService.getWy();
      }else {
      } else {
         return null;
      }
      return cdList;
   }
}