修改:更新产品状态

This commit is contained in:
安浩浩 2024-09-07 19:41:41 +08:00
parent 037ab39ac7
commit c7b1cc9a43
5 changed files with 42 additions and 2 deletions

View File

@ -12,4 +12,5 @@ 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_STATUS_NOT_DELETE = new ErrorCode(1_050_001_002, "产品状是发布状态,不允许删除");
}

View File

@ -6,13 +6,13 @@ import lombok.Getter;
/**
* IOT 产品状态枚举类
* 产品状态, 0: 未发布, 1: 已发布
* 产品状态, 0: 开发中, 1: 已发布
*/
@AllArgsConstructor
@Getter
public enum IotProductStatusEnum implements IntArrayValuable {
UNPUBLISHED(0, "未发布"),
UNPUBLISHED(0, "开发中"),
PUBLISHED(1, "已发布");
/**

View File

@ -51,6 +51,17 @@ public class ProductController {
return success(true);
}
@PutMapping("/update-status")
@Operation(summary = "更新产品状态")
@Parameter(name = "id", description = "编号", required = true)
@Parameter(name = "status", description = "状态", required = true)
@PreAuthorize("@ss.hasPermission('iot:product:update')")
public CommonResult<Boolean> updateProductStatus(@RequestParam("id") Long id,
@RequestParam("status") Integer status) {
productService.updateProductStatus(id, status);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除产品")
@Parameter(name = "id", description = "编号", required = true)

View File

@ -51,4 +51,11 @@ public interface ProductService {
*/
PageResult<ProductDO> getProductPage(ProductPageReqVO pageReqVO);
/**
* 更新产品状态
*
* @param id 编号
* @param status 状态
*/
void updateProductStatus(Long id, Integer status);
}

View File

@ -6,14 +6,17 @@ 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 实现类
@ -62,6 +65,8 @@ public class ProductServiceImpl implements ProductService {
public void deleteProduct(Long id) {
// 校验存在
validateProductExists(id);
// 发布状态不可删除
validateProductStatus(id);
// 删除
productMapper.deleteById(id);
}
@ -72,6 +77,13 @@ public class ProductServiceImpl implements ProductService {
}
}
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);
@ -82,4 +94,13 @@ public class ProductServiceImpl implements ProductService {
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);
}
}