|
@@ -5,19 +5,24 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.qmth.boot.api.exception.ApiException;
|
|
|
import com.qmth.distributed.print.business.bean.dto.BlurryUserDto;
|
|
|
import com.qmth.distributed.print.business.bean.dto.LoginDto;
|
|
|
import com.qmth.distributed.print.business.bean.dto.UserDto;
|
|
|
+import com.qmth.distributed.print.business.bean.params.UserSaveParams;
|
|
|
import com.qmth.distributed.print.business.entity.*;
|
|
|
+import com.qmth.distributed.print.business.enums.FieldUniqueEnum;
|
|
|
import com.qmth.distributed.print.business.enums.RoleTypeEnum;
|
|
|
import com.qmth.distributed.print.business.mapper.SysUserMapper;
|
|
|
import com.qmth.distributed.print.business.service.*;
|
|
|
import com.qmth.distributed.print.business.util.ServletUtil;
|
|
|
-import com.qmth.distributed.print.business.util.security.Md5Utils;
|
|
|
import com.qmth.distributed.print.common.contant.SystemConstant;
|
|
|
import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
|
|
|
+import com.qmth.distributed.print.common.util.ResultUtil;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import sun.misc.BASE64Encoder;
|
|
@@ -55,15 +60,15 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|
|
@Autowired
|
|
|
private SysRolePrivilegeService sysRolePrivilegeService;
|
|
|
|
|
|
- @Resource
|
|
|
- SysUserMapper sysUserMapper;
|
|
|
-
|
|
|
@Autowired
|
|
|
CacheService cacheService;
|
|
|
|
|
|
@Autowired
|
|
|
private CommonService commonService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ SysUserService sysUserService;
|
|
|
+
|
|
|
@Override
|
|
|
public IPage<UserDto> list(String realName, String roleId, Boolean enable, Integer pageNumber, Integer pageSize) {
|
|
|
Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
|
|
@@ -87,78 +92,144 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|
|
|
|
|
@Transactional
|
|
|
@Override
|
|
|
- public boolean saveUser(SysUser user) {
|
|
|
- Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
|
|
|
- user.setSchoolId(schoolId);
|
|
|
- SysUser sysUser1 = (SysUser) ServletUtil.getRequestUser();
|
|
|
-
|
|
|
- QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.lambda().eq(SysUser::getLoginName, user.getLoginName());
|
|
|
- SysUser sysUser = this.getOne(queryWrapper);
|
|
|
- boolean isSuccess;
|
|
|
- // 新增
|
|
|
- if (Objects.isNull(user.getId())) {
|
|
|
- if (sysUser != null) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("用户名已存在");
|
|
|
+ public boolean saveUser(UserSaveParams userSaveParams) {
|
|
|
+// Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
|
|
|
+// user.setSchoolId(schoolId);
|
|
|
+// SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
|
|
|
+//
|
|
|
+// QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
|
|
|
+// queryWrapper.lambda().eq(SysUser::getLoginName, user.getLoginName());
|
|
|
+// SysUser sysUser = this.getOne(queryWrapper);
|
|
|
+// boolean isSuccess;
|
|
|
+// // 新增
|
|
|
+// if (Objects.isNull(user.getId())) {
|
|
|
+// if (sysUser != null) {
|
|
|
+// throw ExceptionResultEnum.ERROR.exception("用户名已存在");
|
|
|
+// }
|
|
|
+// SysConfig sysConfig = sysConfigService.getByKey("sys.user.initPassword");
|
|
|
+// BASE64Encoder encoder = new BASE64Encoder();
|
|
|
+// user.setPassword(encoder.encode(StringUtils.isNoneBlank(sysConfig.getConfigValue()) ? sysConfig.getConfigValue().getBytes() : "123456".getBytes()));
|
|
|
+// user.setId(SystemConstant.getDbUuid());
|
|
|
+// user.setCreateId(sysUser1.getId());
|
|
|
+// user.setCreateTime(System.currentTimeMillis());
|
|
|
+// isSuccess = this.save(user);
|
|
|
+// }
|
|
|
+// // 修改
|
|
|
+// else {
|
|
|
+// if (sysUser != null && user.getId().longValue() != sysUser.getId().longValue()) {
|
|
|
+// throw ExceptionResultEnum.ERROR.exception("用户名已存在");
|
|
|
+// }
|
|
|
+// sysUser.setRealName(user.getRealName());
|
|
|
+// sysUser.setMobileNumber(user.getMobileNumber());
|
|
|
+// sysUser.setOrgId(user.getOrgId());
|
|
|
+// sysUser.setUpdateId(sysUser1.getId());
|
|
|
+// sysUser.setUpdateTime(System.currentTimeMillis());
|
|
|
+// isSuccess = this.updateById(sysUser);
|
|
|
+//
|
|
|
+// //删除角色
|
|
|
+// UpdateWrapper<SysUserRole> updateWrapper = new UpdateWrapper<>();
|
|
|
+// updateWrapper.lambda().eq(SysUserRole::getUserId, sysUser.getId());
|
|
|
+//
|
|
|
+// //删除课程
|
|
|
+// basicUserCourseService.removeByUserId(sysUser.getId());
|
|
|
+//
|
|
|
+// sysUserRoleService.remove(updateWrapper);
|
|
|
+// }
|
|
|
+//
|
|
|
+// //新增用户-角色
|
|
|
+// for (Long roleId : user.getRoleIds()) {
|
|
|
+// List<SysRolePrivilege> rolePrivileges = sysRolePrivilegeService.listByRoleId(roleId);
|
|
|
+// for (SysRolePrivilege rolePrivilege : rolePrivileges) {
|
|
|
+// SysUserRole userRole = new SysUserRole();
|
|
|
+// userRole.setUserId(user.getId());
|
|
|
+// userRole.setRoleId(roleId);
|
|
|
+// userRole.setPrivilegeId(rolePrivilege.getPrivilegeId());
|
|
|
+// sysUserRoleService.save(userRole);
|
|
|
+// }
|
|
|
+//// cacheService.removeRolePrivilegeCache(roleId);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 角色里是否有考务老师角色
|
|
|
+// List<SysRole> sysRoles = sysRoleService.list(user.getRoleIds(), RoleTypeEnum.QUESTION_TEACHER.name());
|
|
|
+// if (sysRoles != null && sysRoles.size() > 0) {
|
|
|
+// Long[] courseIds = user.getCourseIds();
|
|
|
+// if (courseIds.length == 0) {
|
|
|
+// throw ExceptionResultEnum.ERROR.exception("请选择课程");
|
|
|
+// }
|
|
|
+//
|
|
|
+// basicUserCourseService.saveBatch(user);
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 清除缓存
|
|
|
+//// cacheService.removeUserAuthCache(user.getId());
|
|
|
+ boolean isSuccess = true;
|
|
|
+ try {
|
|
|
+ Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
|
|
|
+ SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
|
|
|
+ Gson gson = new Gson();
|
|
|
+ userSaveParams.setSchoolId(schoolId);
|
|
|
+ SysUser sysUser = gson.fromJson(gson.toJson(userSaveParams), SysUser.class);
|
|
|
+ if (Objects.isNull(sysUser.getId())) {//新增用户
|
|
|
+ sysUser.setInsertInfo(requestUser.getId());
|
|
|
+ sysUserService.save(sysUser);
|
|
|
+ for (Long roleId : userSaveParams.getRoleIds()) {
|
|
|
+ commonService.addUserRolePrivilege(sysUser, roleId);
|
|
|
+ }
|
|
|
+ } else {//修改用户
|
|
|
+ List<SysUserRole> sysUserRoleList = cacheService.userRolePrivilegeCache(sysUser.getId());
|
|
|
+ List<Long> userRolesList = Arrays.asList(userSaveParams.getRoleIds());
|
|
|
+ List<Long> dbUserRolesList = sysUserRoleList.stream().map(SysUserRole::getRoleId).distinct().collect(Collectors.toList());
|
|
|
+ int count = (int) dbUserRolesList.stream().filter(s -> userRolesList.contains(s)).count();
|
|
|
+ SysUser dbUser = sysUserService.getById(sysUser.getId());
|
|
|
+ sysUser.setUpdateInfo(requestUser.getId());
|
|
|
+ sysUserService.updateById(sysUser);
|
|
|
+ if (count == 0 || dbUserRolesList.size() != userRolesList.size()) {
|
|
|
+ QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
|
|
|
+ sysUserRoleQueryWrapper.lambda().eq(SysUserRole::getUserId, sysUser.getId());
|
|
|
+ sysUserRoleService.remove(sysUserRoleQueryWrapper);
|
|
|
+
|
|
|
+ cacheService.removeUserRolePrivilegeCache(sysUser.getId());
|
|
|
+ for (Long roleId : userSaveParams.getRoleIds()) {
|
|
|
+ commonService.addUserRolePrivilege(sysUser, roleId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //如果修改了角色,需要重新登录
|
|
|
+ if (count == 0 || dbUserRolesList.size() != userRolesList.size()) {
|
|
|
+ commonService.removeUserInfo(sysUser.getId());
|
|
|
+ }
|
|
|
+ //如果修改了机构,需更新用户缓存
|
|
|
+ if (dbUser.getOrgId().longValue() != sysUser.getOrgId().longValue()) {
|
|
|
+ cacheService.updateUserCache(sysUser.getId());
|
|
|
+ cacheService.updateUserAuthCache(sysUser.getId());
|
|
|
+ }
|
|
|
}
|
|
|
- SysConfig sysConfig = sysConfigService.getByKey("sys.user.initPassword");
|
|
|
- BASE64Encoder encoder = new BASE64Encoder();
|
|
|
- user.setPassword(encoder.encode(StringUtils.isNoneBlank(sysConfig.getConfigValue()) ? sysConfig.getConfigValue().getBytes() : "123456".getBytes()));
|
|
|
- user.setId(SystemConstant.getDbUuid());
|
|
|
- user.setCreateId(sysUser1.getId());
|
|
|
- user.setCreateTime(System.currentTimeMillis());
|
|
|
- isSuccess = this.save(user);
|
|
|
- }
|
|
|
- // 修改
|
|
|
- else {
|
|
|
- if (sysUser != null && user.getId().longValue() != sysUser.getId().longValue()) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("用户名已存在");
|
|
|
+ //用户科目全量删除全量增加
|
|
|
+ QueryWrapper<BasicUserCourse> basicUserCourseQueryWrapper = new QueryWrapper<>();
|
|
|
+ basicUserCourseQueryWrapper.lambda().eq(BasicUserCourse::getUserId, sysUser.getId());
|
|
|
+ basicUserCourseService.remove(basicUserCourseQueryWrapper);
|
|
|
+
|
|
|
+ List<SysRole> sysRoles = sysRoleService.list(sysUser.getRoleIds(), RoleTypeEnum.QUESTION_TEACHER.name());
|
|
|
+ if (sysRoles != null && sysRoles.size() > 0) {
|
|
|
+ Long[] courseIds = sysUser.getCourseIds();
|
|
|
+ if (courseIds.length == 0) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("请选择课程");
|
|
|
+ }
|
|
|
+ basicUserCourseService.saveBatch(sysUser);
|
|
|
}
|
|
|
- sysUser.setRealName(user.getRealName());
|
|
|
- sysUser.setMobileNumber(user.getMobileNumber());
|
|
|
- sysUser.setOrgId(user.getOrgId());
|
|
|
- sysUser.setUpdateId(sysUser1.getId());
|
|
|
- sysUser.setUpdateTime(System.currentTimeMillis());
|
|
|
- isSuccess = this.updateById(sysUser);
|
|
|
-
|
|
|
- //删除角色
|
|
|
- UpdateWrapper<SysUserRole> updateWrapper = new UpdateWrapper<>();
|
|
|
- updateWrapper.lambda().eq(SysUserRole::getUserId, sysUser.getId());
|
|
|
-
|
|
|
- //删除课程
|
|
|
- basicUserCourseService.removeByUserId(sysUser.getId());
|
|
|
-
|
|
|
- sysUserRoleService.remove(updateWrapper);
|
|
|
- }
|
|
|
-
|
|
|
- //新增用户-角色
|
|
|
- for (Long roleId : user.getRoleIds()) {
|
|
|
- List<SysRolePrivilege> rolePrivileges = sysRolePrivilegeService.listByRoleId(roleId);
|
|
|
- for (SysRolePrivilege rolePrivilege : rolePrivileges) {
|
|
|
- SysUserRole userRole = new SysUserRole();
|
|
|
- userRole.setUserId(user.getId());
|
|
|
- userRole.setRoleId(roleId);
|
|
|
- userRole.setPrivilegeId(rolePrivilege.getPrivilegeId());
|
|
|
- sysUserRoleService.save(userRole);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("请求出错", e);
|
|
|
+ isSuccess = false;
|
|
|
+ if (e instanceof DuplicateKeyException) {
|
|
|
+ String errorColumn = e.getCause().toString();
|
|
|
+ String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3, errorColumn.length()).replaceAll("'", "");
|
|
|
+ throw ExceptionResultEnum.SQL_ERROR.exception("[" + FieldUniqueEnum.convertToTitle(columnStr) + "]数据不允许重复插入");
|
|
|
+ } else if (e instanceof ApiException) {
|
|
|
+ ResultUtil.error((ApiException) e, e.getMessage());
|
|
|
+ } else {
|
|
|
+ ResultUtil.error(e.getMessage());
|
|
|
}
|
|
|
- cacheService.removeRolePrivilegeCache(roleId);
|
|
|
- }
|
|
|
-
|
|
|
- // 角色里是否有考务老师角色
|
|
|
- List<SysRole> sysRoles = sysRoleService.list(user.getRoleIds(), RoleTypeEnum.QUESTION_TEACHER.name());
|
|
|
- if (sysRoles != null && sysRoles.size() > 0) {
|
|
|
- Long[] courseIds = user.getCourseIds();
|
|
|
- if (courseIds.length == 0) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("请选择课程");
|
|
|
- }
|
|
|
-
|
|
|
- basicUserCourseService.saveBatch(user);
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
- // 清除缓存
|
|
|
- cacheService.removeUserAuthCache(user.getId());
|
|
|
-
|
|
|
return isSuccess;
|
|
|
}
|
|
|
|
|
@@ -309,7 +380,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|
|
//新增用户-角色
|
|
|
for (Long roleId : user.getRoleIds()) {
|
|
|
SysRole sysRole = sysRoleService.getById(roleId);
|
|
|
- if(!RoleTypeEnum.CUSTOMER.name().equals(sysRole.getType().name())){
|
|
|
+ if (!RoleTypeEnum.CUSTOMER.name().equals(sysRole.getType().name())) {
|
|
|
throw ExceptionResultEnum.ERROR.exception("只能添加客服角色的用户");
|
|
|
}
|
|
|
List<SysRolePrivilege> rolePrivileges = sysRolePrivilegeService.listByRoleId(roleId);
|
|
@@ -346,4 +417,17 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|
|
}
|
|
|
return userDtoIPage;
|
|
|
}
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ Long[] longs = new Long[]{1L, 2L};
|
|
|
+ List<Long> longList = new ArrayList<>();
|
|
|
+ longList.add(4L);
|
|
|
+ longList.add(3L);
|
|
|
+ longList.add(5L);
|
|
|
+ longList.add(1L);
|
|
|
+ List<Long> longList1 = Arrays.asList(longs);
|
|
|
+ int count = (int) longList.stream().filter(s -> longList1.contains(s)).count();
|
|
|
+ System.out.println(count);
|
|
|
+// Arrays.asList(longs);
|
|
|
+ }
|
|
|
}
|