资源分类整改

This commit is contained in:
none
2023-03-08 17:20:39 +08:00
parent e8455a3a58
commit 64309a078a
22 changed files with 372 additions and 188 deletions

View File

@@ -13,14 +13,20 @@ import java.util.List;
*/
public interface ResourceCategoryService extends IService<ResourceCategory> {
List<ResourceCategory> getByType(String type);
List<ResourceCategory> listByParentId(Integer id);
void create(String type, Integer sort, String name);
List<ResourceCategory> all();
ResourceCategory findOrFail(Integer id) throws NotFoundException;
ResourceCategory find(Integer id, String type);
void deleteById(Integer id) throws NotFoundException;
void update(ResourceCategory category, Integer sort, String name);
void update(ResourceCategory category, String name, Integer parentId, Integer sort) throws NotFoundException;
void create(String name, Integer parentId, Integer sort) throws NotFoundException;
String childrenParentChain(ResourceCategory category);
String compParentChain(Integer parentId) throws NotFoundException;
}

View File

@@ -6,6 +6,8 @@ import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
import java.util.List;
/**
* @author tengteng
* @description 针对表【resources】的数据库操作Service
@@ -15,7 +17,7 @@ public interface ResourceService extends IService<Resource> {
PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter);
Resource create(Integer categoryId, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url);
Resource create(String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url);
Resource findOrFail(Integer id) throws NotFoundException;

View File

@@ -9,7 +9,7 @@ import xyz.playedu.api.exception.ServiceException;
* @create 2023/3/8 14:02
*/
public interface UploadService {
Resource storeMinio(MultipartFile file, Integer categoryId) throws ServiceException;
Resource storeMinio(MultipartFile file, String categoryIds) throws ServiceException;
Resource storeBase64Image(String content,Integer categoryId) throws ServiceException;
Resource storeBase64Image(String content, String categoryIds) throws ServiceException;
}

View File

