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();
|
}
|
}
|