فهرست منبع

add:添加产品删除

荭琪枫 2 سال پیش
والد
کامیت
a748b7c2ce

+ 3 - 1
iot-common/iot-common-core/src/main/java/cc/iotkit/common/enums/ErrCode.java

@@ -48,7 +48,7 @@ public enum ErrCode implements IEnum {
 
     /**
      * 大屏通用异常段
-     * */
+     */
     RESOURCE_FILE_NOT_FOUND(00000000, "资源包为空"),
     BIG_SCREEN_NOT_FOUND(00000000, "大屏不存在"),
     BIG_SCREEN_ALREADY(00000000, "大屏已存在"),
@@ -70,6 +70,8 @@ public enum ErrCode implements IEnum {
     DEVICE_NOT_FOUND(00000000, "设备不存在"),
     DEVICE_OFFLINE(00000000, "设备已离线"),
     DEVICE_ALREADY(00000000, "设备已存在"),
+
+    DEVICE_HAS_ASSOCIATED(00000000, "设备已关联"),
     MODEL_ALREADY(00000000, "型号已存在"),
     MODEL_SCRIPT_NOT_FOUND(00000000, "产品型号脚本不存在"),
     PRODUCT_MODEL_NOT_FOUND(00000000, "产品型号不存在"),

+ 9 - 0
iot-common/iot-common-dao/iot-data-service/src/main/java/cc/iotkit/data/manager/IDeviceInfoData.java

@@ -132,7 +132,16 @@ public interface IDeviceInfoData extends IOwnedData<DeviceInfo, String> {
 
     /**
      * 获取所有网关类型设备
+     *
      * @return
      */
     List<DeviceInfo> findByProductNodeType(String uid);
+
+    /**
+     * 是否存在product类型的设备
+     *
+     * @param productKey
+     * @return
+     */
+    boolean existByProductKey(String productKey);
 }

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

@@ -17,7 +17,7 @@ import java.util.List;
 /**
  * 产品接口
  */
-public interface IProductData extends ICommonData<Product,Long> {
+public interface IProductData extends ICommonData<Product, Long> {
 
     /**
      * 按品类取产品列表
@@ -26,4 +26,6 @@ public interface IProductData extends ICommonData<Product,Long> {
 
 
     Product findByProductKey(String name);
+
+    void delByProductKey(String productKey);
 }

+ 5 - 0
iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/DeviceInfoDataCache.java

@@ -135,6 +135,11 @@ public class DeviceInfoDataCache implements IDeviceInfoData, SmartInitializingSi
         return deviceInfoData.findByProductNodeType(uid);
     }
 
+    @Override
+    public boolean existByProductKey(String productKey) {
+        return deviceInfoData.existByProductKey(productKey);
+    }
+
     @Override
     public List<String> findSubDeviceIds(String parentId) {
         return deviceInfoData.findSubDeviceIds(parentId);

+ 5 - 0
iot-common/iot-common-dao/iot-data-serviceImpl-cache/src/main/java/cc/iotkit/data/service/DeviceInfoPropertyDataCache.java

@@ -176,6 +176,11 @@ public class DeviceInfoPropertyDataCache implements IDeviceInfoData {
         return deviceInfoData.findByProductNodeType(uid);
     }
 
+    @Override
+    public boolean existByProductKey(String productKey) {
+        return deviceInfoData.existByProductKey(productKey);
+    }
+
     @Override
     public List<DeviceInfo> findByUid(String uid) {
         return deviceInfoData.findByUid(uid);

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

@@ -29,11 +29,17 @@ public class ProductDataCache implements IProductData {
     }
 
     @Override
-    @Cacheable(value = Constants.CACHE_PRODUCT, key = "#root.method.name+#s", unless = "#result == null")
+    @Cacheable(value = Constants.CACHE_PRODUCT, key = "#root.targetClass+#productKey", unless = "#result == null")
     public Product findByProductKey(String productKey) {
         return productData.findByProductKey(productKey);
     }
 
+    @Override
+    @Cacheable(value = Constants.CACHE_PRODUCT, key = "#root.targetClass+#productKey", unless = "#result == null")
+    public void delByProductKey(String productKey) {
+
+    }
+
 //    @Override
 //    public List<Product> findByUid(String uid) {
 //        return productData.findByUid(uid);
@@ -76,6 +82,8 @@ public class ProductDataCache implements IProductData {
 
     @Override
     public void deleteById(Long s) {
+        Product product = findById(s);
+        delByProductKey(product.getProductKey());
         productData.deleteById(s);
         //清除缓存
         productCacheEvict.findById(s);

+ 11 - 6
iot-common/iot-common-dao/iot-data-serviceImpl-rdb/src/main/java/cc/iotkit/data/service/DeviceInfoDataImpl.java

@@ -132,7 +132,7 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
     private void parseStateToDto(TbDeviceInfo vo, DeviceInfo dto) {
         dto.setState(new DeviceInfo.State("online".equals(vo.getState()),
                 vo.getOnlineTime(), vo.getOfflineTime()));
-        dto.setLocate(new DeviceInfo.Locate(vo.getLongitude(),vo.getLatitude()));
+        dto.setLocate(new DeviceInfo.Locate(vo.getLongitude(), vo.getLatitude()));
     }
 
     /**
@@ -143,7 +143,7 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
         vo.setState(state.isOnline() ? "online" : "offline");
         vo.setOfflineTime(state.getOfflineTime());
         vo.setOnlineTime(state.getOnlineTime());
-        DeviceInfo.Locate locate=dto.getLocate();
+        DeviceInfo.Locate locate = dto.getLocate();
         vo.setLongitude(locate.getLongitude());
         vo.setLatitude(locate.getLatitude());
     }
@@ -198,9 +198,14 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
 
     @Override
     public List<DeviceInfo> findByProductNodeType(String uid) {
-        List<TbDeviceInfo> devices=jpaQueryFactory.select(tbDeviceInfo).from(tbDeviceInfo)
+        List<TbDeviceInfo> devices = jpaQueryFactory.select(tbDeviceInfo).from(tbDeviceInfo)
                 .join(tbProduct).on(tbProduct.nodeType.eq(0).and(tbDeviceInfo.productKey.eq(tbProduct.productKey))).fetch();
-        return MapstructUtils.convert(devices,DeviceInfo.class);
+        return MapstructUtils.convert(devices, DeviceInfo.class);
+    }
+
+    @Override
+    public boolean existByProductKey(String productKey) {
+        return Optional.ofNullable(jpaQueryFactory.selectOne().from(tbDeviceInfo).where(tbDeviceInfo.productKey.eq(productKey)).fetchOne()).orElse(0) > 0;
     }
 
     @Override
@@ -275,7 +280,7 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
                 .model(rs.getString("model"))
                 .secret(rs.getString("secret"))
                 .parentId(rs.getString("parent_id"))
-                .locate(new DeviceInfo.Locate(rs.getString("longitude"),rs.getString("latitude")))
+                .locate(new DeviceInfo.Locate(rs.getString("longitude"), rs.getString("latitude")))
                 .uid(rs.getString("uid"))
                 .state(new DeviceInfo.State(
                         "online".equals(rs.getString("state")),
@@ -384,7 +389,7 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
 
         //按品类分组求合
         rst.stream().collect(Collectors.groupingBy(DataItem::getName,
-                Collectors.summarizingLong(item -> (long) item.getValue())))
+                        Collectors.summarizingLong(item -> (long) item.getValue())))
                 .forEach((key, sum) -> stats.add(new DataItem(key, sum.getSum())));
 
         return stats;

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

@@ -7,6 +7,7 @@ import cc.iotkit.data.dao.ProductRepository;
 import cc.iotkit.data.manager.IProductData;
 import cc.iotkit.data.model.TbProduct;
 import cc.iotkit.model.product.Product;
+import com.querydsl.jpa.impl.JPAQueryFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Primary;
 import org.springframework.data.domain.Page;
@@ -16,12 +17,16 @@ import org.springframework.stereotype.Service;
 
 import java.util.List;
 
+import static cc.iotkit.data.model.QTbProduct.tbProduct;
+
 @Primary
 @Service
 public class ProductDataImpl implements IProductData, IJPACommData<Product, Long> {
 
     @Autowired
     private ProductRepository productRepository;
+    @Autowired
+    private JPAQueryFactory jpaQueryFactory;
 
     @Override
     public JpaRepository getBaseRepository() {
@@ -48,6 +53,11 @@ public class ProductDataImpl implements IProductData, IJPACommData<Product, Long
         return MapstructUtils.convert(productRepository.findByProductKey(productKey), Product.class);
     }
 
+    @Override
+    public void delByProductKey(String productKey) {
+        jpaQueryFactory.delete(tbProduct).where(tbProduct.productKey.eq(productKey)).execute();
+    }
+
     public List<Product> findByUid(String uid) {
         return MapstructUtils.convert(productRepository.findByUid(uid), Product.class);
     }
@@ -63,12 +73,6 @@ public class ProductDataImpl implements IProductData, IJPACommData<Product, Long
         return productRepository.countByUid(uid);
     }
 
-    @Override
-    @Deprecated
-    public Product findById(Long id) {
-        throw new IllegalStateException("Deprecated method");
-    }
-
     @Override
     public Product save(Product data) {
         productRepository.save(MapstructUtils.convert(data, TbProduct.class));

+ 13 - 6
iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/ProductController.java

@@ -44,14 +44,13 @@ import java.util.List;
 public class ProductController {
 
 
-
     @Autowired
     private IProductService productService;
 
     @PostMapping("/list")
     @ApiOperation("列表")
     public Paging<ProductVo> getProducts(@Validated @RequestBody
-            PageRequest<ProductBo> request) {
+                                         PageRequest<ProductBo> request) {
         return productService.selectPageList(request);
     }
 
@@ -74,13 +73,21 @@ public class ProductController {
 
     @ApiOperation("查看详情")
     @PostMapping(value = "/getDetail")
-    public ProductVo getDetail(@RequestBody  @Validated Request<String> request) {
+    public ProductVo getDetail(@RequestBody @Validated Request<String> request) {
         ProductVo dto = productService.getDetail(request.getData());
         return dto;
     }
+
+
+    @ApiOperation("删除产品")
+    @PostMapping(value = "/deleteProduct")
+    public boolean deleteProduct(@RequestBody @Validated Request<String> request) {
+        return productService.deleteProduct(request.getData());
+    }
+
     @PostMapping("/getThingModelByProductKey")
     @ApiOperation("查看物模型")
-    public ThingModelVo getThingModelByProductKey(@RequestBody  @Validated Request<String> request) {
+    public ThingModelVo getThingModelByProductKey(@RequestBody @Validated Request<String> request) {
         return productService.getThingModelByProductKey(request.getData());
     }
 
@@ -94,7 +101,7 @@ public class ProductController {
     @ApiOperation("删除物模型")
     @Log(title = "物模型", businessType = BusinessType.DELETE)
     public boolean deleteThingModel(@Validated @RequestBody Request<Long> id) {
-       return productService.deleteThingModel(id.getData());
+        return productService.deleteThingModel(id.getData());
     }
 
 
@@ -130,7 +137,7 @@ public class ProductController {
     @PostMapping("/uploadImg/{productKey}")
     public String uploadImg(@PathVariable("productKey") String productKey,
                             @RequestParam("file") MultipartFile file) {
-       return productService.uploadImg(productKey,file);
+        return productService.uploadImg(productKey, file);
     }
 
     @PostMapping("/getModelsByProductKey")

+ 30 - 30
iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/bo/product/ProductBo.java

@@ -14,45 +14,45 @@ import lombok.EqualsAndHashCode;
 @Data
 @EqualsAndHashCode(callSuper = true)
 @AutoMapper(target = Product.class, reverseConvertGenerate = false)
-public class ProductBo extends BaseDto  {
+public class ProductBo extends BaseDto {
 
-	private static final long serialVersionUID = -1L;
+    private static final long serialVersionUID = -1L;
 
-	@ApiModelProperty(value="id")
-	private Long id;
-	@ApiModelProperty(value="productKey")
-	private String productKey;
+    @ApiModelProperty(value = "id")
+    private Long id;
+    @ApiModelProperty(value = "productKey")
+    private String productKey;
 
-	@ApiModelProperty(value="品类")
-	@Size(max = 255, message = "品类长度不正确")
-    	private String category;
+    @ApiModelProperty(value = "品类")
+    @Size(max = 255, message = "品类长度不正确")
+    private String category;
 
-    	@ApiModelProperty(value="创建时间")
-    	private Long createAt;
+    @ApiModelProperty(value = "创建时间")
+    private Long createAt;
 
-    	@ApiModelProperty(value="图片")
-	@Size(max = 255, message = "图片长度不正确")
-    	private String img;
+    @ApiModelProperty(value = "图片")
+    @Size(max = 255, message = "图片长度不正确")
+    private String img;
 
-    	@ApiModelProperty(value="产品名称")
-	@Size(max = 255, message = "产品名称长度不正确")
-    	private String name;
+    @ApiModelProperty(value = "产品名称")
+    @Size(max = 255, message = "产品名称长度不正确")
+    private String name;
 
-    	@ApiModelProperty(value="节点类型")
-    	private Integer nodeType;
+    @ApiModelProperty(value = "节点类型")
+    private Integer nodeType;
 
-    	@ApiModelProperty(value="是否透传,true/false")
-	@Size(max = 255, message = "是否透传,true/false长度不正确")
-    	private Boolean transparent;
+    @ApiModelProperty(value = "是否透传,true/false")
+    @Size(max = 255, message = "是否透传,true/false长度不正确")
+    private Boolean transparent;
 
-	@ApiModelProperty(value="是否开启设备定位,true/false")
-	private Boolean isOpenLocate;
+    @ApiModelProperty(value = "是否开启设备定位,true/false")
+    private Boolean isOpenLocate;
 
-	@ApiModelProperty(value="定位更新方式")
-	private String locateUpdateType;
+    @ApiModelProperty(value = "定位更新方式")
+    private String locateUpdateType;
 
-    	@ApiModelProperty(value="用户ID")
-	@Size(max = 255, message = "用户ID长度不正确")
-    	private String uid;
+    @ApiModelProperty(value = "用户ID")
+    @Size(max = 255, message = "用户ID长度不正确")
+    private String uid;
 
-    }
+}

+ 2 - 0
iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IProductService.java

@@ -27,6 +27,8 @@ public interface IProductService {
 
     ProductVo getDetail(String data);
 
+    boolean deleteProduct(String productKey);
+
     ThingModelVo getThingModelByProductKey(String data);
 
     boolean saveThingModel(ThingModelBo data);

+ 20 - 4
iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/ProductServiceImpl.java

@@ -6,10 +6,7 @@ import cc.iotkit.common.enums.ErrCode;
 import cc.iotkit.common.exception.BizException;
 import cc.iotkit.common.utils.JsonUtils;
 import cc.iotkit.common.utils.MapstructUtils;
-import cc.iotkit.data.manager.ICategoryData;
-import cc.iotkit.data.manager.IProductData;
-import cc.iotkit.data.manager.IProductModelData;
-import cc.iotkit.data.manager.IThingModelData;
+import cc.iotkit.data.manager.*;
 import cc.iotkit.manager.config.AliyunConfig;
 import cc.iotkit.manager.dto.bo.category.CategoryBo;
 import cc.iotkit.manager.dto.bo.product.ProductBo;
@@ -43,6 +40,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import java.util.Date;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * @Author: jay
@@ -65,6 +63,7 @@ public class ProductServiceImpl implements IProductService {
     @Autowired
     @Qualifier("categoryDataCache")
     private ICategoryData categoryData;
+
     @Autowired
     private DataOwnerService dataOwnerService;
     @Autowired
@@ -75,6 +74,9 @@ public class ProductServiceImpl implements IProductService {
     @Autowired
     private IDbStructureData dbStructureData;
 
+    @Autowired
+    private IDeviceInfoData deviceInfoData;
+
     private OSS ossClient;
 
     @Override
@@ -107,6 +109,20 @@ public class ProductServiceImpl implements IProductService {
         return MapstructUtils.convert(productData.findByProductKey(productKey), ProductVo.class);
     }
 
+    @Override
+    public boolean deleteProduct(String productKey) {
+        Product product = productData.findByProductKey(productKey);
+        if (Objects.isNull(product)) {
+            throw new BizException(ErrCode.PRODUCT_NOT_FOUND);
+        }
+        boolean exist = deviceInfoData.existByProductKey(productKey);
+        if (exist) {
+            throw new BizException(ErrCode.DEVICE_HAS_ASSOCIATED);
+        }
+        productData.deleteById(product.getId());
+        return true;
+    }
+
     @Override
     public ThingModelVo getThingModelByProductKey(String productKey) {
         ThingModel thingModel = thingModelData.findByProductKey(productKey);