refactor: 商品、分类、属性、库存接口优化调整

This commit is contained in:
郝先瑞 2022-01-04 23:44:24 +08:00
parent 99c594ed67
commit 5db4d269ae
11 changed files with 70 additions and 63 deletions

View File

@ -16,9 +16,12 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author <a href="mailto:xianrui0365@163.com">haoxr</a>
* 系统管理_商品属性控制器
*
* @author <a href="mailto:1490493387@qq.com">haoxr</a>
* @date 2022/1/1
*/
@Api(tags = "系统管理端-属性信息")
@Api(tags = "系统管理_商品属性")
@RestController
@RequestMapping("/api/v1/attributes")
@Slf4j

View File

@ -1,5 +1,6 @@
package com.youlai.mall.pms.controller.admin;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.youlai.common.result.Result;
@ -11,18 +12,21 @@ import com.youlai.mall.pms.service.IPmsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/**
* @author <a href="mailto:xianrui0365@163.com">haoxr</a>
* * 系统管理_商品分类控制器
*
* @author <a href="mailto:1490493387@qq.com">haoxr</a>
* @date 2022/01/01
*/
@Api(tags = "系统管理端-分类信息")
@Api(tags = "系统管理_商品分类")
@RestController
@RequestMapping("/api/v1/categories")
@AllArgsConstructor
@ -31,62 +35,61 @@ public class CategoryController {
private IPmsCategoryService iPmsCategoryService;
private IPmsAttributeService iPmsAttributeService;
@ApiOperation(value = "分类列表")
@ApiOperation(value = "商品分类列表")
@GetMapping
public Result<List<CategoryVO>> list() {
List<CategoryVO> list = iPmsCategoryService.listCategory(null);
return Result.success(list);
}
@ApiOperation(value = "分类级联列表")
@ApiOperation(value = "商品分类级联列表")
@GetMapping("/cascade")
public Result cascadeCategoryList() {
public Result listCascadeCategory() {
List list = iPmsCategoryService.listCascadeCategory();
return Result.success(list);
}
@ApiOperation(value = "分类详情")
@ApiImplicitParam(name = "id", value = "商品分类id", required = true, paramType = "path", dataType = "Long")
@ApiOperation(value = "商品分类详情")
@GetMapping("/{id}")
public Result detail(@PathVariable Integer id) {
public Result detail(
@ApiParam("商品分类ID") @PathVariable Long id
) {
PmsCategory category = iPmsCategoryService.getById(id);
return Result.success(category);
}
@ApiOperation(value = "新增分类")
@ApiOperation(value = "新增商品分类")
@PostMapping
public Result add(@RequestBody PmsCategory category) {
public Result addCategory(@RequestBody PmsCategory category) {
Long id = iPmsCategoryService.saveCategory(category);
return Result.success(id);
}
@ApiOperation(value = "修改分类")
@ApiOperation(value = "修改商品分类")
@PutMapping(value = "/{id}")
public Result update(@PathVariable Long id, @RequestBody PmsCategory category) {
public Result update(
@ApiParam("商品分类ID") @PathVariable Long id,
@RequestBody PmsCategory category
) {
category.setId(id);
id = iPmsCategoryService.saveCategory(category);
return Result.success(id);
}
@ApiOperation(value = "删除分类")
@ApiOperation(value = "删除商品分类")
@ApiImplicitParam(name = "ids", value = "id集合,以英文逗号','分隔", required = true, paramType = "query", dataType = "String")
@DeleteMapping("/{ids}")
@CacheEvict(value = "pms",key = "'categoryList'")
@CacheEvict(value = "pms", key = "'categoryList'")
public Result delete(@PathVariable String ids) {
List<String> categoryIds = Arrays.asList(ids.split(","));
Optional.ofNullable(categoryIds).ifPresent(categoryIdList -> {
categoryIdList.forEach(categoryId -> iPmsAttributeService.remove(new LambdaQueryWrapper<PmsAttribute>().eq(PmsAttribute::getCategoryId, categoryId)));
});
iPmsAttributeService.remove(new LambdaQueryWrapper<PmsAttribute>().in(CollectionUtil.isNotEmpty(categoryIds), PmsAttribute::getCategoryId, categoryIds));
boolean result = iPmsCategoryService.removeByIds(categoryIds);
return Result.judge(result);
}
@ApiOperation(value = "选择性修改分类")
@ApiOperation(value = "选择性修改商品分类")
@PatchMapping(value = "/{id}")
@CacheEvict(value = "pms",key = "'categoryList'")
@CacheEvict(value = "pms", key = "'categoryList'")
public Result patch(@PathVariable Long id, @RequestBody PmsCategory category) {
LambdaUpdateWrapper<PmsCategory> updateWrapper = new LambdaUpdateWrapper<PmsCategory>().eq(PmsCategory::getId, id);
updateWrapper.set(category.getVisible() != null, PmsCategory::getVisible, category.getVisible());

View File

@ -8,19 +8,20 @@ import com.youlai.mall.pms.pojo.dto.admin.GoodsFormDTO;
import com.youlai.mall.pms.pojo.entity.PmsSpu;
import com.youlai.mall.pms.pojo.vo.admin.GoodsDetailVO;
import com.youlai.mall.pms.service.IPmsSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* 系统管理_商品信息
*
* @author <a href="mailto:xianrui0365@163.com">haoxr</a>
*/
@Api(tags = "系统管理端-商品信息")
* @date 2021/1/4
**/
@Api(tags = "系统管理_商品信息")
@RestController
@RequestMapping("/api/v1/goods")
@AllArgsConstructor
@ -29,15 +30,14 @@ public class GoodsController {
private IPmsSpuService iPmsSpuService;
@ApiOperation(value = "商品分页列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long"),
@ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long"),
@ApiImplicitParam(name = "categoryId", value = "分类ID", paramType = "query", dataType = "Long"),
@ApiImplicitParam(name = "name", value = "商品名称", paramType = "query", dataType = "String")
})
@GetMapping("/page")
public Result list(Integer page, Integer limit, String name, Long categoryId) {
IPage<PmsSpu> result = iPmsSpuService.list(new Page<>(page, limit), name, categoryId);
public Result list(
@ApiParam("页码") long pageNum,
@ApiParam("每页数量") long pageSize,
@ApiParam("商品分类ID") Long categoryId,
@ApiParam("商品名称") String name
) {
IPage<PmsSpu> result = iPmsSpuService.list(new Page<>(pageNum, pageSize), name, categoryId);
return Result.success(result.getRecords(), result.getTotal());
}

View File

@ -22,7 +22,7 @@ import java.util.List;
@RequestMapping("/app-api/v1/categories")
@Slf4j
@AllArgsConstructor
public class CategoryController {
public class AppCategoryController {
private IPmsCategoryService iPmsCategoryService;

View File

@ -24,7 +24,7 @@ import java.util.stream.Collectors;
@RestController(value = "appGoodsController")
@RequestMapping("/app-api/v1/goods")
@AllArgsConstructor
public class GoodsController {
public class AppGoodsController {
private IGoodsService goodsService;

View File

@ -18,7 +18,7 @@ import java.util.List;
@RestController(value = "appStockController")
@RequestMapping("/app-api/v1/stocks")
@AllArgsConstructor
public class StockController {
public class AppStockController {
private IPmsSkuService iPmsSkuService;

View File

@ -2,7 +2,7 @@ package com.youlai.mall.pms.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.mall.pms.pojo.entity.PmsCategory;
import com.youlai.mall.pms.pojo.vo.CascadeVO;
import com.youlai.common.domain.ValueLabel;
import com.youlai.mall.pms.pojo.vo.CategoryVO;
import java.util.List;
@ -28,7 +28,7 @@ public interface IPmsCategoryService extends IService<PmsCategory> {
* 分类列表级联
* @return
*/
List<CascadeVO> listCascadeCategory();
List<ValueLabel> listCascadeCategory();
/**

View File

@ -22,7 +22,7 @@ public class PmsAttributeServiceImpl extends ServiceImpl<PmsAttributeMapper, Pms
@Override
public boolean saveBatch(AttributeFormDTO attributeForm) {
Long categoryId = attributeForm.getCategoryId();
Integer type = attributeForm.getType();
Integer attributeType = attributeForm.getType();
List<Long> formIds = attributeForm.getAttributes().stream()
.filter(item -> item.getId() != null)
@ -31,7 +31,7 @@ public class PmsAttributeServiceImpl extends ServiceImpl<PmsAttributeMapper, Pms
List<Long> dbIds = this.list(new LambdaQueryWrapper<PmsAttribute>()
.eq(PmsAttribute::getCategoryId, categoryId)
.eq(PmsAttribute::getType, attributeForm.getType())
.eq(PmsAttribute::getType, attributeType)
.select(PmsAttribute::getId)).stream()
.map(item -> item.getId())
.collect(Collectors.toList());
@ -52,7 +52,7 @@ public class PmsAttributeServiceImpl extends ServiceImpl<PmsAttributeMapper, Pms
List<PmsAttribute> attributeList = new ArrayList<>();
formAttributes.forEach(item -> {
PmsAttribute attribute = PmsAttribute.builder().id(item.getId()).categoryId(categoryId).type(type).name(item.getName()).build();
PmsAttribute attribute = PmsAttribute.builder().id(item.getId()).categoryId(categoryId).type(attributeType).name(item.getName()).build();
attributeList.add(attribute);
});
boolean result = this.saveOrUpdateBatch(attributeList);

View File

@ -6,12 +6,13 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.common.constant.GlobalConstants;
import com.youlai.mall.pms.pojo.entity.PmsCategory;
import com.youlai.mall.pms.mapper.PmsCategoryMapper;
import com.youlai.mall.pms.pojo.vo.CascadeVO;
import com.youlai.common.domain.ValueLabel;
import com.youlai.mall.pms.service.IPmsCategoryService;
import com.youlai.mall.pms.pojo.vo.CategoryVO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@ -32,11 +33,14 @@ public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCa
* @return
* @Cacheable value:缓存名称(分区)key缓存键
*/
@Cacheable(value = "pms", key = "'categoryList'")
// @Cacheable(value = "pms", key = "'categoryList'")
@Override
public List<CategoryVO> listCategory(Long parentId) {
List<PmsCategory> categoryList = this.list(new LambdaQueryWrapper<PmsCategory>()
.eq(PmsCategory::getVisible, GlobalConstants.STATUS_YES).orderByDesc(PmsCategory::getSort));
List<PmsCategory> categoryList = this.list(
new LambdaQueryWrapper<PmsCategory>()
.eq(PmsCategory::getVisible, GlobalConstants.STATUS_YES)
.orderByDesc(PmsCategory::getSort)
);
List<CategoryVO> list = recursionTree(parentId != null ? parentId : 0l, categoryList);
return list;
}
@ -65,28 +69,28 @@ public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCa
* @return
*/
@Override
public List<CascadeVO> listCascadeCategory() {
public List<ValueLabel> listCascadeCategory() {
List<PmsCategory> categoryList = this.list(
new LambdaQueryWrapper<PmsCategory>()
.eq(PmsCategory::getVisible, GlobalConstants.STATUS_YES)
.orderByAsc(PmsCategory::getSort)
);
List<CascadeVO> list = recursionCascade(0l, categoryList);
List<ValueLabel> list = recursionCascade(0l, categoryList);
return list;
}
private List<CascadeVO> recursionCascade(Long parentId, List<PmsCategory> categoryList) {
List<CascadeVO> list = new ArrayList<>();
private List<ValueLabel> recursionCascade(Long parentId, List<PmsCategory> categoryList) {
List<ValueLabel> list = new ArrayList<>();
Optional.ofNullable(categoryList)
.ifPresent(categories ->
categories.stream().filter(category ->
category.getParentId().equals(parentId))
.forEach(category -> {
CascadeVO categoryVO = new CascadeVO()
ValueLabel categoryVO = new ValueLabel()
.setLabel(category.getName())
.setValue(category.getId());
BeanUtil.copyProperties(category, categoryVO);
List<CascadeVO> children = recursionCascade(category.getId(), categoryList);
List<ValueLabel> children = recursionCascade(category.getId(), categoryList);
categoryVO.setChildren(children);
list.add(categoryVO);
})

View File

@ -1,4 +1,4 @@
package com.youlai.mall.pms.pojo.vo;
package com.youlai.common.domain;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
@ -11,12 +11,12 @@ import java.util.List;
*/
@Data
@Accessors(chain = true)
public class CascadeVO {
public class ValueLabel {
private Long value;
private String label;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<CascadeVO> children;
private List<ValueLabel> children;
}

View File

@ -34,9 +34,6 @@ public class RedisCacheConfig {
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}