|
@@ -1,80 +1,334 @@
|
|
|
package com.nokia.service;
|
|
|
|
|
|
-import com.nokia.vo.UserVo;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.nokia.common.R;
|
|
|
import com.nokia.dao.UserDao;
|
|
|
+import com.nokia.dao.UserFunctionDao;
|
|
|
import com.nokia.dao.UserRoleCityDao;
|
|
|
-import com.nokia.pojo.Area;
|
|
|
-import com.nokia.pojo.Role;
|
|
|
import com.nokia.pojo.User;
|
|
|
+import com.nokia.pojo.UserFunction;
|
|
|
import com.nokia.pojo.UserRoleCity;
|
|
|
+import com.nokia.vo.*;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
-public class UserService extends ServiceImpl<UserDao, User> {
|
|
|
+public class UserService {
|
|
|
|
|
|
private final UserDao userDao;
|
|
|
private final UserRoleCityDao userRoleCityDao;
|
|
|
+ private final UserFunctionDao userFunctionDao;
|
|
|
+ private final OperationLogService operationLogService;
|
|
|
|
|
|
- @Autowired
|
|
|
- public UserService(UserDao userDao, UserRoleCityDao userRoleCityDao) {
|
|
|
+ public UserService(UserDao userDao, UserRoleCityDao userRoleCityDao, UserFunctionDao userFunctionDao, OperationLogService operationLogService) {
|
|
|
this.userDao = userDao;
|
|
|
this.userRoleCityDao = userRoleCityDao;
|
|
|
+ this.userFunctionDao = userFunctionDao;
|
|
|
+ this.operationLogService = operationLogService;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 指定的登陆名是否已存在
|
|
|
- */
|
|
|
- public boolean hasLoginName(String loginName) {
|
|
|
- return null != userDao.hasLoginName(loginName);
|
|
|
+ public List<UserVo> baseList() {
|
|
|
+ return userDao.baseList();
|
|
|
+ }
|
|
|
+
|
|
|
+ public R<TopUserVo> getTopUserByLoginName(TopUserDto dto, HttpSession session) {
|
|
|
+ if (hasLoginName(dto.getLoginName())) {
|
|
|
+ return R.error("用户已存在系统中");
|
|
|
+ }
|
|
|
+ TopUserVo vo = userDao.getTopUserByLoginName(dto.getLoginName());
|
|
|
+ if (vo == null) {
|
|
|
+ return R.error("没有查询到top用户信息");
|
|
|
+ }
|
|
|
+ UserRoleCity role = getUserRoleCity(session);
|
|
|
+ if (role.getCityId() != -1 && !role.getCityId().equals(vo.getCityId())) {
|
|
|
+ return R.error("非本地市用户");
|
|
|
+ }
|
|
|
+ return R.ok(vo);
|
|
|
+ }
|
|
|
+
|
|
|
+ public R<List<GetRoleByLoginNameVo>> getRoleByLoginName(GetRoleByLoginNameDto dto, HttpSession session) {
|
|
|
+ User user = userDao.selectByLoginName(dto.getLoginName());
|
|
|
+ if (user == null || user.getDeleted().equals(1)) {
|
|
|
+ return R.error("账号不存在");
|
|
|
+ }
|
|
|
+ UserRoleCity role = getUserRoleCity(session);
|
|
|
+ if (role.getCityId() != -1 && !role.getCityId().equals(user.getCityId())) {
|
|
|
+ return R.error("非本地市用户");
|
|
|
+ }
|
|
|
+ List<GetRoleByLoginNameVo> vo = userDao.getRoleByLoginName(dto.getLoginName());
|
|
|
+ return R.ok(vo);
|
|
|
+ }
|
|
|
+
|
|
|
+ public R<PageVo<ListUserVo>> list(ListUserDto dto, HttpSession session) {
|
|
|
+ // 默认归属地市
|
|
|
+ if (dto.getAreaId() == null) {
|
|
|
+ User userinfo = getUserInfo(session);
|
|
|
+ dto.setAreaId(userinfo.getCityId());
|
|
|
+ }
|
|
|
+ PageVo<ListUserVo> vo = new PageVo<>();
|
|
|
+ Page<ListUserVo> page = new Page<>(dto.getCurrent(), dto.getPageSize());
|
|
|
+ page.addOrder(OrderItem.asc("au.city_id"));
|
|
|
+ List<ListUserVo> list = userDao.list(page, dto);
|
|
|
+ vo.setList(list);
|
|
|
+ vo.setTotal(page.getTotal());
|
|
|
+ return R.ok(vo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R<Object> add(AddUserDto dto, HttpSession session) {
|
|
|
+ User user = userDao.selectByLoginName(dto.getLoginName());
|
|
|
+ if (user != null && user.getDeleted().equals(0)) {
|
|
|
+ return R.error("用户已存在系统中");
|
|
|
+ }
|
|
|
+ TopUserVo topUser = userDao.getTopUserByLoginName(dto.getLoginName());
|
|
|
+ if (topUser == null) {
|
|
|
+ return R.error("没有查询到top用户信息");
|
|
|
+ }
|
|
|
+ if (topUser.getCityId() == null) {
|
|
|
+ topUser.setCityId(topUser.getProvinceId());
|
|
|
+ }
|
|
|
+ UserRoleCity role = getUserRoleCity(session);
|
|
|
+ if (role.getCityId() != -1 && !role.getCityId().equals(topUser.getCityId())) {
|
|
|
+ return R.error("无法添加非本地市用户");
|
|
|
+ }
|
|
|
+ if (user == null) {
|
|
|
+ user = new User();
|
|
|
+ }
|
|
|
+ user.setLoginName(topUser.getLoginName());
|
|
|
+ user.setUserName(topUser.getUserName());
|
|
|
+ user.setPhone(topUser.getPhone());
|
|
|
+ user.setEmail(dto.getEmail());
|
|
|
+ user.setOrg(topUser.getOrgName());
|
|
|
+ user.setDeleted(0);
|
|
|
+ user.setProvinceId(topUser.getProvinceId());
|
|
|
+ user.setCityId(topUser.getCityId());
|
|
|
+ user.setAreaId(topUser.getAreaId());
|
|
|
+ // 插入新用户
|
|
|
+ if (user.getUserId() == null) {
|
|
|
+ userDao.insert(user);
|
|
|
+ } else {
|
|
|
+ // 修改已删除用户信息
|
|
|
+ userDao.updateById(user);
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("添加用户 ").append(topUser.getLoginName()).append(";")
|
|
|
+ .append("用户名:").append(topUser.getUserName()).append(",")
|
|
|
+ .append("手机号:").append(topUser.getPhone()).append(",")
|
|
|
+ .append("邮箱:").append(dto.getEmail()).append(",")
|
|
|
+ .append("部门:").append(topUser.getOrgName()).append(",")
|
|
|
+ .append("省份:").append(topUser.getProvinceName()).append(",")
|
|
|
+ .append("地市:").append(topUser.getCityName()).append(",")
|
|
|
+ .append("区县:").append(topUser.getAreaName()).append(";");
|
|
|
+ if (!CollectionUtils.isEmpty(dto.getRoles())) {
|
|
|
+ int userId = user.getUserId();
|
|
|
+ List<UserRoleCity> userRoleCities = new ArrayList<>();
|
|
|
+ Set<Integer> functionIds = new HashSet<>();
|
|
|
+ sb.append("角色:");
|
|
|
+ for (AddUserRoleDto t : dto.getRoles()) {
|
|
|
+ sb.append(t.getRoleName()).append("-").append(t.getCityName()).append(",");
|
|
|
+ UserRoleCity userRoleCity = new UserRoleCity();
|
|
|
+ userRoleCity.setUserId(userId);
|
|
|
+ userRoleCity.setRoleId(t.getRoleId());
|
|
|
+ userRoleCity.setCityId(t.getCityId());
|
|
|
+ userRoleCities.add(userRoleCity);
|
|
|
+ if (t.getFunctionId() != null) {
|
|
|
+ functionIds.add(t.getFunctionId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userRoleCityDao.insertBatch(userRoleCities);
|
|
|
+ if (!CollectionUtils.isEmpty(functionIds)) {
|
|
|
+ List<UserFunction> userFunctions = functionIds.stream().map(t -> new UserFunction(userId, t))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ userFunctionDao.insertBatch(userFunctions);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 记录日志
|
|
|
+ operationLogService.logOperation(session, "添加用户", sb.toString());
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R<Object> update(UpdateUserDto dto, HttpSession session) {
|
|
|
+ User user = userDao.selectById(dto.getUserId());
|
|
|
+ UserRoleCity role = getUserRoleCity(session);
|
|
|
+ if (role.getCityId() != -1 && !role.getCityId().equals(user.getCityId())) {
|
|
|
+ return R.error("无法修改非本地市用户");
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("修改用户 ").append(user.getLoginName()).append(";");
|
|
|
+ if (StringUtils.hasText(dto.getEmail()) && !dto.getEmail().equals(user.getEmail())) {
|
|
|
+ sb.append("邮箱旧值:").append(user.getEmail()).append(",")
|
|
|
+ .append("邮箱新值:").append(dto.getEmail()).append(";");
|
|
|
+ user.setEmail(dto.getEmail());
|
|
|
+ userDao.updateById(user);
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(dto.getRoles())) {
|
|
|
+ int userId = user.getUserId();
|
|
|
+ List<GetUserDetailRoleVo> oldRoles = userDao.getUserDetailRole(userId);
|
|
|
+ sb.append("角色旧值:");
|
|
|
+ for (GetUserDetailRoleVo t : oldRoles) {
|
|
|
+ sb.append(t.getRoleName()).append("-").append(t.getCityName()).append(",");
|
|
|
+ }
|
|
|
+ List<UserRoleCity> userRoleCities = new ArrayList<>();
|
|
|
+ Set<Integer> functionIds = new HashSet<>();
|
|
|
+ sb.append(";角色新值:");
|
|
|
+ for (AddUserRoleDto t : dto.getRoles()) {
|
|
|
+ sb.append(t.getRoleName()).append("-").append(t.getCityName()).append(",");
|
|
|
+ UserRoleCity userRoleCity = new UserRoleCity();
|
|
|
+ userRoleCity.setUserId(user.getUserId());
|
|
|
+ userRoleCity.setRoleId(t.getRoleId());
|
|
|
+ userRoleCity.setCityId(t.getCityId());
|
|
|
+ userRoleCities.add(userRoleCity);
|
|
|
+ if (t.getFunctionId() != null) {
|
|
|
+ functionIds.add(t.getFunctionId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ UpdateWrapper<UserRoleCity> userRoleCityUpdateWrapper = new UpdateWrapper<>();
|
|
|
+ userRoleCityUpdateWrapper.eq("user_id", dto.getUserId());
|
|
|
+ userRoleCityDao.delete(userRoleCityUpdateWrapper);
|
|
|
+ userRoleCityDao.insertBatch(userRoleCities);
|
|
|
+ if (!CollectionUtils.isEmpty(functionIds)) {
|
|
|
+ UpdateWrapper<UserFunction> userFunctionUpdateWrapper = new UpdateWrapper<>();
|
|
|
+ userFunctionUpdateWrapper.eq("user_id", dto.getUserId());
|
|
|
+ userFunctionDao.delete(userFunctionUpdateWrapper);
|
|
|
+ List<UserFunction> userFunctions = functionIds.stream().map(t -> new UserFunction(userId, t))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ userFunctionDao.insertBatch(userFunctions);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 记录日志
|
|
|
+ operationLogService.logOperation(session, "修改用户", sb.toString());
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R<Object> del(DeleteUserDto dto, HttpSession session) {
|
|
|
+ UserRoleCity role = getUserRoleCity(session);
|
|
|
+ if (role.getCityId() != -1 && userDao.exceptCityUser(role.getCityId(), dto.getUserIds()) != null) {
|
|
|
+ return R.error("无法删除非本地市用户");
|
|
|
+ }
|
|
|
+ User user = new User();
|
|
|
+ user.setDeleted(1);
|
|
|
+ UpdateWrapper<User> wrapper = new UpdateWrapper<>();
|
|
|
+ wrapper.in("user_id", dto.getUserIds());
|
|
|
+ userDao.update(user, wrapper);
|
|
|
+ deleteUserRoleCityByUserIds(dto.getUserIds());
|
|
|
+ deleteUserFunctionByUserIds(dto.getUserIds());
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ List<User> users = userDao.selectBatchIds(dto.getUserIds());
|
|
|
+ String loginNames = users.stream().map(User::getLoginName).collect(Collectors.joining("、"));
|
|
|
+ sb.append("删除用户 ").append(loginNames);
|
|
|
+ // 记录日志
|
|
|
+ operationLogService.logOperation(session, "删除用户", sb.toString());
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ public R<GetUserDetailVo> detail(GetUserDetailDto dto) {
|
|
|
+ GetUserDetailVo vo = userDao.getUserDetail(dto.getUserId());
|
|
|
+ List<GetUserDetailRoleVo> roles = userDao.getUserDetailRole(dto.getUserId());
|
|
|
+ vo.setRoles(roles);
|
|
|
+ return R.ok(vo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R<Object> copyRole(CopyRolesDto dto, HttpSession session) {
|
|
|
+ UserRoleCity role = getUserRoleCity(session);
|
|
|
+ if (role.getCityId() != -1 && userDao.exceptCityUser(role.getCityId(), dto.getUserIds()) != null) {
|
|
|
+ return R.error("无法修改非本地市用户");
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ List<User> users = userDao.selectBatchIds(dto.getUserIds());
|
|
|
+ String loginNames = users.stream().map(User::getLoginName).collect(Collectors.joining("、"));
|
|
|
+ sb.append("批量复制权限给 ").append(loginNames).append(";权限:");
|
|
|
+ List<UserRoleCity> userRoleCities = new ArrayList<>();
|
|
|
+ List<UserFunction> userFunctions = new ArrayList<>();
|
|
|
+ for (AddUserRoleDto t : dto.getRoles()) {
|
|
|
+ sb.append(t.getRoleName()).append("-").append(t.getCityName()).append(",");
|
|
|
+ for (Integer userId : dto.getUserIds()) {
|
|
|
+ UserRoleCity userRoleCity = new UserRoleCity();
|
|
|
+ userRoleCity.setUserId(userId);
|
|
|
+ userRoleCity.setRoleId(t.getRoleId());
|
|
|
+ userRoleCity.setCityId(t.getCityId());
|
|
|
+ userRoleCities.add(userRoleCity);
|
|
|
+ if (t.getFunctionId() != null) {
|
|
|
+ UserFunction userFunction = new UserFunction();
|
|
|
+ userFunction.setUserId(userId);
|
|
|
+ userFunction.setFunctionId(t.getFunctionId());
|
|
|
+ userFunctions.add(userFunction);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ deleteUserRoleCityByUserIds(dto.getUserIds());
|
|
|
+ userRoleCityDao.insertBatch(userRoleCities);
|
|
|
+ if (!CollectionUtils.isEmpty(userFunctions)) {
|
|
|
+ deleteUserFunctionByUserIds(dto.getUserIds());
|
|
|
+ userFunctionDao.insertBatch(userFunctions);
|
|
|
+ }
|
|
|
+ // 记录日志
|
|
|
+ operationLogService.logOperation(session, "批量复制权限", sb.toString());
|
|
|
+ return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 用户是否存在某个权限
|
|
|
+ * 获取用户信息
|
|
|
+ *
|
|
|
+ * @param session 会话
|
|
|
+ * @return {@link User}
|
|
|
*/
|
|
|
- public boolean hasRoleCity(User user, Role role, Area city) {
|
|
|
- return null != userRoleCityDao.hasRoleCity(user.getUserId(), role.getRoleId(), city.getAreaId());
|
|
|
+ public User getUserInfo(HttpSession session) {
|
|
|
+ return (User) session.getAttribute("userinfo");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 添加用户
|
|
|
+ * 得到用户角色城市
|
|
|
+ *
|
|
|
+ * @param session 会话
|
|
|
+ * @return {@link UserRoleCity}
|
|
|
*/
|
|
|
- @Transactional
|
|
|
- public String add(User user) {
|
|
|
- User query = new User();
|
|
|
- query.setLoginName(user.getLoginName());
|
|
|
- User one = baseMapper.selectOne(Wrappers.query(query));
|
|
|
- if (one != null) {
|
|
|
- return "loginName: " + user.getLoginName() + " 已存在";
|
|
|
- } else {
|
|
|
- user.setCityId(user.getCity().getAreaId());
|
|
|
- baseMapper.insert(user);
|
|
|
- }
|
|
|
- return "ok";
|
|
|
+ public UserRoleCity getUserRoleCity(HttpSession session) {
|
|
|
+ return (UserRoleCity) session.getAttribute("role");
|
|
|
}
|
|
|
|
|
|
- /*
|
|
|
- * 添加权限
|
|
|
+ /**
|
|
|
+ * 登录名存在
|
|
|
+ *
|
|
|
+ * @param loginName 登录名
|
|
|
+ * @return boolean
|
|
|
*/
|
|
|
- public void addUserRoleCity(UserRoleCity userRoleCity) {
|
|
|
- userRoleCityDao.insert(userRoleCity);
|
|
|
+ private boolean hasLoginName(String loginName) {
|
|
|
+ return userDao.hasLoginName(loginName) != null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 通过用户的登录名获取用户完整信息
|
|
|
+ * 通过用户id删除用户功能
|
|
|
+ *
|
|
|
+ * @param list 列表
|
|
|
*/
|
|
|
- public User getByLoginName(String loginName) {
|
|
|
- return userDao.getByLoginName(loginName);
|
|
|
+ private void deleteUserFunctionByUserIds(List<Integer> list) {
|
|
|
+ UpdateWrapper<UserFunction> wrapper = new UpdateWrapper<>();
|
|
|
+ wrapper.in("user_id", list);
|
|
|
+ userFunctionDao.delete(wrapper);
|
|
|
}
|
|
|
|
|
|
- public List<UserVo> baseList() {
|
|
|
- return userDao.baseList();
|
|
|
+ /**
|
|
|
+ * 通过用户id删除用户角色城市
|
|
|
+ *
|
|
|
+ * @param list 列表
|
|
|
+ */
|
|
|
+ private void deleteUserRoleCityByUserIds(List<Integer> list) {
|
|
|
+ UpdateWrapper<UserRoleCity> wrapper = new UpdateWrapper<>();
|
|
|
+ wrapper.in("user_id", list);
|
|
|
+ userRoleCityDao.delete(wrapper);
|
|
|
}
|
|
|
-
|
|
|
}
|