guoshilong
2023-08-11 3c096d3cfd56a23e6794f30ccd150bfe19aa4ae8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cn.gistack.nky.service.impl;
 
import cn.gistack.common.utils.CommonUtil;
import cn.gistack.nky.resultpojo.HsybGetFuturePo;
import cn.gistack.nky.service.IHsybService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
@AllArgsConstructor
public class HsybServiceImpl implements IHsybService {
 
    private static String GET_FUTURE = "https://sk.hubeishuiyi.cn/hsybApi/api/fh-admin/skkr/getFuture";
 
    private static String AUTHORIZATION = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJBRE1JTiIsIlVzZXJJZCI6IjEiLCJzY29wZSI6WyJhbGwiXSwiVXNlclJlYWxOYW1lIjoi6LaF57qn566h55CG5ZGYIiwiVXNlclh6cWhkbSI6IjQyMDUiLCJleHAiOjI1MTk3NzIwMTAsImp0aSI6ImExNTcxYzk1LTBkNjMtNDkyYi1iOWEyLTE2ZTIzNTQ5ZTY1ZiIsImNsaWVudF9pZCI6InVzZXItc2VydmljZSJ9.ebNVZrw9LbhKaj2w6RR8b2wccQiDkhvBeq79SxxCK-yWiOlIFqBkotTN4TNJg8umcpyYvLILwvqXWRJhffEtgi25sX2y6MqLIWM4kMZ9d8ptdnSmTpBPhltSiQOM0KFa1kl5nSDCBwYOLn-pESJglam76cjpgZNoC88x3iNHacdiXDItY0rtY85HrQ26uyJu9UovKtmYmZRHsIbGMpDta5Q1p4vfaCIr-YUayDrCweZJiQDEEcOSpWJ7O7RMk3pRkX_4UmPHFzrOI2lMp1jQIhxnSTE7EVAz_4z8h8r46muSkpF54Ic4XSawHKqdSLHx8T05LB0MpvOzWMPS6c1uHA";
 
 
    @Override
    public List<List<String>> getFuture(String resCd) {
        HsybGetFuturePo hsybGetFuturePo = apiRequest(GET_FUTURE, resCd);
 
        if (hsybGetFuturePo != null && hsybGetFuturePo.getRespCode().equals("200")) {
            List<List<String>> data = hsybGetFuturePo.getData();
 
            List<List<String>> collect = filterPredictData(data);
            return collect;
        } else {
            return null;
        }
    }
 
    public HsybGetFuturePo apiRequest(String url, String jsonparams) {
 
//        Long param = Long.parseLong(jsonparams);
        System.out.println("请求参数:" + jsonparams);
        // 声明一个header变量
        HttpHeaders headers = new HttpHeaders();
        // 设置为json格式
        MediaType mediaType = MediaType.parseMediaType("application/json;charset=utf-8");
        headers.setContentType(mediaType);
        //设置请求头
        headers.set("Authorization", AUTHORIZATION);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
 
        HttpEntity<String> httpEntity = new HttpEntity(jsonparams, headers);
        RestTemplate template = new RestTemplate();
        try {
            HsybGetFuturePo hsybGetFuturePo = template.postForObject(url, httpEntity, HsybGetFuturePo.class);
            System.out.println(hsybGetFuturePo.toString());
            return hsybGetFuturePo;
        } catch (Exception e) {
            System.out.println(e);
        }
 
        return null;
    }
 
 
    /**
     * 对预测数据进行过滤
     *
     * @param data
     * @return
     */
    public List<List<String>> filterPredictData(List<List<String>> data) {
        String formatter = "yyyy-MM-dd";
        String today = DateUtil.format(new Date(), formatter);
 
        Long time = new Date().getTime() + 60 * 60 * 24 * 1000;
        Date tomorrowDate = new Date(time);
        String tomorrow = DateUtil.format(tomorrowDate, formatter);
 
        //过滤空值数据,过滤过去时间
        List<List<String>> collect = data.stream().filter(item -> item.get(0) != null).collect(Collectors.toList());
        Integer index = findIndex(collect, "/");
 
        //对0点时间添加日期
        if (index>-1){
            collect.get(index).set(0,tomorrow  + collect.get(index).get(0).substring(5));
        }
 
        for (int i = 0; i < collect.size(); i++) {
            if (i<index && index>-1){
                collect.get(i).set(0,today + " " + collect.get(i).get(0));
            }else if (i>index && index>-1){
                collect.get(i).set(0,tomorrow + " " + collect.get(i).get(0));
            }else if ( i!= index && index == -1){
                collect.get(i).set(0,today + " " + collect.get(i).get(0));
            }
        }
 
        //筛选出未来时间
        List<List<String>> filterData =  collect.stream().filter(item -> CommonUtil.strToDate(item.get(0)).after(DateUtil.now())).collect(Collectors.toList());
 
        //截取掉时间部分
        filterData.forEach(e->{
            e.set(0,e.get(0).split(" ")[0]);
        });
 
        //去除重复数据
        List<List<String>> collect1 = filterData.stream().distinct().collect(Collectors.toList());
 
        return collect1;
    }
 
    public Integer findIndex(List<List<String>> list,String reg){
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).get(0).indexOf(reg)>-1){
                return     i;
            }
        }
        return -1;
    }
}