yuan
3 天以前 7fc5bc0c6f92d65099397690128cbf218935972d
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
package com.ruoyi.http.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.http.config.TqdianbiaoConfig;
import com.ruoyi.http.mapper.TqdianbiaoMeterMapper;
import com.ruoyi.http.pojo.TqdianbiaoMeter;
import com.ruoyi.http.service.TqdianbiaoMeterSyncService;
import com.ruoyi.http.service.TqdianbiaoSyncLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.time.LocalDateTime;
 
@Service
@Slf4j
@RequiredArgsConstructor
public class TqdianbiaoMeterSyncServiceImpl implements TqdianbiaoMeterSyncService {
 
    private final TqdianbiaoConfig config;
    private final TqdianbiaoMeterMapper meterMapper;
    private final TqdianbiaoSyncLogService syncLogService;
 
    @Override
    public int syncMeters() {
        String syncType = "meter";
        try {
            String url = config.getBaseUrl() + "/Api/Meter";
            String param = "auth=" + config.getAuth();
            String raw = HttpUtils.sendGet(url, param);
            JSONArray list = parseList(raw);
            LocalDateTime now = LocalDateTime.now();
            int count = 0;
            for (int i = 0; i < list.size(); i++) {
                JSONObject item = list.getJSONObject(i);
                if (!"0".equals(String.valueOf(item.get("device_type")))) {
                    continue;
                }
                Long meterId = item.getLong("id");
                if (meterId == null) {
                    continue;
                }
                TqdianbiaoMeter entity = meterMapper.selectOne(
                        Wrappers.<TqdianbiaoMeter>lambdaQuery()
                                .eq(TqdianbiaoMeter::getMeterId, meterId)
                                .last("LIMIT 1"));
                boolean isNew = entity == null;
                if (isNew) {
                    entity = new TqdianbiaoMeter();
                    entity.setMeterId(meterId);
                    entity.setSource("sync");
                }
                String savedName = entity.getMeterName();
                String savedAddress = entity.getAddress();
                String savedDesc = entity.getDescription();
                String savedRelay = entity.getRelayState();
 
                entity.setCollectorId(item.getString("cid"));
                entity.setCollectorNo(item.getString("collectorid"));
                entity.setMeterType(item.getString("type"));
                entity.setCsq(item.getInteger("csq"));
                entity.setRate(item.getInteger("rate"));
                entity.setSyncTime(now);
 
                if (isNew) {
                    entity.setAddress(item.getString("address"));
                    entity.setRelayState(item.getString("relay_state"));
                    entity.setDescription(item.getString("description"));
                    entity.setMeterName(StringUtils.hasText(savedName) ? savedName : item.getString("address"));
                } else {
                    entity.setMeterName(savedName);
                    entity.setAddress(savedAddress);
                    entity.setDescription(savedDesc);
                    entity.setRelayState(savedRelay);
                }
                if (entity.getId() == null) {
                    meterMapper.insert(entity);
                } else {
                    meterMapper.updateById(entity);
                }
                count++;
            }
            syncLogService.logSuccess(syncType, null, null, count);
            return count;
        } catch (Exception e) {
            log.error("电表同步失败", e);
            syncLogService.logFailure(syncType, null, null, e.getMessage());
            throw e;
        }
    }
 
    private JSONArray parseList(String raw) {
        Object parsed = JSON.parse(raw);
        if (parsed instanceof JSONArray) {
            return (JSONArray) parsed;
        }
        JSONObject root = (JSONObject) parsed;
        if (root.getIntValue("status") != 1) {
            throw new IllegalStateException("电表接口返回异常: " + raw);
        }
        Object data = root.get("data");
        if (data instanceof JSONArray) {
            return (JSONArray) data;
        }
        return new JSONArray();
    }
}