品类获取父级所有的公共元素

dev
DongZeLiang 2024-03-01 11:58:22 +08:00
parent 2a9c7ef879
commit 7e21256ab6
4 changed files with 225 additions and 8 deletions

View File

@ -0,0 +1,39 @@
package com.muyu.product.domain.resp;
import com.muyu.product.domain.AttributeGroup;
import com.muyu.product.domain.AttributeInfo;
import com.muyu.product.domain.BrandInfo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @author DongZl
* @description:
* @Date 2024-3-1 11:02
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CategoryParentCommonElementResp {
/**
*
*/
private List<AttributeInfo> attributeInfoList;
/**
*
*/
private List<AttributeGroup> attributeGroupList;
/**
*
*/
private List<BrandInfo> brandInfoList;
}

View File

@ -5,7 +5,11 @@ import java.util.function.Supplier;
import javax.servlet.http.HttpServletResponse;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.product.domain.AttributeGroup;
import com.muyu.product.domain.AttributeInfo;
import com.muyu.product.domain.BrandInfo;
import com.muyu.product.domain.model.CategoryInfoSaveModel;
import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@ -111,4 +115,18 @@ public class CategoryInfoController extends BaseController {
public Result<String> remove(@PathVariable List<Long> ids) {
return toAjax(categoryInfoService.removeBatchByIds(ids));
}
/**
* ID
* @param categoryId ID
* @return
*/
@GetMapping("/parentCommonElement/{categoryId}")
@ApiOperation("通过品类ID获取父级以上的属性集合")
@ApiImplicitParam(name = "categoryId", value = "categoryId", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class, example = "1")
public Result<CategoryParentCommonElementResp> parentCommonElement(
@PathVariable(value = "categoryId") Long categoryId
) {
return Result.success(categoryInfoService.parentCommonElement(categoryId));
}
}

View File

@ -1,9 +1,14 @@
package com.muyu.product.service;
import java.util.List;
import com.muyu.product.domain.AttributeGroup;
import com.muyu.product.domain.AttributeInfo;
import com.muyu.product.domain.BrandInfo;
import com.muyu.product.domain.CategoryInfo;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.product.domain.model.CategoryInfoSaveModel;
import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
/**
* Service
@ -27,4 +32,31 @@ public interface CategoryInfoService extends IService<CategoryInfo> {
*/
public boolean save(CategoryInfoSaveModel categoryInfoSaveModel);
/**
* ID
* @param categoryId ID
* @return
*/
List<AttributeGroup> getAttributeGroup (Long categoryId);
/**
* ID
* @param categoryId ID
* @return
*/
List<BrandInfo> getBrand (Long categoryId);
/**
* ID
* @param categoryId ID
* @return
*/
List<AttributeInfo> getAttribute (Long categoryId);
/**
* ID
* @param categoryId ID
* @return
*/
CategoryParentCommonElementResp parentCommonElement (Long categoryId);
}

View File

