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; 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 @RestController
@RequestMapping("/api/v1/attributes") @RequestMapping("/api/v1/attributes")
@Slf4j @Slf4j

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -22,7 +22,7 @@ public class PmsAttributeServiceImpl extends ServiceImpl<PmsAttributeMapper, Pms
@Override @Override
public boolean saveBatch(AttributeFormDTO attributeForm) { public boolean saveBatch(AttributeFormDTO attributeForm) {
Long categoryId = attributeForm.getCategoryId(); Long categoryId = attributeForm.getCategoryId();
Integer type = attributeForm.getType(); Integer attributeType = attributeForm.getType();
List<Long> formIds = attributeForm.getAttributes().stream() List<Long> formIds = attributeForm.getAttributes().stream()
.filter(item -> item.getId() != null) .filter(item -> item.getId() != null)
@ -31,7 +31,7 @@ public class PmsAttributeServiceImpl extends ServiceImpl<PmsAttributeMapper, Pms
List<Long> dbIds = this.list(new LambdaQueryWrapper<PmsAttribute>() List<Long> dbIds = this.list(new LambdaQueryWrapper<PmsAttribute>()
.eq(PmsAttribute::getCategoryId, categoryId) .eq(PmsAttribute::getCategoryId, categoryId)
.eq(PmsAttribute::getType, attributeForm.getType()) .eq(PmsAttribute::getType, attributeType)
.select(PmsAttribute::getId)).stream() .select(PmsAttribute::getId)).stream()
.map(item -> item.getId()) .map(item -> item.getId())
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -52,7 +52,7 @@ public class PmsAttributeServiceImpl extends ServiceImpl<PmsAttributeMapper, Pms
List<PmsAttribute> attributeList = new ArrayList<>(); List<PmsAttribute> attributeList = new ArrayList<>();
formAttributes.forEach(item -> { 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); attributeList.add(attribute);
}); });
boolean result = this.saveOrUpdateBatch(attributeList); 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.common.constant.GlobalConstants;
import com.youlai.mall.pms.pojo.entity.PmsCategory; import com.youlai.mall.pms.pojo.entity.PmsCategory;
import com.youlai.mall.pms.mapper.PmsCategoryMapper; 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.service.IPmsCategoryService;
import com.youlai.mall.pms.pojo.vo.CategoryVO; import com.youlai.mall.pms.pojo.vo.CategoryVO;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -32,11 +33,14 @@ public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCa
* @return * @return
* @Cacheable value:缓存名称(分区)key缓存键 * @Cacheable value:缓存名称(分区)key缓存键
*/ */
@Cacheable(value = "pms", key = "'categoryList'") // @Cacheable(value = "pms", key = "'categoryList'")
@Override @Override
public List<CategoryVO> listCategory(Long parentId) { public List<CategoryVO> listCategory(Long parentId) {
List<PmsCategory> categoryList = this.list(new LambdaQueryWrapper<PmsCategory>() List<PmsCategory> categoryList = this.list(
.eq(PmsCategory::getVisible, GlobalConstants.STATUS_YES).orderByDesc(PmsCategory::getSort)); new LambdaQueryWrapper<PmsCategory>()
.eq(PmsCategory::getVisible, GlobalConstants.STATUS_YES)
.orderByDesc(PmsCategory::getSort)
);
List<CategoryVO> list = recursionTree(parentId != null ? parentId : 0l, categoryList); List<CategoryVO> list = recursionTree(parentId != null ? parentId : 0l, categoryList);
return list; return list;
} }
@ -65,28 +69,28 @@ public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCa
* @return * @return
*/ */
@Override @Override
public List<CascadeVO> listCascadeCategory() { public List<ValueLabel> listCascadeCategory() {
List<PmsCategory> categoryList = this.list( List<PmsCategory> categoryList = this.list(
new LambdaQueryWrapper<PmsCategory>() new LambdaQueryWrapper<PmsCategory>()
.eq(PmsCategory::getVisible, GlobalConstants.STATUS_YES) .eq(PmsCategory::getVisible, GlobalConstants.STATUS_YES)
.orderByAsc(PmsCategory::getSort) .orderByAsc(PmsCategory::getSort)
); );
List<CascadeVO> list = recursionCascade(0l, categoryList); List<ValueLabel> list = recursionCascade(0l, categoryList);
return list; return list;
} }
private List<CascadeVO> recursionCascade(Long parentId, List<PmsCategory> categoryList) { private List<ValueLabel> recursionCascade(Long parentId, List<PmsCategory> categoryList) {
List<CascadeVO> list = new ArrayList<>(); List<ValueLabel> list = new ArrayList<>();
Optional.ofNullable(categoryList) Optional.ofNullable(categoryList)
.ifPresent(categories -> .ifPresent(categories ->
categories.stream().filter(category -> categories.stream().filter(category ->
category.getParentId().equals(parentId)) category.getParentId().equals(parentId))
.forEach(category -> { .forEach(category -> {
CascadeVO categoryVO = new CascadeVO() ValueLabel categoryVO = new ValueLabel()
.setLabel(category.getName()) .setLabel(category.getName())
.setValue(category.getId()); .setValue(category.getId());
BeanUtil.copyProperties(category, categoryVO); BeanUtil.copyProperties(category, categoryVO);
List<CascadeVO> children = recursionCascade(category.getId(), categoryList); List<ValueLabel> children = recursionCascade(category.getId(), categoryList);
categoryVO.setChildren(children); categoryVO.setChildren(children);
list.add(categoryVO); 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 com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data; import lombok.Data;
@ -11,12 +11,12 @@ import java.util.List;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class CascadeVO { public class ValueLabel {
private Long value; private Long value;
private String label; private String label;
@JsonInclude(JsonInclude.Include.NON_EMPTY) @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) { if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive()); config = config.entryTtl(redisProperties.getTimeToLive());
} }
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) { if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues(); config = config.disableCachingNullValues();
} }