From 4dad73799ebf1983469f50675704951abfbb0b71 Mon Sep 17 00:00:00 2001 From: clunt Date: Thu, 17 Mar 2022 15:33:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8C=BA=E5=9F=9F=E6=A8=A1?= =?UTF-8?q?=E5=9D=97controller=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/SysAreaController.java | 121 ++++++++++++++++++ .../ghy/common/constant/UserConstants.java | 4 + .../ghy/system/service/ISysAreaService.java | 14 ++ .../service/impl/SysAreaServiceImpl.java | 20 +++ .../resources/mapper/system/SysAreaMapper.xml | 3 + 5 files changed, 162 insertions(+) create mode 100644 ghy-admin/src/main/java/com/ghy/web/controller/system/SysAreaController.java diff --git a/ghy-admin/src/main/java/com/ghy/web/controller/system/SysAreaController.java b/ghy-admin/src/main/java/com/ghy/web/controller/system/SysAreaController.java new file mode 100644 index 00000000..a9d1bf8d --- /dev/null +++ b/ghy-admin/src/main/java/com/ghy/web/controller/system/SysAreaController.java @@ -0,0 +1,121 @@ +package com.ghy.web.controller.system; + +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.enums.BusinessType; +import com.ghy.system.domain.SysArea; +import com.ghy.system.service.ISysAreaService; +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 clunt + */ +@Controller +@RequestMapping("/system/area") +public class SysAreaController extends BaseController { + + private String prefix = "system/area"; + + @Resource + private ISysAreaService sysAreaService; + + @RequiresPermissions("system:area:list") + @PostMapping("/list") + @ResponseBody + public List list(SysArea sysArea) + { + List areaList = sysAreaService.selectSysAreaList(sysArea); + return areaList; + } + + /** + * 新增保存区域 + */ + @Log(title = "区域管理", businessType = BusinessType.INSERT) + @RequiresPermissions("system:area:add") + @PostMapping("/add") + @ResponseBody + public AjaxResult addSave(@Validated SysArea sysArea) + { + if (UserConstants.AREA_CODE_NOT_UNIQUE.equals(sysAreaService.checkAreaCodeUnique(sysArea))) + { + return error("新增区域'" + sysArea.getAreaName() + "'失败,区域编码已存在"); + } + sysArea.setCreateBy(getLoginName()); + return toAjax(sysAreaService.insertSysArea(sysArea)); + } + + /** + * 修改保存区域 + */ + @Log(title = "区域管理", businessType = BusinessType.UPDATE) + @RequiresPermissions("system:area:edit") + @PostMapping("/edit") + @ResponseBody + public AjaxResult editSave(@Validated SysArea sysArea) + { + if (UserConstants.AREA_CODE_NOT_UNIQUE.equals(sysAreaService.checkAreaCodeUnique(sysArea))) + { + return error("修改区域'" + sysArea.getAreaName() + "'失败,区域编码已存在"); + } + sysArea.setUpdateBy(getLoginName()); + return toAjax(sysAreaService.updateSysArea(sysArea)); + } + + /** + * 删除 + */ + @Log(title = "区域管理", businessType = BusinessType.DELETE) + @RequiresPermissions("system:area:remove") + @GetMapping("/remove/{areaId}") + @ResponseBody + public AjaxResult remove(@PathVariable("areaId") Long areaId) + { + if (sysAreaService.selectAreaCount(areaId) > 0) + { + return AjaxResult.warn("存在下级区域,不允许删除"); + } + return toAjax(sysAreaService.deleteSysAreaByIds(areaId.toString())); + } + + + @RequiresPermissions("system:area:view") + @GetMapping() + public String area() { + return prefix + "/area"; + } + + + /** + * 新增下级区域 + */ + @GetMapping("/add/{parentCode}") + public String add(@PathVariable("parentCode") String parentCode, ModelMap mmap) { + mmap.put("sysCode", sysAreaService.selectByAreaCode(parentCode)); + return prefix + "/add"; + } + + /** + * 修改区域 + */ + @RequiresPermissions("system:area:edit") + @GetMapping("/edit/{areaId}") + public String edit(@PathVariable("areaId") Long areaId, ModelMap mmap) { + SysArea sysArea = sysAreaService.selectById(areaId); + mmap.put("sysArea", sysArea); + return prefix + "/edit"; + } + + +} 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 1e52a64e..7dd52174 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 @@ -61,6 +61,10 @@ public class UserConstants public final static String DEPT_NAME_UNIQUE = "0"; public final static String DEPT_NAME_NOT_UNIQUE = "1"; + /** 区域编码是否唯一的返回结果码 */ + public final static String AREA_CODE_UNIQUE = "0"; + public final static String AREA_CODE_NOT_UNIQUE = "1"; + /** 角色名称是否唯一的返回结果码 */ public final static String ROLE_NAME_UNIQUE = "0"; public final static String ROLE_NAME_NOT_UNIQUE = "1"; diff --git a/ghy-system/src/main/java/com/ghy/system/service/ISysAreaService.java b/ghy-system/src/main/java/com/ghy/system/service/ISysAreaService.java index c0ce1803..08485543 100644 --- a/ghy-system/src/main/java/com/ghy/system/service/ISysAreaService.java +++ b/ghy-system/src/main/java/com/ghy/system/service/ISysAreaService.java @@ -46,4 +46,18 @@ public interface ISysAreaService { */ public int insertSysArea(SysArea sysArea); + + /** + * @param sysArea 区域实体 + * @return 校验结果 + */ + String checkAreaCodeUnique(SysArea sysArea); + + + /** + * @param areaId 区域id + * @return 下级区域条数 + */ + int selectAreaCount(Long areaId); + } diff --git a/ghy-system/src/main/java/com/ghy/system/service/impl/SysAreaServiceImpl.java b/ghy-system/src/main/java/com/ghy/system/service/impl/SysAreaServiceImpl.java index 49d08be5..ccc5bf8d 100644 --- a/ghy-system/src/main/java/com/ghy/system/service/impl/SysAreaServiceImpl.java +++ b/ghy-system/src/main/java/com/ghy/system/service/impl/SysAreaServiceImpl.java @@ -1,7 +1,9 @@ package com.ghy.system.service.impl; +import com.ghy.common.constant.UserConstants; import com.ghy.common.core.text.Convert; import com.ghy.common.exception.ServiceException; +import com.ghy.common.utils.StringUtils; import com.ghy.system.domain.SysArea; import com.ghy.system.mapper.SysAreaMapper; import com.ghy.system.service.ISysAreaService; @@ -59,6 +61,24 @@ public class SysAreaServiceImpl implements ISysAreaService { return sysAreaMapper.insertSysArea(sysArea); } + @Override + public String checkAreaCodeUnique(SysArea sysArea) { + Long areaId = StringUtils.isNull(sysArea.getAreaId()) ? -1L : sysArea.getAreaId(); + SysArea info = sysAreaMapper.selectByAreaCode(sysArea.getAreaCode()); + if(StringUtils.isNotNull(info) && info.getAreaId().longValue() != areaId){ + return UserConstants.AREA_CODE_UNIQUE; + } + return UserConstants.AREA_CODE_NOT_UNIQUE; + } + + @Override + public int selectAreaCount(Long areaId) { + SysArea sysArea = sysAreaMapper.selectById(areaId); + SysArea info = new SysArea(); + info.setParentCode(sysArea.getAreaCode()); + return sysAreaMapper.selectSysAreaList(info).size(); + } + public int countUseSysAreaById(SysArea sysArea){ //TODO 校验区域是否使用 return 0; diff --git a/ghy-system/src/main/resources/mapper/system/SysAreaMapper.xml b/ghy-system/src/main/resources/mapper/system/SysAreaMapper.xml index fe6b4329..c203c21a 100644 --- a/ghy-system/src/main/resources/mapper/system/SysAreaMapper.xml +++ b/ghy-system/src/main/resources/mapper/system/SysAreaMapper.xml @@ -38,6 +38,9 @@ AND area_name like concat('%', #{areaName}, '%') + + AND parent_code = #{parentCode} +