封装了一下品类获取父级所有的公共元素
parent
7e21256ab6
commit
ae30fb2363
|
@ -3,6 +3,7 @@ package com.muyu.product.domain;
|
|||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.muyu.product.domain.base.CategoryBase;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
@ -25,7 +26,7 @@ import com.muyu.common.core.web.domain.BaseEntity;
|
|||
@TableName("as_category_attribute")
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ApiModel(value = "AsCategoryAttribute", description = "品类属性中间")
|
||||
public class AsCategoryAttribute extends BaseEntity {
|
||||
public class AsCategoryAttribute extends BaseEntity implements CategoryBase {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
@ -51,4 +52,9 @@ public class AsCategoryAttribute extends BaseEntity {
|
|||
.attributeId(attributeId)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getBaseId () {
|
||||
return this.attributeId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.muyu.product.domain;
|
|||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.muyu.product.domain.base.CategoryBase;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
@ -25,7 +26,7 @@ import com.muyu.common.core.web.domain.BaseEntity;
|
|||
@TableName("as_category_attribute_group")
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ApiModel(value = "AsCategoryAttributeGroup", description = "品类属性组中间")
|
||||
public class AsCategoryAttributeGroup extends BaseEntity {
|
||||
public class AsCategoryAttributeGroup extends BaseEntity implements CategoryBase {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
@ -51,4 +52,9 @@ public class AsCategoryAttributeGroup extends BaseEntity {
|
|||
.categoryId(categoryInfoId)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getBaseId () {
|
||||
return this.attributeGroupId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.muyu.product.domain;
|
|||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.muyu.product.domain.base.CategoryBase;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
@ -25,7 +26,7 @@ import com.muyu.common.core.web.domain.BaseEntity;
|
|||
@TableName("as_category_brand")
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ApiModel(value = "AsCategoryBrand", description = "品类品牌中间")
|
||||
public class AsCategoryBrand extends BaseEntity {
|
||||
public class AsCategoryBrand extends BaseEntity implements CategoryBase {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
@ -50,4 +51,9 @@ public class AsCategoryBrand extends BaseEntity {
|
|||
.categoryId(categoryInfoId)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getBaseId () {
|
||||
return this.brandId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.muyu.product.domain.base;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: attribute基础方法
|
||||
* @Date 2024-3-1 下午 02:28
|
||||
*/
|
||||
public interface CategoryBase {
|
||||
|
||||
public Long getBaseId();
|
||||
}
|
|
@ -2,10 +2,13 @@ package com.muyu.product.service.impl;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.common.core.utils.ObjUtils;
|
||||
import com.muyu.product.domain.*;
|
||||
import com.muyu.product.domain.base.CategoryBase;
|
||||
import com.muyu.product.domain.model.CategoryInfoSaveModel;
|
||||
import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
|
||||
import com.muyu.product.service.*;
|
||||
|
@ -219,6 +222,40 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
|
|||
return attributeInfoList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public <T,AS> List<T> getCommon(Long categoryId,IService<AS> iService,IService<T> bsiService){
|
||||
List<T> list = new ArrayList();
|
||||
QueryWrapper<AS> asQueryWrapper = new QueryWrapper<>();
|
||||
asQueryWrapper.eq("category_id", categoryId);
|
||||
List<AS> asList = iService.list(asQueryWrapper);
|
||||
if (asList != null && !asList.isEmpty()){
|
||||
List<Long> baseIdList = asList.stream()
|
||||
.map(as -> {
|
||||
if (as instanceof CategoryBase categoryBase){
|
||||
return categoryBase.getBaseId();
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.toList();
|
||||
list.addAll(bsiService.listByIds(baseIdList));
|
||||
}
|
||||
CategoryInfo categoryInfo = this.getById(categoryId);
|
||||
if (categoryInfo.getParentId() != 0){
|
||||
if (list.isEmpty()){
|
||||
list.addAll(getCommon(categoryInfo.getParentId(), iService, bsiService));
|
||||
}else {
|
||||
List<T> commonList = getCommon(categoryInfo.getParentId(), iService, bsiService);
|
||||
commonList.forEach(common -> {
|
||||
if (!list.contains(common)){
|
||||
list.add(common);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过品类ID获取父级以上的属性、属性组、品牌集合
|
||||
*
|
||||
|
@ -229,9 +266,9 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
|
|||
@Override
|
||||
public CategoryParentCommonElementResp parentCommonElement (Long categoryId) {
|
||||
return CategoryParentCommonElementResp.builder()
|
||||
.attributeInfoList(this.getAttribute(categoryId))
|
||||
.attributeGroupList(this.getAttributeGroup(categoryId))
|
||||
.brandInfoList(this.getBrand(categoryId))
|
||||
.attributeInfoList(getCommon(categoryId, asCategoryAttributeService, attributeInfoService))
|
||||
.attributeGroupList(getCommon(categoryId, asCategoryAttributeGroupService, attributeGroupService))
|
||||
.brandInfoList(getCommon(categoryId, asCategoryBrandService, brandInfoService))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue