修改:增加 IOT 前缀

This commit is contained in:
安浩浩 2024-09-08 15:53:53 +08:00
parent 1e69face92
commit a83ce3b667
11 changed files with 207 additions and 257 deletions

View File

@ -6,11 +6,11 @@ import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductPageReqVO; import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductRespVO; import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductSaveReqVO; import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.ProductDO; import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.service.product.ProductService; import cn.iocoder.yudao.module.iot.service.product.IotProductService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
@ -27,27 +27,26 @@ import java.util.List;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
// TODO @haohaoIot 前缀要加不然很容易重复哈
@Tag(name = "管理后台 - IOT 产品") @Tag(name = "管理后台 - IOT 产品")
@RestController @RestController
@RequestMapping("/iot/product") @RequestMapping("/iot/product")
@Validated @Validated
public class ProductController { public class IotProductController {
@Resource @Resource
private ProductService productService; private IotProductService productService;
@PostMapping("/create") @PostMapping("/create")
@Operation(summary = "创建产品") @Operation(summary = "创建产品")
@PreAuthorize("@ss.hasPermission('iot:product:create')") @PreAuthorize("@ss.hasPermission('iot:product:create')")
public CommonResult<Long> createProduct(@Valid @RequestBody ProductSaveReqVO createReqVO) { public CommonResult<Long> createProduct(@Valid @RequestBody IotProductSaveReqVO createReqVO) {
return success(productService.createProduct(createReqVO)); return success(productService.createProduct(createReqVO));
} }
@PutMapping("/update") @PutMapping("/update")
@Operation(summary = "更新产品") @Operation(summary = "更新产品")
@PreAuthorize("@ss.hasPermission('iot:product:update')") @PreAuthorize("@ss.hasPermission('iot:product:update')")
public CommonResult<Boolean> updateProduct(@Valid @RequestBody ProductSaveReqVO updateReqVO) { public CommonResult<Boolean> updateProduct(@Valid @RequestBody IotProductSaveReqVO updateReqVO) {
productService.updateProduct(updateReqVO); productService.updateProduct(updateReqVO);
return success(true); return success(true);
} }
@ -76,30 +75,17 @@ public class ProductController {
@Operation(summary = "获得产品") @Operation(summary = "获得产品")
@Parameter(name = "id", description = "编号", required = true, example = "1024") @Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('iot:product:query')") @PreAuthorize("@ss.hasPermission('iot:product:query')")
public CommonResult<ProductRespVO> getProduct(@RequestParam("id") Long id) { public CommonResult<IotProductRespVO> getProduct(@RequestParam("id") Long id) {
ProductDO product = productService.getProduct(id); IotProductDO product = productService.getProduct(id);
return success(BeanUtils.toBean(product, ProductRespVO.class)); return success(BeanUtils.toBean(product, IotProductRespVO.class));
} }
@GetMapping("/page") @GetMapping("/page")
@Operation(summary = "获得产品分页") @Operation(summary = "获得产品分页")
@PreAuthorize("@ss.hasPermission('iot:product:query')") @PreAuthorize("@ss.hasPermission('iot:product:query')")
public CommonResult<PageResult<ProductRespVO>> getProductPage(@Valid ProductPageReqVO pageReqVO) { public CommonResult<PageResult<IotProductRespVO>> getProductPage(@Valid IotProductPageReqVO pageReqVO) {
PageResult<ProductDO> pageResult = productService.getProductPage(pageReqVO); PageResult<IotProductDO> pageResult = productService.getProductPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, ProductRespVO.class)); return success(BeanUtils.toBean(pageResult, IotProductRespVO.class));
}
@GetMapping("/export-excel")
@Operation(summary = "导出产品 Excel")
@PreAuthorize("@ss.hasPermission('iot:product:export')")
@ApiAccessLog(operateType = EXPORT)
public void exportProductExcel(@Valid ProductPageReqVO pageReqVO,
HttpServletResponse response) throws IOException {
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
List<ProductDO> list = productService.getProductPage(pageReqVO).getList();
// 导出 Excel
ExcelUtils.write(response, "产品.xls", "数据", ProductRespVO.class,
BeanUtils.toBean(list, ProductRespVO.class));
} }
} }

