From b7de4e50d0f3336e9cc93beadf800b4db51afee0 Mon Sep 17 00:00:00 2001 From: ddyd <2073699128@qq.com> Date: Sat, 23 Aug 2025 11:06:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E7=B3=BB=E7=BB=9F=E9=94=80?= =?UTF-8?q?=E5=94=AE=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/constant/ProductConstant.java | 24 +++++ .../product/controller/AttrController.java | 15 ++-- .../bookmall/product/service/AttrService.java | 5 +- .../product/service/impl/AttrServiceImpl.java | 89 +++++++++++++------ 4 files changed, 98 insertions(+), 35 deletions(-) create mode 100644 book-product/src/main/java/com/bookstore/bookmall/product/constant/ProductConstant.java diff --git a/book-product/src/main/java/com/bookstore/bookmall/product/constant/ProductConstant.java b/book-product/src/main/java/com/bookstore/bookmall/product/constant/ProductConstant.java new file mode 100644 index 0000000..31b67f3 --- /dev/null +++ b/book-product/src/main/java/com/bookstore/bookmall/product/constant/ProductConstant.java @@ -0,0 +1,24 @@ +package com.bookstore.bookmall.product.constant; + +public class ProductConstant { + public enum AttrEnum{ + ATTR_TYPE_SALE(0, "销售属性"), ATTR_TYPE_BASE(1,"基本属性"); + + private int code; + private String msg; + + AttrEnum(int code, String msg) { + this.code = code; + this.msg = msg; + } + + public int getCode() { + return code; + } + public String getMsg() { + return msg; + } + + } + +} diff --git a/book-product/src/main/java/com/bookstore/bookmall/product/controller/AttrController.java b/book-product/src/main/java/com/bookstore/bookmall/product/controller/AttrController.java index a6a1238..2bc3d28 100644 --- a/book-product/src/main/java/com/bookstore/bookmall/product/controller/AttrController.java +++ b/book-product/src/main/java/com/bookstore/bookmall/product/controller/AttrController.java @@ -30,22 +30,23 @@ public class AttrController { private AttrService attrService; // /product/attr/base/list/{catelogId} - @GetMapping("/base/list/{catelogId}") + @GetMapping("/{attrType}/list/{catelogId}") public R baseAttrList(@RequestParam Map params, - @PathVariable("catelogId") Long cateLogId) { - PageUtils page = attrService.queryBasePage(params, cateLogId); + @PathVariable("catelogId") Long cateLogId, + @PathVariable("attrType") String attrType) { + PageUtils page = attrService.queryBasePage(params, cateLogId, attrType); return R.ok().put("page", page); } - /** * 列表 */ @RequestMapping("/list/{catelogId}") //@RequiresPermissions("product:attr:list") - public R list(@RequestParam Map params){ + public R list(@RequestParam Map params, + @PathVariable("catelogId") Long catelogId){ PageUtils page = attrService.queryPage(params); return R.ok().put("page", page); @@ -81,8 +82,8 @@ public class AttrController { */ @RequestMapping("/update") //@RequiresPermissions("product:attr:update") - public R update(@RequestBody AttrEntity attr){ - attrService.updateById(attr); + public R update(@RequestBody AttrVo attrVo){ + attrService.updateAttr(attrVo); return R.ok(); } diff --git a/book-product/src/main/java/com/bookstore/bookmall/product/service/AttrService.java b/book-product/src/main/java/com/bookstore/bookmall/product/service/AttrService.java index 6426269..f03f577 100644 --- a/book-product/src/main/java/com/bookstore/bookmall/product/service/AttrService.java +++ b/book-product/src/main/java/com/bookstore/bookmall/product/service/AttrService.java @@ -21,9 +21,12 @@ public interface AttrService extends IService { void saveAttr(AttrVo attr); - PageUtils queryBasePage(Map params, Long cateLogId); + PageUtils queryBasePage(Map params, Long cateLogId, String attrType); AttrRespVo getAttrInfo(Long attrId); + + void updateAttr(AttrVo attrVo); + } diff --git a/book-product/src/main/java/com/bookstore/bookmall/product/service/impl/AttrServiceImpl.java b/book-product/src/main/java/com/bookstore/bookmall/product/service/impl/AttrServiceImpl.java index 332ed03..de1135b 100644 --- a/book-product/src/main/java/com/bookstore/bookmall/product/service/impl/AttrServiceImpl.java +++ b/book-product/src/main/java/com/bookstore/bookmall/product/service/impl/AttrServiceImpl.java @@ -1,14 +1,13 @@ package com.bookstore.bookmall.product.service.impl; -import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A; -import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; +import com.bookstore.bookmall.product.constant.ProductConstant; import com.bookstore.bookmall.product.dao.AttrAttrgroupRelationDao; import com.bookstore.bookmall.product.dao.AttrGroupDao; import com.bookstore.bookmall.product.dao.CategoryDao; import com.bookstore.bookmall.product.entity.AttrAttrgroupRelationEntity; import com.bookstore.bookmall.product.entity.AttrGroupEntity; import com.bookstore.bookmall.product.entity.CategoryEntity; -import com.bookstore.bookmall.product.service.AttrAttrgroupRelationService; import com.bookstore.bookmall.product.service.CategoryService; import com.bookstore.bookmall.product.vo.AttrRespVo; import com.bookstore.bookmall.product.vo.AttrVo; @@ -16,7 +15,6 @@ import com.mysql.cj.util.StringUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service; import java.util.List; @@ -65,18 +63,21 @@ public class AttrServiceImpl extends ServiceImpl implements //保存基本数据 this.save(attrEntity); //2、保存关联关系 - AttrAttrgroupRelationEntity RelationEntity = new AttrAttrgroupRelationEntity(); - RelationEntity.setAttrGroupId(attr.getAttrGroupId()); - RelationEntity.setAttrId(attr.getAttrId()); - RelationDao.insert(RelationEntity); + if (attr.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) { + AttrAttrgroupRelationEntity RelationEntity = new AttrAttrgroupRelationEntity(); + RelationEntity.setAttrGroupId(attr.getAttrGroupId()); + RelationEntity.setAttrId(attr.getAttrId()); + RelationDao.insert(RelationEntity); + } + } //后台管理,查询基本内容和关联分组和关联属性 @Override - public PageUtils queryBasePage(Map params, Long cateLogId) { + public PageUtils queryBasePage(Map params, Long cateLogId, String attrType) { QueryWrapper wrapper = new QueryWrapper(); if (cateLogId != 0) { - wrapper.eq("catelog_id", cateLogId); + wrapper.eq("catelog_id", cateLogId).eq("attr_name", "base".equalsIgnoreCase(attrType)?1:0); } String key = (String)params.get("key"); if (!StringUtils.isNullOrEmpty(key)) { @@ -94,14 +95,17 @@ public class AttrServiceImpl extends ServiceImpl implements BeanUtils.copyProperties(attrEntity, attrRespVo); //设置分组名字和分类名字 //在分组关系中查找id - AttrAttrgroupRelationEntity relationEntity = RelationDao.selectOne( - new QueryWrapper().eq("attr_id",attrEntity.getAttrId())); - if (relationEntity!=null) { - // log.info("查询到relation"); - AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId()); - attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName()); - // log.info(attrGroupEntity.getAttrGroupName()); - } + if ("base".equalsIgnoreCase(attrType)) { + AttrAttrgroupRelationEntity relationEntity = RelationDao.selectOne( + new QueryWrapper().eq("attr_id",attrEntity.getAttrId())); + if (relationEntity!=null) { + // log.info("查询到relation"); + AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId()); + attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName()); + // log.info(attrGroupEntity.getAttrGroupName()); + } + } + CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId()); if (categoryEntity!=null) { // log.info("查询到categoryEntity"); @@ -122,20 +126,24 @@ public class AttrServiceImpl extends ServiceImpl implements AttrRespVo respVo = new AttrRespVo(); AttrEntity attrEntity = this.getById(attrId); - //设置分组id - BeanUtils.copyProperties(attrEntity, respVo); + //如果是base + if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) { + //设置分组id + BeanUtils.copyProperties(attrEntity, respVo); // log.info("beanutilzhengchang"); - AttrAttrgroupRelationEntity RelationEntity = RelationDao.selectOne( - new QueryWrapper().eq("attr_id",attrId)); + AttrAttrgroupRelationEntity RelationEntity = RelationDao.selectOne( + new QueryWrapper().eq("attr_id",attrId)); - if (RelationEntity!=null) { + if (RelationEntity!=null) { // log.info("查询属性,relation正常"); - respVo.setAttrGroupId(RelationEntity.getAttrGroupId()); - AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(RelationEntity.getAttrGroupId()); - if (attrGroupEntity!=null) { - respVo.setGroupName(attrGroupEntity.getAttrGroupName()); + respVo.setAttrGroupId(RelationEntity.getAttrGroupId()); + AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(RelationEntity.getAttrGroupId()); + if (attrGroupEntity!=null) { + respVo.setGroupName(attrGroupEntity.getAttrGroupName()); + } } } + // log.info("设置分组"); //设置分类信息 Long catelogId = attrEntity.getCatelogId(); @@ -149,5 +157,32 @@ public class AttrServiceImpl extends ServiceImpl implements return respVo; } + @Transactional + @Override + public void updateAttr(AttrVo attrVo) { + //保存基本属性 + AttrEntity attrEntity = new AttrEntity(); + BeanUtils.copyProperties(attrVo, attrEntity); + this.updateById(attrEntity); + + AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity(); + relationEntity.setAttrGroupId(attrVo.getAttrGroupId()); + relationEntity.setAttrId(attrVo.getAttrId()); + + if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) { + //如果一开始就没有分组属性,实际要做的就是新增操作,所以这里还要进行判断 + Long count = RelationDao.selectCount(new QueryWrapper().eq("attr_id", attrVo.getAttrId())); + if (count > 0) { + //修改关联属性 + RelationDao.update(relationEntity, new UpdateWrapper().eq( + "attr_id", attrVo.getAttrId() + )); + } else { + RelationDao.insert(relationEntity); + } + } + } + + } \ No newline at end of file