商品分类修改完善

This commit is contained in:
zhh 2018-05-24 11:17:26 +08:00
parent 424cbe0df5
commit 8e48c839ff
7 changed files with 110 additions and 15 deletions

View File

@ -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<PmsProductAttributeCategory> productAttributeCategoryList = productAttributeCategoryService.getList(pageSize,pageNum);
public Object getList(@RequestParam(defaultValue = "5") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) {
List<PmsProductAttributeCategory> productAttributeCategoryList = productAttributeCategoryService.getList(pageSize, pageNum);
return new CommonResult().pageSuccess(productAttributeCategoryList);
}
@ApiOperation("获取所有商品属性分类及其下属性")
@RequestMapping(value = "/list/withAttr", method = RequestMethod.GET)
@ResponseBody
public Object getListWithAttr() {
List<PmsProductAttributeCategoryItem> productAttributeCategoryResultList = productAttributeCategoryService.getListWithAttr();
return new CommonResult().success(productAttributeCategoryResultList);
}
}

View File

@ -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<PmsProductAttributeCategoryItem> getListWithAttr();
}

View File

@ -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<PmsProductAttribute> productAttributeList;
public List<PmsProductAttribute> getProductAttributeList() {
return productAttributeList;
}
public void setProductAttributeList(List<PmsProductAttribute> productAttributeList) {
this.productAttributeList = productAttributeList;
}
}

View File

@ -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<PmsProductAttributeCategory> getList(Integer pageSize, Integer pageNum);
List<PmsProductAttributeCategoryItem> getListWithAttr();
}

View File

@ -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<PmsProductAttributeCategoryItem> getListWithAttr() {
return productAttributeCategoryDao.getListWithAttr();
}
}

View File

@ -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<Long> productAttributeIdList = pmsProductCategoryParam.getProductAttributeIdList();
if(!CollectionUtils.isEmpty(productAttributeIdList)){
List<PmsProductCategoryAttributeRelation> 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<Long> productAttributeIdList) {
List<PmsProductCategoryAttributeRelation> 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);
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.macro.mall.dao.PmsProductAttributeCategoryDao">
<resultMap id="getListWithAttrMap" type="com.macro.mall.dto.PmsProductAttributeCategoryItem" extends="com.macro.mall.mapper.PmsProductAttributeCategoryMapper.BaseResultMap">
<collection property="productAttributeList" columnPrefix="attr_" resultMap="com.macro.mall.mapper.PmsProductAttributeMapper.BaseResultMap">
</collection>
</resultMap>
<select id="getListWithAttr" resultMap="getListWithAttrMap">
SELECT
pac.id,
pac.name,
pa.id attr_id,
pa.name attr_name
FROM
pms_product_attribute_category pac
LEFT JOIN pms_product_attribute pa ON pac.id = pa.product_attribute_category_id
AND pa.type=1;
</select>
</mapper>