View File

@ -0,0 +1,22 @@
package cn.iocoder.yudao.module.iot.controller.admin.product.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
// TODO @haohao涉及到 iot 的拼写要不都用 IoT貌似更规范
@Schema(description = "管理后台 - iot 产品分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class IotProductPageReqVO extends PageParam {
@Schema(description = "产品名称", example = "李四")
private String name;
@Schema(description = "产品标识")
private String productKey;
}

View File

@ -10,7 +10,7 @@ import java.time.LocalDateTime;
@Schema(description = "管理后台 - iot 产品 Response VO") @Schema(description = "管理后台 - iot 产品 Response VO")
@Data @Data
@ExcelIgnoreUnannotated @ExcelIgnoreUnannotated
public class ProductRespVO { public class IotProductRespVO {
@Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26087") @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "26087")
@ExcelProperty("产品ID") @ExcelProperty("产品ID")

View File

@ -3,13 +3,13 @@ package cn.iocoder.yudao.module.iot.controller.admin.product.vo;
import cn.iocoder.yudao.framework.common.validation.InEnum; import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.iot.enums.product.*; import cn.iocoder.yudao.module.iot.enums.product.*;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*; import jakarta.validation.constraints.NotEmpty;
import java.util.*; import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.*; import lombok.Data;
@Schema(description = "管理后台 - iot 产品新增/修改 Request VO") @Schema(description = "管理后台 - iot 产品新增/修改 Request VO")
@Data @Data
public class ProductSaveReqVO { public class IotProductSaveReqVO {
@Schema(description = "产品编号", requiredMode = Schema.RequiredMode.AUTO, example = "1") @Schema(description = "产品编号", requiredMode = Schema.RequiredMode.AUTO, example = "1")
private Long id; private Long id;
@ -26,15 +26,15 @@ public class ProductSaveReqVO {
@NotNull(message = "设备类型不能为空") @NotNull(message = "设备类型不能为空")
private Integer deviceType; private Integer deviceType;
@Schema(description = "联网方式", requiredMode = Schema.RequiredMode.REQUIRED,example = "0") @Schema(description = "联网方式", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
@InEnum(value = IotNetTypeEnum.class, message = "联网方式必须是 {value}") @InEnum(value = IotNetTypeEnum.class, message = "联网方式必须是 {value}")
private Integer netType; private Integer netType;
@Schema(description = "接入网关协议", requiredMode = Schema.RequiredMode.REQUIRED,example = "0") @Schema(description = "接入网关协议", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
@InEnum(value = IotProtocolTypeEnum.class, message = "接入网关协议必须是 {value}") @InEnum(value = IotProtocolTypeEnum.class, message = "接入网关协议必须是 {value}")
private Integer protocolType; private Integer protocolType;
@Schema(description = "数据格式",requiredMode = Schema.RequiredMode.REQUIRED, example = "0") @Schema(description = "数据格式", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
@InEnum(value = IotDataFormatEnum.class, message = "数据格式必须是 {value}") @InEnum(value = IotDataFormatEnum.class, message = "数据格式必须是 {value}")
@NotNull(message = "数据格式不能为空") @NotNull(message = "数据格式不能为空")
private Integer dataFormat; private Integer dataFormat;

View File

@ -1,59 +0,0 @@
package cn.iocoder.yudao.module.iot.controller.admin.product.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
// TODO @haohao涉及到 iot 的拼写要不都用 IoT貌似更规范
// TODO 芋艿需要清理掉一些无用字段
@Schema(description = "管理后台 - iot 产品分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ProductPageReqVO extends PageParam {
@Schema(description = "产品名称", example = "李四")
private String name;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
@Schema(description = "产品标识")
private String productKey;
@Schema(description = "接入网关协议", example = "2")
private Integer protocolType;
@Schema(description = "协议编号(脚本解析 id", example = "13177")
private Long protocolId;
@Schema(description = "产品所属品类标识符", example = "14237")
private Long categoryId;
@Schema(description = "产品描述", example = "你猜")
private String description;
@Schema(description = "数据校验级别", example = "1")
private Integer validateType;
@Schema(description = "产品状态", example = "1")
private Integer status;
@Schema(description = "设备类型", example = "2")
private Integer deviceType;
@Schema(description = "联网方式", example = "2")
private Integer netType;
@Schema(description = "数据格式", example = "0")
private Integer dataFormat;
}

View File

@ -19,7 +19,7 @@ import lombok.*;
@Builder @Builder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class ProductDO extends BaseDO { public class IotProductDO extends BaseDO {
/** /**
* 产品ID * 产品ID
@ -37,7 +37,7 @@ public class ProductDO extends BaseDO {
private String productKey; private String productKey;
/** /**
* 产品所属品类编号 * 产品所属品类编号
* * <p>
* TODO 外键后续加 * TODO 外键后续加
*/ */
private Long categoryId; private Long categoryId;
@ -48,44 +48,44 @@ public class ProductDO extends BaseDO {
/** /**
* 产品状态 * 产品状态
* * <p>
* 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum} * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum}
*/ */
private Integer status; private Integer status;
/** /**
* 设备类型 * 设备类型
* * <p>
* 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProductDeviceTypeEnum} * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProductDeviceTypeEnum}
*/ */
private Integer deviceType; private Integer deviceType;
/** /**
* 联网方式 * 联网方式
* * <p>
* 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotNetTypeEnum} * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotNetTypeEnum}
*/ */
private Integer netType; private Integer netType;
/** /**
* 接入网关协议 * 接入网关协议
* * <p>
* 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProtocolTypeEnum} * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotProtocolTypeEnum}
*/ */
private Integer protocolType; private Integer protocolType;
/** /**
* 协议编号 * 协议编号
* * <p>
* TODO 外键后续加 * TODO 外键后续加
*/ */
private Long protocolId; private Long protocolId;
/** /**
* 数据格式 * 数据格式
* * <p>
* 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotDataFormatEnum} * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotDataFormatEnum}
*/ */
private Integer dataFormat; private Integer dataFormat;
/** /**
* 数据校验级别 * 数据校验级别
* * <p>
* 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotValidateTypeEnum} * 枚举 {@link cn.iocoder.yudao.module.iot.enums.product.IotValidateTypeEnum}
*/ */
private Integer validateType; private Integer validateType;

View File

@ -0,0 +1,28 @@
package cn.iocoder.yudao.module.iot.dal.mysql.product;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductPageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import org.apache.ibatis.annotations.Mapper;
/**
* iot 产品 Mapper
*
* @author ahh
*/
@Mapper
public interface IotProductMapper extends BaseMapperX<IotProductDO> {
default PageResult<IotProductDO> selectPage(IotProductPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<IotProductDO>()
.likeIfPresent(IotProductDO::getName, reqVO.getName())
.likeIfPresent(IotProductDO::getProductKey, reqVO.getProductKey())
.orderByDesc(IotProductDO::getId));
}
default IotProductDO selectByProductKey(String productKey) {
return selectOne(new LambdaQueryWrapperX<IotProductDO>().eq(IotProductDO::getProductKey, productKey));
}
}

View File

@ -1,35 +0,0 @@
package cn.iocoder.yudao.module.iot.dal.mysql.product;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductPageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.ProductDO;
import org.apache.ibatis.annotations.Mapper;
/**
* iot 产品 Mapper
*
* @author ahh
*/
@Mapper
public interface ProductMapper extends BaseMapperX<ProductDO> {
default PageResult<ProductDO> selectPage(ProductPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<ProductDO>()
.likeIfPresent(ProductDO::getName, reqVO.getName())
.betweenIfPresent(ProductDO::getCreateTime, reqVO.getCreateTime())
.likeIfPresent(ProductDO::getProductKey, reqVO.getProductKey())
.eqIfPresent(ProductDO::getProtocolId, reqVO.getProtocolId())
.eqIfPresent(ProductDO::getCategoryId, reqVO.getCategoryId())
.eqIfPresent(ProductDO::getDescription, reqVO.getDescription())
.eqIfPresent(ProductDO::getValidateType, reqVO.getValidateType())
.eqIfPresent(ProductDO::getStatus, reqVO.getStatus())
.eqIfPresent(ProductDO::getDeviceType, reqVO.getDeviceType())
.eqIfPresent(ProductDO::getNetType, reqVO.getNetType())
.eqIfPresent(ProductDO::getProtocolType, reqVO.getProtocolType())
.eqIfPresent(ProductDO::getDataFormat, reqVO.getDataFormat())
.orderByDesc(ProductDO::getId));
}
}

View File

@ -1,9 +1,9 @@
package cn.iocoder.yudao.module.iot.service.product; package cn.iocoder.yudao.module.iot.service.product;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductPageReqVO; import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductSaveReqVO; import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.ProductDO; import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import jakarta.validation.Valid; import jakarta.validation.Valid;
/** /**
@ -11,7 +11,7 @@ import jakarta.validation.Valid;
* *
* @author ahh * @author ahh
*/ */
public interface ProductService { public interface IotProductService {
/** /**
* 创建产品 * 创建产品
@ -19,14 +19,14 @@ public interface ProductService {
* @param createReqVO 创建信息 * @param createReqVO 创建信息
* @return 编号 * @return 编号
*/ */
Long createProduct(@Valid ProductSaveReqVO createReqVO); Long createProduct(@Valid IotProductSaveReqVO createReqVO);
/** /**
* 更新产品 * 更新产品
* *
* @param updateReqVO 更新信息 * @param updateReqVO 更新信息
*/ */
void updateProduct(@Valid ProductSaveReqVO updateReqVO); void updateProduct(@Valid IotProductSaveReqVO updateReqVO);
/** /**
* 删除产品 * 删除产品
@ -41,7 +41,7 @@ public interface ProductService {
* @param id 编号 * @param id 编号
* @return 产品 * @return 产品
*/ */
ProductDO getProduct(Long id); IotProductDO getProduct(Long id);
/** /**
* 获得产品分页 * 获得产品分页
@ -49,7 +49,7 @@ public interface ProductService {
* @param pageReqVO 分页查询 * @param pageReqVO 分页查询
* @return 产品分页 * @return 产品分页
*/ */
PageResult<ProductDO> getProductPage(ProductPageReqVO pageReqVO); PageResult<IotProductDO> getProductPage(IotProductPageReqVO pageReqVO);
/** /**
* 更新产品状态 * 更新产品状态

View File

@ -0,0 +1,117 @@
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.IotProductPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.IotProductSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductMapper;
import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
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.PRODUCT_NOT_EXISTS;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_STATUS_NOT_DELETE;
/**
* IOT 产品 Service 实现类
*
* @author ahh
*/
@Service
@Validated
public class IotProductServiceImpl implements IotProductService {
@Resource
private IotProductMapper productMapper;
@Override
public Long createProduct(IotProductSaveReqVO createReqVO) {
// 1. 生成 ProductKey
createProductKey(createReqVO);
// 2. 插入
IotProductDO product = BeanUtils.toBean(createReqVO, IotProductDO.class);
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_NOT_EXISTS);
}
createReqVO.setProductKey(productKey);
}
@Override
public void updateProduct(IotProductSaveReqVO updateReqVO) {
updateReqVO.setProductKey(null); // 不更新产品标识
// 1.1 校验存在
IotProductDO iotProductDO = validateProductExists(updateReqVO.getId());
// 1.2 发布状态不可更新
validateProductStatus(iotProductDO);
// 2. 更新
IotProductDO updateObj = BeanUtils.toBean(updateReqVO, IotProductDO.class);
productMapper.updateById(updateObj);
}
@Override
public void deleteProduct(Long id) {
// 1.1 校验存在
IotProductDO iotProductDO = validateProductExists(id);
// 1.2 发布状态不可删除
validateProductStatus(iotProductDO);
// 2. 删除
productMapper.deleteById(id);
}
private IotProductDO validateProductExists(Long id) {
IotProductDO iotProductDO = productMapper.selectById(id);
if (iotProductDO == null) {
throw exception(PRODUCT_NOT_EXISTS);
}
return iotProductDO;
}
private void validateProductStatus(IotProductDO iotProductDO) {
if (Objects.equals(iotProductDO.getStatus(), IotProductStatusEnum.PUBLISHED.getType())) {
throw exception(PRODUCT_STATUS_NOT_DELETE);
}
}
@Override
public IotProductDO getProduct(Long id) {
return productMapper.selectById(id);
}
@Override
public PageResult<IotProductDO> getProductPage(IotProductPageReqVO pageReqVO) {
return productMapper.selectPage(pageReqVO);
}
@Override
public void updateProductStatus(Long id, Integer status) {
// 1. 校验存在
validateProductExists(id);
// 2. 更新
IotProductDO updateObj = IotProductDO.builder().id(id).status(status).build();
productMapper.updateById(updateObj);
}
}

View File

@ -1,109 +0,0 @@
package cn.iocoder.yudao.module.iot.service.product;
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.ProductPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.ProductSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.ProductDO;
import cn.iocoder.yudao.module.iot.dal.mysql.product.ProductMapper;
import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
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.PRODUCT_NOT_EXISTS;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_STATUS_NOT_DELETE;
/**
* IOT 产品 Service 实现类
*
* @author ahh
*/
@Service
@Validated
public class ProductServiceImpl implements ProductService {
@Resource
private ProductMapper productMapper;
@Override
public Long createProduct(ProductSaveReqVO createReqVO) {
// 生成 ProductKey
createProductKey(createReqVO);
// 插入
ProductDO product = BeanUtils.toBean(createReqVO, ProductDO.class);
productMapper.insert(product);
return product.getId();
}
/**
* 创建 ProductKey
*
* @param createReqVO 创建信息
*/
private void createProductKey(ProductSaveReqVO createReqVO) {
// TODO @haohao应该前端没传递的时候才生成哇ps需要校验下唯一性万一有重复
// 生成随机的 11 位字符串
String productKey = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
createReqVO.setProductKey(productKey);
}
@Override
public void updateProduct(ProductSaveReqVO updateReqVO) {
updateReqVO.setProductKey(null); // 不更新产品标识
// 校验存在
validateProductExists(updateReqVO.getId());
// TODO @haohao如果已经发布允许编辑么
// 更新
ProductDO updateObj = BeanUtils.toBean(updateReqVO, ProductDO.class);
productMapper.updateById(updateObj);
}
@Override
public void deleteProduct(Long id) {
// TODO @haohao这里最好只查询一次哈
// 1.1 校验存在
validateProductExists(id);
// 1.2 发布状态不可删除
validateProductStatus(id);
// 2. 删除
productMapper.deleteById(id);
}
private void validateProductExists(Long id) {
if (productMapper.selectById(id) == null) {
throw exception(PRODUCT_NOT_EXISTS);
}
}
private void validateProductStatus(Long id) {
ProductDO product = productMapper.selectById(id);
if (Objects.equals(product.getStatus(), IotProductStatusEnum.PUBLISHED.getType())) {
throw exception(PRODUCT_STATUS_NOT_DELETE);
}
}
@Override
public ProductDO getProduct(Long id) {
return productMapper.selectById(id);
}
@Override
public PageResult<ProductDO> getProductPage(ProductPageReqVO pageReqVO) {
return productMapper.selectPage(pageReqVO);
}
@Override
public void updateProductStatus(Long id, Integer status) {
// 校验存在
validateProductExists(id);
// 更新
ProductDO updateObj = ProductDO.builder().id(id).status(status).build();
productMapper.updateById(updateObj);
}
}