商品分类的迁移,未完成,先提交下~
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.mall.productservice;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ProductServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProductServiceApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.iocoder.mall.productservice.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
||||
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@MapperScan("cn.iocoder.mall.productservice.dal.mysql.mapper") // 扫描对应的 Mapper 接口
|
||||
@EnableTransactionManagement(proxyTargetClass = true) // 启动事务管理。
|
||||
public class DatabaseConfiguration {
|
||||
|
||||
// 数据库连接池 Druid
|
||||
|
||||
@Bean
|
||||
public ISqlInjector sqlInjector() {
|
||||
return new DefaultSqlInjector(); // MyBatis Plus 逻辑删除
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
return new PaginationInterceptor(); // MyBatis Plus 分页插件
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.iocoder.mall.productservice.convert.category;
|
||||
|
||||
import cn.iocoder.mall.productservice.dal.mysql.dataobject.category.ProductCategoryDO;
|
||||
import cn.iocoder.mall.productservice.service.category.bo.ProductCategoryBO;
|
||||
import cn.iocoder.mall.productservice.service.category.bo.ProductCategoryCreateBO;
|
||||
import cn.iocoder.mall.productservice.service.category.bo.ProductCategoryUpdateBO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ProductCategoryConvert {
|
||||
|
||||
ProductCategoryConvert INSTANCE = Mappers.getMapper(ProductCategoryConvert.class);
|
||||
|
||||
ProductCategoryDO convert(ProductCategoryCreateBO bean);
|
||||
|
||||
ProductCategoryBO convert(ProductCategoryDO bean);
|
||||
|
||||
List<ProductCategoryBO> convertList(List<ProductCategoryDO> list);
|
||||
|
||||
ProductCategoryDO convert(ProductCategoryUpdateBO bean);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package cn.iocoder.mall.productservice.dal.mysql.dataobject.category;
|
||||
|
||||
import cn.iocoder.common.framework.enums.CommonStatusEnum;
|
||||
import cn.iocoder.mall.mybatis.core.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* 商品分类表
|
||||
*/
|
||||
@TableName("product_category")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Accessors(chain = true)
|
||||
public class ProductCategoryDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 分类编号
|
||||
*/
|
||||
@TableId
|
||||
private Integer id;
|
||||
/**
|
||||
* 父分类编号
|
||||
*/
|
||||
private Integer pid;
|
||||
/**
|
||||
* 分类名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 分类描述
|
||||
*/
|
||||
private String description;
|
||||
/**
|
||||
* 分类图片
|
||||
*/
|
||||
private String picUrl;
|
||||
/**
|
||||
* 分类排序
|
||||
*/
|
||||
private Integer sort;
|
||||
/**
|
||||
* 状态
|
||||
*
|
||||
* 枚举 {@link CommonStatusEnum}
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.mall.productservice.dal.mysql.mapper.category;
|
||||
|
||||
import cn.iocoder.mall.productservice.dal.mysql.dataobject.category.ProductCategoryDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ProductCategoryMapper extends BaseMapper<ProductCategoryDO> {
|
||||
|
||||
default Integer selectCountByPid(Integer pid) {
|
||||
return selectCount(new QueryWrapper<ProductCategoryDO>().eq("pid", pid));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package cn.iocoder.mall.productservice.service.category;
|
||||
|
||||
import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.mall.productservice.convert.category.ProductCategoryConvert;
|
||||
import cn.iocoder.mall.productservice.dal.mysql.dataobject.category.ProductCategoryDO;
|
||||
import cn.iocoder.mall.productservice.dal.mysql.mapper.category.ProductCategoryMapper;
|
||||
import cn.iocoder.mall.productservice.enums.category.ProductCategoryIdEnum;
|
||||
import cn.iocoder.mall.productservice.service.category.bo.ProductCategoryBO;
|
||||
import cn.iocoder.mall.productservice.service.category.bo.ProductCategoryCreateBO;
|
||||
import cn.iocoder.mall.productservice.service.category.bo.ProductCategoryUpdateBO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.mall.productservice.enums.ProductErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 商品分类表 Service
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
public class ProductCategoryService {
|
||||
|
||||
@Autowired
|
||||
private ProductCategoryMapper productCategoryMapper;
|
||||
|
||||
/**
|
||||
* 创建商品分类表
|
||||
*
|
||||
* @param createBO 创建商品分类表 BO
|
||||
* @return 商品分类表
|
||||
*/
|
||||
public ProductCategoryBO createProductCategory(@Valid ProductCategoryCreateBO createBO) {
|
||||
// 校验父分类
|
||||
validParent(createBO.getPid());
|
||||
// 插入到数据库
|
||||
ProductCategoryDO productCategoryDO = ProductCategoryConvert.INSTANCE.convert(createBO);
|
||||
productCategoryMapper.insert(productCategoryDO);
|
||||
// 返回
|
||||
return ProductCategoryConvert.INSTANCE.convert(productCategoryDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新商品分类表
|
||||
*
|
||||
* @param updateBO 更新商品分类表 BO
|
||||
*/
|
||||
public void updateProductCategory(@Valid ProductCategoryUpdateBO updateBO) {
|
||||
// 校验父分类
|
||||
validParent(updateBO.getPid());
|
||||
// 校验不能设置自己为父分类
|
||||
if (updateBO.getId().equals(updateBO.getPid())) {
|
||||
throw ServiceExceptionUtil.exception(PRODUCT_CATEGORY_PARENT_NOT_SELF);
|
||||
}
|
||||
// 校验更新的商品分类表是否存在
|
||||
if (productCategoryMapper.selectById(updateBO.getId()) == null) {
|
||||
throw ServiceExceptionUtil.exception(PRODUCT_CATEGORY_NOT_EXISTS);
|
||||
}
|
||||
// 更新到数据库
|
||||
ProductCategoryDO updateObject = ProductCategoryConvert.INSTANCE.convert(updateBO);
|
||||
productCategoryMapper.updateById(updateObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除商品分类表
|
||||
*
|
||||
* @param productCategoryId 商品分类表编号
|
||||
*/
|
||||
public void deleteProductCategory(Integer productCategoryId) {
|
||||
// 校验删除的商品分类表是否存在
|
||||
if (productCategoryMapper.selectById(productCategoryId) == null) {
|
||||
throw ServiceExceptionUtil.exception(PRODUCT_CATEGORY_NOT_EXISTS);
|
||||
}
|
||||
// 只有不存在子分类才可以删除
|
||||
Integer childCount = productCategoryMapper.selectCountByPid(productCategoryId);
|
||||
if (childCount > 0) {
|
||||
throw ServiceExceptionUtil.exception(PRODUCT_CATEGORY_DELETE_ONLY_NO_CHILD);
|
||||
}
|
||||
// TODO 芋艿 补充只有不存在商品才可以删除
|
||||
// 标记删除
|
||||
productCategoryMapper.deleteById(productCategoryId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得商品分类表
|
||||
*
|
||||
* @param productCategoryId 商品分类表编号
|
||||
* @return 商品分类表
|
||||
*/
|
||||
public ProductCategoryBO getProductCategory(Integer productCategoryId) {
|
||||
ProductCategoryDO productCategoryDO = productCategoryMapper.selectById(productCategoryId);
|
||||
return ProductCategoryConvert.INSTANCE.convert(productCategoryDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得商品分类表列表
|
||||
*
|
||||
* @param productCategoryIds 商品分类表编号列表
|
||||
* @return 商品分类表列表
|
||||
*/
|
||||
public List<ProductCategoryBO> listProductCategories(List<Integer> productCategoryIds) {
|
||||
List<ProductCategoryDO> productCategoryDOs = productCategoryMapper.selectBatchIds(productCategoryIds);
|
||||
return ProductCategoryConvert.INSTANCE.convertList(productCategoryDOs);
|
||||
}
|
||||
|
||||
private void validParent(Integer pid) {
|
||||
if (!ProductCategoryIdEnum.ROOT.getId().equals(pid)) {
|
||||
ProductCategoryDO parentCategory = productCategoryMapper.selectById(pid);
|
||||
// 校验父分类是否存在
|
||||
if (parentCategory == null) {
|
||||
throw ServiceExceptionUtil.exception(PRODUCT_CATEGORY_PARENT_NOT_EXISTS);
|
||||
}
|
||||
// 父分类必须是一级分类
|
||||
if (!ProductCategoryIdEnum.ROOT.getId().equals(parentCategory.getPid())) {
|
||||
throw ServiceExceptionUtil.exception((PRODUCT_CATEGORY_PARENT_CAN_NOT_BE_LEVEL2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public List<ProductCategoryBO> getListByPid(Integer pid) {
|
||||
// List<ProductCategoryDO> categoryList = productCategoryMapper.selectListByPidAndStatusOrderBySort(pid, ProductCategoryConstants.STATUS_ENABLE);
|
||||
// return ProductCategoryConvert.INSTANCE.convertToBO(categoryList);
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package cn.iocoder.mall.productservice.service.category.bo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 商品分类表 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class ProductCategoryBO {
|
||||
|
||||
/**
|
||||
* 分类编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 父分类编号
|
||||
*/
|
||||
private Integer pid;
|
||||
/**
|
||||
* 分类名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 分类描述
|
||||
*/
|
||||
private String description;
|
||||
/**
|
||||
* 分类图片
|
||||
*/
|
||||
private String picUrl;
|
||||
/**
|
||||
* 分类排序
|
||||
*/
|
||||
private Integer sort;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cn.iocoder.mall.productservice.service.category.bo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 商品分类表创建 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class ProductCategoryCreateBO {
|
||||
|
||||
/**
|
||||
* 父分类编号
|
||||
*/
|
||||
@NotNull(message = "父分类编号不能为空")
|
||||
private Integer pid;
|
||||
/**
|
||||
* 分类名称
|
||||
*/
|
||||
@NotEmpty(message = "分类名称不能为空")
|
||||
private String name;
|
||||
/**
|
||||
* 分类描述
|
||||
*/
|
||||
private String description;
|
||||
/**
|
||||
* 分类图片
|
||||
*/
|
||||
private String picUrl;
|
||||
/**
|
||||
* 分类排序
|
||||
*/
|
||||
@NotNull(message = "分类排序不能为空")
|
||||
private Integer sort;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
@NotNull(message = "状态不能为空")
|
||||
private Integer status;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package cn.iocoder.mall.productservice.service.category.bo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 商品分类表更新 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class ProductCategoryUpdateBO {
|
||||
|
||||
/**
|
||||
* 分类编号
|
||||
*/
|
||||
@NotNull(message = "分类编号不能为空")
|
||||
private Integer id;
|
||||
/**
|
||||
* 父分类编号
|
||||
*/
|
||||
@NotNull(message = "父分类编号不能为空")
|
||||
private Integer pid;
|
||||
/**
|
||||
* 分类名称
|
||||
*/
|
||||
@NotEmpty(message = "分类名称不能为空")
|
||||
private String name;
|
||||
/**
|
||||
* 分类描述
|
||||
*/
|
||||
private String description;
|
||||
/**
|
||||
* 分类图片
|
||||
*/
|
||||
private String picUrl;
|
||||
/**
|
||||
* 分类排序
|
||||
*/
|
||||
@NotNull(message = "分类排序不能为空")
|
||||
private Integer sort;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
@NotNull(message = "状态不能为空")
|
||||
private Integer status;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
# 数据源配置项
|
||||
datasource:
|
||||
url: jdbc:mysql://400-infra.server.iocoder.cn:3306/mall_system?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: root
|
||||
password: 3WLiVUBEwTbvAfsh
|
||||
# Spring Cloud 配置项
|
||||
cloud:
|
||||
nacos:
|
||||
# Spring Cloud Nacos Discovery 配置项
|
||||
discovery:
|
||||
server-addr: 400-infra.server.iocoder.cn:8848 # Nacos 服务器地址
|
||||
namespace: dev # Nacos 命名空间
|
||||
|
||||
# Dubbo 配置项
|
||||
dubbo:
|
||||
# Dubbo 注册中心
|
||||
registry:
|
||||
address: spring-cloud://400-infra.server.iocoder.cn:8848 # 指定 Dubbo 服务注册中心的地址
|
||||
@@ -0,0 +1,24 @@
|
||||
spring:
|
||||
# 数据源配置项
|
||||
datasource:
|
||||
url: jdbc:mysql://400-infra.server.iocoder.cn:3306/mall_system?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: root
|
||||
password: 3WLiVUBEwTbvAfsh
|
||||
# Spring Cloud 配置项
|
||||
cloud:
|
||||
nacos:
|
||||
# Spring Cloud Nacos Discovery 配置项
|
||||
discovery:
|
||||
server-addr: 400-infra.server.iocoder.cn:8848 # Nacos 服务器地址
|
||||
namespace: dev # Nacos 命名空间
|
||||
|
||||
# Dubbo 配置项
|
||||
dubbo:
|
||||
# Dubbo 注册中心
|
||||
registry:
|
||||
# address: spring-cloud://400-infra.server.iocoder.cn:8848 # 指定 Dubbo 服务注册中心的地址
|
||||
address: nacos://400-infra.server.iocoder.cn:8848?namespace=dev # 指定 Dubbo 服务注册中心的地址
|
||||
# Dubbo 服务提供者的配置
|
||||
provider:
|
||||
tag: ${DUBBO_TAG} # Dubbo 路由分组
|
||||
@@ -0,0 +1,67 @@
|
||||
spring:
|
||||
# Application 的配置项
|
||||
application:
|
||||
name: product-service
|
||||
# Profile 的配置项
|
||||
profiles:
|
||||
active: local
|
||||
|
||||
# MyBatis Plus 配置项
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。
|
||||
global-config:
|
||||
db-config:
|
||||
id-type: auto
|
||||
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
|
||||
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
|
||||
mapper-locations: classpath*:mapper/*.xml
|
||||
type-aliases-package: cn.iocoder.mall.productservice.dal.mysql.dataobject
|
||||
|
||||
# Dubbo 配置项
|
||||
dubbo:
|
||||
# Spring Cloud Alibaba Dubbo 专属配置
|
||||
cloud:
|
||||
subscribed-services: '' # 设置订阅的应用列表,默认为 * 订阅所有应用
|
||||
# Dubbo 提供者的协议
|
||||
protocol:
|
||||
name: dubbo
|
||||
port: -1
|
||||
# Dubbo 提供服务的扫描基础包
|
||||
scan:
|
||||
base-packages: cn.iocoder.mall.productservice.rpc
|
||||
# Dubbo 服务提供者的配置
|
||||
provider:
|
||||
filter: -exception
|
||||
validation: true # 开启 Provider 参数校验
|
||||
OAuth2Rpc:
|
||||
version: 1.0.0
|
||||
AdminRpc:
|
||||
version: 1.0.0
|
||||
ResourceRpc:
|
||||
version: 1.0.0
|
||||
RoleRpc:
|
||||
version: 1.0.0
|
||||
PermissionRpc:
|
||||
version: 1.0.0
|
||||
DepartmentRpc:
|
||||
version: 1.0.0
|
||||
DataDictRpc:
|
||||
version: 1.0.0
|
||||
ProductExceptionLogRpc:
|
||||
version: 1.0.0
|
||||
ProductAccessLogRpc:
|
||||
version: 1.0.0
|
||||
ErrorCodeRpc:
|
||||
version: 1.0.0
|
||||
# Dubbo 服务消费者的配置
|
||||
consumer:
|
||||
ErrorCodeRpc:
|
||||
version: 1.0.0
|
||||
|
||||
# Mall 配置项
|
||||
mall:
|
||||
# 错误码配置项对应 ErrorCodeProperties 配置类
|
||||
error-code:
|
||||
group: ${spring.application.name}
|
||||
constants-class: cn.iocoder.mall.productservice.enums.ProductErrorCodeConstants
|
||||
Reference in New Issue
Block a user