cjl
2023-07-26 0ce061d9ed37a6c25fff70e4734fa8cc8747237c
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.BenchmarkWindConfig;
import com.moral.api.entity.Device;
import com.moral.api.entity.WindModel;
import com.moral.api.entity.WindModelConfig;
import com.moral.api.mapper.BenchmarkWindConfigMapper;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.WindModelConfigMapper;
import com.moral.api.mapper.WindModelMapper;
import com.moral.api.service.WindModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2022-08-10
 */
@Service
public class WindModelServiceImpl extends ServiceImpl<WindModelMapper, WindModel> implements WindModelService {
 
    @Autowired
    private BenchmarkWindConfigMapper benchmarkWindConfigMapper;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Autowired
    private WindModelMapper windModelMapper;
 
    @Autowired
    private DeviceMapper deviceMapper;
 
    @Autowired
    private WindModelConfigMapper windModelConfigMapper;
 
    @Override
    public void windFieldStatistics() {
        QueryWrapper<WindModelConfig> windModelConfigQueryWrapper = new QueryWrapper<>();
        windModelConfigQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
        List<WindModelConfig> windModelConfigs = windModelConfigMapper.selectList(windModelConfigQueryWrapper);
        List<Integer> orgIdList = windModelConfigs.stream().map(WindModelConfig::getOrganizationId).collect(Collectors.toList());
        QueryWrapper<BenchmarkWindConfig> benchmarkWindConfigQueryWrapper = new QueryWrapper<>();
        benchmarkWindConfigQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
        benchmarkWindConfigQueryWrapper.in("organization_id",orgIdList);
        List<Map<String, Object>> benchmarkWindConfigMaps = benchmarkWindConfigMapper.selectMaps(benchmarkWindConfigQueryWrapper);
        for (Map<String, Object> benchmarkWindConfig:benchmarkWindConfigMaps) {
            String benchmark = benchmarkWindConfig.get("benchmark").toString();
            String preservation = benchmarkWindConfig.get("preservation").toString();
            JSONObject benchmarkJson = JSONObject.parseObject(benchmark);
            JSONObject preservationJson = JSONObject.parseObject(preservation);
            Iterator benchmarkJsonIter = benchmarkJson.entrySet().iterator();
            Iterator preservationJsonIter = preservationJson.entrySet().iterator();
            JSONObject windModelJson = new JSONObject();
            while (benchmarkJsonIter.hasNext()) {
                Map.Entry entry = (Map.Entry) benchmarkJsonIter.next();
                if (redisTemplate.opsForHash().hasKey("data_second",entry.getValue())){
                    Object benchmarkObject = redisTemplate.opsForHash().get("data_second",entry.getValue());
                    Map benchmarkMap = JSONObject.parseObject(JSONObject.toJSONString(benchmarkObject), Map.class);
                    windModelJson.put(entry.getKey().toString(),benchmarkMap.get("a01008"));
                }
            }
            JSONObject windModelValue = new JSONObject();
            while (preservationJsonIter.hasNext()) {
                Map.Entry entry = (Map.Entry) preservationJsonIter.next();
                if (redisTemplate.opsForHash().hasKey("data_second",entry.getValue())){
                    Object preservationObject = redisTemplate.opsForHash().get("data_second",entry.getValue());
                    QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
                    deviceQueryWrapper.eq("is_delete", Constants.NOT_DELETE);
                    deviceQueryWrapper.eq("mac",entry.getValue());
                    Device device = deviceMapper.selectOne(deviceQueryWrapper);
                    Map preservationMap = JSONObject.parseObject(JSONObject.toJSONString(preservationObject), Map.class);
                    Object[] preservationArray = new Object[4];
                    preservationArray[0] = device.getLongitude();
                    preservationArray[1] = device.getLatitude();
                    preservationArray[2] = preservationMap.get("a01007");
                    preservationArray[3] = preservationMap.get("a01008");
                    windModelValue.put(entry.getKey().toString(),preservationArray);
                }
            }
            int dayOfMonth = DateUtils.getDay(new Date());
            int remainder = dayOfMonth % 15;
            String remainderStr = "";
            if (remainder<10){
                remainderStr = "0"+remainder;
            }else {
                remainderStr = remainder+"";
            }
            Map<String,Object> insertMap = new HashMap<>();
            insertMap.put("destinationTab",benchmarkWindConfig.get("destination_tab").toString()+remainderStr);
            insertMap.put("json",windModelJson.toJSONString());
            insertMap.put("value",windModelValue.toJSONString());
            windModelMapper.insert(insertMap);
        }
    }
}