@ -1,21 +1,18 @@
package com.muyu.product.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.muyu.common.core.utils.ObjUtils;
import com.muyu.product.domain.AsCategoryAttribute;
import com.muyu.product.domain.AsCategoryAttributeGroup;
import com.muyu.product.domain.AsCategoryBrand;
import com.muyu.product.domain.*;
import com.muyu.product.domain.model.CategoryInfoSaveModel;
import com.muyu.product.service.AsCategoryAttributeGroupService;
import com.muyu.product.service.AsCategoryAttributeService;
import com.muyu.product.service.AsCategoryBrandService;
import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
import com.muyu.product.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.muyu.product.mapper.CategoryInfoMapper;
import com.muyu.product.domain.CategoryInfo;
import com.muyu.product.service.CategoryInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
@ -40,6 +37,15 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
@Autowired
private AsCategoryBrandService asCategoryBrandService;
@Autowired
private AttributeInfoService attributeInfoService;
@Autowired
private BrandInfoService brandInfoService;
@Autowired
private AttributeGroupService attributeGroupService;
/**
*
*
@ -106,4 +112,126 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
}
return save;
}
/**
* ID
*
* @param categoryId ID
*
* @return
*/
@Override
public List<AttributeGroup> getAttributeGroup (Long categoryId) {
List<AttributeGroup> attributeGroupList = new ArrayList<>();
LambdaQueryWrapper<AsCategoryAttributeGroup> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AsCategoryAttributeGroup::getCategoryId, categoryId);
List<AsCategoryAttributeGroup> asCategoryAttributeGroupList = asCategoryAttributeGroupService.list(queryWrapper);
if (asCategoryAttributeGroupList != null && !asCategoryAttributeGroupList.isEmpty()){
List<Long> attributeGroupIdList = asCategoryAttributeGroupList.stream()
.map(AsCategoryAttributeGroup::getAttributeGroupId)
.toList();
attributeGroupList.addAll(attributeGroupService.listByIds(attributeGroupIdList));
}
CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){
if (attributeGroupList.isEmpty()){
attributeGroupList.addAll(getAttributeGroup(categoryInfo.getParentId()));
}else {
List<AttributeGroup> attributeGroups = getAttributeGroup(categoryInfo.getParentId());
attributeGroups.forEach(attributeGroup -> {
if (!attributeGroupList.contains(attributeGroup)){
attributeGroupList.add(attributeGroup);
}
});
}
}
return attributeGroupList;
}
/**
* ID
*
* @param categoryId ID
*
* @return
*/
@Override
public List<BrandInfo> getBrand (Long categoryId) {
List<BrandInfo> brandInfoList = new ArrayList<>();
LambdaQueryWrapper<AsCategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AsCategoryBrand::getCategoryId, categoryId);
List<AsCategoryBrand> asCategoryBrandList = asCategoryBrandService.list(queryWrapper);
if (asCategoryBrandList != null && !asCategoryBrandList.isEmpty()){
List<Long> brandIdList = asCategoryBrandList.stream()
.map(AsCategoryBrand::getBrandId)
.toList();
brandInfoList.addAll(brandInfoService.listByIds(brandIdList));
}
CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){
if (brandInfoList.isEmpty()){
brandInfoList.addAll(getBrand(categoryInfo.getParentId()));
}else {
List<BrandInfo> brandInfos = getBrand(categoryInfo.getParentId());
brandInfos.forEach(brandInfo -> {
if (!brandInfoList.contains(brandInfo)){
brandInfoList.add(brandInfo);
}
});
}
}
return brandInfoList;
}
/**
* ID
*
* @param categoryId ID
*
* @return
*/
@Override
public List<AttributeInfo> getAttribute (Long categoryId) {
List<AttributeInfo> attributeInfoList = new ArrayList<>();
LambdaQueryWrapper<AsCategoryAttribute> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AsCategoryAttribute::getCategoryId, categoryId);
List<AsCategoryAttribute> asCategoryAttributeList = asCategoryAttributeService.list(queryWrapper);
if (asCategoryAttributeList != null && !asCategoryAttributeList.isEmpty()){
List<Long> attributeIdList = asCategoryAttributeList.stream()
.map(AsCategoryAttribute::getAttributeId)
.toList();
attributeInfoList.addAll(attributeInfoService.listByIds(attributeIdList));
}
CategoryInfo categoryInfo = this.getById(categoryId);
if (categoryInfo.getParentId() != 0){
if (attributeInfoList.isEmpty()){
attributeInfoList.addAll(getAttribute(categoryInfo.getParentId()));
}else {
List<AttributeInfo> attributeInfos = getAttribute(categoryInfo.getParentId());
attributeInfos.forEach(attributeInfoQuery -> {
if (!attributeInfoList.contains(attributeInfoQuery)){
attributeInfoList.add(attributeInfoQuery);
}
});
}
}
return attributeInfoList;
}
/**
* ID
*
* @param categoryId ID
*
* @return
*/
@Override
public CategoryParentCommonElementResp parentCommonElement (Long categoryId) {
return CategoryParentCommonElementResp.builder()
.attributeInfoList(this.getAttribute(categoryId))
.attributeGroupList(this.getAttributeGroup(categoryId))
.brandInfoList(this.getBrand(categoryId))
.build();
}
}