|
@@ -5,7 +5,11 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
+import com.haha.common.constant.CommonConstants;
|
|
|
|
|
+import com.haha.common.constant.DeviceConstants;
|
|
|
import com.haha.common.constant.RedisConstants;
|
|
import com.haha.common.constant.RedisConstants;
|
|
|
|
|
+import com.haha.common.constant.SyncConstants;
|
|
|
|
|
+import com.haha.common.enums.DeviceOnlineStatus;
|
|
|
import com.haha.common.exception.BusinessException;
|
|
import com.haha.common.exception.BusinessException;
|
|
|
import com.haha.entity.Device;
|
|
import com.haha.entity.Device;
|
|
|
import com.haha.entity.Product;
|
|
import com.haha.entity.Product;
|
|
@@ -46,14 +50,6 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
private final SyncLogMapper syncLogMapper;
|
|
private final SyncLogMapper syncLogMapper;
|
|
|
private final StringRedisTemplate stringRedisTemplate;
|
|
private final StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 同步状态常量
|
|
|
|
|
- */
|
|
|
|
|
- private static final int STATUS_PENDING = 0; // 待同步
|
|
|
|
|
- private static final int STATUS_SYNCING = 1; // 同步中
|
|
|
|
|
- private static final int STATUS_SUCCESS = 2; // 同步成功
|
|
|
|
|
- private static final int STATUS_FAILED = 3; // 同步失败
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 同步类型
|
|
* 同步类型
|
|
|
*/
|
|
*/
|
|
@@ -68,7 +64,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
// 1. 创建同步记录
|
|
// 1. 创建同步记录
|
|
|
SyncRecord record = new SyncRecord();
|
|
SyncRecord record = new SyncRecord();
|
|
|
record.setSyncType(SYNC_TYPE_DEVICE);
|
|
record.setSyncType(SYNC_TYPE_DEVICE);
|
|
|
- record.setStatus(STATUS_SYNCING);
|
|
|
|
|
|
|
+ record.setStatus(SyncConstants.STATUS_SYNCING);
|
|
|
record.setStartTime(LocalDateTime.now());
|
|
record.setStartTime(LocalDateTime.now());
|
|
|
record.setOperatorId(operatorId);
|
|
record.setOperatorId(operatorId);
|
|
|
record.setOperatorName(operatorName);
|
|
record.setOperatorName(operatorName);
|
|
@@ -108,7 +104,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
// 4. 更新同步记录
|
|
// 4. 更新同步记录
|
|
|
record.setSuccessCount(successCount);
|
|
record.setSuccessCount(successCount);
|
|
|
record.setFailCount(failCount);
|
|
record.setFailCount(failCount);
|
|
|
- record.setStatus(failCount == 0 ? STATUS_SUCCESS : (successCount > 0 ? STATUS_SUCCESS : STATUS_FAILED));
|
|
|
|
|
|
|
+ record.setStatus(failCount == 0 ? SyncConstants.STATUS_SYNCED : (successCount > 0 ? SyncConstants.STATUS_SYNCED : SyncConstants.STATUS_SYNC_FAILED));
|
|
|
record.setEndTime(LocalDateTime.now());
|
|
record.setEndTime(LocalDateTime.now());
|
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
|
if (errorMsg.length() > 0) {
|
|
if (errorMsg.length() > 0) {
|
|
@@ -120,14 +116,14 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
|
|
|
|
|
} catch (HahaException e) {
|
|
} catch (HahaException e) {
|
|
|
log.error("调用哈哈平台API失败", e);
|
|
log.error("调用哈哈平台API失败", e);
|
|
|
- record.setStatus(STATUS_FAILED);
|
|
|
|
|
|
|
+ record.setStatus(SyncConstants.STATUS_SYNC_FAILED);
|
|
|
record.setEndTime(LocalDateTime.now());
|
|
record.setEndTime(LocalDateTime.now());
|
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
|
record.setErrorMessage("调用哈哈平台API失败: " + e.getMessage());
|
|
record.setErrorMessage("调用哈哈平台API失败: " + e.getMessage());
|
|
|
updateById(record);
|
|
updateById(record);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("设备数据同步异常", e);
|
|
log.error("设备数据同步异常", e);
|
|
|
- record.setStatus(STATUS_FAILED);
|
|
|
|
|
|
|
+ record.setStatus(SyncConstants.STATUS_SYNC_FAILED);
|
|
|
record.setEndTime(LocalDateTime.now());
|
|
record.setEndTime(LocalDateTime.now());
|
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
|
record.setErrorMessage("同步异常: " + e.getMessage());
|
|
record.setErrorMessage("同步异常: " + e.getMessage());
|
|
@@ -150,7 +146,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
// 更新设备信息
|
|
// 更新设备信息
|
|
|
existDevice.setName(deviceInfo.getName());
|
|
existDevice.setName(deviceInfo.getName());
|
|
|
existDevice.setAddress(deviceInfo.getAddress());
|
|
existDevice.setAddress(deviceInfo.getAddress());
|
|
|
- existDevice.setStatus("1".equals(deviceInfo.getStatus()) ? 1 : 0);
|
|
|
|
|
|
|
+ existDevice.setStatus("1".equals(deviceInfo.getStatus()) ? DeviceConstants.STATUS_ONLINE : DeviceOnlineStatus.OFFLINE.getCode());
|
|
|
deviceService.updateById(existDevice);
|
|
deviceService.updateById(existDevice);
|
|
|
|
|
|
|
|
// 记录成功日志
|
|
// 记录成功日志
|
|
@@ -162,7 +158,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
newDevice.setDeviceId(deviceInfo.getId());
|
|
newDevice.setDeviceId(deviceInfo.getId());
|
|
|
newDevice.setName(deviceInfo.getName());
|
|
newDevice.setName(deviceInfo.getName());
|
|
|
newDevice.setAddress(deviceInfo.getAddress());
|
|
newDevice.setAddress(deviceInfo.getAddress());
|
|
|
- newDevice.setStatus("1".equals(deviceInfo.getStatus()) ? 1 : 0);
|
|
|
|
|
|
|
+ newDevice.setStatus("1".equals(deviceInfo.getStatus()) ? DeviceConstants.STATUS_ONLINE : DeviceOnlineStatus.OFFLINE.getCode());
|
|
|
deviceService.save(newDevice);
|
|
deviceService.save(newDevice);
|
|
|
|
|
|
|
|
// 记录成功日志
|
|
// 记录成功日志
|
|
@@ -234,11 +230,11 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
stats.put("totalRecords", totalRecords);
|
|
stats.put("totalRecords", totalRecords);
|
|
|
|
|
|
|
|
// 成功次数
|
|
// 成功次数
|
|
|
- long successRecords = lambdaQuery().eq(SyncRecord::getStatus, STATUS_SUCCESS).count();
|
|
|
|
|
|
|
+ long successRecords = lambdaQuery().eq(SyncRecord::getStatus, SyncConstants.STATUS_SYNCED).count();
|
|
|
stats.put("successRecords", successRecords);
|
|
stats.put("successRecords", successRecords);
|
|
|
|
|
|
|
|
// 失败次数
|
|
// 失败次数
|
|
|
- long failedRecords = lambdaQuery().eq(SyncRecord::getStatus, STATUS_FAILED).count();
|
|
|
|
|
|
|
+ long failedRecords = lambdaQuery().eq(SyncRecord::getStatus, SyncConstants.STATUS_SYNC_FAILED).count();
|
|
|
stats.put("failedRecords", failedRecords);
|
|
stats.put("failedRecords", failedRecords);
|
|
|
|
|
|
|
|
// 设备同步统计
|
|
// 设备同步统计
|
|
@@ -263,7 +259,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
// 1. 创建同步记录
|
|
// 1. 创建同步记录
|
|
|
SyncRecord record = new SyncRecord();
|
|
SyncRecord record = new SyncRecord();
|
|
|
record.setSyncType(SYNC_TYPE_PRODUCT);
|
|
record.setSyncType(SYNC_TYPE_PRODUCT);
|
|
|
- record.setStatus(STATUS_SYNCING);
|
|
|
|
|
|
|
+ record.setStatus(SyncConstants.STATUS_SYNCING);
|
|
|
record.setStartTime(LocalDateTime.now());
|
|
record.setStartTime(LocalDateTime.now());
|
|
|
record.setOperatorId(operatorId);
|
|
record.setOperatorId(operatorId);
|
|
|
record.setOperatorName(operatorName);
|
|
record.setOperatorName(operatorName);
|
|
@@ -332,7 +328,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
record.setTotalCount(totalCount);
|
|
record.setTotalCount(totalCount);
|
|
|
record.setSuccessCount(successCount);
|
|
record.setSuccessCount(successCount);
|
|
|
record.setFailCount(failCount);
|
|
record.setFailCount(failCount);
|
|
|
- record.setStatus(failCount == 0 ? STATUS_SUCCESS : (successCount > 0 ? STATUS_SUCCESS : STATUS_FAILED));
|
|
|
|
|
|
|
+ record.setStatus(failCount == 0 ? SyncConstants.STATUS_SYNCED : (successCount > 0 ? SyncConstants.STATUS_SYNCED : SyncConstants.STATUS_SYNC_FAILED));
|
|
|
record.setEndTime(LocalDateTime.now());
|
|
record.setEndTime(LocalDateTime.now());
|
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
|
if (errorMsg.length() > 0) {
|
|
if (errorMsg.length() > 0) {
|
|
@@ -344,14 +340,14 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
|
|
|
|
|
} catch (HahaException e) {
|
|
} catch (HahaException e) {
|
|
|
log.error("调用哈哈平台API失败", e);
|
|
log.error("调用哈哈平台API失败", e);
|
|
|
- record.setStatus(STATUS_FAILED);
|
|
|
|
|
|
|
+ record.setStatus(SyncConstants.STATUS_SYNC_FAILED);
|
|
|
record.setEndTime(LocalDateTime.now());
|
|
record.setEndTime(LocalDateTime.now());
|
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
|
record.setErrorMessage("调用哈哈平台API失败: " + e.getMessage());
|
|
record.setErrorMessage("调用哈哈平台API失败: " + e.getMessage());
|
|
|
updateById(record);
|
|
updateById(record);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("商品数据同步异常", e);
|
|
log.error("商品数据同步异常", e);
|
|
|
- record.setStatus(STATUS_FAILED);
|
|
|
|
|
|
|
+ record.setStatus(SyncConstants.STATUS_SYNC_FAILED);
|
|
|
record.setEndTime(LocalDateTime.now());
|
|
record.setEndTime(LocalDateTime.now());
|
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
record.setDuration(java.time.Duration.between(record.getStartTime(), record.getEndTime()).toMillis());
|
|
|
record.setErrorMessage("同步异常: " + e.getMessage());
|
|
record.setErrorMessage("同步异常: " + e.getMessage());
|
|
@@ -388,7 +384,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
existProduct.setCostPrice(parseDouble(productData.get("cost_price")));
|
|
existProduct.setCostPrice(parseDouble(productData.get("cost_price")));
|
|
|
existProduct.setRetailPrice(parseDouble(productData.get("cprice")));
|
|
existProduct.setRetailPrice(parseDouble(productData.get("cprice")));
|
|
|
existProduct.setCustomerId(productData.get("customer_id") != null ? productData.get("customer_id").toString() : null);
|
|
existProduct.setCustomerId(productData.get("customer_id") != null ? productData.get("customer_id").toString() : null);
|
|
|
- existProduct.setSyncStatus(STATUS_SUCCESS);
|
|
|
|
|
|
|
+ existProduct.setSyncStatus(SyncConstants.STATUS_SYNCED);
|
|
|
existProduct.setSyncTime(LocalDateTime.now());
|
|
existProduct.setSyncTime(LocalDateTime.now());
|
|
|
existProduct.setUpdateTime(LocalDateTime.now());
|
|
existProduct.setUpdateTime(LocalDateTime.now());
|
|
|
productService.updateById(existProduct);
|
|
productService.updateById(existProduct);
|
|
@@ -409,9 +405,9 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
newProduct.setCostPrice(parseDouble(productData.get("cost_price")));
|
|
newProduct.setCostPrice(parseDouble(productData.get("cost_price")));
|
|
|
newProduct.setRetailPrice(parseDouble(productData.get("cprice")));
|
|
newProduct.setRetailPrice(parseDouble(productData.get("cprice")));
|
|
|
newProduct.setCustomerId(productData.get("customer_id") != null ? productData.get("customer_id").toString() : null);
|
|
newProduct.setCustomerId(productData.get("customer_id") != null ? productData.get("customer_id").toString() : null);
|
|
|
- newProduct.setSyncStatus(STATUS_SUCCESS);
|
|
|
|
|
|
|
+ newProduct.setSyncStatus(SyncConstants.STATUS_SYNCED);
|
|
|
newProduct.setSyncTime(LocalDateTime.now());
|
|
newProduct.setSyncTime(LocalDateTime.now());
|
|
|
- newProduct.setIsDeleted(0);
|
|
|
|
|
|
|
+ newProduct.setIsDeleted(CommonConstants.NOT_DELETED);
|
|
|
newProduct.setCreateTime(LocalDateTime.now());
|
|
newProduct.setCreateTime(LocalDateTime.now());
|
|
|
newProduct.setUpdateTime(LocalDateTime.now());
|
|
newProduct.setUpdateTime(LocalDateTime.now());
|
|
|
productService.save(newProduct);
|
|
productService.save(newProduct);
|
|
@@ -473,7 +469,7 @@ public class DataSyncServiceImpl extends ServiceImpl<SyncRecordMapper, SyncRecor
|
|
|
newDevice.setDeviceId(target.getId());
|
|
newDevice.setDeviceId(target.getId());
|
|
|
newDevice.setName(target.getName());
|
|
newDevice.setName(target.getName());
|
|
|
newDevice.setAddress(target.getAddress());
|
|
newDevice.setAddress(target.getAddress());
|
|
|
- newDevice.setStatus("1".equals(target.getStatus()) ? 1 : 0);
|
|
|
|
|
|
|
+ newDevice.setStatus("1".equals(target.getStatus()) ? DeviceConstants.STATUS_ONLINE : DeviceOnlineStatus.OFFLINE.getCode());
|
|
|
deviceService.save(newDevice);
|
|
deviceService.save(newDevice);
|
|
|
|
|
|
|
|
log.info("自动同步:设备 {} ({}) 创建成功", deviceId, target.getName());
|
|
log.info("自动同步:设备 {} ({}) 创建成功", deviceId, target.getName());
|