houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.iailab.module.data.http.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.iailab.module.data.common.enums.CommonConstant;
import com.iailab.framework.common.service.impl.BaseServiceImpl;
import com.iailab.module.data.common.utils.HttpsRequest;
import com.iailab.module.data.common.utils.PageUtils;
import com.iailab.module.data.common.utils.Query;
import com.iailab.module.data.http.dao.HttpTagDao;
import com.iailab.module.data.http.dto.*;
import com.iailab.module.data.http.entity.HttpApiEntity;
import com.iailab.module.data.http.entity.HttpTagEntity;
import com.iailab.module.data.http.service.HttpApiService;
import com.iailab.module.data.http.service.HttpTagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
 
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
 
/**
 * @author Houzhongjian
 * @Description
 * @createTime 2024年04月10日 17:35:00
 */
@Slf4j
@Service
public class HttpTagServiceImpl extends BaseServiceImpl<HttpTagDao, HttpTagEntity> implements HttpTagService {
 
    @Resource
    private HttpsRequest httpsRequest;
 
    @Resource
    private HttpApiService httpApiService;
 
 
    /**
     * 分页查询tag
     *
     * @param params
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String httpApiCode = (String) params.get("httpApiCode");
        String tagType = (String) params.get("tagType");
        String tagCode = (String) params.get("tagCode");
        String tagName = (String) params.get("tagName");
 
        IPage<HttpTagEntity> page = baseDao.selectPage(
                new Query<HttpTagEntity>().getPage(params),
                new QueryWrapper<HttpTagEntity>()
                        .like(StringUtils.isNotBlank(httpApiCode), "http_api_code", httpApiCode)
                        .like(StringUtils.isNotBlank(tagType), "tag_type", tagType)
                        .like(StringUtils.isNotBlank(tagCode), "tag_code", tagCode)
                        .like(StringUtils.isNotBlank(tagName), "tag_name", tagName)
                        .orderByDesc("create_time")
        );
        return new PageUtils(page);
    }
 
    @Override
    public List<HttpTagEntity> selectList(Map<String, Object> params) {
        String httpId = (String) params.get("httpId");
        HttpApiEntity httpApiEntity = httpApiService.selectById(httpId);
        return baseDao.selectList(new QueryWrapper<HttpTagEntity>()
                        .eq("http_api_code", httpApiEntity.getCode())
                        .orderByDesc("create_time"));
    }
 
    @Override
    public List<HttpTagEntity> getByCode(String code) {
        return baseDao.selectList(new QueryWrapper<HttpTagEntity>()
                .eq("http_api_code", code)
                .orderByDesc("create_time"));
    }
 
    @Override
    public List<String> getByTagType(String deviceId) {
        QueryWrapper<HttpTagEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_type", deviceId);
        List<HttpTagEntity> entityList = baseDao.selectList(queryWrapper);
        List<String> list = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(entityList)) {
            entityList.stream().forEach(entity -> {
                String tagCode = entity.getTagCode();
                list.add(tagCode);
            });
        }
        return list;
    }
 
    @Override
    public List<HttpTagDTO> list(Map<String, Object> params) {
        List<HttpTagDTO> list = baseDao.getList(params);
        return list;
    }
 
    @Override
    public Map<String, BigDecimal> getTagsValues(String httpApiCode, List<TagCommonCurrentDto> tags) {
        Map<String, BigDecimal> stringBigDecimalMap = new HashMap<>();
        if (CommonConstant.YEAR_PEI_HTTP_TAG.equals(httpApiCode)) {
            stringBigDecimalMap = yearPeiTagsValues(tags);
        } else if(CommonConstant.CURRENT_PERFORMANCE_HTTP_TAG.equals(httpApiCode)) {
            stringBigDecimalMap = currentPerformanceTagsValues(tags);
        } else if(CommonConstant.CURRENT_SALE_HTTP_TAG.equals(httpApiCode)) {
            stringBigDecimalMap = currentSaleTagsValues(tags);
        } else if(CommonConstant.PRD_TIME_DIST_HTTP_TAG.equals(httpApiCode)) {
            stringBigDecimalMap = prdTimeDistTagsValues(tags);
        }
        return stringBigDecimalMap;
    }
 
    private Map<String, BigDecimal> yearPeiTagsValues(List<TagCommonCurrentDto> tags) {
        Map<String, BigDecimal> result = new HashMap<>(tags.size());
        Calendar calendar = Calendar.getInstance();
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
//        String url = httpApiService.getByCode(API_CODE).getUrl();
//        String responseStr = httpsRequest.doGet(url, params,"utf-8", "");
        String url = "http://192.168.55.121/Sunny/Action/BdApi/Invoke?code=Prd.YearPEI";
        String responseStr = "{\"sta\":true,\"msg\":null,\"res\":{\"yearData\":{\"year\":2024,\"xxPlan\":21850000.00,\"xxPerformance\":6794492.00,\"zqPlan\":12500000.00,\"zqPerformance\":3149110.41},\"monthData\":[{\"month\":1,\"xxPlan\":1400000.00,\"xxPerformance\":2022725.00,\"zqPlan\":800000.00,\"zqPerformance\":955066.76},{\"month\":2,\"xxPlan\":1400000.00,\"xxPerformance\":1829671.00,\"zqPlan\":800000.00,\"zqPerformance\":809494.02},{\"month\":3,\"xxPlan\":1700000.00,\"xxPerformance\":2225923.00,\"zqPlan\":1000000.00,\"zqPerformance\":971406.22},{\"month\":4,\"xxPlan\":1620000.00,\"xxPerformance\":716173.00,\"zqPlan\":1000000.00,\"zqPerformance\":345324.75},{\"month\":5,\"xxPlan\":0.0,\"xxPerformance\":0.00,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":6,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":7,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":8,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":9,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":10,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":11,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0},{\"month\":12,\"xxPlan\":0.0,\"xxPerformance\":0.0,\"zqPlan\":0.0,\"zqPerformance\":0.0}]}}";
        if (StringUtils.isNotBlank(responseStr)) {
            TagYearPeiJsonDto yearPeiJsonDto = parseYearPeiDto(responseStr);
            tags.stream().forEach(
                    item -> {
                        String tagType = item.getTagType();
                        String tagCode = item.getTagCode().split(CommonConstant.UNDERLINE)[1];
                        TagYearPeiJsonDto.DATA res = yearPeiJsonDto.getRes();
                        TagYearPeiJsonDto.DATA.YEAR yearData = res.getYearData();
                        List<TagYearPeiJsonDto.DATA.MONTH> monthDataList = res.getMonthData();
                        TagYearPeiJsonDto.DATA.MONTH month = monthDataList.get(currentMonth - 1);
                        Class<?> yearPeiClass = res.getClass();
                        String name = yearPeiClass.getName();
                        try {
                            BigDecimal value;
                            Class<?> yearPeiTypeClass1 = Class.forName(name + CommonConstant.DOLLAR + tagType.toUpperCase());
                            Field field = yearPeiTypeClass1.getDeclaredField(tagCode);
                            field.setAccessible(true);
                            if(CommonConstant.YEAR.equals(tagType)){
                                value = new BigDecimal((String) field.get(yearData));
                                result.put(item.getTagCode(), value);
                            } else if(CommonConstant.MONTH.equals(tagType)){
                                value = new BigDecimal((String) field.get(month));
                                result.put(item.getTagCode(), value);
                            }
                        } catch (NoSuchFieldException e) {
                            log.info("没有找到tag" + item.getTagType() + ";" + item.getTagCode());
                        } catch (IllegalAccessException e) {
                            log.info("没有反射权限");
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException(e);
                        }
                    }
            );
        }
        return result;
    }
 
    private Map<String, BigDecimal> currentPerformanceTagsValues(List<TagCommonCurrentDto> tags) {
        Map<String, BigDecimal> result = new HashMap<>(tags.size());
        Map<String, String> params = new HashMap<>(2);
//        String url = httpApiService.getByCode(API_CODE).getUrl();
//        String responseStr = httpsRequest.doGet(url, params,"utf-8", "");
        String responseStr = "{\"sta\":true,\"msg\":null,\"res\":{\"today\":{\"xxPerformance\":30866.00,\"zqPerformance\":0.0},\"yesterday\":{\"xxPerformance\":65748.00,\"zqPerformance\":36263.20},\"currentMonth\":{\"xxPerformance\":716173.00,\"zqPerformance\":345324.75},\"currentYear\":{\"xxPerformance\":6794492.00,\"zqPerformance\":3149110.41}}}";
        if (StringUtils.isNotBlank(responseStr)) {
            TagCurrentPerformanceJsonDto currentPerformanceJsonDto = parseCurrentPerformanceDto(responseStr);
            tags.stream().forEach(
                    item -> {
                        String tagType = item.getTagType();
                        String tagCode = item.getTagCode().split(CommonConstant.UNDERLINE)[1];
                        Class<?> currentPerformanceClass = currentPerformanceJsonDto.getRes().getClass();
                        String name = currentPerformanceClass.getName();
                        try {
                            Field declaredField = currentPerformanceClass.getDeclaredField(tagType);
                            Class<?> typeClass = Class.forName(name + CommonConstant.DOLLAR + tagType.toUpperCase());
                            Field field = typeClass.getDeclaredField(tagCode);
                            field.setAccessible(true);
                            declaredField.setAccessible(true);
                            BigDecimal value = new BigDecimal((String) field.get(declaredField.get(currentPerformanceJsonDto.getRes())));
                            result.put(item.getTagCode(), value);
                        } catch (NoSuchFieldException e) {
                            log.info("没有找到tag" + item.getTagType() + ";" + item.getTagCode());
                        } catch (IllegalAccessException e) {
                            log.info("没有反射权限");
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException(e);
                        }
                    }
            );
        }
        return result;
    }
 
    private Map<String, BigDecimal> currentSaleTagsValues(List<TagCommonCurrentDto> tags) {
        Map<String, BigDecimal> result = new HashMap<>(tags.size());
        Map<String, String> params = new HashMap<>(2);
//        String url = httpApiService.getByCode(API_CODE).getUrl();
//        String responseStr = httpsRequest.doGet(url, params,"utf-8", "");
        String responseStr = "{\"sta\":true,\"msg\":null,\"res\":{\"today\":{\"groundsales\":0.0,\"medblock\":0.0,\"nubmeasure\":0.0,\"gangue\":0.0,\"reshipped\":0.0,\"trainTon\":0.0,\"trainCount\":0},\"yesterday\":{\"groundsales\":15696.00,\"medblock\":14757.00,\"nubmeasure\":0.00,\"gangue\":4384.00,\"reshipped\":2447.00,\"trainTon\":36263.20,\"trainCount\":10},\"currentMonth\":{\"groundsales\":0.0,\"medblock\":0.0,\"nubmeasure\":0.0,\"gangue\":0.0,\"reshipped\":0.0,\"trainTon\":0.0,\"trainCount\":0},\"currentYear\":{\"groundsales\":521638.00,\"medblock\":266372.00,\"nubmeasure\":0.00,\"gangue\":143475.00,\"reshipped\":139695.00,\"trainTon\":0.0,\"trainCount\":0}}}";
        if (StringUtils.isNotBlank(responseStr)) {
            TagCurrentSaleJsonDto currentPerformanceJsonDto = parseCurrentSaleDto(responseStr);
            tags.stream().forEach(
                    item -> {
                        String tagType = item.getTagType();
                        String tagCode = item.getTagCode().split(CommonConstant.UNDERLINE)[1];
                        Class<?> currentSaleClass = currentPerformanceJsonDto.getRes().getClass();
                        String name = currentSaleClass.getName();
                        try {
                            Field declaredField = currentSaleClass.getDeclaredField(tagType);
                            Class<?> typeClass = Class.forName(name + CommonConstant.DOLLAR + tagType.toUpperCase());
                            Field field = typeClass.getDeclaredField(tagCode);
                            field.setAccessible(true);
                            declaredField.setAccessible(true);
                            BigDecimal value = new BigDecimal((String) field.get(declaredField.get(currentPerformanceJsonDto.getRes())));
                            result.put(item.getTagCode(), value);
                        } catch (NoSuchFieldException e) {
                            log.info("没有找到tag" + item.getTagType() + ";" + item.getTagCode());
                        } catch (IllegalAccessException e) {
                            log.info("没有反射权限");
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException(e);
                        }
                    }
            );
        }
        return result;
    }
 
    private Map<String, BigDecimal> prdTimeDistTagsValues(List<TagCommonCurrentDto> tags) {
        Map<String, BigDecimal> result = new HashMap<>(tags.size());
        Map<String, String> params = new HashMap<>(2);
//        String url = httpApiService.getByCode(API_CODE).getUrl();
//        String responseStr = httpsRequest.doGet(url, params,"utf-8", "");
        String responseStr = "{\"sta\":true,\"msg\":null,\"res\":{\"xx\":{\"runMinutes\":1120.0,\"overhaulMinutes\":305.0,\"affectMinutes\":15.0},\"zq\":{\"runMinutes\":645.0,\"overhaulMinutes\":223.0,\"affectMinutes\":572.0}}}";
        if (StringUtils.isNotBlank(responseStr)) {
            TagPrdTimeDistJsonDto prdTimeDistJsonDto = parsePrdTimeDistDto(responseStr);
            tags.stream().forEach(
                    item -> {
                        String tagType = item.getTagType();
                        String tagCode = item.getTagCode().split(CommonConstant.UNDERLINE)[1];
                        Class<?> prdTimeDistClass = prdTimeDistJsonDto.getRes().getClass();
                        String name = prdTimeDistClass.getName();
                        try {
                            Field declaredField = prdTimeDistClass.getDeclaredField(tagType);
                            Class<?> typeClass = Class.forName(name + CommonConstant.DOLLAR + tagType.toUpperCase());
                            Field field = typeClass.getDeclaredField(tagCode);
                            field.setAccessible(true);
                            declaredField.setAccessible(true);
                            BigDecimal value = new BigDecimal((String) field.get(declaredField.get(prdTimeDistJsonDto.getRes())));
                            result.put(item.getTagCode(), value);
                        } catch (NoSuchFieldException e) {
                            log.info("没有找到tag" + item.getTagType() + ";" + item.getTagCode());
                        } catch (IllegalAccessException e) {
                            log.info("没有反射权限");
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException(e);
                        }
                    }
            );
        }
        return result;
    }
 
    private TagYearPeiJsonDto parseYearPeiDto(String responseStr) {
        TagYearPeiJsonDto result = new TagYearPeiJsonDto();
        if (!StringUtils.isEmpty(responseStr)) {
            JSONObject items = JSONObject.parseObject(responseStr);
            result = items.toJavaObject(TagYearPeiJsonDto.class);
        }
        return result;
    }
 
    private TagCurrentPerformanceJsonDto parseCurrentPerformanceDto(String responseStr) {
        TagCurrentPerformanceJsonDto result = new TagCurrentPerformanceJsonDto();
        if (!StringUtils.isEmpty(responseStr)) {
            JSONObject items = JSONObject.parseObject(responseStr);
            result = items.toJavaObject(TagCurrentPerformanceJsonDto.class);
        }
        return result;
    }
 
    private TagCurrentSaleJsonDto parseCurrentSaleDto(String responseStr) {
        TagCurrentSaleJsonDto result = new TagCurrentSaleJsonDto();
        if (!StringUtils.isEmpty(responseStr)) {
            JSONObject items = JSONObject.parseObject(responseStr);
            result = items.toJavaObject(TagCurrentSaleJsonDto.class);
        }
        return result;
    }
 
    private TagPrdTimeDistJsonDto parsePrdTimeDistDto(String responseStr) {
        TagPrdTimeDistJsonDto result = new TagPrdTimeDistJsonDto();
        if (!StringUtils.isEmpty(responseStr)) {
            JSONObject items = JSONObject.parseObject(responseStr);
            result = items.toJavaObject(TagPrdTimeDistJsonDto.class);
        }
        return result;
    }
}