From 8e48c839ff9c7ded3eea36cf3a87d76fccc842aa Mon Sep 17 00:00:00 2001 From: zhh Date: Thu, 24 May 2018 11:17:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E5=88=86=E7=B1=BB=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...PmsProductAttributeCategoryController.java | 17 +++++--- .../dao/PmsProductAttributeCategoryDao.java | 13 ++++++ .../dto/PmsProductAttributeCategoryItem.java | 22 ++++++++++ .../PmsProductAttributeCategoryService.java | 3 ++ ...msProductAttributeCategoryServiceImpl.java | 9 ++++ .../impl/PmsProductCategoryServiceImpl.java | 42 ++++++++++++++----- .../dao/PmsProductAttributeCategoryDao.xml | 19 +++++++++ 7 files changed, 110 insertions(+), 15 deletions(-) create mode 100644 mall-admin/src/main/java/com/macro/mall/dao/PmsProductAttributeCategoryDao.java create mode 100644 mall-admin/src/main/java/com/macro/mall/dto/PmsProductAttributeCategoryItem.java create mode 100644 mall-admin/src/main/resources/dao/PmsProductAttributeCategoryDao.xml diff --git a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductAttributeCategoryController.java b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductAttributeCategoryController.java index 2aa6fd7..ec0f1e0 100644 --- a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductAttributeCategoryController.java +++ b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductAttributeCategoryController.java @@ -1,12 +1,11 @@ package com.macro.mall.controller; import com.macro.mall.dto.CommonResult; +import com.macro.mall.dto.PmsProductAttributeCategoryItem; import com.macro.mall.model.PmsProductAttributeCategory; import com.macro.mall.service.PmsProductAttributeCategoryService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @@ -18,7 +17,7 @@ import java.util.List; * Created by macro on 2018/4/26. */ @Controller -@Api(tags = "PmsProductAttributeCategoryController",description = "商品属性分类管理") +@Api(tags = "PmsProductAttributeCategoryController", description = "商品属性分类管理") @RequestMapping("/productAttribute/category") public class PmsProductAttributeCategoryController { @Autowired @@ -71,8 +70,16 @@ public class PmsProductAttributeCategoryController { @ApiOperation("分页获取所有商品属性分类") @RequestMapping(value = "/list", method = RequestMethod.GET) @ResponseBody - public Object getList(@RequestParam(defaultValue = "5") Integer pageSize,@RequestParam(defaultValue = "1") Integer pageNum) { - List productAttributeCategoryList = productAttributeCategoryService.getList(pageSize,pageNum); + public Object getList(@RequestParam(defaultValue = "5") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) { + List productAttributeCategoryList = productAttributeCategoryService.getList(pageSize, pageNum); return new CommonResult().pageSuccess(productAttributeCategoryList); } + + @ApiOperation("获取所有商品属性分类及其下属性") + @RequestMapping(value = "/list/withAttr", method = RequestMethod.GET) + @ResponseBody + public Object getListWithAttr() { + List productAttributeCategoryResultList = productAttributeCategoryService.getListWithAttr(); + return new CommonResult().success(productAttributeCategoryResultList); + } } diff --git a/mall-admin/src/main/java/com/macro/mall/dao/PmsProductAttributeCategoryDao.java b/mall-admin/src/main/java/com/macro/mall/dao/PmsProductAttributeCategoryDao.java new file mode 100644 index 0000000..92036c0 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/dao/PmsProductAttributeCategoryDao.java @@ -0,0 +1,13 @@ +package com.macro.mall.dao; + +import com.macro.mall.dto.PmsProductAttributeCategoryItem; + +import java.util.List; + +/** + * 自定义商品属性分类Dao + * Created by macro on 2018/5/24. + */ +public interface PmsProductAttributeCategoryDao { + List getListWithAttr(); +} diff --git a/mall-admin/src/main/java/com/macro/mall/dto/PmsProductAttributeCategoryItem.java b/mall-admin/src/main/java/com/macro/mall/dto/PmsProductAttributeCategoryItem.java new file mode 100644 index 0000000..9109796 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/dto/PmsProductAttributeCategoryItem.java @@ -0,0 +1,22 @@ +package com.macro.mall.dto; + +import com.macro.mall.model.PmsProductAttribute; +import com.macro.mall.model.PmsProductAttributeCategory; + +import java.util.List; + +/** + * 包含有分类下属性的dto + * Created by macro on 2018/5/24. + */ +public class PmsProductAttributeCategoryItem extends PmsProductAttributeCategory { + private List productAttributeList; + + public List getProductAttributeList() { + return productAttributeList; + } + + public void setProductAttributeList(List productAttributeList) { + this.productAttributeList = productAttributeList; + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/PmsProductAttributeCategoryService.java b/mall-admin/src/main/java/com/macro/mall/service/PmsProductAttributeCategoryService.java index 384881b..a04f295 100644 --- a/mall-admin/src/main/java/com/macro/mall/service/PmsProductAttributeCategoryService.java +++ b/mall-admin/src/main/java/com/macro/mall/service/PmsProductAttributeCategoryService.java @@ -1,5 +1,6 @@ package com.macro.mall.service; +import com.macro.mall.dto.PmsProductAttributeCategoryItem; import com.macro.mall.model.PmsProductAttributeCategory; import java.util.List; @@ -18,4 +19,6 @@ public interface PmsProductAttributeCategoryService { PmsProductAttributeCategory getItem(Long id); List getList(Integer pageSize, Integer pageNum); + + List getListWithAttr(); } diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/PmsProductAttributeCategoryServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/PmsProductAttributeCategoryServiceImpl.java index b722a2e..c8b3bac 100644 --- a/mall-admin/src/main/java/com/macro/mall/service/impl/PmsProductAttributeCategoryServiceImpl.java +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/PmsProductAttributeCategoryServiceImpl.java @@ -1,6 +1,8 @@ package com.macro.mall.service.impl; import com.github.pagehelper.PageHelper; +import com.macro.mall.dao.PmsProductAttributeCategoryDao; +import com.macro.mall.dto.PmsProductAttributeCategoryItem; import com.macro.mall.mapper.PmsProductAttributeCategoryMapper; import com.macro.mall.model.PmsProductAttributeCategory; import com.macro.mall.model.PmsProductAttributeCategoryExample; @@ -18,6 +20,8 @@ import java.util.List; public class PmsProductAttributeCategoryServiceImpl implements PmsProductAttributeCategoryService { @Autowired private PmsProductAttributeCategoryMapper productAttributeCategoryMapper; + @Autowired + private PmsProductAttributeCategoryDao productAttributeCategoryDao; @Override public int create(String name) { @@ -49,4 +53,9 @@ public class PmsProductAttributeCategoryServiceImpl implements PmsProductAttribu PageHelper.startPage(pageNum,pageSize); return productAttributeCategoryMapper.selectByExample(new PmsProductAttributeCategoryExample()); } + + @Override + public List getListWithAttr() { + return productAttributeCategoryDao.getListWithAttr(); + } } diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/PmsProductCategoryServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/PmsProductCategoryServiceImpl.java index c5839be..b34435e 100644 --- a/mall-admin/src/main/java/com/macro/mall/service/impl/PmsProductCategoryServiceImpl.java +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/PmsProductCategoryServiceImpl.java @@ -3,6 +3,7 @@ package com.macro.mall.service.impl; import com.github.pagehelper.PageHelper; import com.macro.mall.dao.PmsProductCategoryAttributeRelationDao; import com.macro.mall.dto.PmsProductCategoryParam; +import com.macro.mall.mapper.PmsProductCategoryAttributeRelationMapper; import com.macro.mall.mapper.PmsProductCategoryMapper; import com.macro.mall.mapper.PmsProductMapper; import com.macro.mall.model.*; @@ -26,8 +27,9 @@ public class PmsProductCategoryServiceImpl implements PmsProductCategoryService @Autowired private PmsProductMapper productMapper; @Autowired - private PmsProductCategoryAttributeRelationDao pmsProductCategoryAttributeRelationDao; - + private PmsProductCategoryAttributeRelationDao productCategoryAttributeRelationDao; + @Autowired + private PmsProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper; @Override public int create(PmsProductCategoryParam pmsProductCategoryParam) { PmsProductCategory productCategory = new PmsProductCategory(); @@ -39,18 +41,27 @@ public class PmsProductCategoryServiceImpl implements PmsProductCategoryService //创建筛选属性关联 List productAttributeIdList = pmsProductCategoryParam.getProductAttributeIdList(); if(!CollectionUtils.isEmpty(productAttributeIdList)){ - List relationList = new ArrayList<>(); - for (Long productAttrId : productAttributeIdList) { - PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation(); - relation.setProductAttributeId(productAttrId); - relation.setProductCategoryId(productCategory.getId()); - relationList.add(relation); - } - pmsProductCategoryAttributeRelationDao.insertList(relationList); + insertRelationList(productCategory.getId(), productAttributeIdList); } return count; } + /** + * 批量插入商品分类与筛选属性关系表 + * @param productCategoryId 商品分类id + * @param productAttributeIdList 相关商品筛选属性id集合 + */ + private void insertRelationList(Long productCategoryId, List productAttributeIdList) { + List relationList = new ArrayList<>(); + for (Long productAttrId : productAttributeIdList) { + PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation(); + relation.setProductAttributeId(productAttrId); + relation.setProductCategoryId(productCategoryId); + relationList.add(relation); + } + productCategoryAttributeRelationDao.insertList(relationList); + } + @Override public int update(Long id, PmsProductCategoryParam pmsProductCategoryParam) { PmsProductCategory productCategory = new PmsProductCategory(); @@ -63,6 +74,17 @@ public class PmsProductCategoryServiceImpl implements PmsProductCategoryService PmsProductExample example = new PmsProductExample(); example.createCriteria().andProductCategoryIdEqualTo(id); productMapper.updateByExampleSelective(product,example); + //同时更新筛选属性的信息 + if(!CollectionUtils.isEmpty(pmsProductCategoryParam.getProductAttributeIdList())){ + PmsProductCategoryAttributeRelationExample relationExample = new PmsProductCategoryAttributeRelationExample(); + relationExample.createCriteria().andProductCategoryIdEqualTo(id); + productCategoryAttributeRelationMapper.deleteByExample(relationExample); + insertRelationList(id,pmsProductCategoryParam.getProductAttributeIdList()); + }else{ + PmsProductCategoryAttributeRelationExample relationExample = new PmsProductCategoryAttributeRelationExample(); + relationExample.createCriteria().andProductCategoryIdEqualTo(id); + productCategoryAttributeRelationMapper.deleteByExample(relationExample); + } return productCategoryMapper.updateByPrimaryKeySelective(productCategory); } diff --git a/mall-admin/src/main/resources/dao/PmsProductAttributeCategoryDao.xml b/mall-admin/src/main/resources/dao/PmsProductAttributeCategoryDao.xml new file mode 100644 index 0000000..3239a50 --- /dev/null +++ b/mall-admin/src/main/resources/dao/PmsProductAttributeCategoryDao.xml @@ -0,0 +1,19 @@ + + + + + + + + + \ No newline at end of file