فهرست منبع

优化设备统计性能

李进 2 سال پیش
والد
کامیت
934e74c82e
1فایلهای تغییر یافته به همراه21 افزوده شده و 11 حذف شده
  1. 21 11
      device-api-service/src/main/java/com/xy/service/DeviceDataServiceImpl.java

+ 21 - 11
device-api-service/src/main/java/com/xy/service/DeviceDataServiceImpl.java

@@ -4,6 +4,11 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.xy.annotation.Lock;
+import com.xy.collections.list.JArrayList;
+import com.xy.collections.list.JList;
+import com.xy.collections.map.JConcurrentHashMap;
+import com.xy.collections.map.JHashMap;
+import com.xy.collections.map.JMap;
 import com.xy.config.ThreadPoolConfig;
 import com.xy.dto.DeviceDataDto;
 import com.xy.entity.DeviceData;
@@ -15,7 +20,6 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -68,28 +72,33 @@ public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceD
 
     @PostMapping("saveOrAccum")
     @ApiOperation("添加/累加")
-    @Transactional(rollbackFor = Exception.class)
     @Lock(value = "saveOrAccum.deviceId", prefix = "data_save_accum_")
     public R saveOrAccum(@RequestBody @Validated DeviceDataDto.SaveOrAccum saveOrAccum) {
         LocalDateTime now = LocalDateTime.now();
         String stringTime = DataTime.toString(now, "yyyyMMdd");
+        //获取字典
         Map<String, SysDictRedis> map = SysDictUtils.get(DeviceEnum.DEVICE_DATA_TYPE.getKey());
+        //查询已存在数据
+        List<DeviceData> list = list(new LambdaQueryWrapper<DeviceData>()
+                .eq(DeviceData::getDeviceId, saveOrAccum.getDeviceId())
+                .in(DeviceData::getType, new JHashMap<>(map).toList().key())
+        );
+        JMap<String, DeviceData> typeMaps = new JArrayList<>(list).toMap(DeviceData::getType).cover();
+        //新增或修改的多线程桶map集合
+        JMap<String, DeviceData> dbMaps = new JConcurrentHashMap<>(map.size());
+        //多线程封装新增或修改对象
         ThreadPoolUtils.Execute execute = ThreadPoolUtils.excPoll(ThreadPoolConfig.DEVICE_DATA_SAVE_OR_ACCUM, map.size());
         map.forEach((type, sysDictRedis) -> execute.execute(() -> {
-            Integer dateValue = Integer.valueOf(stringTime.substring(0, Integer.valueOf(sysDictRedis.getValue())));
-            DeviceData saveOrUpdateInfo = copy(DeviceData.class, saveOrAccum);
-            DeviceData deviceData = getOne(new LambdaQueryWrapper<DeviceData>()
-                    .eq(DeviceData::getDeviceId, saveOrAccum.getDeviceId())
-                    .eq(DeviceData::getType, type)
-            );
+            DeviceData deviceData = typeMaps.get(type);
             //添加
             if (deviceData == null) {
-                saveOrUpdateInfo
+                Integer dateValue = Integer.valueOf(stringTime.substring(0, Integer.valueOf(sysDictRedis.getValue())));
+                DeviceData saveOrUpdateInfo = copy(DeviceData.class, saveOrAccum)
                         .setCreateTime(now)
                         .setUpdateTime(now)
                         .setType(type)
                         .setDateValue(dateValue);
-                save(saveOrUpdateInfo);
+                dbMaps.put(type, saveOrUpdateInfo);
                 return;
             }
             //累加
@@ -101,9 +110,10 @@ public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceD
                     .setRefundCount(saveOrAccum.getRefundCount() != null ? saveOrAccum.getRefundCount() + deviceData.getRefundCount() : null)
                     .setZeroCount(saveOrAccum.getZeroCount() != null ? saveOrAccum.getZeroCount() + deviceData.getZeroCount() : null)
                     .setDeviceFaultCount(saveOrAccum.getDeviceFaultCount() != null ? saveOrAccum.getDeviceFaultCount() + deviceData.getDeviceFaultCount() : null);
-            updateById(updateDeviceData);
+            dbMaps.put(type, updateDeviceData);
         }));
         execute.end();
+        saveOrUpdateBatch(dbMaps.toList().value());
         return R.ok();
     }