Bläddra i källkod

refactor:产品和模型实体和接口调整

xiwa 2 år sedan
förälder
incheckning
125b1c2ea3
23 ändrade filer med 113 tillägg och 326 borttagningar
  1. 2 12
      iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequest.java
  2. 10 11
      iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/Request.java
  3. 2 4
      iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/RequestEmpty.java
  4. 0 183
      iot-common/iot-common-core/src/main/java/cc/iotkit/common/utils/SnowflakeIdGeneratorUtil.java
  5. 10 7
      iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/product/Product.java
  6. 10 6
      iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/product/ThingModel.java
  7. 2 2
      iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/manager/IProductData.java
  8. 1 1
      iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/manager/IThingModelData.java
  9. 2 2
      iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/cache/ProductCacheEvict.java
  10. 1 1
      iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/cache/ThingModelCacheEvict.java
  11. 16 16
      iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/ProductDataCache.java
  12. 4 4
      iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/ThingModelDataCache.java
  13. 1 1
      iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/dao/ProductRepository.java
  14. 1 1
      iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/dao/ThingModelRepository.java
  15. 4 1
      iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbProduct.java
  16. 1 2
      iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbThingModel.java
  17. 3 9
      iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductDataImpl.java
  18. 18 23
      iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java
  19. 5 5
      iot-common/iot-common-web/src/main/java/cc/iotkit/common/web/handler/ResponseResultHandler.java
  20. 2 7
      iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/ProductController.java
  21. 1 2
      iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/thingmodel/ThingModelBo.java
  22. 1 3
      iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IProductService.java
  23. 16 23
      iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/ProductServiceImpl.java

+ 2 - 12
iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/PageRequest.java

@@ -1,8 +1,7 @@
 package cc.iotkit.common.api;
 
 import cc.iotkit.common.utils.MapstructUtils;
-import cc.iotkit.common.utils.SnowflakeIdGeneratorUtil;
-import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.IdUtil;
 import jakarta.validation.constraints.Max;
 import jakarta.validation.constraints.Min;
 import jakarta.validation.constraints.NotNull;
@@ -63,16 +62,7 @@ public class PageRequest<T> extends Request<T> implements Serializable {
         pageRequest.setPageSize(DEFAULT_PAGE_SIZE);
         pageRequest.setPageNum(DEFAULT_PAGE_NUM);
         pageRequest.setData(data);
-        pageRequest.setRequestId(String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId()));
-        return pageRequest;
-    }
-
-    public static <DTO> PageRequest<DTO> request2PageRequest(Request<DTO> request) {
-        PageRequest<DTO> pageRequest = new PageRequest<>();
-        pageRequest.setData(request.getData());
-        pageRequest.setPageNum(DEFAULT_PAGE_NUM);
-        pageRequest.setPageSize(DEFAULT_PAGE_SIZE);
-        pageRequest.setRequestId(request.getRequestId());
+        pageRequest.setRequestId(IdUtil.simpleUUID());
         return pageRequest;
     }
 

+ 10 - 11
iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/Request.java

@@ -1,12 +1,11 @@
 package cc.iotkit.common.api;
 
-import cc.iotkit.common.utils.SnowflakeIdGeneratorUtil;
+import cn.hutool.core.util.IdUtil;
 import jakarta.validation.Valid;
 import jakarta.validation.constraints.NotNull;
 import lombok.Data;
 
 import java.io.Serializable;
