1新增用户权限分配和查询

This commit is contained in:
fengjundan 2021-07-20 18:27:35 +08:00
parent bd849f6d26
commit 13a5b8560e
17 changed files with 686 additions and 97 deletions

View File

@ -22,7 +22,7 @@
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -31,13 +31,12 @@ import java.util.stream.Collectors;
/**
* 用户信息
*
*
* @author ruoyi
*/
@RestController
@RequestMapping("/user")
public class SysUserController extends BaseController
{
public class SysUserController extends BaseController {
@Autowired
private ISysUserService userService;
@ -55,8 +54,7 @@ public class SysUserController extends BaseController
*/
@PreAuthorize(hasPermi = "system:user:list")
@GetMapping("/list")
public TableDataInfo list(SysUser user)
{
public TableDataInfo list(SysUser user) {
startPage();
List<SysUser> list = userService.selectUserList(user);
return getDataTable(list);
@ -65,8 +63,7 @@ public class SysUserController extends BaseController
@Log(title = "用户管理", businessType = BusinessType.EXPORT)
@PreAuthorize(hasPermi = "system:user:export")
@PostMapping("/export")
public void export(HttpServletResponse response, SysUser user) throws IOException
{
public void export(HttpServletResponse response, SysUser user) throws IOException {
List<SysUser> list = userService.selectUserList(user);
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
util.exportExcel(response, list, "用户数据");
@ -75,8 +72,7 @@ public class SysUserController extends BaseController
@Log(title = "用户管理", businessType = BusinessType.IMPORT)
@PreAuthorize(hasPermi = "system:user:import")
@PostMapping("/importData")
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
{
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
List<SysUser> userList = util.importExcel(file.getInputStream());
String operName = SecurityUtils.getUsername();
@ -85,8 +81,7 @@ public class SysUserController extends BaseController
}
@PostMapping("/importTemplate")
public void importTemplate(HttpServletResponse response) throws IOException
{
public void importTemplate(HttpServletResponse response) throws IOException {
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
util.importTemplateExcel(response, "用户数据");
}
@ -95,11 +90,9 @@ public class SysUserController extends BaseController
* 获取当前用户信息
*/
@GetMapping("/info/{username}")
public R<LoginUser> info(@PathVariable("username") String username)
{
public R<LoginUser> info(@PathVariable("username") String username) {
SysUser sysUser = userService.selectUserByUserName(username);
if (StringUtils.isNull(sysUser))
{
if (StringUtils.isNull(sysUser)) {
return R.fail("用户名或密码错误");
}
// 角色集合
@ -115,12 +108,11 @@ public class SysUserController extends BaseController
/**
* 获取用户信息
*
*
* @return 用户信息
*/
@GetMapping("getInfo")
public AjaxResult getInfo()
{
public AjaxResult getInfo() {
Long userId = SecurityUtils.getUserId();
// 角色集合
Set<String> roles = permissionService.getRolePermission(userId);
@ -137,15 +129,13 @@ public class SysUserController extends BaseController
* 根据用户编号获取详细信息
*/
@PreAuthorize(hasPermi = "system:user:query")
@GetMapping(value = { "/", "/{userId}" })
public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
{
@GetMapping(value = {"/", "/{userId}"})
public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) {
AjaxResult ajax = AjaxResult.success();
List<SysRole> roles = roleService.selectRoleAll();
ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
ajax.put("posts", postService.selectPostAll());
if (StringUtils.isNotNull(userId))
{
if (StringUtils.isNotNull(userId)) {
ajax.put(AjaxResult.DATA_TAG, userService.selectUserById(userId));
ajax.put("postIds", postService.selectPostListByUserId(userId));
ajax.put("roleIds", roleService.selectRoleListByUserId(userId));
@ -159,15 +149,11 @@ public class SysUserController extends BaseController
@PreAuthorize(hasPermi = "system:user:add")
@Log(title = "用户管理", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@Validated @RequestBody SysUser user)
{
if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName())))
{
public AjaxResult add(@Validated @RequestBody SysUser user) {
if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,登录账号已存在");
}
else if (StringUtils.isNotEmpty(user.getPhonenumber())
&& UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
{
} else if (StringUtils.isNotEmpty(user.getPhonenumber())
&& UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,手机号码已存在");
}
user.setCreateBy(SecurityUtils.getUsername());
@ -181,17 +167,13 @@ public class SysUserController extends BaseController
@PreAuthorize(hasPermi = "system:user:edit")
@Log(title = "用户管理", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@Validated @RequestBody SysUser user)
{
public AjaxResult edit(@Validated @RequestBody SysUser user) {
userService.checkUserAllowed(user);
if (StringUtils.isNotEmpty(user.getPhonenumber())
&& UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
{
&& UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
}
else if (StringUtils.isNotEmpty(user.getEmail())
&& UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
{
} else if (StringUtils.isNotEmpty(user.getEmail())
&& UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
}
user.setUpdateBy(SecurityUtils.getUsername());
@ -204,8 +186,7 @@ public class SysUserController extends BaseController
@PreAuthorize(hasPermi = "system:user:remove")
@Log(title = "用户管理", businessType = BusinessType.DELETE)
@DeleteMapping("/{userIds}")
public AjaxResult remove(@PathVariable Long[] userIds)
{
public AjaxResult remove(@PathVariable Long[] userIds) {
return toAjax(userService.deleteUserByIds(userIds));
}
@ -215,8 +196,7 @@ public class SysUserController extends BaseController
@PreAuthorize(hasPermi = "system:user:edit")
@Log(title = "用户管理", businessType = BusinessType.UPDATE)
@PutMapping("/resetPwd")
public AjaxResult resetPwd(@RequestBody SysUser user)
{
public AjaxResult resetPwd(@RequestBody SysUser user) {
userService.checkUserAllowed(user);
user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
user.setUpdateBy(SecurityUtils.getUsername());
@ -229,8 +209,7 @@ public class SysUserController extends BaseController
@PreAuthorize(hasPermi = "system:user:edit")
@Log(title = "用户管理", businessType = BusinessType.UPDATE)
@PutMapping("/changeStatus")
public AjaxResult changeStatus(@RequestBody SysUser user)
{
public AjaxResult changeStatus(@RequestBody SysUser user) {
userService.checkUserAllowed(user);
user.setUpdateBy(SecurityUtils.getUsername());
return toAjax(userService.updateUserStatus(user));
@ -241,8 +220,7 @@ public class SysUserController extends BaseController
*/
@PreAuthorize(hasPermi = "system:user:query")
@GetMapping("/authRole/{userId}")
public AjaxResult authRole(@PathVariable("userId") Long userId)
{
public AjaxResult authRole(@PathVariable("userId") Long userId) {
AjaxResult ajax = AjaxResult.success();
SysUser user = userService.selectUserById(userId);
List<SysRole> roles = roleService.selectRolesByUserId(userId);
@ -257,9 +235,10 @@ public class SysUserController extends BaseController
@PreAuthorize(hasPermi = "system:user:edit")
@Log(title = "用户管理", businessType = BusinessType.GRANT)
@PutMapping("/authRole")
public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
{
public AjaxResult insertAuthRole(Long userId, Long[] roleIds) {
userService.insertUserAuth(userId, roleIds);
return success();
}
}

View File

@ -0,0 +1,88 @@
package com.ruoyi.system.controller;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.XhpcUserPrivilege;
import com.ruoyi.system.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 用户信息
*
* @author ruoyi
*/
@RestController
@RequestMapping("/user/privilege")
public class XhpcUserPrivilegeController extends BaseController {
@Autowired
private ISysRoleService roleService;
@Autowired
private ISysMenuService menuService;
@Autowired
private IXhpcUserPrivilegeService iXhpcUserPrivilegeService;
/**
* 加载对应角色菜单列表树
*/
@PreAuthorize(hasPermi = "user:privilege:power")
@GetMapping(value = "/power")
public AjaxResult roleMenuTreeselect(@PathVariable("userId") Long userId) {
Long SecurityUserId = SecurityUtils.getUserId();
List<SysMenu> menus = menuService.selectMenuList(SecurityUserId);
//登录用户拥有的数据权限树
List<Map<String, Object>> data = iXhpcUserPrivilegeService.dataList(SecurityUserId);
//选择用户拥有的权限
List<String> dataCheckedKeys = iXhpcUserPrivilegeService.dataCheckedKeys(userId);
AjaxResult ajax = AjaxResult.success();
SysRole sysRole = roleService.getRoleByUserID(userId);
ajax.put("checkedKeys", menuService.selectMenuListByRoleId(sysRole.getRoleId()));
ajax.put("menus", menuService.buildMenuTreeSelect(menus));
ajax.put("dataCheckedKeys", dataCheckedKeys);
ajax.put("data", data);
return ajax;
}
/**
* 新增数据权限
*/
@ApiOperation("新增数据权限")
@PreAuthorize(hasPermi = "user:privilege:add")
@PostMapping("/add")
public AjaxResult add(@RequestBody List<XhpcUserPrivilege> list) {
if (null != list && list.size() > 0) {
Long userId = list.get(0).getUserId();
iXhpcUserPrivilegeService.deleteByIds(userId);
for (XhpcUserPrivilege xhpcUserPrivilege : list) {
iXhpcUserPrivilegeService.insert(xhpcUserPrivilege);
}
}
return AjaxResult.success();
}
}

View File

@ -0,0 +1,49 @@
package com.ruoyi.system.domain;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* 角色和部门关联 sys_role_dept
*
* @author ruoyi
*/
public class XhpcUserPrivilege implements Serializable
{
private static final long serialVersionUID = 1L;
/**
* 用户id
*/
@NotBlank(message = "用户id不能为空")
private Long userId;
/**
* 电站id
*/
@NotBlank(message = "电站id不能为空")
private Long chargingStationId;
public XhpcUserPrivilege() {
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getChargingStationId() {
return chargingStationId;
}
public void setChargingStationId(Long chargingStationId) {
this.chargingStationId = chargingStationId;
}
}

View File

@ -3,6 +3,7 @@ package com.ruoyi.system.mapper;
import java.util.List;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.domain.SysUserRole;
/**
* 角色表 数据层
@ -105,4 +106,13 @@ public interface SysRoleMapper
* @return 结果
*/
public int deleteRoleByIds(Long[] roleIds);
/**
* 通过用户id查询角色
*
* @param userId 用户id
* @return 结果
*/
public SysRole getRoleByUserID(Long userId);
}

View File

@ -1,26 +1,27 @@
package com.ruoyi.system.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.ruoyi.system.api.domain.SysUser;
/**
* 用户表 数据层
*
*
* @author ruoyi
*/
public interface SysUserMapper
{
public interface SysUserMapper {
/**
* 根据条件分页查询用户列表
*
*
* @param sysUser 用户信息
* @return 用户信息集合信息
*/
public List<SysUser> selectUserList(SysUser sysUser);
/**
* 根据条件分页查询未已配用户角色列表
*
*
* @param user 用户信息
* @return 用户信息集合信息
*/
@ -28,7 +29,7 @@ public interface SysUserMapper
/**
* 根据条件分页查询未分配用户角色列表
*
*
* @param user 用户信息
* @return 用户信息集合信息
*/
@ -36,7 +37,7 @@ public interface SysUserMapper
/**
* 通过用户名查询用户
*
*
* @param userName 用户名
* @return 用户对象信息
*/
@ -44,7 +45,7 @@ public interface SysUserMapper
/**
* 通过用户ID查询用户
*
*
* @param userId 用户ID
* @return 用户对象信息
*/
@ -52,7 +53,7 @@ public interface SysUserMapper
/**
* 新增用户信息
*
*
* @param user 用户信息
* @return 结果
*/
@ -60,7 +61,7 @@ public interface SysUserMapper
/**
* 修改用户信息
*
*
* @param user 用户信息
* @return 结果
*/
@ -68,16 +69,16 @@ public interface SysUserMapper
/**
* 修改用户头像
*
*
* @param userName 用户名
* @param avatar 头像地址
* @param avatar 头像地址
* @return 结果
*/
public int updateUserAvatar(@Param("userName") String userName, @Param("avatar") String avatar);
/**
* 重置用户密码
*
*
* @param userName 用户名
* @param password 密码
* @return 结果
@ -86,7 +87,7 @@ public interface SysUserMapper
/**
* 通过用户ID删除用户
*
*
* @param userId 用户ID
* @return 结果
*/
@ -94,7 +95,7 @@ public interface SysUserMapper
/**
* 批量删除用户信息
*
*
* @param userIds 需要删除的用户ID
* @return 结果
*/
@ -102,7 +103,7 @@ public interface SysUserMapper
/**
* 校验用户名称是否唯一
*
*
* @param userName 用户名称
* @return 结果
*/

View File

@ -0,0 +1,90 @@
package com.ruoyi.system.mapper;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.XhpcUserPrivilege;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* 数据权限 数据层
*
* @author ruoyi
*/
public interface XhpcUserPrivilegeMapper {
/**
* 新增数据权限
*
* @param xhpcUserPrivilege 用户信息
* @return 结果
*/
public int insert(XhpcUserPrivilege xhpcUserPrivilege);
/**
* 通过用户ID删除用户数据权限
*
* @param userId 用户ID
* @return 结果
*/
public int delete(Long userId);
/**
* 用户拥有数据权限
*
* @param userId 用户id
* @return 结果
*/
public List<String> dataCheckedKeys(@Param("userId") Long userId);
/**
* 通过运营商id查询运营商
*
* @param operatorIds 用户id
* @return 结果
*/
public List<Map<String, Object>> getOperatorId(@Param("operatorIds") String[] operatorIds);
/**
* 通过用户id查询运营商
*
* @param userId 用户id
* @return 结果
*/
public List<Map<String, Object>> getOperatorIdByUserId(@Param("userId") Long userId);
/**
* 通过运营商id分组查询省地址
*
* @param operatorId 运营商id
* @return 结果
*/
public List<Map<String, Object>> groupProvinceByOperatorId(@Param("userId") Long userId,@Param("operatorId") Long operatorId);
/**
* 通过运营商id分组查询市地址
*
* @param operatorId 运营商id
* @return 结果
*/
public List<Map<String, Object>> groupCityByOperatorId(@Param("userId") Long userId,@Param("provinceCode") String provinceCode,@Param("operatorId") Long operatorId);
/**
* 通过运营商id分组查询市地址
*
* @param operatorId 运营商id
* @return 结果
*/
public List<Map<String, Object>> groupAreaByOperatorId(@Param("userId") Long userId,@Param("cityCode") String cityCode,@Param("operatorId") Long operatorId);
/**
* 通过运营商id分组查询市地址
*
* @param operatorId 运营商id
* @return 结果
*/
public List<Map<String, Object>> dataPowerByOperatorId(@Param("userId") Long userId,@Param("code") String cityCode,@Param("operatorId") Long operatorId);
}

View File

@ -162,4 +162,12 @@ public interface ISysRoleService
* @return 结果
*/
public int insertAuthUsers(Long roleId, Long[] userIds);
/**
* 通过用户id查询角色
*
* @param userId 用户id
* @return 结果
*/
public SysRole getRoleByUserID(Long userId);
}

View File

@ -1,18 +1,18 @@
package com.ruoyi.system.service;
import java.util.List;
import com.ruoyi.system.api.domain.SysUser;
/**
* 用户 业务层
*
*
* @author ruoyi
*/
public interface ISysUserService
{
public interface ISysUserService {
/**
* 根据条件分页查询用户列表
*
*
* @param user 用户信息
* @return 用户信息集合信息
*/
@ -20,7 +20,7 @@ public interface ISysUserService
/**
* 根据条件分页查询已分配用户角色列表
*
*
* @param user 用户信息
* @return 用户信息集合信息
*/
@ -28,7 +28,7 @@ public interface ISysUserService
/**
* 根据条件分页查询未分配用户角色列表
*
*
* @param user 用户信息
* @return 用户信息集合信息
*/
@ -36,7 +36,7 @@ public interface ISysUserService
/**
* 通过用户名查询用户
*
*
* @param userName 用户名
* @return 用户对象信息
*/
@ -44,7 +44,7 @@ public interface ISysUserService
/**
* 通过用户ID查询用户
*
*
* @param userId 用户ID
* @return 用户对象信息
*/
@ -52,7 +52,7 @@ public interface ISysUserService
/**
* 根据用户ID查询用户所属角色组
*
*
* @param userName 用户名
* @return 结果
*/
@ -60,7 +60,7 @@ public interface ISysUserService
/**
* 根据用户ID查询用户所属岗位组
*
*
* @param userName 用户名
* @return 结果
*/
@ -68,7 +68,7 @@ public interface ISysUserService
/**
* 校验用户名称是否唯一
*
*
* @param userName 用户名称
* @return 结果
*/
@ -92,14 +92,14 @@ public interface ISysUserService
/**
* 校验用户是否允许操作
*
*
* @param user 用户信息
*/
public void checkUserAllowed(SysUser user);
/**
* 新增用户信息
*
*
* @param user 用户信息
* @return 结果
*/
@ -107,23 +107,23 @@ public interface ISysUserService
/**
* 修改用户信息
*
*
* @param user 用户信息
* @return 结果
*/
public int updateUser(SysUser user);
/**
* 用户授权角色
*
* @param userId 用户ID
*
* @param userId 用户ID
* @param roleIds 角色组
*/
public void insertUserAuth(Long userId, Long[] roleIds);
/**
* 修改用户状态
*
*
* @param user 用户信息
* @return 结果
*/
@ -131,7 +131,7 @@ public interface ISysUserService
/**
* 修改用户基本信息
*
*
* @param user 用户信息
* @return 结果
*/
@ -139,16 +139,16 @@ public interface ISysUserService
/**
* 修改用户头像
*
*
* @param userName 用户名
* @param avatar 头像地址
* @param avatar 头像地址
* @return 结果
*/
public boolean updateUserAvatar(String userName, String avatar);
/**
* 重置用户密码
*
*
* @param user 用户信息
* @return 结果
*/
@ -156,7 +156,7 @@ public interface ISysUserService
/**
* 重置用户密码
*
*
* @param userName 用户名
* @param password 密码
* @return 结果
@ -165,7 +165,7 @@ public interface ISysUserService
/**
* 通过用户ID删除用户
*
*
* @param userId 用户ID
* @return 结果
*/
@ -173,7 +173,7 @@ public interface ISysUserService
/**
* 批量删除用户信息
*
*
* @param userIds 需要删除的用户ID
* @return 结果
*/
@ -181,10 +181,10 @@ public interface ISysUserService
/**
* 导入用户数据
*
* @param userList 用户数据列表
*
* @param userList 用户数据列表
* @param isUpdateSupport 是否更新支持如果已存在则进行更新数据
* @param operName 操作用户
* @param operName 操作用户
* @return 结果
*/
public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName);

View File

@ -0,0 +1,47 @@
package com.ruoyi.system.service;
import com.ruoyi.system.domain.XhpcUserPrivilege;
import java.util.List;
import java.util.Map;
/**
* 数据权限 服务层
*
* @author ruoyi
*/
public interface IXhpcUserPrivilegeService {
/**
* 新增数据权限
*
* @param xhpcUserPrivilege 数据权限信息
* @return 结果
*/
public int insert(XhpcUserPrivilege xhpcUserPrivilege);
/**
* 批量删数据权限信息
*
* @param userId 需要删除的数据ID
* @return 结果
*/
public void deleteByIds(Long userId);
/**
* 用户拥有数据权限
*
* @param userId 用户id
* @return 结果
*/
public List<String> dataCheckedKeys(Long userId);
/**
* 数据权限构建前端所需要下拉树结构
*
* @param userId 用户id
* @return 结果
*/
public List<Map<String, Object>> dataList(Long userId);
}

View File

@ -398,4 +398,16 @@ public class SysRoleServiceImpl implements ISysRoleService
}
return userRoleMapper.batchUserRole(list);
}
/**
* 通过用户id查询角色
*
* @param userId 用户id
* @return 结果
*/
@Override
public SysRole getRoleByUserID(Long userId)
{
return roleMapper.getRoleByUserID(userId);
}
}

View File

@ -525,5 +525,4 @@ public class SysUserServiceImpl implements ISysUserService
}
return successMsg.toString();
}
}

View File

@ -0,0 +1,150 @@
package com.ruoyi.system.service.impl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.XhpcUserPrivilege;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.XhpcUserPrivilegeMapper;
import com.ruoyi.system.service.IXhpcUserPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 数据权限 服务层
*
* @author ruoyi
*/
@Service
public class XhpcUserPrivilegeServiceImpl implements IXhpcUserPrivilegeService {
@Autowired
private XhpcUserPrivilegeMapper XhpcUserPrivilege;
@Autowired
private SysUserMapper sysUserMapper;
/**
* 新增数据权限
*
* @param xhpcUserPrivilege 流量用户设置流量方黑名单信息
*/
@Override
public int insert(XhpcUserPrivilege xhpcUserPrivilege) {
return XhpcUserPrivilege.insert(xhpcUserPrivilege);
}
/**
* 通过用户ID删除用户数据权限
*
* @param userId 需要删除的数据ID
* @return 结果
*/
@Override
public void deleteByIds(Long userId) {
XhpcUserPrivilege.delete(userId);
}
/**
* 用户拥有数据权限
*
* @param userId 用户id
* @return 结果
*/
@Override
public List<String> dataCheckedKeys(Long userId) {
return XhpcUserPrivilege.dataCheckedKeys(userId);
}
/**
* 用户拥有数据权限
*
* @param userId 用户id
* @return 结果
*/
@Override
public List<Map<String, Object>> dataList(Long userId) {
SysUser sysUser = sysUserMapper.selectUserById(userId);
List<Map<String, Object>> listOperator = new ArrayList<>();
if (0 == sysUser.getDataPowerType()) {
listOperator = XhpcUserPrivilege.getOperatorId(null);
} else if (1 == sysUser.getDataPowerType()) {
String[] strings = new String[1];
strings[0] = StringUtils.valueOf(sysUser.getOperatorId());
listOperator = XhpcUserPrivilege.getOperatorId(strings);
} else {
listOperator = XhpcUserPrivilege.getOperatorIdByUserId(userId);
}
if (2 != sysUser.getDataPowerType()) {
return allDataPower(listOperator);
} else {
return dataPower(listOperator, userId);
}
}
public List<Map<String, Object>> allDataPower(List<Map<String, Object>> listOperator) {
if (null != listOperator && listOperator.size() > 0) {
for (Map<String, Object> map : listOperator) {
String operatorId = StringUtils.valueOf(map.get("operatorId"));
List<Map<String, Object>> groupProvince = XhpcUserPrivilege.groupProvinceByOperatorId(null, Long.parseLong(operatorId));
if (null != groupProvince && groupProvince.size() > 0) {
for (Map<String, Object> province : groupProvince) {
String provinceCode = StringUtils.valueOf(province.get("code"));
List<Map<String, Object>> groupCity = XhpcUserPrivilege.groupCityByOperatorId(null, provinceCode, Long.parseLong(operatorId));
if (null != groupCity && groupCity.size() > 0) {
for (Map<String, Object> city : groupCity) {
String cityCode = StringUtils.valueOf(city.get("code"));
List<Map<String, Object>> groupArea = XhpcUserPrivilege.groupAreaByOperatorId(null, cityCode, Long.parseLong(operatorId));
if (null != groupArea && groupArea.size() > 0) {
for (Map<String, Object> area : groupArea) {
String code = StringUtils.valueOf(area.get("code"));
area.put("dataPower", XhpcUserPrivilege.dataPowerByOperatorId(null, code, Long.parseLong(operatorId)));
}
}
city.put("areaList", groupArea);
}
}
province.put("cityList", groupCity);
}
}
map.put("provinceList", groupProvince);
}
}
return listOperator;
}
public List<Map<String, Object>> dataPower(List<Map<String, Object>> listOperator, Long userId) {
if (null != listOperator && listOperator.size() > 0) {
for (Map<String, Object> map : listOperator) {
String operatorId = StringUtils.valueOf(map.get("operatorId"));
List<Map<String, Object>> groupProvince = XhpcUserPrivilege.groupProvinceByOperatorId(userId, Long.parseLong(operatorId));
if (null != groupProvince && groupProvince.size() > 0) {
for (Map<String, Object> province : groupProvince) {
String provinceCode = StringUtils.valueOf(province.get("code"));
List<Map<String, Object>> groupCity = XhpcUserPrivilege.groupCityByOperatorId(userId, provinceCode, Long.parseLong(operatorId));
if (null != groupCity && groupCity.size() > 0) {
for (Map<String, Object> city : groupCity) {
String cityCode = StringUtils.valueOf(city.get("code"));
List<Map<String, Object>> groupArea = XhpcUserPrivilege.groupAreaByOperatorId(userId, cityCode, Long.parseLong(operatorId));
if (null != groupArea && groupArea.size() > 0) {
for (Map<String, Object> area : groupArea) {
String code = StringUtils.valueOf(area.get("code"));
area.put("dataPower", XhpcUserPrivilege.dataPowerByOperatorId(userId, code, Long.parseLong(operatorId)));
}
}
city.put("areaList", groupArea);
}
}
province.put("cityList", groupCity);
}
}
map.put("provinceList", groupProvince);
}
}
return listOperator;
}
}

View File

@ -145,5 +145,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{roleId}
</foreach>
</delete>
<select id="getRoleByUserID" parameterType="Long" resultMap="SysRoleResult">
<include refid="selectRoleVo"/>
where r.role_key=#{roleKey} ORDER BY r.update_time DESC limit 1
</select>
</mapper>

View File

@ -229,5 +229,4 @@
#{userId}
</foreach>
</delete>
</mapper>

View File

@ -0,0 +1,151 @@
<?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.ruoyi.system.mapper.XhpcUserPrivilegeMapper">
<resultMap type="XhpcUserPrivilege" id="XhpcUserPrivilegeResult">
<result column="user_id" property="userId"/>
<result column="charging_station_id" property="chargingStationId"/>
</resultMap>
<insert id="insert" parameterType="XhpcUserPrivilege">
insert into xhpc_user_privilege(
<if test="null != userId and '' != userId">
user_id,
</if>
<if test="null != chargingStationId and '' != chargingStationId">
charging_station_id
</if>
)values(
<if test="null != userId and '' != userId">
#{userId},
</if>
<if test="null != chargingStationId and '' != chargingStationId">
#{chargingStationId}
</if>
)
</insert>
<delete id="delete" parameterType="Long">
delete from xhpc_station_internet_blacklist where user_id = #{userId}
</delete>
<select id="dataCheckedKeys" parameterType="String" resultType="java.lang.String">
select charging_station_id chargingStationId from xhpc_user_privilege where user_id = #{userId}
</select>
<select id="getOperatorId" parameterType="String" resultType="java.util.Map">
select xo.operator_id operatorId, xo.name, xo.contact_name contactName,
xo.contact_phone contactPhone, xo.phone, xo.attribute,
xdb.dict_value attributenName
from xhpc_operator `xo`
LEFT JOIN xhpc_dict_biz xdb on xdb.`code` = 'operator_attribute' and xdb.dict_key = xo.attribute
where xo.del_flag = 0
<if test="null != operatorIds and '' != operatorIds">
operator_id in
<foreach collection="operatorIds" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
ORDER BY xo.create_time DESC
</select>
<select id="getOperatorIdByUserId" parameterType="String" resultType="java.util.Map">
select xo.operator_id operatorId, xo.name, xo.contact_name contactName,
xo.contact_phone contactPhone, xo.phone, xo.attribute,
xdb.dict_value attributenName
from xhpc_operator `xo`
LEFT JOIN xhpc_dict_biz xdb on xdb.`code` = 'operator_attribute' and xdb.dict_key = xo.attribute
where xo.del_flag = 0
<if test="null != operatorIds and '' != operatorIds">
and operator_id in
(select operator_id
from xhpc_charging_station
where charging_station_id in (
select charging_station_id from xhpc_user_privilege where user_id = #{userId})
GROUP BY operator_id)
</if>
ORDER BY xo.create_time DESC
</select>
<select id="groupProvinceByOperatorId" parameterType="String" resultType="java.util.Map">
select `code` ,`name` from xhpc_area where `code` in (
select `pxa`.pcode province
from xhpc_charging_station xcs
LEFT JOIN xhpc_area `xa` on `xa`.`code` = xcs.area_code
LEFT JOIN xhpc_area pxa on `xa`.pcode = pxa.`code`
where xcs.del_flag = 0
<if test="null != userId and '' != userId">
and xcs.charging_station_id in (
select charging_station_id from xhpc_user_privilege where user_id = #{userId})
</if>
<if test="null != operatorId and '' != operatorId">
and xcs.operator_id = #{operatorId}
</if>
GROUP BY `pxa`.pcode
)
</select>
<select id="groupCityByOperatorId" parameterType="String" resultType="java.util.Map">
select `code` ,`name` from xhpc_area where `code` in (
select `pxa`.code city
from xhpc_charging_station xcs
LEFT JOIN xhpc_area `xa` on `xa`.`code` = xcs.area_code
LEFT JOIN xhpc_area pxa on `xa`.pcode = pxa.`code`
where xcs.del_flag = 0
<if test="null != userId and '' != userId">
and xcs.charging_station_id in (
select charging_station_id from xhpc_user_privilege where user_id = #{userId})
</if>
<if test="null != operatorId and '' != operatorId">
and xcs.operator_id = #{operatorId}
</if>
<if test="null != provinceCode and '' != provinceCode">
and pxa.pcode = #{provinceCode}
</if>
GROUP BY `pxa`.code
)
</select>
<select id="groupAreaByOperatorId" parameterType="String" resultType="java.util.Map">
select `code` ,`name` from xhpc_area where `code` in (
select `xa`.code
from xhpc_charging_station xcs
LEFT JOIN xhpc_area `xa` on `xa`.`code` = xcs.area_code
LEFT JOIN xhpc_area pxa on `xa`.pcode = pxa.`code`
where xcs.del_flag = 0
<if test="null != userId and '' != userId">
and xcs.charging_station_id in (
select charging_station_id from xhpc_user_privilege where user_id = #{userId})
</if>
<if test="null != operatorId and '' != operatorId">
and xcs.operator_id = #{operatorId}
</if>
<if test="null != cityCode and '' != cityCode">
and pxa.code = #{cityCode}
</if>
GROUP BY `xa`.code
)
</select>
<select id="dataPowerByOperatorId" parameterType="String" resultType="java.util.Map">
select xcs.charging_station_id ,xcs.name
from xhpc_charging_station xcs
LEFT JOIN xhpc_area `xa` on `xa`.`code` = xcs.area_code
LEFT JOIN xhpc_area pxa on `xa`.pcode = pxa.`code`
where xcs.del_flag = 0
<if test="null != userId and '' != userId">
and xcs.charging_station_id in (
select charging_station_id from xhpc_user_privilege where user_id = #{userId})
</if>
<if test="null != operatorId and '' != operatorId">
and xcs.operator_id = #{operatorId}
</if>
<if test="null != code and '' != code">
and `xa`.`code` = #{code}
</if>
</select>
</mapper>

View File

@ -161,4 +161,6 @@ public class XhpcUserController extends BaseController {
iXhpcUserService.status(userId);
return AjaxResult.success();
}
}