From 49ff0490044a586413c3231752d1cbc76b18d283 Mon Sep 17 00:00:00 2001 From: rouchen <3133657697@qq.com> Date: Wed, 6 Mar 2024 21:30:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../muyu/product/domain/AttributeInfo.java | 10 +- .../com/muyu/product/domain/CategoryInfo.java | 18 +- .../controller/CategoryInfoController.java | 11 +- .../product/service/CategoryInfoService.java | 22 ++ .../service/impl/CategoryInfoServiceImpl.java | 258 +++++++++++++++++- .../service/impl/RuleInfoServiceImpl.java | 23 +- 6 files changed, 317 insertions(+), 25 deletions(-) diff --git a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/AttributeInfo.java b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/AttributeInfo.java index 5175d8d..ac94fcd 100644 --- a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/AttributeInfo.java +++ b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/AttributeInfo.java @@ -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(); + } } diff --git a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/CategoryInfo.java b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/CategoryInfo.java index 3273e06..b947222 100644 --- a/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/CategoryInfo.java +++ b/muyu-modules/muyu-product/muyu-product-common/src/main/java/com/muyu/product/domain/CategoryInfo.java @@ -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(); + } } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/CategoryInfoController.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/CategoryInfoController.java index 397acbc..0a92547 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/CategoryInfoController.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/controller/CategoryInfoController.java @@ -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 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 getTemplateAttributeByCateGoryId(@PathVariable("cateGoryId") Long cateGoryId) { + return Result.success(categoryInfoService.getTemplateAttributeByCateGoryId(cateGoryId)); } /** diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/CategoryInfoService.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/CategoryInfoService.java index b16634a..789988f 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/CategoryInfoService.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/CategoryInfoService.java @@ -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 { void util(CategoryInfoEditReq categoryInfoEditReq, Long id); + + public boolean save(CategoryInfoSaveModel categoryInfoSaveModel); + + List getAttributeGroup(Long categoryId); + + List getBrand(Long categoryId); + + List getAttribute(Long categoryId); + + CategoryParentCommonElementResp paretCoommonElement(Long categoryId); + + /** + * 品类共有属性 + * @param cateGoryId 品类Id + * @return + */ + CategoryCommontElementResp getTemplateAttributeByCateGoryId(Long cateGoryId); } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/CategoryInfoServiceImpl.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/CategoryInfoServiceImpl.java index a4c63f4..fad9fb1 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/CategoryInfoServiceImpl.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/CategoryInfoServiceImpl.java @@ -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 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 cateGoryIdList = categoryInfoEditReq.getCateGoryIdList(); +// List cateGoryIdList = categoryInfoEditReq.getCateGoryIdList(); +// // 删除现有的 AsCategoryAttribute 对象 +// List existingAttributes = asCategoryAttributeService.list(); +// List existingCategoryIds = existingAttributes.stream() +// .map(AsCategoryAttribute::getCategoryId) +// .collect(Collectors.toList()); +// asCategoryAttributeService.removeBatchByIds(existingCategoryIds); +// +// // 保存新的 AsCategoryAttribute 对象 +// List newAttributes = cateGoryIdList.stream() +// .map(categoryId -> new AsCategoryAttribute()) +// .collect(Collectors.toList()); +// asCategoryAttributeService.saveBatch(newAttributes); + AsCategoryAttribute categoryAttribute = AsCategoryAttribute.categoryAttribute(groupId); + List list = asCategoryAttributeService.list(categoryAttribute); + ArrayList longArrayList = new ArrayList<>(); + for (AsCategoryAttribute asCategoryAttribute : list) { + longArrayList.add(asCategoryAttribute.getId()); + } + asCategoryAttributeService.removeBatchByIds(longArrayList); + List goryIdList = categoryInfoEditReq.getCateGoryIdList(); + asCategoryAttributeService.saveBatch( + goryIdList.stream() + .map(categoryId->AsCategoryAttribute.categoryBuild(groupId,categoryId)) + .toList() + ); + } - // 删除现有的 AsCategoryAttribute 对象 - List existingAttributes = asCategoryAttributeService.list(); - List 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 attributeIdList = categoryInfoSaveModel.getAttributeIdList(); + if (attributeIdList!=null && !attributeIdList.isEmpty()){ + asCategoryAttributeService.saveBatch( + attributeIdList.stream() + .map(attributeId->AsCategoryAttribute.categoryBuild(categoryInfoId,attributeId)) + .toList() + ); + } + List attributeGroupIdList = categoryInfoSaveModel.getAttributeGroupIdList(); + if (attributeGroupIdList!=null && !attributeGroupIdList.isEmpty()){ + asCategoryAttributeGroupService.saveBatch( + attributeGroupIdList.stream() + .map(attributeGroupId -> AsCategoryAttributeGroup.categoryBuild(categoryInfoId,attributeGroupId)) + .toList() + ); + } + List brandIdList = categoryInfoSaveModel.getBrandIdList(); + if (brandIdList!=null && !brandIdList.isEmpty()){ + asCategoryBrandService.saveBatch( + brandIdList.stream() + .map(brandId->AsCategoryBrand.categoryBuild(categoryInfoId,brandId)) + .toList() + ); + } + return false; + } - // 保存新的 AsCategoryAttribute 对象 - List newAttributes = cateGoryIdList.stream() - .map(categoryId -> new AsCategoryAttribute()) - .collect(Collectors.toList()); - asCategoryAttributeService.saveBatch(newAttributes); + @Override + public List getAttributeGroup(Long categoryId) { + ArrayList attributeGroupList = new ArrayList<>(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(AsCategoryAttributeGroup::getCategoryId,categoryId); + List 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 attributeGroups = getAttributeGroup(categoryInfo.getParentId()); + attributeGroups.forEach(attributeGroup -> { + if (!attributeGroupList.contains(attributeGroup)){ + attributeGroupList.add(attributeGroup); + } + }); + } + } + return attributeGroupList; + } + + @Override + public List getBrand(Long categoryId) { + ArrayList brandInfoList = new ArrayList<>(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(AsCategoryBrand::getCategoryId,categoryId); + List asCategoryBrandList = asCategoryBrandService.list(queryWrapper); + if (asCategoryBrandList!=null &&!asCategoryBrandList.isEmpty()){ + List 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 brandInfos = getBrand(categoryInfo.getParentId()); + brandInfos.forEach(brandInfo -> { + if (!brandInfoList.contains(brandInfo)){ + brandInfoList.add(brandInfo); + } + }); + } + + } + + return null; + } + + @Override + public List getAttribute(Long categoryId) { + ArrayList attributeInfoList = new ArrayList<>(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(AsCategoryAttribute::getCategoryId,categoryId); + List asCategoryAttributeList = asCategoryAttributeService.list(queryWrapper); + if (asCategoryAttributeList!=null && !asCategoryAttributeList.isEmpty()){ + List 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 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 cateGoryIdList = new ArrayList<>(); + getParentIdListByCateGoryId(cateGoryIdList, cateGoryId); + // 取出和品类相关联的属性组关系 - 中间表 + LambdaQueryWrapper asCategoryAttributeGroupLambdaQueryWrapper = new LambdaQueryWrapper<>(){{ + in(AsCategoryAttributeGroup::getCategoryId, cateGoryIdList); + }}; + List categoryAttributeGroupList = asCategoryAttributeGroupService.list(asCategoryAttributeGroupLambdaQueryWrapper); + List attributeGroupModelList = categoryAttributeGroupList.stream() + .map(asCategoryAttributeGroup -> TemplateAttributeGroupModel.attributeGroupBuild( + attributeGroupService.getById(asCategoryAttributeGroup.getAttributeGroupId()), + attributeGroupId -> { + LambdaQueryWrapper asAttributeGroupQueryWrapper = new LambdaQueryWrapper<>() {{ + eq(AsAttributeGroup::getGroupId, asCategoryAttributeGroup.getAttributeGroupId()); + }}; + List 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 attributeIdSet = new HashSet<>(); + // 获取组内所有的属性Id + if (!attributeGroupModelList.isEmpty()){ + attributeIdSet.addAll( + attributeGroupModelList.stream() + .flatMap((Function>) templateAttributeGroupModel -> templateAttributeGroupModel.getAttributeList().stream()) + .map(TempateAttributeModel::getId) + .toList() + ); + } + + List tempateAttributeModelList=new ArrayList<>(); + LambdaQueryWrapper categoryAttributeQueryWrapper = new LambdaQueryWrapper<>(); + categoryAttributeQueryWrapper.in(AsCategoryAttribute::getAttributeId,cateGoryIdList); + List asCategoryAttributeList = asCategoryAttributeService.list(categoryAttributeQueryWrapper); + if (asCategoryAttributeList!=null && asCategoryAttributeList.isEmpty()){ + List templateAttributeIdList = asCategoryAttributeList.stream() + .map(AsCategoryAttribute::getAttributeId) + .filter(templateAttributeId -> !attributeIdSet.contains(templateAttributeId)) + .toList(); + tempateAttributeModelList =attributeInfoService.listByIds( + templateAttributeIdList + ).stream().map(AttributeInfo::buildTemplateModel) + .toList(); + } + List attributeModelList=new ArrayList<>(); + + if (!tempateAttributeModelList.isEmpty()){ + attributeIdSet.addAll( + tempateAttributeModelList.stream().map(TempateAttributeModel::getId).toList() + ); + } + + LambdaQueryWrapper 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(); } diff --git a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/RuleInfoServiceImpl.java b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/RuleInfoServiceImpl.java index 7919ab7..12b3ac0 100644 --- a/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/RuleInfoServiceImpl.java +++ b/muyu-modules/muyu-product/muyu-product-server/src/main/java/com/muyu/product/service/impl/RuleInfoServiceImpl.java @@ -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 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 integers = new ArrayList<>(); + Collections.addAll(integers,11,22,33,44); + System.out.println("排序之前的集合:"+integers); + Collections.sort(integers, new Comparator() { + @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 return ruleAttrInfoService.list(queryWrapper).stream().map(RuleAttrAddModel::infoBuild).toList(); })) .toList(); + return TableDataInfo.builder() .rows(ruleInfoRespList) .total(new PageInfo<>(list).getTotal()) .build(); } + + }