From 335c19e62dac377da8252022e7366c1a77be6c1a Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Thu, 7 Mar 2019 00:37:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20spu=20attr=20=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admins/AdminsProductAttrController.java | 34 +++++-- .../convert/ProductAttrConvert.java | 8 ++ .../vo/admins/AdminsProductAttrVO.java | 35 +++++++ .../mall/product/api/ProductAttrService.java | 17 +++- .../mall/product/api/bo/ProductAttrBO.java | 62 ++++++++++++ .../product/api/bo/ProductAttrValueBO.java | 62 ++++++++++++ .../api/constant/ProductErrorCodeEnum.java | 3 +- .../product/api/dto/ProductAttrAddDTO.java | 25 +++++ .../product/api/dto/ProductAttrUpdateDTO.java | 40 ++++++++ .../api/dto/ProductAttrValueAddDTO.java | 40 ++++++++ .../api/dto/ProductAttrValueUpdateDTO.java | 42 +++++++++ .../product/convert/ProductAttrConvert.java | 21 ++++- .../mall/product/dao/ProductAttrMapper.java | 6 ++ .../service/ProductAttrServiceImpl.java | 94 +++++++++++++++++-- .../resources/mapper/ProductAttrMapper.xml | 67 ++++++------- 15 files changed, 497 insertions(+), 59 deletions(-) create mode 100644 product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductAttrBO.java create mode 100644 product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductAttrValueBO.java create mode 100644 product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrAddDTO.java create mode 100644 product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrUpdateDTO.java create mode 100644 product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrValueAddDTO.java create mode 100644 product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrValueUpdateDTO.java diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductAttrController.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductAttrController.java index aab184f00..f470bd867 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductAttrController.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductAttrController.java @@ -1,13 +1,18 @@ package cn.iocoder.mall.product.application.controller.admins; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder; import cn.iocoder.mall.product.api.ProductAttrService; +import cn.iocoder.mall.product.api.bo.ProductAttrBO; import cn.iocoder.mall.product.api.bo.ProductAttrPageBO; import cn.iocoder.mall.product.api.bo.ProductAttrSimpleBO; +import cn.iocoder.mall.product.api.dto.ProductAttrAddDTO; import cn.iocoder.mall.product.api.dto.ProductAttrPageDTO; +import cn.iocoder.mall.product.api.dto.ProductAttrUpdateDTO; import cn.iocoder.mall.product.application.convert.ProductAttrConvert; import cn.iocoder.mall.product.application.vo.admins.AdminsProductAttrPageVO; import cn.iocoder.mall.product.application.vo.admins.AdminsProductAttrSimpleVO; +import cn.iocoder.mall.product.application.vo.admins.AdminsProductAttrVO; import com.alibaba.dubbo.config.annotation.Reference; import io.swagger.annotations.Api; import org.springframework.web.bind.annotation.*; @@ -43,29 +48,44 @@ public class AdminsProductAttrController { } @PostMapping("/attr/add") - public CommonResult addAttr() { - return null; + public CommonResult addAttr(@RequestParam("name") String name) { + // 创建 ProductAttrAddDTO 对象 + ProductAttrAddDTO productAttrAddDTO = new ProductAttrAddDTO().setName(name); + // 添加 + CommonResult result = productAttrService.addProductAttr(AdminSecurityContextHolder.getContext().getAdminId(), productAttrAddDTO); + // 返回结果 + return ProductAttrConvert.INSTANCE.convert3(result); } - public CommonResult updateAttr() { - return null; + @PostMapping("/attr/update") + public CommonResult updateAttr(@RequestParam("id") Integer id, + @RequestParam("name") String name) { + // 创建 ProductAttrUpdateDTO 对象 + ProductAttrUpdateDTO productAttrUpdateDTO = new ProductAttrUpdateDTO().setId(id).setName(name); + // 更新 + return productAttrService.updateProductAttr(AdminSecurityContextHolder.getContext().getAdminId(), productAttrUpdateDTO); } - public CommonResult updateAttrStatus() { - return null; + @PostMapping("/attr/update_status") + public CommonResult updateAttrStatus(@RequestParam("id") Integer id, + @RequestParam("status") Integer status) { + return productAttrService.updateProductAttrStatus(AdminSecurityContextHolder.getContext().getAdminId(), id, status); } // TODO 芋艿 暂时不考虑 delete Attr 。因为关联逻辑比较多 + @PostMapping("/attr_value/add") public CommonResult addAttrValue() { return null; } + @PostMapping("/attr_value/update") public CommonResult updateAttrValue() { return null; } - public CommonResult updateAttrValueStr() { + @PostMapping("/attr_value/update_status") + public CommonResult updateAttrValueStatus() { return null; } diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/convert/ProductAttrConvert.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/convert/ProductAttrConvert.java index 026caa4d4..0fc0fcb9f 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/convert/ProductAttrConvert.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/convert/ProductAttrConvert.java @@ -1,10 +1,12 @@ package cn.iocoder.mall.product.application.convert; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.product.api.bo.ProductAttrBO; import cn.iocoder.mall.product.api.bo.ProductAttrPageBO; import cn.iocoder.mall.product.api.bo.ProductAttrSimpleBO; import cn.iocoder.mall.product.application.vo.admins.AdminsProductAttrPageVO; import cn.iocoder.mall.product.application.vo.admins.AdminsProductAttrSimpleVO; +import cn.iocoder.mall.product.application.vo.admins.AdminsProductAttrVO; import org.mapstruct.Mapper; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; @@ -22,4 +24,10 @@ public interface ProductAttrConvert { @Mappings({}) CommonResult> convert(CommonResult> result); + @Mappings({}) + AdminsProductAttrVO convert3(ProductAttrBO productAttrBO); + + @Mappings({}) + CommonResult convert3(CommonResult productAttrBO); + } \ No newline at end of file diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/admins/AdminsProductAttrVO.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/admins/AdminsProductAttrVO.java index 5cbedeb18..c0767df56 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/admins/AdminsProductAttrVO.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/vo/admins/AdminsProductAttrVO.java @@ -17,4 +17,39 @@ public class AdminsProductAttrVO { @ApiModelProperty(value = "创建时间", required = true, example = "时间戳") private Date createTime; + public Integer getId() { + return id; + } + + public AdminsProductAttrVO setId(Integer id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public AdminsProductAttrVO setName(String name) { + this.name = name; + return this; + } + + public Integer getStatus() { + return status; + } + + public AdminsProductAttrVO setStatus(Integer status) { + this.status = status; + return this; + } + + public Date getCreateTime() { + return createTime; + } + + public AdminsProductAttrVO setCreateTime(Date createTime) { + this.createTime = createTime; + return this; + } } \ No newline at end of file diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductAttrService.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductAttrService.java index 07d69d49c..00dc3a588 100644 --- a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductAttrService.java +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductAttrService.java @@ -1,9 +1,11 @@ package cn.iocoder.mall.product.api; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.product.api.bo.ProductAttrBO; import cn.iocoder.mall.product.api.bo.ProductAttrPageBO; import cn.iocoder.mall.product.api.bo.ProductAttrSimpleBO; -import cn.iocoder.mall.product.api.dto.ProductAttrPageDTO; +import cn.iocoder.mall.product.api.bo.ProductAttrValueBO; +import cn.iocoder.mall.product.api.dto.*; import java.util.List; @@ -20,4 +22,17 @@ public interface ProductAttrService { */ CommonResult> getProductAttrList(); + CommonResult addProductAttr(Integer adminId, ProductAttrAddDTO productAttrAddDTO); + + CommonResult updateProductAttr(Integer adminId, ProductAttrUpdateDTO productAttrUpdateDTO); + + CommonResult updateProductAttrStatus(Integer adminId, Integer productAttrId, Integer status); + + + CommonResult addProductAttrValue(Integer adminId, ProductAttrValueAddDTO productAttrValueAddDTO); + + CommonResult updateProductAttrValue(Integer adminId, ProductAttrValueUpdateDTO productAttrValueUpdateDTO); + + CommonResult updateProductAttrValueStatus(Integer adminId, Integer productAttrValueId, Integer status); + } \ No newline at end of file diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductAttrBO.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductAttrBO.java new file mode 100644 index 000000000..1c1759624 --- /dev/null +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductAttrBO.java @@ -0,0 +1,62 @@ +package cn.iocoder.mall.product.api.bo; + +import java.util.Date; + +/** + * 商品规格 VO + */ +public class ProductAttrBO { + + /** + * 规格编号 + */ + private Integer id; + /** + * 规格名 + */ + private String name; + /** + * 状态 + */ + private Integer status; + /** + * 创建时间 + */ + private Date createTime; + + public Integer getId() { + return id; + } + + public ProductAttrBO setId(Integer id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public ProductAttrBO setName(String name) { + this.name = name; + return this; + } + + public Integer getStatus() { + return status; + } + + public ProductAttrBO setStatus(Integer status) { + this.status = status; + return this; + } + + public Date getCreateTime() { + return createTime; + } + + public ProductAttrBO setCreateTime(Date createTime) { + this.createTime = createTime; + return this; + } +} \ No newline at end of file diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductAttrValueBO.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductAttrValueBO.java new file mode 100644 index 000000000..c908cc218 --- /dev/null +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductAttrValueBO.java @@ -0,0 +1,62 @@ +package cn.iocoder.mall.product.api.bo; + +import java.util.Date; + +/** + * 商品规格值 VO + */ +public class ProductAttrValueBO { + + /** + * 规格值编号 + */ + private Integer id; + /** + * 规格值名 + */ + private String name; + /** + * 状态 + */ + private Integer status; + /** + * 创建时间 + */ + private Date createTime; + + public Integer getId() { + return id; + } + + public ProductAttrValueBO setId(Integer id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public ProductAttrValueBO setName(String name) { + this.name = name; + return this; + } + + public Integer getStatus() { + return status; + } + + public ProductAttrValueBO setStatus(Integer status) { + this.status = status; + return this; + } + + public Date getCreateTime() { + return createTime; + } + + public ProductAttrValueBO setCreateTime(Date createTime) { + this.createTime = createTime; + return this; + } +} \ No newline at end of file diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/constant/ProductErrorCodeEnum.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/constant/ProductErrorCodeEnum.java index 122cd2e54..7d6be43f5 100644 --- a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/constant/ProductErrorCodeEnum.java +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/constant/ProductErrorCodeEnum.java @@ -23,8 +23,9 @@ public enum ProductErrorCodeEnum { // ========== PRODUCT ATTR + ATTR_VALUE 模块 ========== PRODUCT_ATTR_VALUE_NOT_EXIST(1003003000, "商品属性值不存在"), - PRODUCT_ATTR_NOT_EXIST(1003003001, "商品属性值不存在"), + PRODUCT_ATTR_EXISTS(1003003002, "商品规格已经存在"), + PRODUCT_ATTR_STATUS_EQUALS(1003003003, "商品规格已经是该状态"), ; diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrAddDTO.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrAddDTO.java new file mode 100644 index 000000000..1990facde --- /dev/null +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrAddDTO.java @@ -0,0 +1,25 @@ +package cn.iocoder.mall.product.api.dto; + +import javax.validation.constraints.NotEmpty; + +/** + * Product 规格添加 DTO + */ +public class ProductAttrAddDTO { + + /** + * 名称 + */ + @NotEmpty(message = "规格名不能为空") + private String name; + + public String getName() { + return name; + } + + public ProductAttrAddDTO setName(String name) { + this.name = name; + return this; + } + +} \ No newline at end of file diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrUpdateDTO.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrUpdateDTO.java new file mode 100644 index 000000000..70ed8c3ef --- /dev/null +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrUpdateDTO.java @@ -0,0 +1,40 @@ +package cn.iocoder.mall.product.api.dto; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +/** + * Product 规格修改 DTO + */ +public class ProductAttrUpdateDTO { + + /** + * 规格编号 + */ + @NotNull(message = "规格编号不能为空") + private Integer id; + /** + * 名称 + */ + @NotEmpty(message = "规格名不能为空") + private String name; + + public String getName() { + return name; + } + + public ProductAttrUpdateDTO setName(String name) { + this.name = name; + return this; + } + + public Integer getId() { + return id; + } + + public ProductAttrUpdateDTO setId(Integer id) { + this.id = id; + return this; + } + +} \ No newline at end of file diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrValueAddDTO.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrValueAddDTO.java new file mode 100644 index 000000000..1d11c4f18 --- /dev/null +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrValueAddDTO.java @@ -0,0 +1,40 @@ +package cn.iocoder.mall.product.api.dto; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +/** + * Product 规格值添加 DTO + */ +public class ProductAttrValueAddDTO { + + /** + * 规格编号 + */ + @NotNull(message = "规格编号不能为空") + private Integer attrId; + /** + * 名称 + */ + @NotEmpty(message = "规格值名不能为空") + private String name; + + public Integer getAttrId() { + return attrId; + } + + public ProductAttrValueAddDTO setAttrId(Integer attrId) { + this.attrId = attrId; + return this; + } + + public String getName() { + return name; + } + + public ProductAttrValueAddDTO setName(String name) { + this.name = name; + return this; + } + +} \ No newline at end of file diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrValueUpdateDTO.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrValueUpdateDTO.java new file mode 100644 index 000000000..633d9f905 --- /dev/null +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrValueUpdateDTO.java @@ -0,0 +1,42 @@ +package cn.iocoder.mall.product.api.dto; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +/** + * Product 规格值修改 DTO + * + * 注意,不允许修改所属规格 + */ +public class ProductAttrValueUpdateDTO { + + /** + * 规格值编号 + */ + @NotNull(message = "规格编号不能为空") + private Integer id; + /** + * 名称 + */ + @NotEmpty(message = "规格名不能为空") + private String name; + + public String getName() { + return name; + } + + public ProductAttrValueUpdateDTO setName(String name) { + this.name = name; + return this; + } + + public Integer getId() { + return id; + } + + public ProductAttrValueUpdateDTO setId(Integer id) { + this.id = id; + return this; + } + +} \ No newline at end of file diff --git a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/convert/ProductAttrConvert.java b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/convert/ProductAttrConvert.java index 717f398e7..74625ad43 100644 --- a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/convert/ProductAttrConvert.java +++ b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/convert/ProductAttrConvert.java @@ -1,9 +1,8 @@ package cn.iocoder.mall.product.convert; -import cn.iocoder.mall.product.api.bo.ProductAttrDetailBO; -import cn.iocoder.mall.product.api.bo.ProductAttrSimpleBO; -import cn.iocoder.mall.product.api.bo.ProductAttrValueDetailBO; -import cn.iocoder.mall.product.api.bo.ProductAttrValueSimpleBO; +import cn.iocoder.mall.product.api.bo.*; +import cn.iocoder.mall.product.api.dto.ProductAttrAddDTO; +import cn.iocoder.mall.product.api.dto.ProductAttrUpdateDTO; import cn.iocoder.mall.product.dataobject.ProductAttrDO; import cn.iocoder.mall.product.dataobject.ProductAttrValueDO; import org.mapstruct.Mapper; @@ -34,5 +33,17 @@ public interface ProductAttrConvert { // @Mappings({}) List convert4(List values); - + + @Mappings({}) + ProductAttrDO convert(ProductAttrAddDTO productAttrAddDTO); + + @Mappings({}) + ProductAttrDO convert(ProductAttrUpdateDTO productAttrUpdateDTO); + + @Mappings({}) + ProductAttrBO convert(ProductAttrDO productAttrDO); + + @Mappings({}) + ProductAttrValueBO convert2(ProductAttrValueDO productAttrValueDO); + } \ No newline at end of file diff --git a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/dao/ProductAttrMapper.java b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/dao/ProductAttrMapper.java index 376d74af7..ca2726ca7 100644 --- a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/dao/ProductAttrMapper.java +++ b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/dao/ProductAttrMapper.java @@ -12,6 +12,8 @@ public interface ProductAttrMapper { ProductAttrDO selectById(@Param("id") Integer id); + ProductAttrDO selectByName(@Param("name") String name); + List selectListByIds(@Param("ids") Collection ids); List selectListByStatus(@Param("status") Integer status); @@ -22,4 +24,8 @@ public interface ProductAttrMapper { Integer selectCountByNameLike(@Param("name") String name); + void insert(ProductAttrDO productAttrDO); + + void update(ProductAttrDO productAttrDO); + } \ No newline at end of file diff --git a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductAttrServiceImpl.java b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductAttrServiceImpl.java index c478c93d2..646062c57 100644 --- a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductAttrServiceImpl.java +++ b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductAttrServiceImpl.java @@ -1,15 +1,14 @@ package cn.iocoder.mall.product.service; +import cn.iocoder.common.framework.constant.SysErrorCodeEnum; +import cn.iocoder.common.framework.dataobject.BaseDO; import cn.iocoder.common.framework.util.ServiceExceptionUtil; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.product.api.ProductAttrService; -import cn.iocoder.mall.product.api.bo.ProductAttrAndValuePairBO; -import cn.iocoder.mall.product.api.bo.ProductAttrDetailBO; -import cn.iocoder.mall.product.api.bo.ProductAttrPageBO; -import cn.iocoder.mall.product.api.bo.ProductAttrSimpleBO; +import cn.iocoder.mall.product.api.bo.*; import cn.iocoder.mall.product.api.constant.ProductAttrConstants; import cn.iocoder.mall.product.api.constant.ProductErrorCodeEnum; -import cn.iocoder.mall.product.api.dto.ProductAttrPageDTO; +import cn.iocoder.mall.product.api.dto.*; import cn.iocoder.mall.product.convert.ProductAttrConvert; import cn.iocoder.mall.product.dao.ProductAttrMapper; import cn.iocoder.mall.product.dao.ProductAttrValueMapper; @@ -20,10 +19,7 @@ import com.google.common.collect.Multimaps; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; /** @@ -114,4 +110,84 @@ public class ProductAttrServiceImpl implements ProductAttrService { return CommonResult.success(attrs); } + @Override + public CommonResult addProductAttr(Integer adminId, ProductAttrAddDTO productAttrAddDTO) { + // 校验规格名不重复 + if (productAttrMapper.selectByName(productAttrAddDTO.getName()) != null) { + return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_EXISTS.getCode()); + } + // 插入到数据库 + ProductAttrDO productAttrDO = ProductAttrConvert.INSTANCE.convert(productAttrAddDTO) + .setStatus(ProductAttrConstants.ATTR_STATUS_ENABLE); + productAttrDO.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO); + productAttrMapper.insert(productAttrDO); + // 返回成功 + return CommonResult.success(ProductAttrConvert.INSTANCE.convert(productAttrDO)); + } + + @Override + public CommonResult updateProductAttr(Integer adminId, ProductAttrUpdateDTO productAttrUpdateDTO) { + // 校验存在 + if (productAttrMapper.selectById(productAttrUpdateDTO.getId()) == null) { + return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_NOT_EXIST.getCode()); + } + // 校验规格名不重复 + ProductAttrDO existsAttrDO = productAttrMapper.selectByName(productAttrUpdateDTO.getName()); + if (existsAttrDO != null && !existsAttrDO.getId().equals(productAttrUpdateDTO.getId())) { + return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_EXISTS.getCode()); + } + // 更新到数据库 + ProductAttrDO updateProductAttr = ProductAttrConvert.INSTANCE.convert(productAttrUpdateDTO); + productAttrMapper.update(updateProductAttr); + // 返回成功 + return CommonResult.success(true); + } + + @Override + public CommonResult updateProductAttrStatus(Integer adminId, Integer productAttrId, Integer status) { + // 校验参数 + if (!isValidAttrStatus(status)) { + return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启(1)或关闭(2)"); // TODO 有点搓 + } + // 校验存在 + ProductAttrDO productAttrDO = productAttrMapper.selectById(productAttrId); + if (productAttrDO == null) { + return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_NOT_EXIST.getCode()); + } + // 校验状态 + if (productAttrDO.getStatus().equals(status)) { + return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_STATUS_EQUALS.getCode()); + } + // 更新到数据库 + ProductAttrDO updateProductAttr = new ProductAttrDO().setId(productAttrId).setStatus(status); + productAttrMapper.update(updateProductAttr); + // 返回成功 + return CommonResult.success(true); + } + + @Override + public CommonResult addProductAttrValue(Integer adminId, ProductAttrValueAddDTO productAttrValueAddDTO) { + return null; + } + + @Override + public CommonResult updateProductAttrValue(Integer adminId, ProductAttrValueUpdateDTO productAttrValueUpdateDTO) { + return null; + } + + @Override + public CommonResult updateProductAttrValueStatus(Integer adminId, Integer productAttrValueId, Integer status) { + return null; + } + + private boolean isValidAttrStatus(Integer status) { + return ProductAttrConstants.ATTR_STATUS_ENABLE.equals(status) + || ProductAttrConstants.ATTR_STATUS_DISABLE.equals(status); + } + + private boolean isValidAttrValueStatus(Integer status) { + return ProductAttrConstants.ATTR_VALUE_STATUS_ENABLE.equals(status) + || ProductAttrConstants.ATTR_VALUE_STATUS_DISABLE.equals(status); + } + } \ No newline at end of file diff --git a/product/product-service-impl/src/main/resources/mapper/ProductAttrMapper.xml b/product/product-service-impl/src/main/resources/mapper/ProductAttrMapper.xml index b6c377b01..374c138b9 100644 --- a/product/product-service-impl/src/main/resources/mapper/ProductAttrMapper.xml +++ b/product/product-service-impl/src/main/resources/mapper/ProductAttrMapper.xml @@ -21,43 +21,38 @@ AND deleted = 0 - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + INSERT INTO product_attr ( + name, status, create_time, deleted + ) VALUES ( + #{name}, #{status}, #{createTime}, #{deleted} + ) + + + + UPDATE product_attr + + + name = #{name}, + + + status = #{status}, + + + deleted = #{deleted} + + + WHERE id = #{id} +