package org.springnewfiber.dataadapter.ziguang.gq.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.gq.dto.GqDto; import org.springnewfiber.dataadapter.ziguang.hd.dto.HdDto; import javax.annotation.PostConstruct; import java.util.List; import java.util.Map; /** * @program: newfiber-data-adapter * @description: * @author: djt * @create: 2022-01-20 13:56 **/ //@Component @Slf4j @AllArgsConstructor public class GqAction { 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", "", "bz", "")); private final RabbitTemplate rabbitTemplate; /** * 基本信息,列表 */ // @PostConstruct public void list() { HttpRequest request = loginAction.getPostAuthRequest(ziGuangConfig.getGq().getListUrl()); request.body(JSONObject.toJSONString(baseListPramMap)); HttpResponse response = request.execute(); log.info("港渠基础信息list:{}", response.body()); List<GqDto> list = JSONObject.parseArray(response.body(), GqDto.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.getGq().getRealUrl()); request.body(JSONObject.toJSONString(listByPramMap)); HttpResponse response = request.execute(); log.info("港渠单个实时数据,stcd:{},数据:{}", stcd, response.body()); List<GqDto> list = JSONObject.parseArray(response.body(), GqDto.class); if (CollectionUtil.isNotEmpty(list)) { Boolean flag = bladeRedis.exists("gq:" + stcd); List<GqDto> newList = CollectionUtil.reverseNew(list); newList.forEach(i -> { GqDto dto = new GqDto(); if ((flag && (dto = bladeRedis.get("gq:" + stcd)) != null && dto.getTM().before(i.getTM())) || !flag) { log.info("港渠redis的数据:{},请求到的最新数据:{}", JSONObject.toJSONString(dto), JSONObject.toJSONString(i.getTM())); rabbitTemplate.convertAndSend(JSONObject.toJSONString(GqDto.convert(i))); bladeRedis.set("gq:" + stcd, i); log.info("发送港渠的数据:{},数据:{}", stcd, JSONObject.toJSONString(GqDto.convert(i))); return; } }); } }catch (Exception e){ log.error("请求港渠的数据错误:stcd:{},exception:{}",stcd,e.getCause()); } } }