package org.springnewfiber.dataadapter.ziguang.hd.action; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.ImmutableMap; 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.gq.dto.GqDto; import org.springnewfiber.dataadapter.ziguang.hd.dto.HdDto; import org.springnewfiber.dataadapter.ziguang.hp.dto.HpDto; import java.util.List; import java.util.Map; /** * @program: newfiber-data-adapter * @description: * @author: djt * @create: 2022-01-19 10:11 **/ //@Component @Slf4j @AllArgsConstructor public class HdAction { private final BladeRedis bladeRedis; private final ZiGuangConfig ziGuangConfig; private final LoginAction loginAction; private static Map<String, Object> baseListPramMap = ImmutableMap.of("searchFilters", ImmutableMap.of("rvnm", "")); private final RabbitTemplate rabbitTemplate; /** * 基本信息,列表 */ // @PostConstruct public void list() { HttpRequest request = loginAction.getPostAuthRequest(ziGuangConfig.getHd().getListUrl()); request.body(JSONObject.toJSONString(baseListPramMap)); HttpResponse response = request.execute(); log.info("河道基础信息list:{}", response.body()); List<HdDto> list = JSONObject.parseArray(response.body(), HdDto.class); list.forEach(i -> { realTimeData(i.getSTCD()); }); } /** * 获取实时数据 * * @param stcd */ public void realTimeData(String stcd) { try { Map<String, Object> listByPramMap = ImmutableMap.of("searchFilters", ImmutableMap.of("stcd", stcd)); HttpRequest request = loginAction.getPostAuthRequest(ziGuangConfig.getHd().getRealUrl()); request.body(JSONObject.toJSONString(listByPramMap)); HttpResponse response = request.execute(); log.info("单个河道实时数据:stcd:{},数据:{}", stcd, response.body()); List<HdDto> list = JSONObject.parseArray(response.body(), HdDto.class); if (CollectionUtil.isNotEmpty(list)) { Boolean flag = bladeRedis.exists("hd:" + stcd); List<HdDto> newList = CollectionUtil.reverseNew(list); newList.forEach(i -> { HdDto dto = new HdDto(); if ((flag && (dto = bladeRedis.get("hd:" + stcd)) != null && dto.getTM().before(i.getTM())) || !flag) { log.info("河道redis的数据:{},请求到的最新数据:{}", JSONObject.toJSONString(dto), JSONObject.toJSONString(i.getTM())); rabbitTemplate.convertAndSend(JSONObject.toJSONString(HdDto.convert(i))); bladeRedis.set("hd:" + stcd,i); log.info("发送河道的数据:{},数据:{}", stcd, JSONObject.toJSONString(HdDto.convert(i))); return; } }); } }catch (Exception e){ log.error("请求河道的数据错误:stcd:{},exception:{}",stcd,e.getCause()); } } }