diff --git a/ghy-admin/src/main/java/com/ghy/web/controller/goods/GoodsCategoryController.java b/ghy-admin/src/main/java/com/ghy/web/controller/goods/GoodsCategoryController.java new file mode 100644 index 00000000..dbcd03d2 --- /dev/null +++ b/ghy-admin/src/main/java/com/ghy/web/controller/goods/GoodsCategoryController.java @@ -0,0 +1,136 @@ +package com.ghy.web.controller.goods; + +import com.ghy.common.annotation.Log; +import com.ghy.common.constant.UserConstants; +import com.ghy.common.core.controller.BaseController; +import com.ghy.common.core.domain.AjaxResult; +import com.ghy.common.core.page.TableDataInfo; +import com.ghy.common.enums.BusinessType; +import com.ghy.goods.domain.GoodsCategory; +import com.ghy.goods.service.GoodsCategoryService; +import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * @author HH 2022/3/19 + */ +@Controller +@RequestMapping("/goods/category") +public class GoodsCategoryController extends BaseController { + + private static final String PREFIX = "goods/category"; + + @Resource + private GoodsCategoryService goodsCategoryService; + + @RequiresPermissions("goods:category:view") + @GetMapping() + public String goodsCategory() { + return PREFIX + "/category"; + } + + /** + * 新增商品类别页面 + */ + @GetMapping("/add") + public String add() { + return PREFIX + "/add"; + } + + /** + * 修改商品类别页面 + */ + @RequiresPermissions("goods:category:edit") + @GetMapping("/edit/{goodsId}") + public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap) { + mmap.put("goodsCategory", goodsCategoryService.selectById(goodsId)); + return PREFIX + "/edit"; + } + + /** + * 新增商品类别接口 + */ + @RequiresPermissions("goods:category:add") + @Log(title = "商品类别管理", businessType = BusinessType.INSERT) + @PostMapping("/add") + @ResponseBody + public AjaxResult addSave(@Validated GoodsCategory category) { + if (goodsCategoryService.checkGoodsCategoryNameUnique(category)) { + return error("新增商品类别'" + category.getGoodsCategoryName() + "'失败,商品类别名称已存在"); + } else if (goodsCategoryService.checkGoodsCategoryCodeUnique(category)) { + return error("新增商品类别'" + category.getGoodsCategoryCode() + "'失败,商品类别编码已存在"); + } + category.setCreateBy(getLoginName()); + return toAjax(goodsCategoryService.insertGoodsCategory(category)); + } + + /** + * 删除商品类别接口 + */ + @RequiresPermissions("goods:category:remove") + @Log(title = "商品类别管理", businessType = BusinessType.DELETE) + @PostMapping("/remove") + @ResponseBody + public AjaxResult remove(String ids) { + try { + return toAjax(goodsCategoryService.deleteGoodsCategoryByIds(ids)); + } catch (Exception e) { + return error(e.getMessage()); + } + } + + /** + * 修改商品类别接口 + */ + @RequiresPermissions("goods:category:edit") + @Log(title = "商品类别管理", businessType = BusinessType.UPDATE) + @PostMapping("/edit") + @ResponseBody + public AjaxResult editSave(@Validated GoodsCategory category) { + if (goodsCategoryService.checkGoodsCategoryNameUnique(category)) { + return error("新增商品类别'" + category.getGoodsCategoryName() + "'失败,商品类别名称已存在"); + } else if (goodsCategoryService.checkGoodsCategoryCodeUnique(category)) { + return error("新增商品类别'" + category.getGoodsCategoryCode() + "'失败,商品类别编码已存在"); + } + category.setUpdateBy(getLoginName()); + return toAjax(goodsCategoryService.updateGoodsCategory(category)); + } + + /** + * 商品类别表 + */ + @RequiresPermissions("goods:goods:list") + @PostMapping("/list") + @ResponseBody + public TableDataInfo list(GoodsCategory category) { + startPage(); + List list = goodsCategoryService.selectGoodsCategoryList(category); + return getDataTable(list); + } + + /** + * 校验商品类别名称 + */ + @PostMapping("/checkGoodsNameUnique") + @ResponseBody + public String checkGoodsNameUnique(GoodsCategory category) { + return goodsCategoryService.checkGoodsCategoryNameUnique(category) ? + UserConstants.GOODS_CATEGORY_NAME_NOT_UNIQUE : UserConstants.GOODS_CATEGORY_NAME_UNIQUE; + } + + /** + * 校验商品类别编码 + */ + @PostMapping("/checkGoodsCodeUnique") + @ResponseBody + public String checkGoodsCodeUnique(GoodsCategory category) { + return goodsCategoryService.checkGoodsCategoryCodeUnique(category) ? + UserConstants.GOODS_CATEGORY_CODE_NOT_UNIQUE : UserConstants.GOODS_CATEGORY_CODE_UNIQUE; + } +} diff --git a/ghy-common/src/main/java/com/ghy/common/constant/UserConstants.java b/ghy-common/src/main/java/com/ghy/common/constant/UserConstants.java index 7dd52174..d2e199a5 100644 --- a/ghy-common/src/main/java/com/ghy/common/constant/UserConstants.java +++ b/ghy-common/src/main/java/com/ghy/common/constant/UserConstants.java @@ -89,6 +89,14 @@ public class UserConstants public final static String GOODS_NAME_UNIQUE = "0"; public final static String GOODS_NAME_NOT_UNIQUE = "1"; + /** 商品类别编码是否唯一的返回结果 */ + public final static String GOODS_CATEGORY_CODE_UNIQUE = "0"; + public final static String GOODS_CATEGORY_CODE_NOT_UNIQUE = "1"; + + /** 商品类别名称是否唯一的返回结果 */ + public final static String GOODS_CATEGORY_NAME_UNIQUE = "0"; + public final static String GOODS_CATEGORY_NAME_NOT_UNIQUE = "1"; + /** 菜单名称是否唯一的返回结果码 */ public final static String MENU_NAME_UNIQUE = "0"; public final static String MENU_NAME_NOT_UNIQUE = "1";