FengChaoYu 1 mese fa
parent
commit
ab5109bd1a

+ 21 - 0
src/main/java/com/gree/mall/manager/bean/admin/AdminWebsitGrantBean.java

@@ -0,0 +1,21 @@
+package com.gree.mall.manager.bean.admin;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@ApiModel
+@Data
+public class AdminWebsitGrantBean {
+
+    @ApiModelProperty("角色id")
+    private String adminRoleId;
+    @ApiModelProperty("功能模块ids")
+    private List<String> adminModuleIds;
+    @ApiModelProperty("连带的功能模块ids")
+    private List<String> adminModuleIds2 = new ArrayList<>();
+
+}

+ 9 - 8
src/main/java/com/gree/mall/manager/controller/admin/AdminRoleController.java

@@ -16,32 +16,33 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 
 @Slf4j
 @RestController
-@Api(value = "系统角色管理", tags ={"系统角色管理"} )
+@Api(value = "系统角色管理", tags = {"系统角色管理"})
 @RequestMapping(value = "/admin/role", produces = "application/json; charset=utf-8")
 public class AdminRoleController {
 
-    @Autowired
+    @Resource
     AdminRoleLogic adminRoleLogic;
 
     @GetMapping("/list")
     @ApiOperation(value = "角色列表")
     public ResponseHelper<IPage<AdminRole>> list(
             HttpServletRequest request,
-            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
-            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize
+            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
+            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
     ) throws RemoteServiceException {
-        IPage<AdminRole> adminRoleIPage = adminRoleLogic.listPage(request,pageNum, pageSize);
+        IPage<AdminRole> adminRoleIPage = adminRoleLogic.listPage(request, pageNum, pageSize);
         return ResponseHelper.success(adminRoleIPage);
     }
 
     @GetMapping("/detail")
     @ApiOperation(value = "角色详情")
     public ResponseHelper<AdminRole> detail(
-            @ApiParam(value = "角色id",required = true) @RequestParam(required = true) String adminRoleId
+            @ApiParam(value = "角色id", required = true) @RequestParam(required = true) String adminRoleId
     ) throws RemoteServiceException {
         AdminRole detail = adminRoleLogic.detail(adminRoleId);
         return ResponseHelper.success(detail);
@@ -53,7 +54,7 @@ public class AdminRoleController {
             HttpServletRequest request,
             @RequestBody AdminRole adminRole
     ) throws RemoteServiceException {
-        adminRoleLogic.add(request,adminRole);
+        adminRoleLogic.add(request, adminRole);
         return ResponseHelper.success();
     }
 
@@ -67,7 +68,7 @@ public class AdminRoleController {
     @PostMapping("/delete")
     @ApiOperation(value = "删除角色")
     public ResponseHelper delete(
-            @ApiParam(value = "角色id",required = true) @RequestParam String adminRoleId
+            @ApiParam(value = "角色id", required = true) @RequestParam String adminRoleId
     ) throws RemoteServiceException {
         adminRoleLogic.delete(adminRoleId);
         return ResponseHelper.success();

+ 5 - 9
src/main/java/com/gree/mall/manager/controller/admin/AdminUserController.java

@@ -4,13 +4,10 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.gree.mall.manager.annotation.ApiNotAuth;
 import com.gree.mall.manager.bean.ExcelData;
 import com.gree.mall.manager.bean.SVerification;
-import com.gree.mall.manager.bean.admin.AdminUserCom;
-import com.gree.mall.manager.bean.admin.ExternalMapBean;
+import com.gree.mall.manager.bean.admin.*;
 import com.gree.mall.manager.bean.admin.reqDto.AdminCompanyWechatReqBean;
 import com.gree.mall.manager.bean.admin.reqDto.AdminUserAddReqBean;
 import com.gree.mall.manager.bean.admin.respDto.AdminCompanyWechatRespPageBean;
-import com.gree.mall.manager.bean.admin.AdminModuleTree;
-import com.gree.mall.manager.bean.admin.AdminUserBean;
 import com.gree.mall.manager.logic.admin.AdminCompanyWechatLogic;
 import com.gree.mall.manager.logic.admin.AdminUserLogic;
 import com.gree.mall.manager.exception.RemoteServiceException;
@@ -155,7 +152,7 @@ public class AdminUserController {
     @GetMapping("/module/id/checked")
     @ApiOperation("查询选中的功能模块ids")
     public ResponseHelper<List<String>> queryModuleIdChecked(
-            @ApiParam(value = "角色id", required = true) @RequestParam(required = true) String adminRoleId
+            @ApiParam(value = "角色id", required = true) @RequestParam String adminRoleId
     ) throws RemoteServiceException {
         List<String> moduleIds = adminUserLogic.queryModuleIdChecked(adminRoleId);
         return ResponseHelper.success(moduleIds);
@@ -164,7 +161,7 @@ public class AdminUserController {
     @GetMapping("/module/all")
     @ApiOperation(value = "全部功能菜单权限列表")
     public ResponseHelper<List<AdminModuleTree>> allModuleList(
-            @ApiParam(value = "角色", required = true) @RequestParam(required = true) String adminRoleId,
+            @ApiParam(value = "角色", required = true) @RequestParam String adminRoleId,
             HttpServletRequest request
     ) throws RemoteServiceException {
         List<AdminModuleTree> adminModuleTrees = adminUserLogic.queryAllAdminModuleTree(adminRoleId, request);
@@ -174,10 +171,9 @@ public class AdminUserController {
     @PostMapping("/module/grant")
     @ApiOperation(value = "授权动能菜单权限")
     public ResponseHelper grant(
-            @ApiParam(value = "角色id", required = true) @RequestParam(required = true) String adminRoleId,
-            @ApiParam(value = "功能模块ids", required = true) @RequestParam(required = true) List<String> adminModuleIds
+            @RequestBody AdminWebsitGrantBean adminWebsitGrantBean
     ) throws RemoteServiceException {
-        adminUserLogic.grantModules(adminRoleId, adminModuleIds);
+        adminUserLogic.grantModules(adminWebsitGrantBean);
         return ResponseHelper.success();
     }
 

+ 1 - 1
src/main/java/com/gree/mall/manager/controller/common/SysDictCompanyController.java

@@ -138,7 +138,7 @@ public class SysDictCompanyController {
     }
 
     @PostMapping("/importExcel")
-    @ApiOperation(value = "导入")
+    @ApiOperation(value = "导入(附件文件名:数据字典导入)")
     public ResponseHelper importExcel(
             @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
             HttpServletRequest request

+ 5 - 5
src/main/java/com/gree/mall/manager/enums/RoleTypeEnum.java

@@ -5,13 +5,13 @@ import lombok.Getter;
 
 @Getter
 public enum RoleTypeEnum {
-    WEBSIT(1,"部门"),
-    COMPANY(2,"企业"),
-    ADMIN(3,"admin"),
+    WEBSIT(2,"商家"),
+    COMPANY(1,"商户"),
+    ADMIN(0,"平台"),
     ;
 
-    private Integer code;
-    private String msg;
+    private final Integer code;
+    private final String msg;
 
     RoleTypeEnum(Integer code,String msg){
         this.code=code;

+ 72 - 21
src/main/java/com/gree/mall/manager/logic/admin/AdminRoleLogic.java

@@ -1,5 +1,6 @@
 package com.gree.mall.manager.logic.admin;
 
+import com.alibaba.excel.util.StringUtils;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -9,8 +10,11 @@ import com.gree.mall.manager.exception.RemoteServiceException;
 import com.gree.mall.manager.logic.common.CommonLogic;
 import com.gree.mall.manager.plus.entity.AdminRole;
 import com.gree.mall.manager.plus.entity.AdminUser;
+import com.gree.mall.manager.plus.entity.AdminUserModuleRela;
 import com.gree.mall.manager.plus.service.AdminRoleService;
+import com.gree.mall.manager.plus.service.AdminUserModuleRelaService;
 import com.gree.mall.manager.plus.service.AdminUserService;
+import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -18,16 +22,16 @@ import org.springframework.transaction.annotation.Transactional;
 import javax.servlet.http.HttpServletRequest;
 import java.util.Date;
 import java.util.List;
+import java.util.Objects;
 
 @Service
+@RequiredArgsConstructor
 public class AdminRoleLogic {
 
-    @Autowired
-    AdminRoleService adminRoleService;
-    @Autowired
-    AdminUserService adminUserService;
-    @Autowired
-    CommonLogic commonLogic;
+    private final AdminRoleService adminRoleService;
+    private final AdminUserService adminUserService;
+    private final CommonLogic commonLogic;
+    private final AdminUserModuleRelaService adminUserModuleRelaService;
 
 
     /**
@@ -37,30 +41,44 @@ public class AdminRoleLogic {
         AdminUserCom adminUser = commonLogic.getAdminUser(request);
         List<String> companyWechatIds = adminUser.getAdminCompanyIds();
 
-        Integer type = adminRoleService.getById(adminUser.getRoleId()).getType();
-        if("admin".equals(adminUser.getUserName())){
-            type = RoleTypeEnum.ADMIN.getCode();
-        }
+        Integer userType = this.checkUserType(adminUser);
 
         return adminRoleService.lambdaQuery()
-                .le(AdminRole::getType, type)
                 .in(CollectionUtils.isNotEmpty(companyWechatIds), AdminRole::getCompanyWechatId, companyWechatIds)
-                .orderByDesc(AdminRole::getType)
+                .eq(userType > 0, AdminRole::getType, userType)
+                .orderByAsc(AdminRole::getType)
                 .page(new Page<>(pageNo, pageSize));
     }
 
+    private Integer checkUserType(AdminUserCom adminUser) {
+        int userType = 0;
+        if (adminUser.getType() == 1 || adminUser.getType() == 3) {
+            userType = 1;
+        } else if (adminUser.getType() == 2 || adminUser.getType() == 4) {
+            userType = 2;
+        }
+        return userType;
+    }
+
     /**
      * 新增角色
      */
     public void add(HttpServletRequest request, AdminRole adminRole) {
+        AdminUserCom adminUser = commonLogic.getAdminUser(request);
 
-        //默认部门角色
-        adminRole.setType(RoleTypeEnum.WEBSIT.getCode());
+        if (Objects.isNull(adminRole.getType())) {
+            throw new RemoteServiceException("适用机构不能为空");
+        }
+
+        if (adminUser.getType() != 0 && adminRole.getType() == 0) {
+            throw new RemoteServiceException("适用机构参数异常");
+        }
+
+        if (StringUtils.isNotBlank(adminRole.getParentRoleId())) {
+            final AdminRole parentRole = adminRoleService.getById(adminRole.getParentRoleId());
+            adminRole.setParentRoleName(parentRole.getName());
+        }
 
-        AdminUserCom adminUser = commonLogic.getAdminUser(request);
-        adminRole.setCompanyWechatId(adminUser.getLoginCompanyWechatId());
-        adminRole.setCompanyName(adminUser.getLoginCompanyName());
-        adminRole.setCreateTime(new Date());
         adminRoleService.save(adminRole);
     }
 
@@ -69,11 +87,37 @@ public class AdminRoleLogic {
      */
     @Transactional
     public void update(AdminRole adminRole) {
-        adminRoleService.updateById(adminRole);
+        AdminUserCom adminUser = commonLogic.getAdminUser();
+
+        final AdminRole oldData = adminRoleService.getById(adminRole.getAdminRoleId());
+        // 父级角色id不为空查询父级角色名填充
+        if (StringUtils.isNotBlank(adminRole.getParentRoleId())) {
+            final AdminRole parentRole = adminRoleService.getById(adminRole.getParentRoleId());
+            adminRole.setParentRoleName(parentRole.getName());
+        }
 
         if (adminRole.getName() != null) {
-            adminUserService.lambdaUpdate().set(AdminUser::getRoleName, adminRole.getName())
-                    .eq(AdminUser::getRoleId, adminRole.getAdminRoleId()).update();
+            // 更新账号使用对应的角色名
+            adminUserService.lambdaUpdate()
+                    .set(AdminUser::getRoleName, adminRole.getName())
+                    .eq(AdminUser::getRoleId, adminRole.getAdminRoleId())
+                    .update();
+
+            // 更新所有子级的父级角色名
+            adminRoleService.lambdaUpdate()
+                    .set(AdminRole::getParentRoleName, adminRole.getName())
+                    .eq(AdminRole::getParentRoleId, adminRole.getAdminRoleId())
+                    .update();
+        }
+
+        adminRoleService.updateById(adminRole);
+
+        // 本次编辑与历史父级不一致
+        if (!adminRole.getParentRoleId().equals(oldData.getParentRoleId())) {
+            // 清空角色已选权限
+            adminUserModuleRelaService.lambdaUpdate()
+                    .eq(AdminUserModuleRela::getAdminRoleId, oldData.getAdminRoleId())
+                    .remove();
         }
     }
 
@@ -81,6 +125,13 @@ public class AdminRoleLogic {
      * 删除角色
      */
     public void delete(String adminRoleId) throws RemoteServiceException {
+        final Integer isOpen = adminRoleService.lambdaQuery()
+                .eq(AdminRole::getAdminRoleId, adminRoleId)
+                .eq(AdminRole::getIsOpen, false)
+                .count();
+        if (isOpen > 0) {
+            throw new RemoteServiceException("该角色不允许删除");
+        }
         //不允许删除使用该角色的帐号
         Integer count = adminUserService.lambdaQuery().eq(AdminUser::getRoleId, adminRoleId).count();
         if (count > 0) {

+ 74 - 14
src/main/java/com/gree/mall/manager/logic/admin/AdminUserLogic.java

@@ -10,10 +10,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.google.code.kaptcha.impl.DefaultKaptcha;
 import com.gree.mall.manager.bean.ExcelData;
 import com.gree.mall.manager.bean.SVerification;
-import com.gree.mall.manager.bean.admin.AdminModuleTree;
-import com.gree.mall.manager.bean.admin.AdminUserBean;
-import com.gree.mall.manager.bean.admin.AdminUserCom;
-import com.gree.mall.manager.bean.admin.ExternalMapBean;
+import com.gree.mall.manager.bean.admin.*;
 import com.gree.mall.manager.bean.admin.reqDto.AdminUserAddReqBean;
 import com.gree.mall.manager.bean.admin.reqDto.AdminUserPermissions;
 import com.gree.mall.manager.commonmapper.AdminMapper;
@@ -105,7 +102,10 @@ public class AdminUserLogic {
         if (!value.equals(codeValue)) {
             throw new RemoteServiceException("验证码错误");
         }
-        AdminUser adminUser = adminUserService.lambdaQuery().eq(AdminUser::getUserName, userName).eq(AdminUser::getPassword, MD5Utils.md5(password)).one();
+        AdminUser adminUser = adminUserService.lambdaQuery()
+                .eq(AdminUser::getUserName, userName)
+                .eq(AdminUser::getPassword, MD5Utils.md5(password))
+                .one();
         if (adminUser == null) {
             throw new RemoteServiceException("帐号密码错误");
         }
@@ -353,21 +353,81 @@ public class AdminUserLogic {
      * 给角色授权
      */
     @Transactional
-    public void grantModules(String roleId, List<String> adminModuleIds) {
-        //1.清空该角色的权限
-        adminUserModuleRelaService.lambdaUpdate().eq(AdminUserModuleRela::getAdminRoleId, roleId).remove();
-
-        //2.生成权限
-        List<AdminUserModuleRela> list = new ArrayList<>();
-        for (String adminModuleId : adminModuleIds) {
+    public void grantModules(AdminWebsitGrantBean adminWebsitGrantBean) {
+        String roleId = adminWebsitGrantBean.getAdminRoleId();
+        final Integer subSize = adminRoleService.lambdaQuery()
+                .eq(AdminRole::getParentRoleId, roleId)
+                .count();
+
+        //1.生成权限
+        List<AdminUserModuleRela> newList = new ArrayList<>();
+        for (String adminModuleId : adminWebsitGrantBean.getAdminModuleIds()) {
+            AdminUserModuleRela adminUserModuleRela = new AdminUserModuleRela();
+            adminUserModuleRela.setAdminRoleId(roleId);
+            adminUserModuleRela.setCreateTime(new Date());
+            adminUserModuleRela.setStatus(true);
+            adminUserModuleRela.setAdminModuleId(adminModuleId);
+            adminUserModuleRela.setFlag(1);
+            newList.add(adminUserModuleRela);
+        }
+        for (String adminModuleId : adminWebsitGrantBean.getAdminModuleIds2()) {
             AdminUserModuleRela adminUserModuleRela = new AdminUserModuleRela();
             adminUserModuleRela.setAdminRoleId(roleId);
             adminUserModuleRela.setCreateTime(new Date());
             adminUserModuleRela.setStatus(true);
             adminUserModuleRela.setAdminModuleId(adminModuleId);
-            list.add(adminUserModuleRela);
+            adminUserModuleRela.setFlag(0);
+            newList.add(adminUserModuleRela);
+        }
+
+        // 当前角色授权存在子角色,需要对比新旧数据,新加权限不处理,找出去掉的权限清除子角色的相应权限
+        if (subSize > 0) {
+            final List<AdminUserModuleRela> oldRelaList = adminUserModuleRelaService.lambdaQuery().eq(AdminUserModuleRela::getAdminRoleId, roleId).list();
+            // 对比出新数据没,旧数据有的roleId
+            final List<String> newModuleIds = newList.stream().map(AdminUserModuleRela::getAdminModuleId).collect(Collectors.toList());
+            List<String> removeSubModuleList = new ArrayList<>();
+            for (AdminUserModuleRela oldRela : oldRelaList) {
+                if (newModuleIds.contains(oldRela.getAdminModuleId())) {
+                    continue;
+                }
+                removeSubModuleList.add(oldRela.getAdminModuleId());
+            }
+            if (CollectionUtil.isNotEmpty(removeSubModuleList)) {
+                // 删除子角色关联菜单
+                this.removeRelaSubModule(roleId, removeSubModuleList);
+            }
+        }
+
+        //2.清空该角色的权限
+        adminUserModuleRelaService.lambdaUpdate().eq(AdminUserModuleRela::getAdminRoleId, roleId).remove();
+
+        adminUserModuleRelaService.saveBatch(newList);
+    }
+
+    // 递归执行删除子角色关联菜单
+    private void removeRelaSubModule(String parentRoleId, List<String> removeSubModuleList) {
+        final List<AdminRole> subRoleList = adminRoleService.lambdaQuery()
+                .select(AdminRole::getAdminRoleId)
+                .eq(AdminRole::getParentRoleId, parentRoleId)
+                .list();
+
+        if (CollectionUtil.isNotEmpty(subRoleList)) {
+            final List<String> roleIds = subRoleList.stream()
+                    .map(AdminRole::getAdminRoleId)
+                    .collect(Collectors.toList());
+
+            for (String moduleId : removeSubModuleList) {
+                adminUserModuleRelaService.lambdaUpdate()
+                        .eq(AdminUserModuleRela::getAdminModuleId, moduleId)
+                        .in(AdminUserModuleRela::getAdminRoleId, roleIds)
+                        .remove();
+            }
+
+            for (String roleId : roleIds) {
+                this.removeRelaSubModule(roleId, removeSubModuleList);
+            }
         }
-        adminUserModuleRelaService.saveBatch(list);
+
     }
 
     /**

+ 2 - 3
src/main/java/com/gree/mall/manager/logic/common/CommonLogic.java

@@ -47,8 +47,7 @@ public class CommonLogic {
     private String active;
     @Value("${wechat.payment.notifyUrl}")
     private String notifyUrl;
-    //    @Value("${wechat.payment.refundNotifyUrl}")
-//    private String refundNotifyUrl;
+
     @Value("${wechat.keyPath}")
     private String keyPath;
 
@@ -130,7 +129,7 @@ public class CommonLogic {
                 .collect(Collectors.toList());
         adminUserCom.setAdminDeptIds(deptList);
 
-        adminUserCom.setOnlyRead(companyList.size() > 1);
+//        adminUserCom.setOnlyRead(companyList.size() > 1);
 
         return adminUserCom;
     }

BIN
src/main/resources/template/数据字典导入.xlsx