一、商品分类管理核心逻辑
生鲜商城的商品分类管理需要满足生鲜行业特性,包括:
1. 多级分类体系:大类(水果、蔬菜、肉类等)→中类(进口水果、国产水果)→小类(苹果、香蕉)→单品(红富士苹果)
2. 属性关联:保质期、产地、储存条件等生鲜特有属性
3. 季节性管理:应季商品自动上下架
4. 库存联动:分类库存预警设置
二、万象源码部署架构
1. 数据库设计
```sql
-- 商品分类表
CREATE TABLE `product_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL COMMENT 分类名称,
`parent_id` int(11) DEFAULT 0 COMMENT 父分类ID,
`level` tinyint(4) DEFAULT 1 COMMENT 分类层级,
`sort_order` int(11) DEFAULT 0 COMMENT 排序,
`is_active` tinyint(1) DEFAULT 1 COMMENT 是否启用,
`season_start` date DEFAULT NULL COMMENT 应季开始日期,
`season_end` date DEFAULT NULL COMMENT 应季结束日期,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB;
-- 商品分类属性关联表
CREATE TABLE `category_attribute` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) NOT NULL,
`attribute_id` int(11) NOT NULL,
`is_required` tinyint(1) DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `category_attribute` (`category_id`,`attribute_id`)
) ENGINE=InnoDB;
```
2. 后端服务逻辑
```java
// 分类服务接口示例
public interface CategoryService {
// 获取分类树形结构
List
getCategoryTree(Boolean includeInactive);
// 创建/更新分类
CategoryDTO saveOrUpdateCategory(CategoryDTO categoryDTO);
// 获取分类关联属性
List getCategoryAttributes(Long categoryId);
// 季节性商品自动处理
void processSeasonalProducts();
}
// 实现类关键逻辑
@Service
public class CategoryServiceImpl implements CategoryService {
@Override
public List getCategoryTree(Boolean includeInactive) {
// 1. 查询所有分类
List categories = categoryRepository.findAll(
includeInactive ? QProductCategory.productCategory.isNotNull()
: QProductCategory.productCategory.isActive.isTrue()
);
// 2. 构建树形结构
return buildCategoryTree(categories);
}
@Scheduled(cron = "0 0 2 * * ?") // 每天凌晨2点执行
@Override
public void processSeasonalProducts() {
// 1. 查询所有带季节属性的分类
List seasonalCategories = categoryRepository.findBySeasonStartIsNotNull();
// 2. 检查当前日期是否在季节范围内
LocalDate now = LocalDate.now();
seasonalCategories.forEach(category -> {
boolean isInSeason = now.isAfter(category.getSeasonStart().minusDays(1))
&& now.isBefore(category.getSeasonEnd().plusDays(1));
// 3. 更新分类状态和关联商品状态
updateCategoryAndProductsSeasonalStatus(category, isInSeason);
});
}
}
```
3. 前端交互逻辑
```javascript
// Vue组件示例
export default {
data() {
return {
categoryTree: [],
selectedCategory: null,
attributes: []
}
},
created() {
this.loadCategoryTree();
},
methods: {
async loadCategoryTree() {
const res = await api.get(/api/categories/tree);
this.categoryTree = this.buildTree(res.data);
},
async onCategorySelect(category) {
this.selectedCategory = category;
if (category.id) {
const res = await api.get(`/api/categories/${category.id}/attributes`);
this.attributes = res.data;
}
},
buildTree(categories, parentId = 0) {
return categories
.filter(cat => cat.parentId === parentId)
.map(cat => ({
...cat,
children: this.buildTree(categories, cat.id)
}));
}
}
}
```
三、关键业务逻辑实现
1. 分类树形结构生成
```java
// 递归构建分类树
private List buildCategoryTree(List categories) {
Map> categoryMap = categories.stream()
.collect(Collectors.groupingBy(ProductCategory::getParentId));
return buildTreeRecursive(0L, categoryMap);
}
private List buildTreeRecursive(Long parentId,
Map> categoryMap) {
return categoryMap.getOrDefault(parentId, Collections.emptyList()).stream()
.map(category -> {
CategoryTreeDTO node = new CategoryTreeDTO();
BeanUtils.copyProperties(category, node);
node.setChildren(buildTreeRecursive(category.getId(), categoryMap));
return node;
})
.collect(Collectors.toList());
}
```
2. 季节性商品处理
```java
@Transactional
private void updateCategoryAndProductsSeasonalStatus(ProductCategory category, boolean isInSeason) {
// 1. 更新分类状态
category.setActive(isInSeason);
categoryRepository.save(category);
// 2. 更新关联商品状态
if (isInSeason) {
// 应季商品上架
productRepository.updateStatusByCategory(category.getId(), ProductStatus.ON_SALE);
} else {
// 非应季商品下架
productRepository.updateStatusByCategory(category.getId(), ProductStatus.OFF_SALE);
}
// 3. 发送通知(可选)
if (!isInSeason) {
notificationService.sendSeasonEndAlert(category.getId());
}
}
```
四、部署优化建议
1. 缓存策略:
- 使用Redis缓存分类树形结构(设置合理过期时间)
- 对季节性商品查询使用本地缓存
2. 数据库优化:
- 为分类表添加复合索引:`(parent_id, level, sort_order)`
- 对季节性字段添加单独索引
3. 异步处理:
- 季节性商品处理使用异步任务
- 分类变更时通过消息队列通知相关服务
4. API设计:
- 提供分类树形结构API(支持扁平/树形两种格式)
- 提供分类路径查询API(如:/api/categories/path/123)
五、扩展功能考虑
1. 智能分类建议:基于商品名称自动推荐分类
2. 分类权重设置:控制不同分类在首页的展示优先级
3. 多级价格体系:不同分类设置不同的加价规则
4. 分类维度分析:按分类查看销售、库存等数据
通过以上逻辑清晰的部署方案,可以构建一个高效、可扩展的生鲜商城商品分类管理系统,满足生鲜行业特有的业务需求。