package org.springnewfiber.dataadapter.ziguang.zs.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.Lists; 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.hp.dto.HpDto; import org.springnewfiber.dataadapter.ziguang.yl.dto.YlDto; 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 08:53 **/ //@Component @Slf4j @AllArgsConstructor public class ZsAction { private final BladeRedis bladeRedis; private final ZiGuangConfig ziGuangConfig; private final LoginAction loginAction; private static Map<String, Object> baseListPramMap = ImmutableMap.of("searchFilters", ImmutableMap.of("lgtd", "", "stnm", "", "stcd", "", "lttd", "")); private final RabbitTemplate rabbitTemplate; /** * 基本信息,列表 */ // @PostConstruct public void list() { HttpRequest request = loginAction.getPostAuthRequest(ziGuangConfig.getZs().getListUrl()); request.body(JSONObject.toJSONString(baseListPramMap)); HttpResponse response = request.execute(); log.info("渍水基础信息list:{}", response.body()); List<ZsDto> list = JSONObject.parseArray(response.body(), ZsDto.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.getZs().getRealUrl()); request.body(JSONObject.toJSONString(listByPramMap)); HttpResponse response = request.execute(); log.info("单个渍水实时数据:stcd:{},数据:{}", stcd, response.body()); List<ZsDto> list = JSONObject.parseArray(response.body(), ZsDto.class); if (CollectionUtil.isNotEmpty(list)) { Boolean flag = bladeRedis.exists("zs:" + stcd); List<ZsDto> newList = CollectionUtil.reverseNew(list); newList.forEach(i->{ ZsDto dto = new ZsDto(); if ((flag && (dto = bladeRedis.get("zs:" + stcd)) != null && dto.getTM().before(i.getTM())) || !flag) { log.info("渍水redis的数据:{},请求到的最新数据:{}", JSONObject.toJSONString(dto), JSONObject.toJSONString(i.getTM())); rabbitTemplate.convertAndSend(JSONObject.toJSONString(ZsDto.convert(i))); bladeRedis.set("zs:" + stcd, i); log.info("发送渍水的数据:{},数据:{}", stcd, JSONObject.toJSONString(ZsDto.convert(i))); return; } }); } }catch (Exception e){ log.error("渍水数据出错:stcd:{},Cause:{}", stcd, e.getCause()); } } }