Browse Source

添加产品分类接口

zhh 7 years ago
parent
commit
569508e96f

+ 6 - 3
mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java

@@ -3,6 +3,7 @@ package com.macro.mall.controller;
 import com.macro.mall.dto.CommonResult;
 import com.macro.mall.dto.PmsBrandParam;
 import com.macro.mall.service.PmsBrandService;
+import com.macro.mall.validator.FlagValidator;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.slf4j.Logger;
@@ -90,7 +91,7 @@ public class PmsBrandController {
     @ResponseBody
     public Object listBrand(@RequestParam(value = "keyword", required = false) String keyword,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
-                            @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
+                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
         return new CommonResult().pageSuccess(brandService.listBrand(keyword, pageNum, pageSize));
     }
 
@@ -118,7 +119,8 @@ public class PmsBrandController {
     @ApiOperation(value = "批量更新显示状态")
     @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
     @ResponseBody
-    public Object updateShowStatus(@RequestParam("ids") List<Long> ids, @RequestParam("showStatus") Integer showStatus) {
+    public Object updateShowStatus(@RequestParam("ids") List<Long> ids,
+                                   @RequestParam("showStatus") Integer showStatus) {
         int count = brandService.updateShowStatus(ids, showStatus);
         if (count > 0) {
             LOGGER.debug("updateShowStatus success:ids={}", ids);
@@ -132,7 +134,8 @@ public class PmsBrandController {
     @ApiOperation(value = "批量更新厂家制造商状态")
     @RequestMapping(value = "/update/factoryStatus", method = RequestMethod.GET)
     @ResponseBody
-    public Object updateFactoryStatus(@RequestParam("ids") List<Long> ids, @RequestParam("factoryStatus") Integer factoryStatus) {
+    public Object updateFactoryStatus(@RequestParam("ids") List<Long> ids,
+                                      @RequestParam("factoryStatus") Integer factoryStatus) {
         int count = brandService.updateFactoryStatus(ids, factoryStatus);
         if (count > 0) {
             LOGGER.debug("updateFactoryStatus success:{}", ids);

+ 88 - 0
mall-admin/src/main/java/com/macro/mall/controller/PmsProductCategoryController.java

@@ -0,0 +1,88 @@
+package com.macro.mall.controller;
+
+import com.macro.mall.dto.CommonResult;
+import com.macro.mall.dto.PmsProductCategoryParam;
+import com.macro.mall.model.PmsProductCategory;
+import com.macro.mall.service.PmsProductCategoryService;
+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.validation.BindingResult;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+/**
+ * 商品分类模块Controller
+ */
+@Controller
+@RequestMapping("/productCategory")
+public class PmsProductCategoryController {
+    private static final Logger LOGGER = LoggerFactory.getLogger(PmsProductCategoryController.class);
+    @Autowired
+    private PmsProductCategoryService productCategoryService;
+
+    @ApiOperation("添加产品分类")
+    @RequestMapping(value = "/create", method = RequestMethod.POST)
+    @ResponseBody
+    public Object create(@Validated @RequestBody PmsProductCategoryParam pmsProductCategoryParam, BindingResult result) {
+        if (result.hasErrors()) {
+            return new CommonResult().validateFailed(result);
+        }
+        int count = productCategoryService.create(pmsProductCategoryParam);
+        if (count > 0) {
+            LOGGER.debug("create success {}", pmsProductCategoryParam);
+            return new CommonResult().success(count);
+        } else {
+            LOGGER.debug("create failed {}", pmsProductCategoryParam);
+            return new CommonResult().failed();
+        }
+    }
+
+    @ApiOperation("修改商品分类")
+    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
+    @ResponseBody
+    public Object update(@PathVariable Long id,
+                         @Validated
+                         @RequestBody PmsProductCategoryParam pmsProductCategoryParam,
+                         BindingResult result) {
+        if (result.hasErrors()) {
+            return new CommonResult().validateFailed(result);
+        }
+        int count = productCategoryService.update(id, pmsProductCategoryParam);
+        if (count > 0) {
+            LOGGER.debug("update success {}", pmsProductCategoryParam);
+            return new CommonResult().success(count);
+        } else {
+            LOGGER.debug("update failed {}", pmsProductCategoryParam);
+            return new CommonResult().failed();
+        }
+    }
+
+    @ApiOperation("分页查询商品分类")
+    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
+    @ResponseBody
+    public Object list(@PathVariable Long parentId,
+                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
+                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
+        List<PmsProductCategory> productCategoryList = productCategoryService.list(parentId, pageSize, pageNum);
+        return new CommonResult().pageSuccess(productCategoryList);
+    }
+
+    @ApiOperation("删除商品分类")
+    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
+    @ResponseBody
+    public Object delete(@PathVariable Long id) {
+        int count = productCategoryService.delete(id);
+        if (count > 0) {
+            LOGGER.debug("delete success id:{}", id);
+            return new CommonResult().success(count);
+        } else {
+            LOGGER.debug("delete failed id:{}", id);
+            return new CommonResult().failed();
+        }
+    }
+}

+ 12 - 0
mall-admin/src/main/java/com/macro/mall/controller/PmsProductController.java

@@ -0,0 +1,12 @@
+package com.macro.mall.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+/**
+ * 商品管理Controller
+ */
+@Controller
+@RequestMapping("/product")
+public class PmsProductController {
+}

+ 11 - 1
mall-admin/src/main/java/com/macro/mall/dto/CommonResult.java

@@ -1,6 +1,7 @@
 package com.macro.mall.dto;
 
 import com.github.pagehelper.PageInfo;
+import org.springframework.validation.BindingResult;
 
 import java.util.HashMap;
 import java.util.List;
@@ -37,7 +38,7 @@ public class CommonResult {
         long totalPage = pageInfo.getTotal() / pageInfo.getPageSize();
         Map<String, Object> result = new HashMap<>();
         result.put("pageSize", pageInfo.getPageSize());
-        result.put("totalPage", totalPage);
+        result.put("totalPage", totalPage+1);
         result.put("pageNum", pageInfo.getPageNum());
         result.put("list", pageInfo.getList());
         this.code = SUCCESS;
@@ -66,6 +67,15 @@ public class CommonResult {
         return this;
     }
 
+    /**
+     * 参数验证失败使用
+     * @param result 错误信息
+     */
+    public CommonResult validateFailed(BindingResult result) {
+        validateFailed(result.getFieldError().getDefaultMessage());
+        return this;
+    }
+
     public int getCode() {
         return code;
     }

+ 3 - 2
mall-admin/src/main/java/com/macro/mall/dto/PmsBrandParam.java

@@ -3,6 +3,7 @@ package com.macro.mall.dto;
 import com.macro.mall.validator.FlagValidator;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
+import org.hibernate.validator.constraints.NotEmpty;
 
 import javax.validation.constraints.Min;
 import javax.validation.constraints.NotNull;
@@ -13,7 +14,7 @@ import javax.validation.constraints.NotNull;
 @ApiModel(value = "PmsBrandParam")
 public class PmsBrandParam {
     @ApiModelProperty(value = "品牌名称",required = true)
-    @NotNull(message = "名称不能为空")
+    @NotEmpty(message = "名称不能为空")
     private String name;
     @ApiModelProperty(value = "品牌首字母")
     private String firstLetter;
@@ -27,7 +28,7 @@ public class PmsBrandParam {
     @FlagValidator(value = {"0","1"}, message = "显示状态不正确")
     private Integer showStatus;
     @ApiModelProperty(value = "品牌logo",required = true)
-    @NotNull(message = "品牌logo不能为空")
+    @NotEmpty(message = "品牌logo不能为空")
     private String logo;
     @ApiModelProperty(value = "品牌大图")
     private String bigPic;

+ 107 - 0
mall-admin/src/main/java/com/macro/mall/dto/PmsProductCategoryParam.java

@@ -0,0 +1,107 @@
+package com.macro.mall.dto;
+
+import com.macro.mall.validator.FlagValidator;
+import io.swagger.annotations.ApiModelProperty;
+import org.hibernate.validator.constraints.NotEmpty;
+
+import javax.validation.constraints.Min;
+
+/**
+ * 添加更新产品分类的参数
+ */
+public class PmsProductCategoryParam {
+    @ApiModelProperty("父分类的编号")
+    private Long parentId;
+    @ApiModelProperty(value = "商品分类名称",required = true)
+    @NotEmpty(message = "商品分类名称不能为空")
+    private String name;
+    @ApiModelProperty("分类单位")
+    private String productUnit;
+    @ApiModelProperty("是否在导航栏显示")
+    @FlagValidator(value = {"0","1"},message = "状态只能为0或1")
+    private Integer navStatus;
+    @ApiModelProperty("是否进行显示")
+    @FlagValidator(value = {"0","1"},message = "状态只能为0或1")
+    private Integer showStatus;
+    @ApiModelProperty("排序")
+    @Min(value = 0,message = "排序最小为0")
+    private Integer sort;
+    @ApiModelProperty("图标")
+    private String icon;
+    @ApiModelProperty("关键字")
+    private String keywords;
+    @ApiModelProperty("描述")
+    private String description;
+
+    public Long getParentId() {
+        return parentId;
+    }
+
+    public void setParentId(Long parentId) {
+        this.parentId = parentId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getProductUnit() {
+        return productUnit;
+    }
+
+    public void setProductUnit(String productUnit) {
+        this.productUnit = productUnit;
+    }
+
+    public Integer getNavStatus() {
+        return navStatus;
+    }
+
+    public void setNavStatus(Integer navStatus) {
+        this.navStatus = navStatus;
+    }
+
+    public Integer getShowStatus() {
+        return showStatus;
+    }
+
+    public void setShowStatus(Integer showStatus) {
+        this.showStatus = showStatus;
+    }
+
+    public Integer getSort() {
+        return sort;
+    }
+
+    public void setSort(Integer sort) {
+        this.sort = sort;
+    }
+
+    public String getIcon() {
+        return icon;
+    }
+
+    public void setIcon(String icon) {
+        this.icon = icon;
+    }
+
+    public String getKeywords() {
+        return keywords;
+    }
+
+    public void setKeywords(String keywords) {
+        this.keywords = keywords;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+}

+ 1 - 1
mall-admin/src/main/java/com/macro/mall/service/PmsBrandService.java

@@ -19,7 +19,7 @@ public interface PmsBrandService {
 
     int deleteBrand(List<Long> ids);
 
-    List<PmsBrand> listBrand(String keyword,int pageNum, int pageSize);
+    List<PmsBrand> listBrand(String keyword, int pageNum, int pageSize);
 
     PmsBrand getBrand(Long id);
 

+ 19 - 0
mall-admin/src/main/java/com/macro/mall/service/PmsProductCategoryService.java

@@ -0,0 +1,19 @@
+package com.macro.mall.service;
+
+import com.macro.mall.dto.PmsProductCategoryParam;
+import com.macro.mall.model.PmsProductCategory;
+
+import java.util.List;
+
+/**
+ * 产品分类Service
+ */
+public interface PmsProductCategoryService {
+    int create(PmsProductCategoryParam pmsProductCategoryParam);
+
+    int update(Long id, PmsProductCategoryParam pmsProductCategoryParam);
+
+    List<PmsProductCategory> list(Long parentId, Integer pageSize, Integer pageNum);
+
+    int delete(Long id);
+}

+ 4 - 2
mall-admin/src/main/java/com/macro/mall/service/impl/PmsBrandServiceImpl.java

@@ -65,9 +65,11 @@ public class PmsBrandServiceImpl implements PmsBrandService {
     public List<PmsBrand> listBrand(String keyword, int pageNum, int pageSize) {
         PageHelper.startPage(pageNum, pageSize);
         PmsBrandExample pmsBrandExample = new PmsBrandExample();
-        pmsBrandExample.setOrderByClause("sort asc");
+        pmsBrandExample.setOrderByClause("sort desc");
+        PmsBrandExample.Criteria criteria = pmsBrandExample.createCriteria();
+        criteria.andShowStatusEqualTo(1);
         if (!StringUtils.isEmpty(keyword)) {
-            pmsBrandExample.createCriteria().andNameLike("%" + keyword + "%");
+            criteria.andNameLike("%" + keyword + "%");
         }
         return brandMapper.selectByExample(pmsBrandExample);
     }

+ 72 - 0
mall-admin/src/main/java/com/macro/mall/service/impl/PmsProductCategoryServiceImpl.java

@@ -0,0 +1,72 @@
+package com.macro.mall.service.impl;
+
+import com.github.pagehelper.PageHelper;
+import com.macro.mall.dto.PmsProductCategoryParam;
+import com.macro.mall.mapper.PmsProductCategoryMapper;
+import com.macro.mall.model.PmsProductCategory;
+import com.macro.mall.model.PmsProductCategoryExample;
+import com.macro.mall.service.PmsProductCategoryService;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * PmsProductCategoryService实现类
+ */
+@Service
+public class PmsProductCategoryServiceImpl implements PmsProductCategoryService {
+    @Autowired
+    private PmsProductCategoryMapper productCategoryMapper;
+
+    @Override
+    public int create(PmsProductCategoryParam pmsProductCategoryParam) {
+        PmsProductCategory productCategory = new PmsProductCategory();
+        BeanUtils.copyProperties(pmsProductCategoryParam, productCategory);
+        //没有父分类时为一级分类
+        setCategoryLevel(productCategory);
+        return productCategoryMapper.insertSelective(productCategory);
+    }
+
+    @Override
+    public int update(Long id, PmsProductCategoryParam pmsProductCategoryParam) {
+        PmsProductCategory productCategory = new PmsProductCategory();
+        productCategory.setId(id);
+        BeanUtils.copyProperties(pmsProductCategoryParam, productCategory);
+        setCategoryLevel(productCategory);
+        return productCategoryMapper.updateByPrimaryKeySelective(productCategory);
+    }
+
+    @Override
+    public List<PmsProductCategory> list(Long parentId, Integer pageSize, Integer pageNum) {
+        PageHelper.startPage(pageNum, pageSize);
+        PmsProductCategoryExample example = new PmsProductCategoryExample();
+        example.setOrderByClause("sort desc");
+        example.createCriteria().andParentIdEqualTo(parentId);
+        return productCategoryMapper.selectByExample(example);
+    }
+
+    @Override
+    public int delete(Long id) {
+        return productCategoryMapper.deleteByPrimaryKey(id);
+    }
+
+    /**
+     * 根据分类的parentId设置分类的level
+     */
+    private void setCategoryLevel(PmsProductCategory productCategory) {
+        //没有父分类时为一级分类
+        if (productCategory.getParentId() == 0) {
+            productCategory.setLevel(0);
+        } else {
+            //有父分类时选择根据父分类level设置
+            PmsProductCategory parentCategory = productCategoryMapper.selectByPrimaryKey(productCategory.getParentId());
+            if (parentCategory != null) {
+                productCategory.setLevel(parentCategory.getLevel() + 1);
+            } else {
+                productCategory.setLevel(0);
+            }
+        }
+    }
+}