Kaynağa Gözat

【功能新增】IoT:产品详情,优化相关展示

YunaiV 8 ay önce
ebeveyn
işleme
9841c869a2

+ 20 - 2
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/IotProductController.java

@@ -2,11 +2,14 @@ package cn.iocoder.yudao.module.iot.controller.admin.product;
 
 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
+import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.iot.controller.admin.product.vo.product.IotProductPageReqVO;
 import cn.iocoder.yudao.module.iot.controller.admin.product.vo.product.IotProductRespVO;
 import cn.iocoder.yudao.module.iot.controller.admin.product.vo.product.IotProductSaveReqVO;
+import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductCategoryDO;
 import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
+import cn.iocoder.yudao.module.iot.service.product.IotProductCategoryService;
 import cn.iocoder.yudao.module.iot.service.product.IotProductService;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
@@ -18,6 +21,7 @@ import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
+import java.util.Map;
 
 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@@ -30,6 +34,8 @@ public class IotProductController {
 
     @Resource
     private IotProductService productService;
+    @Resource
+    private IotProductCategoryService categoryService;
 
     @PostMapping("/create")
     @Operation(summary = "创建产品")
@@ -72,7 +78,13 @@ public class IotProductController {
     @PreAuthorize("@ss.hasPermission('iot:product:query')")
     public CommonResult<IotProductRespVO> getProduct(@RequestParam("id") Long id) {
         IotProductDO product = productService.getProduct(id);
-        return success(BeanUtils.toBean(product, IotProductRespVO.class));
+        // 拼接数据
+        IotProductCategoryDO category = categoryService.getProductCategory(product.getCategoryId());
+        return success(BeanUtils.toBean(product, IotProductRespVO.class, bean -> {
+            if (category != null) {
+                bean.setCategoryName(category.getName());
+            }
+        }));
     }
 
     @GetMapping("/page")
@@ -80,7 +92,13 @@ public class IotProductController {
     @PreAuthorize("@ss.hasPermission('iot:product:query')")
     public CommonResult<PageResult<IotProductRespVO>> getProductPage(@Valid IotProductPageReqVO pageReqVO) {
         PageResult<IotProductDO> pageResult = productService.getProductPage(pageReqVO);
-        return success(BeanUtils.toBean(pageResult, IotProductRespVO.class));
+        // 拼接数据
+        Map<Long, IotProductCategoryDO> categoryMap = categoryService.getProductCategoryMap(
+                convertList(pageResult.getList(), IotProductDO::getCategoryId));
+        return success(BeanUtils.toBean(pageResult, IotProductRespVO.class, bean -> {
+            MapUtils.findAndThen(categoryMap, bean.getCategoryId(),
+                    category -> bean.setCategoryName(category.getName()));
+        }));
     }
 
     @GetMapping("/simple-list")

+ 3 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/product/IotProductRespVO.java

@@ -27,6 +27,9 @@ public class IotProductRespVO {
     @Schema(description = "产品分类编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
     private Long categoryId;
 
+    @Schema(description = "产品分类名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
+    private String categoryName;
+
     @Schema(description = "产品图标", example = "https://iocoder.cn/1.svg")
     private String icon;
 

+ 22 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductCategoryService.java

@@ -6,7 +6,11 @@ import cn.iocoder.yudao.module.iot.controller.admin.product.vo.category.IotProdu
 import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductCategoryDO;
 import jakarta.validation.Valid;
 
+import java.util.Collection;
 import java.util.List;
+import java.util.Map;
+
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
 
 /**
  * IoT 产品分类 Service 接口
@@ -45,6 +49,24 @@ public interface IotProductCategoryService {
      */
     IotProductCategoryDO getProductCategory(Long id);
 
+    /**
+     * 获得产品分类列表
+     *
+     * @param ids 编号
+     * @return 产品分类列表
+     */
+    List<IotProductCategoryDO> getProductCategoryList(Collection<Long> ids);
+
+    /**
+     * 获得产品分类 Map
+     *
+     * @param ids 编号
+     * @return 产品分类 Map
+     */
+    default Map<Long, IotProductCategoryDO> getProductCategoryMap(Collection<Long> ids) {
+        return convertMap(getProductCategoryList(ids), IotProductCategoryDO::getId);
+    }
+
     /**
      * 获得产品分类分页
      *

+ 10 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductCategoryServiceImpl.java

@@ -1,5 +1,6 @@
 package cn.iocoder.yudao.module.iot.service.product;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.iot.controller.admin.product.vo.category.IotProductCategoryPageReqVO;
@@ -10,6 +11,7 @@ import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
 
+import java.util.Collection;
 import java.util.List;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -64,6 +66,14 @@ public class IotProductCategoryServiceImpl implements IotProductCategoryService
         return productCategoryMapper.selectById(id);
     }
 
+    @Override
+    public List<IotProductCategoryDO> getProductCategoryList(Collection<Long> ids) {
+        if (CollUtil.isEmpty(ids)) {
+            return CollUtil.newArrayList();
+        }
+        return productCategoryMapper.selectBatchIds(ids);
+    }
+
     @Override
     public PageResult<IotProductCategoryDO> getProductCategoryPage(IotProductCategoryPageReqVO pageReqVO) {
         return productCategoryMapper.selectPage(pageReqVO);