Browse Source

设备工具类

李进 2 năm trước cách đây
mục cha
commit
d819b1a64f

+ 37 - 0
device-api-service/src/main/java/com/xy/utils/DeviceUtils.java

@@ -0,0 +1,37 @@
+package com.xy.utils;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.xy.entity.DeviceInfo;
+import com.xy.mapper.DeviceInfoMapper;
+import org.springframework.stereotype.Component;
+
+import java.time.LocalDate;
+
+@Component
+public class DeviceUtils {
+
+    /**
+     * 创建设备id
+     *
+     * @return
+     */
+    public static Long createDeviceId() {
+        LocalDate localDate = LocalDate.now();
+        String year = String.valueOf(localDate.getYear());
+        String month = String.valueOf(localDate.getMonthValue());
+        String yearMonth = year.substring(2) + (month.length() == 1 ? "0" + month : month);
+        DeviceInfoMapper deviceInfoMapper = SpringBeanUtils.getBean(DeviceInfoMapper.class);
+        String property = LambdaUtils.getProperty(DeviceInfo::getDeviceId);
+        QueryWrapper<DeviceInfo> queryWrapper = new QueryWrapper<DeviceInfo>().select(String.format("max(%s) as %s", property, property));
+        DeviceInfo deviceInfo = deviceInfoMapper.selectOne(queryWrapper);
+        String tail = "000001";
+        if (deviceInfo != null) {
+            String substring = String.valueOf(deviceInfo.getDeviceId()).substring(0, 4);
+            if (substring.equals(yearMonth)) {
+                tail = String.valueOf(deviceInfo.getDeviceId() + 1).substring(4);
+            }
+        }
+        return Long.valueOf(yearMonth + tail);
+    }
+
+}