【功能新增】IoT:产品新增时,productKey 由前端生成;同时增加 icon、picUrl 字段

This commit is contained in:
YunaiV 2024-12-07 19:50:07 +08:00
parent 9b9fd30c90
commit a8c87d168a
8 changed files with 60 additions and 41 deletions

View File

@ -11,7 +11,7 @@ public interface ErrorCodeConstants {
// ========== 产品相关 1-050-001-000 ============
ErrorCode PRODUCT_NOT_EXISTS = new ErrorCode(1_050_001_000, "产品不存在");
ErrorCode PRODUCT_IDENTIFICATION_EXISTS = new ErrorCode(1_050_001_001, "产品标识已经存在");
ErrorCode PRODUCT_KEY_EXISTS = new ErrorCode(1_050_001_001, "产品标识已经存在");
ErrorCode PRODUCT_STATUS_NOT_DELETE = new ErrorCode(1_050_001_002, "产品状是发布状态,不允许删除");
ErrorCode PRODUCT_STATUS_NOT_ALLOW_FUNCTION = new ErrorCode(1_050_001_003, "产品状是发布状态,不允许操作物模型");

View File

@ -18,12 +18,12 @@ public enum IotProductStatusEnum implements IntArrayValuable {
UNPUBLISHED(0, "开发中"),
PUBLISHED(1, "已发布");
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(IotProductStatusEnum::getType).toArray();
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(IotProductStatusEnum::getStatus).toArray();
/**
* 类型
*/
private final Integer type;
private final Integer status;
/**
* 描述
*/

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.iot.controller.admin.product;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@ -17,7 +18,10 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@Tag(name = "管理后台 - IoT 产品分类")
@RestController
@ -29,14 +33,14 @@ public class IotProductCategoryController {
private IotProductCategoryService productCategoryService;
@PostMapping("/create")
@Operation(summary = "创建IoT 产品分类")
@Operation(summary = "创建产品分类")
@PreAuthorize("@ss.hasPermission('iot:product-category:create')")
public CommonResult<Long> createProductCategory(@Valid @RequestBody IotProductCategorySaveReqVO createReqVO) {
return success(productCategoryService.createProductCategory(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新IoT 产品分类")
@Operation(summary = "更新产品分类")
@PreAuthorize("@ss.hasPermission('iot:product-category:update')")
public CommonResult<Boolean> updateProductCategory(@Valid @RequestBody IotProductCategorySaveReqVO updateReqVO) {
productCategoryService.updateProductCategory(updateReqVO);
@ -44,7 +48,7 @@ public class IotProductCategoryController {
}
@DeleteMapping("/delete")
@Operation(summary = "删除IoT 产品分类")
@Operation(summary = "删除产品分类")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('iot:product-category:delete')")
public CommonResult<Boolean> deleteProductCategory(@RequestParam("id") Long id) {
@ -53,7 +57,7 @@ public class IotProductCategoryController {
}
@GetMapping("/get")
@Operation(summary = "获得IoT 产品分类")
@Operation(summary = "获得产品分类")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('iot:product-category:query')")
public CommonResult<IotProductCategoryRespVO> getProductCategory(@RequestParam("id") Long id) {
@ -62,11 +66,21 @@ public class IotProductCategoryController {
}
@GetMapping("/page")
@Operation(summary = "获得IoT 产品分类分页")
@Operation(summary = "获得产品分类分页")
@PreAuthorize("@ss.hasPermission('iot:product-category:query')")
public CommonResult<PageResult<IotProductCategoryRespVO>> getProductCategoryPage(@Valid IotProductCategoryPageReqVO pageReqVO) {
PageResult<IotProductCategoryDO> pageResult = productCategoryService.getProductCategoryPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, IotProductCategoryRespVO.class));
}
@GetMapping("/simple-list")
@Operation(summary = "获得所有产品分类列表")
@PreAuthorize("@ss.hasPermission('iot:product-category:query')")
public CommonResult<List<IotProductCategoryRespVO>> getSimpleProductCategoryList() {
List<IotProductCategoryDO> list = productCategoryService.getProductCategoryListByStatus(
CommonStatusEnum.ENABLE.getStatus());
return success(convertList(list, category ->
new IotProductCategoryRespVO().setId(category.getId()).setName(category.getName())));
}
}

View File

@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.iot.controller.admin.product.vo.category.IotProdu
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductCategoryDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* IoT 产品分类 Mapper
*
@ -22,4 +24,8 @@ public interface IotProductCategoryMapper extends BaseMapperX<IotProductCategory
.orderByDesc(IotProductCategoryDO::getId));
}
default List<IotProductCategoryDO> selectListByStatus(Integer status) {
return selectList(IotProductCategoryDO::getStatus, status);
}
}

View File

@ -6,6 +6,8 @@ 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.List;
/**
* IoT 产品分类 Service 接口
*
@ -14,7 +16,7 @@ import jakarta.validation.Valid;
public interface IotProductCategoryService {
/**
* 创建IoT 产品分类
* 创建产品分类
*
* @param createReqVO 创建信息
* @return 编号
@ -22,33 +24,41 @@ public interface IotProductCategoryService {
Long createProductCategory(@Valid IotProductCategorySaveReqVO createReqVO);
/**
* 更新IoT 产品分类
* 更新产品分类
*
* @param updateReqVO 更新信息
*/
void updateProductCategory(@Valid IotProductCategorySaveReqVO updateReqVO);
/**
* 删除IoT 产品分类
* 删除产品分类
*
* @param id 编号
*/
void deleteProductCategory(Long id);
/**
* 获得IoT 产品分类
* 获得产品分类
*
* @param id 编号
* @return IoT 产品分类
* @return 产品分类
*/
IotProductCategoryDO getProductCategory(Long id);
/**
* 获得IoT 产品分类分页
* 获得产品分类分页
*
* @param pageReqVO 分页查询
* @return IoT 产品分类分页
* @return 产品分类分页
*/
PageResult<IotProductCategoryDO> getProductCategoryPage(IotProductCategoryPageReqVO pageReqVO);
/**
* 获得产品分类列表根据状态
*
* @param status 状态
* @return 产品分类列表
*/
List<IotProductCategoryDO> getProductCategoryListByStatus(Integer status);
}

View File

@ -10,6 +10,8 @@ import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_CATEGORY_NOT_EXISTS;
@ -67,4 +69,9 @@ public class IotProductCategoryServiceImpl implements IotProductCategoryService
return productCategoryMapper.selectPage(pageReqVO);
}
@Override
public List<IotProductCategoryDO> getProductCategoryListByStatus(Integer status) {
return productCategoryMapper.selectListByStatus(status);
}
}

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.iot.service.product;
import cn.hutool.core.util.StrUtil;
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.product.IotProductPageReqVO;
@ -11,14 +10,12 @@ import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
import cn.iocoder.yudao.module.iot.service.thinkmodelfunction.IotThinkModelFunctionService;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
@ -42,31 +39,16 @@ public class IotProductServiceImpl implements IotProductService {
@Override
public Long createProduct(IotProductSaveReqVO createReqVO) {
// 1. 生成 ProductKey
createProductKey(createReqVO);
if (productMapper.selectByProductKey(createReqVO.getProductKey()) != null) {
throw exception(PRODUCT_KEY_EXISTS);
}
// 2. 插入
IotProductDO product = BeanUtils.toBean(createReqVO, IotProductDO.class);
IotProductDO product = BeanUtils.toBean(createReqVO, IotProductDO.class)
.setStatus(IotProductStatusEnum.UNPUBLISHED.getStatus());
productMapper.insert(product);
return product.getId();
}
/**
* 创建 ProductKey
*
* @param createReqVO 创建信息
*/
private void createProductKey(IotProductSaveReqVO createReqVO) {
String productKey = createReqVO.getProductKey();
// 1. productKey为空生成随机的 11 位字符串
if (StrUtil.isEmpty(productKey)) {
productKey = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
}
// 2. 校验唯一性
if (productMapper.selectByProductKey(productKey) != null) {
throw exception(PRODUCT_IDENTIFICATION_EXISTS);
}
createReqVO.setProductKey(productKey);
}
@Override
public void updateProduct(IotProductSaveReqVO updateReqVO) {
updateReqVO.setProductKey(null); // 不更新产品标识
@ -98,7 +80,7 @@ public class IotProductServiceImpl implements IotProductService {
}
private void validateProductStatus(IotProductDO iotProductDO) {
if (Objects.equals(iotProductDO.getStatus(), IotProductStatusEnum.PUBLISHED.getType())) {
if (Objects.equals(iotProductDO.getStatus(), IotProductStatusEnum.PUBLISHED.getStatus())) {
throw exception(PRODUCT_STATUS_NOT_DELETE);
}
}
@ -121,7 +103,7 @@ public class IotProductServiceImpl implements IotProductService {
// 2. 更新
IotProductDO updateObj = IotProductDO.builder().id(id).status(status).build();
// 3. 产品是发布状态
if (Objects.equals(status, IotProductStatusEnum.PUBLISHED.getType())) {
if (Objects.equals(status, IotProductStatusEnum.PUBLISHED.getStatus())) {
// 3.1 创建超级表数据模型
thinkModelFunctionService.createSuperTableDataModel(id);
}

View File

@ -82,7 +82,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
private void validateProductStatus(Long createReqVO) {
IotProductDO product = productService.getProduct(createReqVO);
if (Objects.equals(product.getStatus(), IotProductStatusEnum.PUBLISHED.getType())) {
if (Objects.equals(product.getStatus(), IotProductStatusEnum.PUBLISHED.getStatus())) {
throw exception(PRODUCT_STATUS_NOT_ALLOW_FUNCTION);
}
}