公共属性

master
rouchen 2024-03-06 21:30:51 +08:00
parent 034c7a1b05
commit 49ff049004
6 changed files with 317 additions and 25 deletions

View File

@ -1,10 +1,9 @@
package com.muyu.product.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.muyu.product.domain.resp.TempateAttributeModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@ -81,4 +80,11 @@ public class AttributeInfo extends BaseEntity {
.build();
}
public TempateAttributeModel buildTemplateModel(){
return TempateAttributeModel.builder()
.id(this.getId())
.code(this.getCode())
.name(this.getName())
.build();
}
}

View File

@ -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.model.CategoryInfoSaveModel;
import com.muyu.product.domain.req.CategoryInfoSaveReq;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -15,6 +16,8 @@ import com.muyu.product.domain.req.CategoryInfoQueryReq;
import com.muyu.product.domain.req.CategoryInfoEditReq;
import com.muyu.common.core.web.domain.TreeEntity;
import java.util.Date;
/**
* category_info
*
@ -84,9 +87,7 @@ public class CategoryInfo extends TreeEntity {
.build();
}
/**
*
*/
/**
*
*/
@ -113,4 +114,15 @@ public class CategoryInfo extends TreeEntity {
.build();
}
public static CategoryInfo saveModelBuild(CategoryInfoSaveModel categoryInfoSaveModel){
return CategoryInfo.builder()
.name(categoryInfoSaveModel.getName())
.image(categoryInfoSaveModel.getImage())
.start(categoryInfoSaveModel.getStart())
.introduction(categoryInfoSaveModel.getIntroduction())
.parentId(categoryInfoSaveModel.getParentId())
.createBy(categoryInfoSaveModel.getCreateBy())
.createTime(new Date())
.build();
}
}

View File