@@ -1,12 +1,14 @@
package xyz.playedu.api.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import xyz.playedu.api.domain.ResourceCategory;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.service.ResourceCategoryService;
import xyz.playedu.api.mapper.ResourceCategoryMapper;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -18,27 +20,20 @@ import java.util.List;
@Service
public class ResourceCategoryServiceImpl extends ServiceImpl<ResourceCategoryMapper, ResourceCategory>
implements ResourceCategoryService {
@Override
public List<ResourceCategory> getByType(String type) {
return list(query().getWrapper().eq("type", type).orderByAsc("sort"));
public List<ResourceCategory> listByParentId(Integer id) {
return list(query().getWrapper().eq("parent_id", id).orderByAsc("sort"));
}
@Override
public void create(String type, Integer sort, String name) {
ResourceCategory category = new ResourceCategory();
category.setType(type);
category.setSort(sort);
category.setName(name);
category.setCreatedAt(new Date());
category.setUpdatedAt(new Date());
save(category);
public List<ResourceCategory> all() {
return list(query().getWrapper().orderByAsc("sort"));
}
@Override
public ResourceCategory findOrFail(Integer id) throws NotFoundException {
ResourceCategory category = getOne(query().getWrapper().eq("id", id));
ResourceCategory category = getById(id);
if (category == null) {
throw new NotFoundException("分类不存在");
}
@@ -46,18 +41,120 @@ public class ResourceCategoryServiceImpl extends ServiceImpl<ResourceCategoryMap
}
@Override
public void update(ResourceCategory category, Integer sort, String name) {
ResourceCategory newCategory = new ResourceCategory();
newCategory.setId(category.getId());
newCategory.setSort(sort);
newCategory.setName(name);
updateById(newCategory);
@Transactional
public void deleteById(Integer id) throws NotFoundException {
ResourceCategory category = findOrFail(id);
//更新parent_chain
updateParentChain(category.getParentChain(), childrenParentChain(category));
//删除记录
removeById(category.getId());
}
@Override
public ResourceCategory find(Integer id, String type) {
return getOne(query().getWrapper().eq("id", id).eq("type", type));
@Transactional
public void update(ResourceCategory category, String name, Integer parentId, Integer sort) throws NotFoundException {
String childrenChainPrefix = childrenParentChain(category);
ResourceCategory data = new ResourceCategory();
data.setId(category.getId());
if (!category.getName().equals(name)) {
data.setName(name);
}
if (!category.getParentId().equals(parentId)) {
data.setParentId(parentId);
if (parentId.equals(0)) {
data.setParentChain("");
} else {
ResourceCategory parentResourceCategory = findOrFail(parentId);
data.setParentChain(childrenParentChain(parentResourceCategory));
}
}
if (!category.getSort().equals(sort)) {
data.setSort(sort);
}
//提交更换
updateById(data);
category = getById(category.getId());
updateParentChain(childrenParentChain(category), childrenChainPrefix);
}
private void updateParentChain(String newChildrenPC, String oldChildrenPC) {
List<ResourceCategory> children = list(query().getWrapper().like("parent_chain", oldChildrenPC + "%"));
if (children.size() == 0) {
return;
}
ArrayList<ResourceCategory> updateRows = new ArrayList<>();
for (ResourceCategory tmpResourceCategory : children) {
ResourceCategory tmpUpdateResourceCategory = new ResourceCategory();
tmpUpdateResourceCategory.setId(tmpResourceCategory.getId());
// parentChain计算
String pc = newChildrenPC;
if (!tmpResourceCategory.getParentChain().equals(oldChildrenPC)) {
pc = tmpResourceCategory.getParentChain().replaceFirst(oldChildrenPC + ",", newChildrenPC.length() == 0 ? newChildrenPC : newChildrenPC + ',');
}
tmpUpdateResourceCategory.setParentChain(pc);
// parentId计算
int parentId = 0;
if (pc != null && pc.length() > 0) {
String[] parentIds = pc.split(",");
parentId = Integer.parseInt(parentIds[parentIds.length - 1]);
}
tmpUpdateResourceCategory.setParentId(parentId);
updateRows.add(tmpUpdateResourceCategory);
}
updateBatchById(updateRows);
}
@Override
public void create(String name, Integer parentId, Integer sort) throws NotFoundException {
String parentChain = "";
if (parentId != 0) {
parentChain = compParentChain(parentId);
}
ResourceCategory category = new ResourceCategory();
category.setName(name);
category.setParentId(parentId);
category.setParentChain(parentChain);
category.setSort(sort);
category.setCreatedAt(new Date());
category.setUpdatedAt(new Date());
save(category);
}
@Override
public String childrenParentChain(ResourceCategory category) {
String prefix = category.getId() + "";
if (category.getParentChain() != null && category.getParentChain().length() > 0) {
prefix = category.getParentChain() + "," + prefix;
}
return prefix;
}
@Override
public String compParentChain(Integer parentId) throws NotFoundException {
String parentChain = "";
if (parentId != 0) {
ResourceCategory parentResourceCategory = getById(parentId);
if (parentResourceCategory == null) {
throw new NotFoundException("父级分类不存在");
}
String pc = parentResourceCategory.getParentChain();
parentChain = pc == null || pc.length() == 0 ? parentId + "" : pc + "," + parentId;
}
return parentChain;
}
}

View File

@@ -5,18 +5,23 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.domain.ResourceCategoryRelation;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.service.ResourceService;
import xyz.playedu.api.mapper.ResourceMapper;
import org.springframework.stereotype.Service;
import xyz.playedu.api.service.ResourceVideoService;
import xyz.playedu.api.service.internal.ResourceCategoryRelationService;
import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author tengteng
@@ -29,6 +34,9 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
@Autowired
private ResourceVideoService resourceVideoService;
@Autowired
private ResourceCategoryRelationService relationService;
@Override
public PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter) {
QueryWrapper<Resource> wrapper = query().getWrapper().eq("is_hidden", 0);
@@ -46,7 +54,7 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
wrapper.eq("type", filter.getType());
}
if (filter.getCategoryIds() != null && filter.getCategoryIds().length > 0) {
wrapper.in("category_id", Arrays.asList(filter.getCategoryIds()));
// todo 资源分类过滤
}
String sortFiled = filter.getSortField();
@@ -74,10 +82,10 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
}
@Override
public Resource create(Integer categoryId, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url) {
@Transactional
public Resource create(String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url) {
Resource resource = new Resource();
resource.setType(type);
resource.setCategoryId(categoryId);
resource.setName(filename);
resource.setExtension(ext);
resource.setSize(size);
@@ -87,6 +95,21 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
resource.setUrl(url);
resource.setCreatedAt(new Date());
save(resource);
if (categoryIds != null && categoryIds.trim().length() > 0) {
String[] idArray = categoryIds.split(",");
List<ResourceCategoryRelation> relations = new ArrayList<>();
for (int i = 0; i < idArray.length; i++) {
String tmpId = idArray[i];
relations.add(new ResourceCategoryRelation() {{
setCid(Integer.valueOf(tmpId));
setRid(resource.getId());
}});
}
relationService.saveBatch(relations);
}
return resource;
}

View File

@@ -13,6 +13,8 @@ import xyz.playedu.api.service.UploadService;
import xyz.playedu.api.util.Base64Util;
import xyz.playedu.api.util.HelperUtil;
import java.util.List;
/**
* @Author 杭州白书科技有限公司
* @create 2023/3/8 14:02
@@ -20,9 +22,6 @@ import xyz.playedu.api.util.HelperUtil;
@Service
public class UploadServiceImpl implements UploadService {
@Autowired
private ResourceCategoryService resourceCategoryService;
@Autowired
private ResourceService resourceService;
@@ -30,7 +29,7 @@ public class UploadServiceImpl implements UploadService {
private MinioService minioService;
@Override
public Resource storeMinio(MultipartFile file, Integer cid) throws ServiceException {
public Resource storeMinio(MultipartFile file, String categoryIds) throws ServiceException {
if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
throw new ServiceException("请上传文件");
}
@@ -50,11 +49,6 @@ public class UploadServiceImpl implements UploadService {
throw new ServiceException("格式不支持");
}
// 分类校验
if (cid != null && !cid.equals(0) && resourceCategoryService.find(cid, type) == null) {
throw new ServiceException("分类不存在");
}
// 上传原文件的文件名
String oFilename = filename.replaceAll("." + ext, "");
// 自定义新的存储文件名
@@ -64,17 +58,22 @@ public class UploadServiceImpl implements UploadService {
// 保存文件
String url = minioService.saveFile(file, savePath, contentType);
// 上传记录
return resourceService.create(cid, type, oFilename, ext, file.getSize(), BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url);
return resourceService.create(categoryIds, type, oFilename, ext, file.getSize(), BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url);
}
@Override
public Resource storeBase64Image(String content, Integer categoryId) throws ServiceException {
public Resource storeBase64Image(String content, String categoryIds) throws ServiceException {
// data:image/jpeg;base64,
String[] base64Rows = content.split(",");
// 解析出content-type
String contentType = base64Rows[0].replaceAll("data:", "").replaceAll(";base64", "").toLowerCase();
// 解析出文件格式
String ext = contentType.replaceAll("image/", "");
// 通过文件格式解析资源类型
String type = BackendConstant.RESOURCE_EXT_2_TYPE.get(ext);
// 通过资源类型获取安全的content-type
String safeContentType = BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext);
// 资源类型必须存在 && 安全的 content-type 必须存在 且与解析出来的 content-type 一致
if (type == null || safeContentType == null || !safeContentType.equals(contentType)) {
throw new ServiceException("格式不支持");
}
@@ -86,6 +85,6 @@ public class UploadServiceImpl implements UploadService {
// 保存文件
String url = minioService.saveBytes(binary, savePath, contentType);
// 上传记录
return resourceService.create(categoryId, type, filename, ext, (long) binary.length, BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url);
return resourceService.create(categoryIds, type, filename, ext, (long) binary.length, BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url);
}
}

View File

@@ -0,0 +1,22 @@
package xyz.playedu.api.service.impl.internal;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.playedu.api.domain.ResourceCategoryRelation;
import xyz.playedu.api.service.internal.ResourceCategoryRelationService;
import xyz.playedu.api.mapper.ResourceCategoryRelationMapper;
import org.springframework.stereotype.Service;
/**
* @author tengteng
* @description 针对表【resource_category】的数据库操作Service实现
* @createDate 2023-03-08 16:54:56
*/
@Service
public class ResourceCategoryRelationServiceImpl extends ServiceImpl<ResourceCategoryRelationMapper, ResourceCategoryRelation>
implements ResourceCategoryRelationService{
}

View File

@@ -0,0 +1,13 @@
package xyz.playedu.api.service.internal;
import xyz.playedu.api.domain.ResourceCategoryRelation;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author tengteng
* @description 针对表【resource_category】的数据库操作Service
* @createDate 2023-03-08 16:54:56
*/
public interface ResourceCategoryRelationService extends IService<ResourceCategoryRelation> {
}