package org.springnewfiber.dataadapter.ziguang.hp.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 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.hd.dto.HdDto; import org.springnewfiber.dataadapter.ziguang.hp.dto.HpDto; import org.springnewfiber.dataadapter.ziguang.zs.dto.ZsDto; import javax.annotation.PostConstruct; import java.util.List; import java.util.Map; /** * @program: newfiber-data-adapter * @description: * @author: djt * @create: 2022-01-19 19:26 **/ //@Component @Slf4j @AllArgsConstructor public class HpAction { private final BladeRedis bladeRedis; private final ZiGuangConfig ziGuangConfig; private final LoginAction loginAction; private static Map<String, Object> baseListPramMap = ImmutableMap.of("searchFilters", ImmutableMap.of("addvcd", "420113", "stnm", "", "stcd", "")); private final RabbitTemplate rabbitTemplate; /** * 基本信息,列表 */ // @PostConstruct public void list() { HttpRequest request = loginAction.getPostAuthRequest(ziGuangConfig.getHp().getListUrl()); request.body(JSONObject.toJSONString(baseListPramMap)); HttpResponse response = request.execute(); log.info("湖泊基础信息list:{}", response.body()); List<HpDto> list = JSONObject.parseArray(response.body(), HpDto.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, "datatime", DateUtil.today())); HttpRequest request = loginAction.getPostAuthRequest(ziGuangConfig.getHp().getRealUrl()); request.body(JSONObject.toJSONString(listByPramMap)); HttpResponse response = request.execute(); log.info("单个湖泊实时数据:stcd:{},数据:{}", stcd, response.body()); List<HpDto> list = JSONObject.parseArray(response.body(), HpDto.class); if (CollectionUtil.isNotEmpty(list)) { Boolean flag = bladeRedis.exists("hp:" + stcd); List<HpDto> newList = CollectionUtil.reverseNew(list); newList.forEach(i -> { HpDto dto = new HpDto(); if ((flag && (dto = bladeRedis.get("hp:" + stcd)) != null && dto.getTM().before(i.getTM())) || !flag) { log.info("湖泊redis的数据:{},请求到的最新数据:{}", JSONObject.toJSONString(dto), JSONObject.toJSONString(i.getTM())); rabbitTemplate.convertAndSend(JSONObject.toJSONString(HpDto.convert(i))); bladeRedis.set("hp:" + stcd, i); log.info("发送湖泊的数据:{},数据:{}", stcd, JSONObject.toJSONString(HpDto.convert(i))); return; } }); } }catch (Exception e){ log.error("请求湖泊错误:stcd:{},exception:{}",stcd,e.getCause()); } } }