diff --git a/op-api/op-api-system/src/main/java/com/op/system/api/RemoteDeviceService.java b/op-api/op-api-system/src/main/java/com/op/system/api/RemoteDeviceService.java index 721ae6a8..bcda8719 100644 --- a/op-api/op-api-system/src/main/java/com/op/system/api/RemoteDeviceService.java +++ b/op-api/op-api-system/src/main/java/com/op/system/api/RemoteDeviceService.java @@ -30,4 +30,7 @@ public interface RemoteDeviceService { @PostMapping("/deviceTask/createEquipmentOperationTask") public R createEquipmentOperationTask(); + + @PostMapping("/deviceTask/equipmentBKTask") + public R equipmentBKTask(); } diff --git a/op-api/op-api-system/src/main/java/com/op/system/api/factory/RemoteDeviceFallbackFactory.java b/op-api/op-api-system/src/main/java/com/op/system/api/factory/RemoteDeviceFallbackFactory.java index d46844da..9868e0fb 100644 --- a/op-api/op-api-system/src/main/java/com/op/system/api/factory/RemoteDeviceFallbackFactory.java +++ b/op-api/op-api-system/src/main/java/com/op/system/api/factory/RemoteDeviceFallbackFactory.java @@ -42,6 +42,11 @@ public class RemoteDeviceFallbackFactory implements FallbackFactory>> dateSources0 = remoteUserService.getPoolNameList(sysUser); + List> dateSources = dateSources0.getData(); + ExecutorService executorService = new ThreadPoolExecutor( + dateSources.size(), + dateSources.size(), + 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue()); + + try { + dateSources.forEach(dateSource -> { + logger.info("++++++++++++" + dateSource.get("poolName") + "++++设备运行记录数据库备份开始++++++++++"); + Runnable run = () -> backupEquipmentOperation(dateSource.get("poolName")); + executorService.execute(run); + }); + } catch (Exception e) { + logger.error("service == dataClearTask == exception", e); + return error("service == dataClearTask == exception"); + } finally { + executorService.shutdown(); + } + + return success(); + } + public void createSpotCheckPlanFunc(String poolName){ DynamicDataSourceContextHolder.push(poolName);// 这是数据源的key /**equ_plan equ_plan_equ**/ @@ -431,7 +465,7 @@ public class DeviceTaskServiceImpl implements IDeviceTaskService { operation.setCreateTime(DateUtils.getNowDate()); } - int batchSize = 200;// 批次数量 + int batchSize = 100;// 批次数量 // 分批次处理 int batchCount = (operationList.size() + batchSize - 1)/batchSize;// 计算批次数量 @@ -450,6 +484,40 @@ public class DeviceTaskServiceImpl implements IDeviceTaskService { } + private void backupEquipmentOperation(String poolName) { + DynamicDataSourceContextHolder.push(poolName);// 这是数据源的key + // 校验当前YEAR-2是否备份过数据 + EquOperation check = equOperationMapper.checkBackupOperation(); + if (check == null) { + // 获取插入数量 + int insertNum = equOperationMapper.getBackupOperationNum(); + int insertSize = 100;// 新增批次数量 + + // 分批次备份 + int batchCount = (insertNum + insertSize - 1)/insertSize;// 计算批次数量 + for (int i = 0;i < batchCount; i++) { + int startIndex = i * insertSize;// 索引开始值 + int endIndex = Math.min((i + 1)*insertSize,insertNum);// 索引结束值 + + // 备份至数据库 + int num = equOperationMapper.backupOperation(startIndex,insertSize); + + System.out.println("数据源:"+poolName+"--->"+"第"+(i+1)+"轮备份"+"开始索引:"+startIndex+"________结束索引:"+endIndex+"________成功插入数量:"+num); + } + + // 删除源数据库数据 + int delSize = 100;// 删除批次数量 + for (int i = 0; i < insertNum/delSize+1; i++) { + // 在源数据库中删除 + int num = equOperationMapper.deleteOperation(delSize); + System.out.println("数据源:"+poolName+"--->"+"第"+(i+1)+"轮删除"+"------>成功删除数量:"+num); + } + + }else { + System.out.println("数据源:"+poolName+"---->"+"年份:"+( LocalDate.now().getYear()-2) +"--->已备份"); + } + } + public static void main(String[] args) { int batchSize = 200;// 批次数量 diff --git a/op-modules/op-device/src/main/resources/mapper/device/EquOperationMapper.xml b/op-modules/op-device/src/main/resources/mapper/device/EquOperationMapper.xml index 04a90d78..f47df026 100644 --- a/op-modules/op-device/src/main/resources/mapper/device/EquOperationMapper.xml +++ b/op-modules/op-device/src/main/resources/mapper/device/EquOperationMapper.xml @@ -119,6 +119,14 @@ where CONVERT(date, create_time) = CONVERT(date, GETDATE()) + + + + insert into equ_operation @@ -188,6 +196,15 @@ + + insert into equ_operation_backup + select s.* + from equ_operation s + where YEAR (s.create_time) = YEAR (GETDATE())-2 + ORDER BY id + OFFSET #{startIndex} ROWS FETCH NEXT #{batchSize} ROWS ONLY + + update equ_operation @@ -230,4 +247,12 @@ #{id} + + + delete + from equ_operation + where id in (select s.id from equ_operation s where YEAR (s.create_time) = YEAR (GETDATE())-2 + ORDER BY id + OFFSET 0 ROWS FETCH NEXT #{delSize} ROWS ONLY) + \ No newline at end of file diff --git a/op-modules/op-job/src/main/java/com/op/job/task/RyTask.java b/op-modules/op-job/src/main/java/com/op/job/task/RyTask.java index 443cd9a0..e2815c4d 100644 --- a/op-modules/op-job/src/main/java/com/op/job/task/RyTask.java +++ b/op-modules/op-job/src/main/java/com/op/job/task/RyTask.java @@ -144,4 +144,10 @@ public class RyTask { logger.info("++设备运行记录生成任务开始++createEquipmentOperationTask+++++"); remoteDeviceService.createEquipmentOperationTask(); } + + /** 每年1月1日1:50执行 **/ + public void equipmentBKTask(){ + logger.info("++设备运行记录+数据库备份的数据+开始++equipmentBkTask+++++"); + remoteDeviceService.equipmentBKTask(); + } }