@ -5,6 +5,7 @@ import javax.servlet.http.HttpServletResponse;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.product.domain.model.CategoryInfoSaveModel;
import com.muyu.product.domain.resp.CategoryCommontElementResp;
import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
@ -67,12 +68,12 @@ public class CategoryInfoController extends BaseController {
/**
*
*/
@ApiOperation("获取品类信息详细信息")
@ApiOperation("获取品类信息共有属性信息")
@RequiresPermissions("product:category:query")
@GetMapping(value = "/{id}")
@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
public Result<CategoryInfo> getInfo(@PathVariable("id") Long id) {
return Result.success(categoryInfoService.getById(id));
@GetMapping(value = "/getTemplateAttributeByCateGoryId/{CateGoryId}")
@ApiImplicitParam(name = "cateGoryId", value = "cateGoryId", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
public Result<CategoryCommontElementResp> getTemplateAttributeByCateGoryId(@PathVariable("cateGoryId") Long cateGoryId) {
return Result.success(categoryInfoService.getTemplateAttributeByCateGoryId(cateGoryId));
}
/**

View File

@ -1,10 +1,15 @@
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.req.CategoryInfoEditReq;
import com.muyu.product.domain.resp.CategoryCommontElementResp;
import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
/**
@ -26,4 +31,21 @@ public interface CategoryInfoService extends IService<CategoryInfo> {
void util(CategoryInfoEditReq categoryInfoEditReq, Long id);
public boolean save(CategoryInfoSaveModel categoryInfoSaveModel);
List<AttributeGroup> getAttributeGroup(Long categoryId);
List<BrandInfo> getBrand(Long categoryId);
List<AttributeInfo> getAttribute(Long categoryId);
CategoryParentCommonElementResp paretCoommonElement(Long categoryId);
/**
*
* @param cateGoryId Id
* @return
*/
CategoryCommontElementResp getTemplateAttributeByCateGoryId(Long cateGoryId);
}

View File

@ -1,8 +1,10 @@
package com.muyu.product.service.impl;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -12,7 +14,10 @@ import com.muyu.product.domain.*;
import com.muyu.product.domain.base.CategoryBase;
import com.muyu.product.domain.model.CategoryInfoSaveModel;
import com.muyu.product.domain.req.CategoryInfoEditReq;
import com.muyu.product.domain.resp.CategoryCommontElementResp;
import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
import com.muyu.product.domain.resp.TempateAttributeModel;
import com.muyu.product.domain.model.TemplateAttributeGroupModel;
import com.muyu.product.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -20,7 +25,6 @@ import org.springframework.stereotype.Service;
import com.muyu.product.mapper.CategoryInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
/**
* Service
@ -40,6 +44,8 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
@Autowired
private AsCategoryBrandService asCategoryBrandService;
@Autowired
private AsAttributeGroupService asAttributeGroupService;
@Autowired
private AttributeInfoServiceImpl attributeInfoService;
@ -91,22 +97,246 @@ public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, Cat
.build();
}
private void getParentIdListByCateGoryId(List<Long> parentIdList,Long cateGoryId){
if (cateGoryId.equals(0L)){
return;
}
CategoryInfo categoryInfo = this.getById(cateGoryId);
parentIdList.add(categoryInfo.getParentId());
getParentIdListByCateGoryId(parentIdList,categoryInfo.getParentId());
}
@Override
public void util(CategoryInfoEditReq categoryInfoEditReq, Long groupId) {
List<Long> cateGoryIdList = categoryInfoEditReq.getCateGoryIdList();
// List<Long> cateGoryIdList = categoryInfoEditReq.getCateGoryIdList();
// // 删除现有的 AsCategoryAttribute 对象
// List<AsCategoryAttribute> existingAttributes = asCategoryAttributeService.list();
// List<Long> existingCategoryIds = existingAttributes.stream()
// .map(AsCategoryAttribute::getCategoryId)
// .collect(Collectors.toList());
// asCategoryAttributeService.removeBatchByIds(existingCategoryIds);
//
// // 保存新的 AsCategoryAttribute 对象
// List<AsCategoryAttribute> newAttributes = cateGoryIdList.stream()
// .map(categoryId -> new AsCategoryAttribute())
// .collect(Collectors.toList());
// asCategoryAttributeService.saveBatch(newAttributes);
AsCategoryAttribute categoryAttribute = AsCategoryAttribute.categoryAttribute(groupId);
List<AsCategoryAttribute> list = asCategoryAttributeService.list(categoryAttribute);
ArrayList<Long> longArrayList = new ArrayList<>();
for (AsCategoryAttribute asCategoryAttribute : list) {
longArrayList.add(asCategoryAttribute.getId());
}
asCategoryAttributeService.removeBatchByIds(longArrayList);
List<Long> goryIdList = categoryInfoEditReq.getCateGoryIdList();
asCategoryAttributeService.saveBatch(
goryIdList.stream()
.map(categoryId->AsCategoryAttribute.categoryBuild(groupId,categoryId))
.toList()
);
}
// 删除现有的 AsCategoryAttribute 对象
List<AsCategoryAttribute> existingAttributes = asCategoryAttributeService.list();
List<Long> existingCategoryIds = existingAttributes.stream()
.map(AsCategoryAttribute::getCategoryId)
.collect(Collectors.toList());
asCategoryAttributeService.removeBatchByIds(existingCategoryIds);
@Override
public boolean save(CategoryInfoSaveModel categoryInfoSaveModel) {
CategoryInfo categoryInfo = CategoryInfo.saveModelBuild(categoryInfoSaveModel);
boolean save = this.save(categoryInfo);
Long categoryInfoId = categoryInfo.getId();
List<Long> attributeIdList = categoryInfoSaveModel.getAttributeIdList();
if (attributeIdList!=null && !attributeIdList.isEmpty()){
asCategoryAttributeService.saveBatch(
attributeIdList.stream()
.map(attributeId->AsCategoryAttribute.categoryBuild(categoryInfoId,attributeId))
.toList()
);
}
List<Long> attributeGroupIdList = categoryInfoSaveModel.getAttributeGroupIdList();
if (attributeGroupIdList!=null && !attributeGroupIdList.isEmpty()){
asCategoryAttributeGroupService.saveBatch(
attributeGroupIdList.stream()
.map(attributeGroupId -> AsCategoryAttributeGroup.categoryBuild(categoryInfoId,attributeGroupId))
.toList()
);
}
List<Long> brandIdList = categoryInfoSaveModel.getBrandIdList();
if (brandIdList!=null && !brandIdList.isEmpty()){
asCategoryBrandService.saveBatch(
brandIdList.stream()
.map(brandId->AsCategoryBrand.categoryBuild(categoryInfoId,brandId))
.toList()
);
}
return false;
}
// 保存新的 AsCategoryAttribute 对象
List<AsCategoryAttribute> newAttributes = cateGoryIdList.stream()
.map(categoryId -> new AsCategoryAttribute())
.collect(Collectors.toList());
asCategoryAttributeService.saveBatch(newAttributes);
@Override
public List<AttributeGroup> getAttributeGroup(Long categoryId) {
ArrayList<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()){
asCategoryAttributeGroupList.stream()
.map(AsCategoryAttributeGroup::getAttributeGroupId)
.toList();
attributeGroupList.addAll(attributeGroupService.listByIds(attributeGroupList));
}
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;
}
@Override
public List<BrandInfo> getBrand(Long categoryId) {
ArrayList<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 null;
}
@Override
public List<AttributeInfo> getAttribute(Long categoryId) {
ArrayList<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::getCategoryId)
.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(attributeInfosQurey -> {
if (!attributeInfoList.contains(attributeInfosQurey)){
if (!attributeInfoList.contains(attributeInfosQurey)){
attributeInfoList.add(attributeInfosQurey);
}
}
});
}
}
return attributeInfoList;
}
@Override
public CategoryParentCommonElementResp paretCoommonElement(Long categoryId) {
return CategoryParentCommonElementResp.builder()
.attributeInfoList(getCommon(categoryId,asCategoryAttributeService,attributeInfoService))
.attributeGroupList(getCommon(categoryId,asCategoryAttributeGroupService,attributeGroupService))
.brandInfoList(getCommon(categoryId,asCategoryBrandService,brandInfoService))
.build();
}
@Override
public CategoryCommontElementResp getTemplateAttributeByCateGoryId(Long cateGoryId) {
List<Long> cateGoryIdList = new ArrayList<>();
getParentIdListByCateGoryId(cateGoryIdList, cateGoryId);
// 取出和品类相关联的属性组关系 - 中间表
LambdaQueryWrapper<AsCategoryAttributeGroup> asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(){{
in(AsCategoryAttributeGroup::getCategoryId, cateGoryIdList);
}};
List<AsCategoryAttributeGroup> categoryAttributeGroupList = asCategoryAttributeGroupService.list(asCategoryAttributeGroupLambdaQueryWrapper);
List<TemplateAttributeGroupModel> attributeGroupModelList = categoryAttributeGroupList.stream()
.map(asCategoryAttributeGroup -> TemplateAttributeGroupModel.attributeGroupBuild(
attributeGroupService.getById(asCategoryAttributeGroup.getAttributeGroupId()),
attributeGroupId -> {
LambdaQueryWrapper<AsAttributeGroup> asAttributeGroupQueryWrapper = new LambdaQueryWrapper<>() {{
eq(AsAttributeGroup::getGroupId, asCategoryAttributeGroup.getAttributeGroupId());
}};
List<Long> attributeIdList = asAttributeGroupService.list(asAttributeGroupQueryWrapper).stream()
.map(AsAttributeGroup::getAttributeId)
.toList();
if (attributeIdList.isEmpty()){
return new ArrayList<>();
}
return attributeInfoService.listByIds(attributeIdList).stream()
.map(AttributeInfo::buildTemplateModel)
.toList();
}
))
.filter(TemplateAttributeGroupModel::isEffective)
.toList();
// 查重集合
Set<Long> attributeIdSet = new HashSet<>();
// 获取组内所有的属性Id
if (!attributeGroupModelList.isEmpty()){
attributeIdSet.addAll(
attributeGroupModelList.stream()
.flatMap((Function<TemplateAttributeGroupModel, Stream<TempateAttributeModel>>) templateAttributeGroupModel -> templateAttributeGroupModel.getAttributeList().stream())
.map(TempateAttributeModel::getId)
.toList()
);
}
List<TempateAttributeModel> tempateAttributeModelList=new ArrayList<>();
LambdaQueryWrapper<AsCategoryAttribute> categoryAttributeQueryWrapper = new LambdaQueryWrapper<>();
categoryAttributeQueryWrapper.in(AsCategoryAttribute::getAttributeId,cateGoryIdList);
List<AsCategoryAttribute> asCategoryAttributeList = asCategoryAttributeService.list(categoryAttributeQueryWrapper);
if (asCategoryAttributeList!=null && asCategoryAttributeList.isEmpty()){
List<Long> templateAttributeIdList = asCategoryAttributeList.stream()
.map(AsCategoryAttribute::getAttributeId)
.filter(templateAttributeId -> !attributeIdSet.contains(templateAttributeId))
.toList();
tempateAttributeModelList =attributeInfoService.listByIds(
templateAttributeIdList
).stream().map(AttributeInfo::buildTemplateModel)
.toList();
}
List<TempateAttributeModel> attributeModelList=new ArrayList<>();
if (!tempateAttributeModelList.isEmpty()){
attributeIdSet.addAll(
tempateAttributeModelList.stream().map(TempateAttributeModel::getId).toList()
);
}
LambdaQueryWrapper<AttributeInfo> attributeInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
attributeInfoLambdaQueryWrapper.notIn(!attributeIdSet.isEmpty(),AttributeInfo::getId,attributeIdSet);
attributeInfoService.list(attributeInfoLambdaQueryWrapper).stream().map(AttributeInfo::buildTemplateModel).toList();
return CategoryCommontElementResp.builder()
.templateAttributeGroupList(null)
.templateAttributeList(null)
.attributeList(null)
.build();
}

View File

@ -1,6 +1,6 @@
package com.muyu.product.service.impl;
import java.util.List;
import java.util.*;
import java.util.function.Function;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -53,7 +53,25 @@ public class RuleInfoServiceImpl extends ServiceImpl<RuleInfoMapper, RuleInfo>
if (ObjUtils.notNull(ruleInfo.getStatus())){
queryWrapper.eq(RuleInfo::getStatus, ruleInfo.getStatus());
}
Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("线程需要的执行底阿妈");
}
});
t.start();
ArrayList<Integer> integers = new ArrayList<>();
Collections.addAll(integers,11,22,33,44);
System.out.println("排序之前的集合:"+integers);
Collections.sort(integers, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
Collections.sort(integers,(Integer o1, Integer o2) ->{return o2-o1;});
System.out.println("排序之后的集合"+integers);
return list(queryWrapper);
}
@ -79,9 +97,12 @@ public class RuleInfoServiceImpl extends ServiceImpl<RuleInfoMapper, RuleInfo>
return ruleAttrInfoService.list(queryWrapper).stream().map(RuleAttrAddModel::infoBuild).toList();
}))
.toList();
return TableDataInfo.<RuleInfoResp>builder()
.rows(ruleInfoRespList)
.total(new PageInfo<>(list).getTotal())
.build();
}
}