package org.springnewfiber.dataadapter.ziguang.yl.action; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Component; import org.springnewfiber.dataadapter.config.BladeRedis; import org.springnewfiber.dataadapter.ziguang.common.LoginAction; import org.springnewfiber.dataadapter.ziguang.common.ZiGuangConfig; import org.springnewfiber.dataadapter.ziguang.yl.dto.YlDto; import org.springnewfiber.dataadapter.ziguang.yl.util.RainfallAccumulationUtil; import java.math.BigDecimal; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /** * @program: newfiber-data-adapter * @description: * @author: djt * @create: 2022-01-20 17:09 **/ //@Component @Slf4j @AllArgsConstructor public class YlAction { private final BladeRedis bladeRedis; private final ZiGuangConfig ziGuangConfig; private final LoginAction loginAction; private static Map<String, Object> baseListPramMap; private final RabbitTemplate rabbitTemplate; private final RainfallAccumulationUtil rainfallAccumulationUtil = new RainfallAccumulationUtil(); { baseListPramMap = Maps.newHashMap(); Map<String, Object> pramChildMap = Maps.newHashMap(); pramChildMap.put("daydrp", ""); pramChildMap.put("drp", "1"); pramChildMap.put("stcd", ""); pramChildMap.put("stnm", ""); pramChildMap.put("type", ""); pramChildMap.put("addvcd", "420113"); baseListPramMap.put("searchFilters", pramChildMap); } /** * 基本信息,列表 */ // @PostConstruct //todo 雨量不知道怎么接 public void list() { HttpRequest request = loginAction.getPostAuthRequest(ziGuangConfig.getYl().getListUrl()); request.body(JSONObject.toJSONString(baseListPramMap)); HttpResponse response = request.execute(); log.info("雨量基础信息list:{}", response.body()); List<YlDto> list = JSONObject.parseArray(response.body(), YlDto.class); list.forEach(i -> { realTimeData(i); }); } // private void realTimeHourData(YlDto realYl) { // Map<String, Object> childPramMap = Maps.newHashMap(); // childPramMap.put("stcd", realYl.getSTCD()); // childPramMap.put("addvcd", "420113"); // childPramMap.put("drp", "1"); // childPramMap.put("daydrp", ""); // childPramMap.put("type", ""); // Map<String, Object> listByPramMap = ImmutableMap.of("searchFilters", childPramMap); // HttpRequest request = loginAction.getPostAuthRequest(ziGuangConfig.getYl().getOneHourUrl()); // request.body(JSONObject.toJSONString(listByPramMap)); // HttpResponse response = request.execute(); // log.info("单个雨量小时数据:stcd:{},数据:{}", realYl.getSTCD(), response.body()); // List<YlDto> list = JSONObject.parseArray(response.body(), YlDto.class); // if (CollectionUtil.isNotEmpty(list)) { // Boolean flag = bladeRedis.exists("yl:" + realYl.getSTCD()); // List<YlDto> newList = CollectionUtil.reverseNew(list); // newList.forEach(i -> { // YlDto dto = new YlDto(); // if ((flag && (dto = bladeRedis.get("yl:" + realYl.getSTCD())) != null && dto.getTM().before(i.getTM())) || !flag) { // i.setCountPt(dto.getCountPt().add(new BigDecimal(i.getDRP()))); // // } else if () { // // } // }); // } // } // private void checkTime(YlDto redisDto, YlDto nowDto) { // Date redisDtoTM = redisDto.getTM(); // Date nowDtoTm = nowDto.getTM(); // Calendar redisC= Calendar.getInstance(); // redisC.setTime(redisDtoTM); // Calendar nowDtoC= Calendar.getInstance(); // nowDtoC.setTime(nowDtoTm); // if (redisDtoTM.before(nowDtoTm)&&redisC.get) { // // } // } /** * 获取实时数据 * * @param stcd */ public void realTimeData(YlDto realYl) { try { Map<String, Object> listByPramMap = ImmutableMap.of("searchFilters", ImmutableMap.of("stcd", realYl.getSTCD(), "datatime", DateUtil.today())); HttpRequest request = loginAction.getPostAuthRequest(ziGuangConfig.getYl().getRealUrl()); request.body(JSONObject.toJSONString(listByPramMap)); HttpResponse response = request.execute(); log.info("单个雨量实时数据:stcd:{},数据:{}", realYl.getSTCD(), response.body()); List<YlDto> list = JSONObject.parseArray(response.body(), YlDto.class); if (CollectionUtil.isNotEmpty(list)) { Boolean flag = bladeRedis.exists("yl:" + realYl.getSTCD()); List<YlDto> newList = CollectionUtil.reverseNew(list); newList.forEach(i -> { YlDto dto = new YlDto(); if ((flag && (dto = bladeRedis.get("yl:" + realYl.getSTCD())) != null && dto.getTM().before(i.getTM())) || !flag) { log.info("雨量redis的数据:{},请求到的最新数据:{}", JSONObject.toJSONString(dto), JSONObject.toJSONString(i.getTM())); // getData(stcd, i.getTM(), i); Future future = rainfallAccumulationUtil.submit(i, i.getSTCD()); //自己累计雨量 nb-plus i.setCountPt(dto.getCountPt().add(new BigDecimal(i.getDRP()))); i.setDAYDRP(realYl.getDAYDRP()); try { future.get(60, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { log.error("累计错误:stcd:{},Cause:{}", realYl.getSTCD(), e.getCause()); } rabbitTemplate.convertAndSend(JSONObject.toJSONString(YlDto.convert(i))); bladeRedis.set("yl:" + realYl.getSTCD(), i); log.info("发送雨量的数据:{},数据:{}", realYl.getSTCD(), JSONObject.toJSONString(YlDto.convert(i))); return; } }); } } catch (Exception e) { log.error("雨量数据出错:stcd:{},Cause:{}", realYl.getSTCD(), e.getCause()); } } @Deprecated private void getData(String stcd, Date TM, YlDto dto) { Float one = gethourData(stcd, TM, ziGuangConfig.getYl().getOneHourUrl()); Float Three = gethourData(stcd, TM, ziGuangConfig.getYl().getThreeHourUrl()); Float Six = gethourData(stcd, TM, ziGuangConfig.getYl().getSixHourUrl()); Float Day = gethourData(stcd, TM, ziGuangConfig.getYl().getDayUrl()); dto.setP1(one); dto.setP3(Three); dto.setP6(Six); dto.setP24(Day); //todo 历史每个月查询 } @Deprecated private Float gethourData(String stcd, Date TM, String url) { Map<String, Object> childPramMap = Maps.newHashMap(); childPramMap.put("stcd", stcd); childPramMap.put("addvcd", "420113"); childPramMap.put("drp", "1"); childPramMap.put("daydrp", ""); childPramMap.put("type", ""); Map<String, Object> listByPramMap = ImmutableMap.of("searchFilters", childPramMap); HttpRequest request = loginAction.getPostAuthRequest(url); request.body(JSONObject.toJSONString(listByPramMap)); HttpResponse response = request.execute(); log.info("单个雨量小时数据:stcd:{},数据:{}", stcd, response.body()); List<YlDto> list = JSONObject.parseArray(response.body(), YlDto.class); if (CollectionUtil.isNotEmpty(list) && list.get(0).getTM().compareTo(TM) == 0) { log.info("分钟级别时间:{},小时级别时间:{}", list.get(0).getTM(), TM); return list.get(0).getDRP(); } return null; } }