软件项目管理系统 用户管理功能 修改 4.12

master
zhc077 2 years ago
parent cbe05ba834
commit be9e09c84c
  1. 29
      jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserController.java
  2. 67
      jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java

@ -14,18 +14,19 @@ import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.jeecg.common.util.ImportExcelUtil;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.system.entity.*;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.model.SysUserSysDepartModel;
@ -57,7 +58,7 @@ import java.util.stream.Collectors;
* </p>
*
* @Author scott
* @since 2018-12-20
* @since 2023-04-12
*/
@Slf4j
@RestController
@ -193,12 +194,12 @@ public class SysUserController {
//@RequiresRoles({"admin"})
//@RequiresPermissions("user:add")
@AutoLog
@RequestMapping(value = "/add", method = RequestMethod.POST)
public Result<SysUser> add(@RequestBody JSONObject jsonObject) {
Result<SysUser> result = new Result<SysUser>();
String selectedRoles = jsonObject.getString("selectedroles");
String selectedDeparts = jsonObject.getString("selecteddeparts");
// System.out.println("我的测试" + selectedRoles);
try {
SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
user.setCreateTime(new Date());//设置创建时间
@ -208,9 +209,11 @@ public class SysUserController {
user.setPassword(passwordEncode);
user.setStatus(1);
user.setDelFlag(CommonConstant.DEL_FLAG_0);
//用户表字段org_code不能在这里设置他的值
user.setOrgCode(null);
// 保存用户走一个service 保证事务
this.sysUserService.save(user);
// sysUserService.saveUser(user, selectedRoles, selectedDeparts);
sysUserService.saveUser(user, selectedRoles, selectedDeparts);
// baseCommonService.addLog("添加用户,username: " + user.getUsername(), CommonConstant.LOG_TYPE_2, 2);
result.success("添加成功!");
} catch (Exception e) {
log.error(e.getMessage(), e);
@ -221,6 +224,7 @@ public class SysUserController {
//@RequiresRoles({"admin"})
//@RequiresPermissions("user:edit")
@AutoLog
@RequestMapping(value = "/edit", method = RequestMethod.PUT)
public Result<SysUser> edit(@RequestBody JSONObject jsonObject) {
Result<SysUser> result = new Result<SysUser>();
@ -237,8 +241,8 @@ public class SysUserController {
String roles = jsonObject.getString("selectedroles");
String departs = jsonObject.getString("selecteddeparts");
// 修改用户走一个service 保证事务
sysUserService.updateById(user);
// sysUserService.editUser(user, roles, departs);
// sysUserService.updateById(user);
sysUserService.editUser(user, roles, departs);
result.success("修改成功!");
}
} catch (Exception e) {
@ -252,6 +256,7 @@ public class SysUserController {
* 删除用户
*/
//@RequiresRoles({"admin"})
@AutoLog
@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
this.sysUserService.removeById(id);
@ -266,8 +271,8 @@ public class SysUserController {
//@RequiresRoles({"admin"})
@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
baseCommonService.addLog("批量删除用户, ids: " + ids, CommonConstant.LOG_TYPE_2, 3);
this.sysUserService.deleteBatchUsers(ids);
// baseCommonService.addLog("批量删除用户, ids: " + ids, CommonConstant.LOG_TYPE_2, 3);
this.sysUserService.removeByIds(Arrays.asList(ids.split(",")));
return Result.ok("批量删除用户成功");
}

@ -169,6 +169,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
/**
* 通过用户名获取用户角色集合
*
* @param username 用户名
* @return 角色集合
*/
@ -248,10 +249,12 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
public IPage<SysUser> getUserByDepIds(Page<SysUser> page, List<String> departIds, String username) {
return userMapper.getUserByDepIds(page, departIds, username);
}
@Override
public List<SysUser> getUserByDepIds(List<String> departIds, String username) {
return userMapper.getUserByDepIds(departIds, username);
}
@Override
public Map<String, String> getDepNamesByUserIds(List<String> userIds) {
List<SysUserDepVo> list = this.baseMapper.getDepNamesByUserIds(userIds);
@ -366,6 +369,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
/**
* 校验用户是否有效
*
* @param sysUser
* @return
*/
@ -469,45 +473,20 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Override
@Transactional(rollbackFor = Exception.class)
public void saveUser(SysUser user, String selectedRoles, String selectedDeparts) {
//首先根据id看修改前有没有这个学生,如果有的话,调用edit修改,没有的话直接添加。
int ans=jxStudentService.cont(user.getId());
//step.1 保存用户
this.save(user);
//step.2 保存角色
if (oConvertUtils.isNotEmpty(selectedRoles)) {
String[] arr = selectedRoles.split(",");
for (String roleId : arr) {
//这串数字是学生角色id
if(roleId.equals("1296245993988284417")){
JxStudent jxStudent=new JxStudent();
jxStudent.setId(user.getId());
jxStudent.setName(user.getRealname());
jxStudent.setPhone(user.getPhone());
jxStudent.setBirthday(user.getBirthday());
if(user.getSex()==1)
jxStudent.setSex("1");
else
jxStudent.setSex("2");
//这里的判断主要是为了防止学生表新加后再来用户表新加会出现的问题。
if(ans==0)
jxStudentService.save(jxStudent);
}
SysUserRole userRole = new SysUserRole(user.getId(), roleId);
sysUserRoleMapper.insert(userRole);
}
}
//step.3 保存所属部门
if (oConvertUtils.isNotEmpty(selectedDeparts)) {
ans=jxStudentService.cont(user.getId());
String[] arr = selectedDeparts.split(",");
System.out.println(arr.length);
for (String deaprtId : arr) {
if(arr.length==1&&ans!=0){
JxStudent jxStudent=new JxStudent();
jxStudent.setId(user.getId());
jxStudent.setFaculty(deaprtId);
jxStudentService.updateById(jxStudent);
}
SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId);
sysUserDepartMapper.insert(userDeaprt);
}
@ -518,49 +497,18 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Transactional(rollbackFor = Exception.class)
@CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
public void editUser(SysUser user, String roles, String departs) {
//首先根据id看修改前有没有这个学生,如果有的话,调用edit修改,没有的话直接添加。
int ans=jxStudentService.cont(user.getId());
//ans不等于0的话,就是有这条数据,调用edit修改,否则就是添加。
//判断修改后,对应用户是否还有学生这个角色。
boolean student=false;
//step.1 修改用户基础信息
this.updateById(user);
//step.2 修改角色
//处理用户角色 先删后加
sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, user.getId()));
//这里if中判断的是否角色为空,为空直接不执行了,所以说,如果之前有学生的话,要给删掉的。
if (oConvertUtils.isNotEmpty(roles)) {
String[] arr = roles.split(",");
for (String roleId : arr) {
//在修改用户角色时,如果没了学生这个角色,一会就要把对应学生表中的数据给删掉了。
if(roleId.equals("1296245993988284417")){
student=true;
JxStudent jxStudent=new JxStudent();
jxStudent.setId(user.getId());
jxStudent.setName(user.getRealname());
jxStudent.setPhone(user.getPhone());
jxStudent.setBirthday(user.getBirthday());
if(user.getSex()==1)
jxStudent.setSex("1");
else
jxStudent.setSex("2");
//判断是不是第一次给用户学生这个角色,如果是的话就会调用save新增,如果不是的话,就会在原表中进行修改。
if(ans==0)
jxStudentService.save(jxStudent);
else
jxStudentService.updateById(jxStudent);
}
SysUserRole userRole = new SysUserRole(user.getId(), roleId);
sysUserRoleMapper.insert(userRole);
}
//如果student为false,则证明修改之后,没有学生这个角色了,要把学生表中对应的信息给删除。
if(!student&&ans!=0){
jxStudentService.removeById(user.getId());
}
}else{
//修改完一个角色都没有,而且学生表还存在的话,就直接删除。
if(ans!=0)
jxStudentService.removeById(user.getId());}
//step.3 修改部门
String[] arr = {};
@ -586,14 +534,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
//先删后加
sysUserDepartMapper.delete(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId()));
if (oConvertUtils.isNotEmpty(departs)) {
//ans=jxStudentService.cont(user.getId());
for (String departId : arr) {
if(arr.length==1&&ans!=0){
JxStudent jxStudent=new JxStudent();
jxStudent.setId(user.getId());
jxStudent.setFaculty(departId);
jxStudentService.updateById(jxStudent);
}
SysUserDepart userDepart = new SysUserDepart(user.getId(), departId);
sysUserDepartMapper.insert(userDepart);
}

Loading…
Cancel
Save