368 lines
10 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.designhub.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.designhub.entity.*;
import com.designhub.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 配置管理Service
*/
@Service
@RequiredArgsConstructor
public class ConfigService {
private final DemandTypeMapper demandTypeMapper;
private final BrandMapper brandMapper;
private final PlatformMapper platformMapper;
private final CategoryMapper categoryMapper;
// ============ 需求类型管理 ============
/**
* 获取所有需求类型(包括禁用的,用于管理页面)- 平铺列表
*/
public List<DemandType> getAllDemandTypes() {
List<DemandType> list = demandTypeMapper.selectList(
new LambdaQueryWrapper<DemandType>()
.orderByAsc(DemandType::getParentId)
.orderByAsc(DemandType::getSortOrder)
);
// 填充父分类名称
for (DemandType dt : list) {
if (dt.getParentId() != null) {
DemandType parent = demandTypeMapper.selectById(dt.getParentId());
if (parent != null) {
dt.setParentName(parent.getName());
}
}
}
return list;
}
/**
* 获取所有需求类型(树形结构)
*/
public List<DemandType> getDemandTypesTree() {
List<DemandType> allTypes = demandTypeMapper.selectList(
new LambdaQueryWrapper<DemandType>()
.orderByAsc(DemandType::getSortOrder)
);
// 构建树形结构
return buildDemandTypeTree(allTypes);
}
/**
* 获取所有大分类(parentId为null的)
*/
public List<DemandType> getParentDemandTypes() {
return demandTypeMapper.selectList(
new LambdaQueryWrapper<DemandType>()
.isNull(DemandType::getParentId)
.orderByAsc(DemandType::getSortOrder)
);
}
/**
* 根据父ID获取子分类
*/
public List<DemandType> getDemandTypesByParentId(Long parentId) {
return demandTypeMapper.selectList(
new LambdaQueryWrapper<DemandType>()
.eq(DemandType::getParentId, parentId)
.orderByAsc(DemandType::getSortOrder)
);
}
/**
* 获取所有启用的需求类型(用于下拉选择)- 树形结构
*/
public List<DemandType> getEnabledDemandTypes() {
List<DemandType> allTypes = demandTypeMapper.selectList(
new LambdaQueryWrapper<DemandType>()
.eq(DemandType::getIsEnabled, 1)
.orderByAsc(DemandType::getSortOrder)
);
return buildDemandTypeTree(allTypes);
}
/**
* 获取所有启用的需求类型(平铺列表,用于表单选择)
*/
public List<DemandType> getEnabledDemandTypesList() {
List<DemandType> list = demandTypeMapper.selectList(
new LambdaQueryWrapper<DemandType>()
.eq(DemandType::getIsEnabled, 1)
.orderByAsc(DemandType::getParentId)
.orderByAsc(DemandType::getSortOrder)
);
// 填充父分类名称
for (DemandType dt : list) {
if (dt.getParentId() != null) {
DemandType parent = demandTypeMapper.selectById(dt.getParentId());
if (parent != null) {
dt.setParentName(parent.getName());
}
}
}
return list;
}
/**
* 构建需求类型树形结构
*/
private List<DemandType> buildDemandTypeTree(List<DemandType> allTypes) {
// 获取所有根节点(parentId为null)
List<DemandType> roots = allTypes.stream()
.filter(dt -> dt.getParentId() == null)
.collect(java.util.stream.Collectors.toList());
// 为每个根节点填充子节点
for (DemandType root : roots) {
List<DemandType> children = allTypes.stream()
.filter(dt -> root.getId().equals(dt.getParentId()))
.collect(java.util.stream.Collectors.toList());
root.setChildren(children);
}
return roots;
}
/**
* 根据ID获取需求类型
*/
public DemandType getDemandTypeById(Long id) {
DemandType dt = demandTypeMapper.selectById(id);
if (dt != null && dt.getParentId() != null) {
DemandType parent = demandTypeMapper.selectById(dt.getParentId());
if (parent != null) {
dt.setParentName(parent.getName());
}
}
return dt;
}
/**
* 创建需求类型
*/
public DemandType createDemandType(DemandType demandType) {
demandTypeMapper.insert(demandType);
return demandType;
}
/**
* 更新需求类型
*/
public boolean updateDemandType(DemandType demandType) {
return demandTypeMapper.updateById(demandType) > 0;
}
/**
* 删除需求类型(如果是大分类,同时删除所有子分类)
*/
public boolean deleteDemandType(Long id) {
// 先删除所有子分类
demandTypeMapper.delete(
new LambdaQueryWrapper<DemandType>()
.eq(DemandType::getParentId, id)
);
// 再删除自身
return demandTypeMapper.deleteById(id) > 0;
}
// ============ 品牌管理 ============
/**
* 获取所有品牌(包括禁用的,用于管理页面)
*/
public List<Brand> getAllBrands() {
return brandMapper.selectList(
new LambdaQueryWrapper<Brand>()
.orderByAsc(Brand::getSortOrder)
);
}
/**
* 获取所有启用的品牌(用于下拉选择)
*/
public List<Brand> getEnabledBrands() {
return brandMapper.selectList(
new LambdaQueryWrapper<Brand>()
.eq(Brand::getIsEnabled, 1)
.orderByAsc(Brand::getSortOrder)
);
}
/**
* 根据ID获取品牌
*/
public Brand getBrandById(Long id) {
return brandMapper.selectById(id);
}
/**
* 创建品牌
*/
public Brand createBrand(Brand brand) {
brandMapper.insert(brand);
return brand;
}
/**
* 更新品牌
*/
public boolean updateBrand(Brand brand) {
return brandMapper.updateById(brand) > 0;
}
/**
* 删除品牌
*/
public boolean deleteBrand(Long id) {
return brandMapper.deleteById(id) > 0;
}
// ============ 平台管理 ============
/**
* 获取所有平台(包括禁用的,用于管理页面)
*/
public List<Platform> getAllPlatforms() {
return platformMapper.selectList(
new LambdaQueryWrapper<Platform>()
.orderByAsc(Platform::getSortOrder)
);
}
/**
* 获取所有启用的平台(用于下拉选择)
*/
public List<Platform> getEnabledPlatforms() {
return platformMapper.selectList(
new LambdaQueryWrapper<Platform>()
.eq(Platform::getIsEnabled, 1)
.orderByAsc(Platform::getSortOrder)
);
}
/**
* 根据ID获取平台
*/
public Platform getPlatformById(Long id) {
return platformMapper.selectById(id);
}
/**
* 创建平台
*/
public Platform createPlatform(Platform platform) {
platformMapper.insert(platform);
return platform;
}
/**
* 更新平台
*/
public boolean updatePlatform(Platform platform) {
return platformMapper.updateById(platform) > 0;
}
/**
* 删除平台
*/
public boolean deletePlatform(Long id) {
return platformMapper.deleteById(id) > 0;
}
// ============ 类目管理 ============
/**
* 获取所有叶子类目(包括禁用的,用于管理页面)
*/
public List<Category> getAllLeafCategories() {
return categoryMapper.selectList(
new LambdaQueryWrapper<Category>()
.eq(Category::getIsLeaf, 1)
.orderByAsc(Category::getSortOrder)
);
}
/**
* 获取所有类目(包括禁用的,用于管理页面)
*/
public List<Category> getAllCategories() {
return categoryMapper.selectList(
new LambdaQueryWrapper<Category>()
.orderByAsc(Category::getSortOrder)
);
}
/**
* 获取所有启用的类目(树形结构)
*/
public List<Category> getEnabledCategories() {
return categoryMapper.selectList(
new LambdaQueryWrapper<Category>()
.eq(Category::getIsEnabled, 1)
.orderByAsc(Category::getSortOrder)
);
}
/**
* 获取所有启用的叶子类目(用于下拉选择)
*/
public List<Category> getEnabledLeafCategories() {
return categoryMapper.selectList(
new LambdaQueryWrapper<Category>()
.eq(Category::getIsEnabled, 1)
.eq(Category::getIsLeaf, 1)
.orderByAsc(Category::getSortOrder)
);
}
/**
* 根据父ID获取子类目
*/
public List<Category> getCategoriesByParentId(Long parentId) {
return categoryMapper.selectList(
new LambdaQueryWrapper<Category>()
.eq(Category::getParentId, parentId)
.eq(Category::getIsEnabled, 1)
.orderByAsc(Category::getSortOrder)
);
}
/**
* 根据ID获取类目
*/
public Category getCategoryById(Long id) {
return categoryMapper.selectById(id);
}
/**
* 创建类目
*/
public Category createCategory(Category category) {
categoryMapper.insert(category);
return category;
}
/**
* 更新类目
*/
public boolean updateCategory(Category category) {
return categoryMapper.updateById(category) > 0;
}
/**
* 删除类目
*/
public boolean deleteCategory(Long id) {
return categoryMapper.deleteById(id) > 0;
}
}