package com.oying.modules.pc.store.service.impl;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.oying.exception.EntityNotFoundException;
|
import com.oying.modules.pc.common.exception.LevelExceededException;
|
import com.oying.modules.pc.store.domain.StoreCategory;
|
import com.oying.modules.pc.store.domain.dto.StoreCategoryQueryCriteria;
|
import com.oying.modules.pc.store.mapper.StoreCategoryMapper;
|
import com.oying.modules.pc.store.service.StoreCategoryService;
|
import com.oying.utils.FileUtil;
|
import com.oying.utils.PageResult;
|
import com.oying.utils.PageUtil;
|
import com.oying.utils.SecurityUtils;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.util.*;
|
|
/**
|
* @author lzp
|
* @date 2025-04-24
|
**/
|
@Service
|
@RequiredArgsConstructor
|
public class StoreCategoryServiceImpl extends ServiceImpl<StoreCategoryMapper, StoreCategory> implements StoreCategoryService {
|
|
private static final int MAX_LEVEL = 2;
|
|
private final StoreCategoryMapper storeCategoryMapper;
|
|
@Override
|
public PageResult<StoreCategory> queryAll(StoreCategoryQueryCriteria criteria, Page<Object> page) {
|
return PageUtil.toPage(storeCategoryMapper.findAll(criteria, page));
|
}
|
|
@Override
|
public List<StoreCategory> queryAll(StoreCategoryQueryCriteria criteria) {
|
return storeCategoryMapper.findAll(criteria);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void create(StoreCategory resources) {
|
// 重名
|
this.calculateAndSetLevel(resources);
|
resources.setCreateBy(SecurityUtils.getCurrentUserId());
|
storeCategoryMapper.insert(resources);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void update(StoreCategory resources) {
|
StoreCategory storeCategory = getById(resources.getCategoryId());
|
storeCategory.copy(resources);
|
resources.setUpdateBy(SecurityUtils.getCurrentUserId());
|
storeCategoryMapper.updateById(storeCategory);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void deleteAll(List<Long> ids) {
|
storeCategoryMapper.deleteBatchIds(ids);
|
}
|
|
/**
|
* 计算并设置类目的层级
|
*/
|
private void calculateAndSetLevel(StoreCategory category) {
|
|
if (category == null) {
|
throw new IllegalArgumentException("对象不能为null");
|
}
|
|
Long parentCategoryId = category.getParentId();
|
if (parentCategoryId == null) {
|
category.setParentId(0L);
|
category.setLevel(1);
|
return;
|
}
|
|
StoreCategory parentCategory = Optional.ofNullable(getById(parentCategoryId))
|
.orElseThrow(() -> new EntityNotFoundException(StoreCategory.class, "parentId", parentCategoryId.toString()));
|
|
int currentLevel = parentCategory.getLevel() + 1;
|
|
if (currentLevel > MAX_LEVEL) {
|
throw new LevelExceededException(category.getLevel(), MAX_LEVEL);
|
}
|
|
category.setLevel(currentLevel);
|
}
|
}
|