-import java.util.UUID;
 
 /**
  * @author: Longjun.Tu
@@ -17,14 +16,14 @@ import java.util.UUID;
 @Data
 public class Request<T> extends RequestEmpty implements Serializable {
 
-  @Valid
-  @NotNull
-  private T data;
+    @Valid
+    @NotNull
+    private T data;
 
-  public static <T> Request<T> of(T data) {
-    Request<T> request = new Request<>();
-    request.setData(data);
-    request.setRequestId(String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId()));
-    return request;
-  }
+    public static <T> Request<T> of(T data) {
+        Request<T> request = new Request<>();
+        request.setData(data);
+        request.setRequestId(IdUtil.simpleUUID());
+        return request;
+    }
 }

+ 2 - 4
iot-common/iot-common-core/src/main/java/cc/iotkit/common/api/RequestEmpty.java

@@ -1,12 +1,10 @@
 package cc.iotkit.common.api;
 
-import cc.iotkit.common.utils.SnowflakeIdGeneratorUtil;
+import cn.hutool.core.util.IdUtil;
 import jakarta.validation.constraints.NotBlank;
-import jakarta.validation.constraints.NotNull;
 import lombok.Data;
 
 import java.io.Serializable;
-import java.util.UUID;
 
 /**
  * @author: Longjun.Tu
@@ -22,7 +20,7 @@ public class RequestEmpty implements Serializable {
 
   public static RequestEmpty of() {
     RequestEmpty request = new RequestEmpty();
-    request.setRequestId(String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId()));
+    request.setRequestId(IdUtil.simpleUUID());
     return request;
   }
 

+ 0 - 183
iot-common/iot-common-core/src/main/java/cc/iotkit/common/utils/SnowflakeIdGeneratorUtil.java

@@ -1,183 +0,0 @@
-package cc.iotkit.common.utils;
-
-
-import java.net.InetAddress;
-import java.net.NetworkInterface;
-import java.net.SocketException;
-import java.net.UnknownHostException;
-import java.util.Enumeration;
-import java.util.HashSet;
-import java.util.Random;
-import java.util.Set;
-
-/**
- * @author: Longjun.Tu
- * @description:
- * @date:created in 2023/5/18 10:20
- * @modificed by:
- */
-public class SnowflakeIdGeneratorUtil {
-
-    /**
-     * 起始的时间戳
-     */
-    private final static long START_STMP = 1577808000000L;
-
-    /**
-     * 每一部分占用的位数
-     */
-    private final static long SEQUENCE_BIT = 12; //序列号占用的位数
-    private final static long MACHINE_BIT = 5;   //机器标识占用的位数
-    private final static long DATACENTER_BIT = 5;//数据中心占用的位数
-
-    /**
-     * 每一部分的最大值
-     */
-    private final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT);
-    private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
-    private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
-
-    /**
-     * 每一部分向左的位移
-     */
-    private final static long MACHINE_LEFT = SEQUENCE_BIT;
-    private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
-    private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;
-
-    private long datacenterId;  //数据中心
-    private long machineId;     //机器标识
-    private long sequence = 0L; //序列号
-    private long lastStmp = -1L;//上一次时间戳
-
-    private static volatile SnowflakeIdGeneratorUtil snowflake = null;
-    private static Object lock = new Object();
-
-    public SnowflakeIdGeneratorUtil(long datacenterId, long machineId) {
-        if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) {
-            throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
-        }
-        if (machineId > MAX_MACHINE_NUM || machineId < 0) {
-            throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
-        }
-        this.datacenterId = datacenterId;
-        this.machineId = machineId;
-    }
-
-    /**
-     * 获取单列
-     *
-     * @return
-     */
-    public static SnowflakeIdGeneratorUtil getInstanceSnowflake() {
-        if (snowflake == null) {
-            synchronized (lock) {
-                if(snowflake == null){
-                    long workerId;
-                    long dataCenterId = getRandom();
-                    try {
-                        //第一次使用获取mac地址的
-                        workerId = getWorkerId();
-                    } catch (Exception e) {
-                        workerId = getRandom();
-                    }
-                    snowflake = new SnowflakeIdGeneratorUtil(dataCenterId, workerId);
-                }
-            }
-        }
-        return snowflake;
-    }
-
-    /**
-     * 产生下一个ID
-     *
-     * @return
-     */
-    public synchronized long nextId() {
-        long currStmp = getNewstmp();
-        if (currStmp < lastStmp) {
-            throw new RuntimeException("Clock moved backwards.  Refusing to generate id");
-        }
-
-        if (currStmp == lastStmp) {
-            //相同毫秒内,序列号自增
-            sequence = (sequence + 1) & MAX_SEQUENCE;
-            //同一毫秒的序列数已经达到最大
-            if (sequence == 0L) {
-                currStmp = getNextMill();
-            }
-        } else {
-            //不同毫秒内,序列号置为0
-            sequence = 0L;
-        }
-
-        lastStmp = currStmp;
-
-        return (currStmp - START_STMP) << TIMESTMP_LEFT //时间戳部分
-                | datacenterId << DATACENTER_LEFT       //数据中心部分
-                | machineId << MACHINE_LEFT             //机器标识部分
-                | sequence;                             //序列号部分
-    }
-
-    private long getNextMill() {
-        long mill = getNewstmp();
-        while (mill <= lastStmp) {
-            mill = getNewstmp();
-        }
-        return mill;
-    }
-
-    private long getNewstmp() {
-        return System.currentTimeMillis();
-    }
-
-    /**
-     * 生成1-31之间的随机数
-     *
-     * @return
-     */
-    private static long getRandom() {
-        int max = (int) (MAX_MACHINE_NUM);
-        int min = 1;
-        Random random = new Random();
-        long result = random.nextInt(max - min) + min;
-        return result;
-    }
-
-    private static long getWorkerId() throws SocketException, UnknownHostException, NullPointerException {
-        @SuppressWarnings("unused")
-        InetAddress ip = InetAddress.getLocalHost();
-
-        NetworkInterface network = null;
-        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
-        while (en.hasMoreElements()) {
-            NetworkInterface nint = en.nextElement();
-            if (!nint.isLoopback() && nint.getHardwareAddress() != null) {
-                network = nint;
-                break;
-            }
-        }
-
-        if (network == null) {
-            throw new NullPointerException("network is null");
-        }
-        @SuppressWarnings("ConstantConditions")
-        byte[] mac = network.getHardwareAddress();
-        long id = ((0x000000FF & (long) mac[mac.length - 1]) | (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 11;
-        if (id > MAX_MACHINE_NUM) {
-            return getRandom();
-        }
-        return id;
-    }
-
-    public static void main(String[] args) {
-        Set<Long> idList = new HashSet<>();
-        long start = System.currentTimeMillis();
-        for (int i = 0; i < 1000000; i++) {
-            long id = SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId();
-            idList.add(id);
-            // System.out.println("id="+id);
-        }
-        // System.out.println(idList.size());
-        // System.out.println(System.currentTimeMillis() - start);
-    }
-}

+ 10 - 7
iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/product/Product.java

@@ -9,19 +9,22 @@
  */
 package cc.iotkit.model.product;
 
-import cc.iotkit.model.Owned;
-import lombok.AllArgsConstructor;
-import lombok.Builder;
-import lombok.Data;
-import lombok.NoArgsConstructor;
+import cc.iotkit.model.Id;
+import cc.iotkit.model.TenantModel;
+import lombok.*;
 
+import java.io.Serializable;
+
+@EqualsAndHashCode(callSuper = true)
 @Data
 @Builder
 @NoArgsConstructor
 @AllArgsConstructor
-public class Product implements Owned<String> {
+public class Product extends TenantModel implements Id<Long>, Serializable {
+
+    private Long id;
 
-    private String id;
+    private String key;
 
     private String name;
 

+ 10 - 6
iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/product/ThingModel.java

@@ -10,24 +10,28 @@
 package cc.iotkit.model.product;
 
 import cc.iotkit.model.Id;
+import cc.iotkit.model.TenantModel;
 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-import lombok.AllArgsConstructor;
-import lombok.Builder;
-import lombok.Data;
-import lombok.NoArgsConstructor;
+import lombok.*;
 
+import java.io.Serializable;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 
+/**
+ * @author sjg
+ */
+@EqualsAndHashCode(callSuper = true)
 @Data
 @Builder
 @NoArgsConstructor
 @AllArgsConstructor
-public class ThingModel implements Id<String> {
+public class ThingModel extends TenantModel implements Id<Long>, Serializable {
+    private static final long serialVersionUID = 1L;
 
-    private String id;
+    private Long id;
 
     private String productKey;
 

+ 2 - 2
iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/manager/IProductData.java

@@ -9,7 +9,7 @@
  */
 package cc.iotkit.data.manager;
 
-import cc.iotkit.data.IOwnedData;
+import cc.iotkit.data.ICommonData;
 import cc.iotkit.model.product.Product;
 
 import java.util.List;
@@ -17,7 +17,7 @@ import java.util.List;
 /**
  * 产品接口
  */
-public interface IProductData extends IOwnedData<Product, String> {
+public interface IProductData extends ICommonData<Product,Long> {
 
     /**
      * 按品类取产品列表

+ 1 - 1
iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/manager/IThingModelData.java

@@ -3,6 +3,6 @@ package cc.iotkit.data.manager;
 import cc.iotkit.data.ICommonData;
 import cc.iotkit.model.product.ThingModel;
 
-public interface IThingModelData extends ICommonData<ThingModel, String> {
+public interface IThingModelData extends ICommonData<ThingModel, Long> {
 
 }

+ 2 - 2
iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/cache/ProductCacheEvict.java

@@ -7,8 +7,8 @@ import org.springframework.stereotype.Component;
 @Component
 public class ProductCacheEvict {
 
-    @CacheEvict(value = Constants.CACHE_PRODUCT, key = "#root.method.name+#pk")
-    public void findById(String pk) {
+    @CacheEvict(value = Constants.CACHE_PRODUCT, key = "#root.method.name+#id")
+    public void findById(Long id) {
     }
 
 }

+ 1 - 1
iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/cache/ThingModelCacheEvict.java

@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
 public class ThingModelCacheEvict {
 
     @CacheEvict(value = Constants.CACHE_THING_MODEL, key = "#root.method.name+#s")
-    public void findById(String s) {
+    public void findById(Long s) {
     }
 
 }

+ 16 - 16
iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/ProductDataCache.java

@@ -28,29 +28,29 @@ public class ProductDataCache implements IProductData {
         return productData.findByCategory(category);
     }
 
-    @Override
-    public List<Product> findByUid(String uid) {
-        return productData.findByUid(uid);
-    }
+//    @Override
+//    public List<Product> findByUid(String uid) {
+//        return productData.findByUid(uid);
+//    }
 
-    @Override
-    public Paging<Product> findByUid(String uid, int page, int size) {
-        return productData.findByUid(uid, page, size);
-    }
+//    @Override
+//    public Paging<Product> findByUid(String uid, int page, int size) {
+//        return productData.findByUid(uid, page, size);
+//    }
 
-    @Override
-    public long countByUid(String uid) {
-        return productData.countByUid(uid);
-    }
+//    @Override
+//    public long countByUid(String uid) {
+//        return productData.countByUid(uid);
+//    }
 
     @Override
     @Cacheable(value = Constants.CACHE_PRODUCT, key = "#root.method.name+#s", unless = "#result == null")
-    public Product findById(String s) {
+    public Product findById(Long s) {
         return productData.findById(s);
     }
 
     @Override
-    public List<Product> findByIds(Collection<String> id) {
+    public List<Product> findByIds(Collection<Long> id) {
         return null;
     }
 
@@ -68,14 +68,14 @@ public class ProductDataCache implements IProductData {
     }
 
     @Override
-    public void deleteById(String s) {
+    public void deleteById(Long s) {
         productData.deleteById(s);
         //清除缓存
         productCacheEvict.findById(s);
     }
 
     @Override
-    public void deleteByIds(Collection<String> strings) {
+    public void deleteByIds(Collection<Long> ids) {
 
     }
 

+ 4 - 4
iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/ThingModelDataCache.java

@@ -25,12 +25,12 @@ public class ThingModelDataCache implements IThingModelData {
 
     @Override
     @Cacheable(value = Constants.CACHE_THING_MODEL, key = "#root.method.name+#s", unless = "#result == null")
-    public ThingModel findById(String s) {
+    public ThingModel findById(Long s) {
         return thingModelData.findById(s);
     }
 
     @Override
-    public List<ThingModel> findByIds(Collection<String> id) {
+    public List<ThingModel> findByIds(Collection<Long> id) {
         return null;
     }
 
@@ -47,12 +47,12 @@ public class ThingModelDataCache implements IThingModelData {
     }
 
     @Override
-    public void deleteById(String s) {
+    public void deleteById(Long s) {
         thingModelData.deleteById(s);
     }
 
     @Override
-    public void deleteByIds(Collection<String> strings) {
+    public void deleteByIds(Collection<Long> strings) {
 
     }
 

+ 1 - 1
iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/dao/ProductRepository.java

@@ -16,7 +16,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
 
 import java.util.List;
 
-public interface ProductRepository extends JpaRepository<TbProduct, String> {
+public interface ProductRepository extends JpaRepository<TbProduct, Long> {
 
     List<TbProduct> findByCategory(String category);
 

+ 1 - 1
iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/dao/ThingModelRepository.java

@@ -12,6 +12,6 @@ package cc.iotkit.data.dao;
 import cc.iotkit.data.model.TbThingModel;
 import org.springframework.data.jpa.repository.JpaRepository;
 
-public interface ThingModelRepository extends JpaRepository<TbThingModel, String> {
+public interface ThingModelRepository extends JpaRepository<TbThingModel, Long> {
 
 }

+ 4 - 1
iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbProduct.java

@@ -32,7 +32,10 @@ public class TbProduct {
     @GeneratedValue(generator = "SnowflakeIdGenerator")
     @GenericGenerator(name = "SnowflakeIdGenerator", strategy = "cc.iotkit.data.config.id.SnowflakeIdGenerator")
     @ApiModelProperty(value = "产品id")
-    private String id;
+    private Long id;
+
+    @ApiModelProperty(value = "产品key")
+    private String productKey;
 
     @ApiModelProperty(value = "产品名称")
     private String name;

+ 1 - 2
iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/model/TbThingModel.java

@@ -29,7 +29,7 @@ public class TbThingModel {
     @GeneratedValue(generator = "SnowflakeIdGenerator")
     @GenericGenerator(name = "SnowflakeIdGenerator", strategy = "cc.iotkit.data.config.id.SnowflakeIdGenerator")
     @ApiModelProperty(value = "主键")
-    private String id;
+    private Long id;
 
     @ApiModelProperty(value = "产品key")
     private String productKey;
@@ -38,7 +38,6 @@ public class TbThingModel {
     @Column(columnDefinition = "text")
     @AutoMapping(ignore = true)
     @ReverseAutoMapping(ignore = true)
-
     private String model;
 
 }

+ 3 - 9
iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ProductDataImpl.java

@@ -18,7 +18,7 @@ import java.util.List;
 
 @Primary
 @Service
-public class ProductDataImpl implements IProductData, IJPACommData<Product, String> {
+public class ProductDataImpl implements IProductData, IJPACommData<Product, Long> {
 
     @Autowired
     private ProductRepository productRepository;
@@ -43,12 +43,10 @@ public class ProductDataImpl implements IProductData, IJPACommData<Product, Stri
         return MapstructUtils.convert(productRepository.findByCategory(category), Product.class);
     }
 
-    @Override
     public List<Product> findByUid(String uid) {
         return MapstructUtils.convert(productRepository.findByUid(uid), Product.class);
     }
 
-    @Override
     public Paging<Product> findByUid(String uid, int page, int size) {
         Page<TbProduct> productPage = productRepository.findByUid(uid,
                 Pageable.ofSize(size).withPage(page - 1));
@@ -56,23 +54,19 @@ public class ProductDataImpl implements IProductData, IJPACommData<Product, Stri
                 MapstructUtils.convert(productPage.getContent(), Product.class));
     }
 
-    @Override
     public long countByUid(String uid) {
         return productRepository.countByUid(uid);
     }
 
-
     @Override
-    public Product findById(String s) {
-        return MapstructUtils.convert(productRepository.findById(s).orElse(null), Product.class);
+    public Product findById(Long id) {
+        return MapstructUtils.convert(productRepository.findById(id).orElse(null), Product.class);
     }
 
-
     @Override
     public Product save(Product data) {
         productRepository.save(MapstructUtils.convert(data, TbProduct.class));
         return data;
     }
 
-
 }

+ 18 - 23
iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java

@@ -9,29 +9,26 @@
  */
 package cc.iotkit.data.service;
 
-import cc.iotkit.common.api.PageRequest;
+import cc.iotkit.common.utils.JsonUtils;
 import cc.iotkit.common.utils.MapstructUtils;
 import cc.iotkit.data.dao.IJPACommData;
-import cc.iotkit.data.manager.IThingModelData;
 import cc.iotkit.data.dao.ThingModelRepository;
+import cc.iotkit.data.manager.IThingModelData;
 import cc.iotkit.data.model.TbThingModel;
-import cc.iotkit.common.api.Paging;
 import cc.iotkit.model.product.ThingModel;
 import lombok.RequiredArgsConstructor;
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Primary;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Service;
 
-import java.util.Collection;
-import java.util.List;
-import java.util.UUID;
-
+/**
+ * @author sjg
+ */
 @Primary
 @Service
 @RequiredArgsConstructor
-public class ThingModelDataImpl implements IThingModelData, IJPACommData<ThingModel, String> {
+public class ThingModelDataImpl implements IThingModelData, IJPACommData<ThingModel, Long> {
 
     @Autowired
     private ThingModelRepository thingModelRepository;
@@ -52,28 +49,26 @@ public class ThingModelDataImpl implements IThingModelData, IJPACommData<ThingMo
     }
 
     @Override
-    public ThingModel findById(String s) {
-        return MapstructUtils.convert(thingModelRepository.findById(s).orElse(null),ThingModel.class);
+    public ThingModel findById(Long id) {
+        TbThingModel tbThingModel = thingModelRepository.findById(id).orElse(null);
+        ThingModel convert = MapstructUtils.convert(tbThingModel, ThingModel.class);
+        if (tbThingModel != null && convert != null) {
+            convert.setModel(JsonUtils.parseObject(tbThingModel.getModel(), ThingModel.Model.class));
+        }
+        return convert;
     }
 
-
     @Override
     public ThingModel save(ThingModel data) {
-        if (StringUtils.isBlank(data.getId())) {
-            data.setId(UUID.randomUUID().toString());
-        }
-        thingModelRepository.save(MapstructUtils.convert(data, TbThingModel.class));
+        TbThingModel to = data.to(TbThingModel.class);
+        to.setModel(JsonUtils.toJsonString(data.getModel()));
+        thingModelRepository.save(to);
         return data;
     }
 
-
-
     @Override
-    public void deleteById(String s) {
-        thingModelRepository.deleteById(s);
+    public void deleteById(Long id) {
+        thingModelRepository.deleteById(id);
     }
 
-
-
-
 }

+ 5 - 5
iot-common/iot-common-web/src/main/java/cc/iotkit/common/web/handler/ResponseResultHandler.java

@@ -10,8 +10,8 @@
 package cc.iotkit.common.web.handler;
 
 import cc.iotkit.common.api.Response;
-import cc.iotkit.common.utils.SnowflakeIdGeneratorUtil;
 import cn.dev33.satoken.util.SaResult;
+import cn.hutool.core.util.IdUtil;
 import org.springframework.core.MethodParameter;
 import org.springframework.http.MediaType;
 import org.springframework.http.converter.HttpMessageConverter;
@@ -37,20 +37,20 @@ public class ResponseResultHandler implements ResponseBodyAdvice<Object> {
         if (body instanceof GlobalExceptionHandler.RequestResult) {
             GlobalExceptionHandler.RequestResult requestResult = (GlobalExceptionHandler.RequestResult) body;
             return new Response(requestResult.getCode(), requestResult.getMessage(),
-                    "", String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId()));
+                    "", IdUtil.simpleUUID());
         } else if (body instanceof SaResult) {
             SaResult result = (SaResult) body;
-            return new Response(result.getCode(), result.getMsg(), result.getData(), String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId()));
+            return new Response(result.getCode(), result.getMsg(), result.getData(), IdUtil.simpleUUID());
         } else if (body instanceof Map) {
             Map map = (Map) body;
             //spring mvc内部异常
             if (map.containsKey("timestamp") && map.containsKey("status") && map.containsKey("error")) {
                 return new Response((Integer) map.get("status"), (String) map.get("error"),
-                        "", String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId()));
+                        "", IdUtil.simpleUUID());
             }
         }
 
-        return new Response(200, "", body, String.valueOf(SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId()));
+        return new Response(200, "", body, IdUtil.simpleUUID());
     }
 
 }

+ 2 - 7
iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/ProductController.java

@@ -81,25 +81,20 @@ public class ProductController {
     @PostMapping("/getThingModelByProductKey")
     @ApiOperation("查看物模型")
     public ThingModelVo getThingModelByProductKey(@RequestBody  @Validated Request<String> request) {
-
         return productService.getThingModelByProductKey(request.getData());
-
     }
 
     @ApiOperation("保存物模型")
     @PostMapping("/thingModel/save")
     public boolean saveThingModel(@Validated @RequestBody Request<ThingModelBo> request) {
-
         return productService.saveThingModel(request.getData());
-
-
     }
 
     @PostMapping("/thingModel/delete")
     @ApiOperation("删除物模型")
     @Log(title = "物模型", businessType = BusinessType.DELETE)
-    public boolean deleteThingModel(@Validated @RequestBody Request<String> productKey) {
-       return productService.deleteThingModel(productKey.getData());
+    public boolean deleteThingModel(@Validated @RequestBody Request<Long> id) {
+       return productService.deleteThingModel(id.getData());
     }
 
 

+ 1 - 2
iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/thingmodel/ThingModelBo.java

@@ -31,7 +31,6 @@ import lombok.EqualsAndHashCode;
 @EqualsAndHashCode(callSuper = true)
 @AutoMapper(target = ThingModel.class, reverseConvertGenerate = false)
 public class ThingModelBo extends BaseDto {
-
     private static final long serialVersionUID = -1L;
 
     @ApiModelProperty(value = "模型内容")
@@ -41,7 +40,7 @@ public class ThingModelBo extends BaseDto {
     private String model;
 
     @ApiModelProperty(value = "产品key")
-    @Size(max = 255, message = "产品key长度不正确")
+    @Size(min = 16, max = 16, message = "产品key长度不正确")
     private String productKey;
 
 }

+ 1 - 3
iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IProductService.java

@@ -31,9 +31,7 @@ public interface IProductService {
 
     boolean saveThingModel(ThingModelBo data);
 
-    boolean deleteThingModel(String data);
-
-
+    boolean deleteThingModel(Long id);
 
     boolean deleteCategory(String data);
 

+ 16 - 23
iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/ProductServiceImpl.java

@@ -29,6 +29,7 @@ import cc.iotkit.temporal.IDbStructureData;
 import com.aliyun.oss.OSS;
 import com.aliyun.oss.OSSClientBuilder;
 import com.aliyun.oss.model.PutObjectResult;
+import com.github.yitter.idgen.YitIdHelper;
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
@@ -71,28 +72,23 @@ public class ProductServiceImpl implements IProductService {
     @Autowired
     private IDbStructureData dbStructureData;
 
-
     private OSS ossClient;
 
     @Override
     public ProductVo addEntity(ProductBo data) {
         Product product = data.to(Product.class);
 
-        dataOwnerService.checkOwnerSave(productData, product);
-
         if (product.getCreateAt() == null) {
             product.setCreateAt(System.currentTimeMillis());
         }
         productData.save(product);
         return MapstructUtils.convert(product, ProductVo.class);
-}
+    }
 
     @Override
     public boolean updateEntity(ProductBo productBo) {
         Product product = productBo.to(Product.class);
 
-        dataOwnerService.checkOwnerSave(productData, product);
-
         if (product.getCreateAt() == null) {
             product.setCreateAt(System.currentTimeMillis());
         }
@@ -102,27 +98,29 @@ public class ProductServiceImpl implements IProductService {
 
     @Override
     public ProductVo getDetail(String productKey) {
-       return MapstructUtils.convert(dataOwnerService.checkOwner(productData.findById(productKey)), ProductVo.class);
+        return MapstructUtils.convert(productData.findById(productKey), ProductVo.class);
     }
 
     @Override
     public ThingModelVo getThingModelByProductKey(String productKey) {
-        checkProductOwner(productKey);
-        ThingModel thingModel = thingModelData.findById(productKey);
+//        ThingModel thingModel = thingModelData.findById(productKey);
+        // todo
+        ThingModel thingModel = thingModelData.findById(0L);
         return MapstructUtils.convert(thingModel, ThingModelVo.class);
     }
 
     @Override
     public boolean saveThingModel(ThingModelBo data) {
         String productKey = data.getProductKey();
-        checkProductOwner(productKey);
         String model = data.getModel();
-        ThingModel oldData = thingModelData.findById(productKey);
-        ThingModel thingModel = new ThingModel(productKey, productKey, JsonUtils.parseObject(model, ThingModel.Model.class));
+        ThingModel oldData = thingModelData.findOneByCondition(ThingModel.builder().productKey(productKey).build());
+        ThingModel thingModel = new ThingModel(YitIdHelper.nextId(), productKey, JsonUtils.parseObject(model, ThingModel.Model.class));
+
         if (oldData == null) {
             //定义时序数据库物模型数据结构
             dbStructureData.defineThingModel(thingModel);
         } else {
+            thingModel.setId(oldData.getId());
             //更新时序数据库物模型数据结构
             dbStructureData.updateThingModel(thingModel);
         }
@@ -131,17 +129,15 @@ public class ProductServiceImpl implements IProductService {
     }
 
     @Override
-    public boolean deleteThingModel(String productKey) {
-        checkProductOwner(productKey);
-        ThingModel thingModel = thingModelData.findById(productKey);
+    public boolean deleteThingModel(Long id) {
+        ThingModel thingModel = thingModelData.findById(id);
         //删除时序数据库物模型数据结构
         dbStructureData.defineThingModel(thingModel);
-        thingModelData.deleteById(productKey);
+        thingModelData.deleteById(id);
         return true;
     }
 
 
-
     @Override
     public boolean deleteCategory(String id) {
         categoryData.deleteById(id);
@@ -160,8 +156,6 @@ public class ProductServiceImpl implements IProductService {
     @SneakyThrows
 
     public String uploadImg(String productKey, MultipartFile file) {
-        productKey = getProduct(productKey).getId();
-
         String fileName = file.getOriginalFilename();
         String end = fileName.substring(fileName.lastIndexOf("."));
         if (ossClient == null) {
@@ -176,7 +170,7 @@ public class ProductServiceImpl implements IProductService {
         PutObjectResult result = ossClient.putObject(bucket, fileName,
                 file.getInputStream());
         return ossClient.generatePresignedUrl(bucket, fileName,
-                new Date(new Date().getTime() + 3600L * 1000 * 24 * 365 * 10)).toString();
+                new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10)).toString();
     }
 
     @Override
@@ -216,7 +210,6 @@ public class ProductServiceImpl implements IProductService {
         if (product == null) {
             throw new BizException(ErrCode.PRODUCT_NOT_FOUND);
         }
-        dataOwnerService.checkOwner(product);
 
         ProductModel oldScript = productModelData.findByModel(model);
         if (oldScript != null && !oldScript.getProductKey().equals(productKey)) {
@@ -229,13 +222,13 @@ public class ProductServiceImpl implements IProductService {
     }
 
     private Product getProduct(String productKey) {
-       return dataOwnerService.checkOwner(productData.findById(productKey));
+        return productData.findById(productKey);
     }
 
 
     /***********/
     private void checkProductOwner(String productKey) {
-        dataOwnerService.checkOwner(productData.findById(productKey));
+//        dataOwnerService.checkOwner(productData.findById(productKey));
     }
 
 }