wangwei 7 rokov pred
rodič
commit
5e6d43f5e6

+ 135 - 2
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/provider/UserCloudServiceProvider.java

@@ -1,15 +1,36 @@
 package cn.com.qmth.examcloud.core.basic.api.provider;
 
+import java.util.List;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import com.google.common.collect.Lists;
+
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
+import cn.com.qmth.examcloud.commons.web.security.enums.RoleMeta;
 import cn.com.qmth.examcloud.commons.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.core.basic.api.UserCloudService;
+import cn.com.qmth.examcloud.core.basic.api.bean.RoleBean;
+import cn.com.qmth.examcloud.core.basic.api.bean.UserBean;
 import cn.com.qmth.examcloud.core.basic.api.request.AddUserReq;
 import cn.com.qmth.examcloud.core.basic.api.request.GetUserReq;
 import cn.com.qmth.examcloud.core.basic.api.response.AddUserResp;
 import cn.com.qmth.examcloud.core.basic.api.response.GetUserResp;
+import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
+import cn.com.qmth.examcloud.core.basic.dao.RoleRepo;
+import cn.com.qmth.examcloud.core.basic.dao.UserRepo;
+import cn.com.qmth.examcloud.core.basic.dao.UserRoleRelationRepo;
+import cn.com.qmth.examcloud.core.basic.dao.constants.Consts;
+import cn.com.qmth.examcloud.core.basic.dao.entity.Org;
+import cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity;
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserEntity;
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserRoleRelationEntity;
 import io.swagger.annotations.ApiOperation;
 
 /**
@@ -19,24 +40,136 @@ import io.swagger.annotations.ApiOperation;
  * @date 2018年7月23日
  * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  */
+@Transactional
 @RestController
 @RequestMapping("${$rmp.cloud.basic}" + "user")
 public class UserCloudServiceProvider extends ControllerSupport implements UserCloudService {
 
 	private static final long serialVersionUID = -7881522922704971610L;
 
+	@Autowired
+	UserRepo userRepo;
+
+	@Autowired
+	OrgRepo orgRepo;
+
+	@Autowired
+	RoleRepo roleRepo;
+
+	@Autowired
+	UserRoleRelationRepo userRoleRelationRepo;
+
 	@ApiOperation(value = "保存用户")
 	@PostMapping("addUser")
 	@Override
 	public AddUserResp addUser(AddUserReq req) {
-		return null;
+		trim(req);
+		Long rootOrgId = req.getRootOrgId();
+		String loginName = req.getLoginName();
+		UserEntity userEntity = userRepo.findByRootOrgIdAndLoginName(rootOrgId, loginName);
+
+		if (null != userEntity) {
+			throw new StatusException("B-650001", "用户名已经存在");
+		}
+
+		userEntity = new UserEntity();
+		userEntity.setLoginName(loginName);
+		userEntity.setRootOrgId(rootOrgId);
+		if (StringUtils.isBlank(req.getPassword())) {
+			userEntity.setPassword(Consts.DEFAULT_PASSWORD);
+		} else {
+			userEntity.setPassword(req.getPassword());
+		}
+
+		UserEntity saved = userRepo.save(userEntity);
+
+		List<String> roleCodeList = req.getRoleCodeList();
+
+		if (CollectionUtils.isEmpty(roleCodeList)) {
+			throw new StatusException("B-650002", "角色不能为空");
+		}
+
+		List<UserRoleRelationEntity> userRoles = Lists.newArrayList();
+		for (String roleCode : roleCodeList) {
+			RoleEntity roleEntity = roleRepo.findByCode(roleCode);
+			if (null == roleEntity) {
+				throw new StatusException("B-002002", "role code is wrong. roleCode=" + roleCode);
+			}
+			if (roleEntity.getCode().equals(RoleMeta.SUPER_ADMIN.name())) {
+				throw new StatusException("B-150007", "不允许新增或修改超级管理员");
+			}
+			UserRoleRelationEntity relation = new UserRoleRelationEntity(saved.getId(),
+					roleEntity.getId(), roleEntity.getCode());
+			userRoles.add(relation);
+		}
+
+		userRoleRelationRepo.deleteByUserId(saved.getId());
+		userRoleRelationRepo.save(userRoles);
+		AddUserResp resp = new AddUserResp();
+		resp.setUserId(saved.getId());
+
+		return resp;
 	}
 
 	@ApiOperation(value = "获取用户")
 	@PostMapping("getUser")
 	@Override
 	public GetUserResp getUser(GetUserReq req) {
-		return null;
+		Long rootOrgId = req.getRootOrgId();
+		String loginName = req.getLoginName();
+		UserEntity userEntity = userRepo.findByRootOrgIdAndLoginName(rootOrgId, loginName);
+
+		GetUserResp resp = new GetUserResp();
+		if (null == userEntity) {
+			resp.setExisting(false);
+			return resp;
+		}
+
+		resp.setExisting(true);
+
+		UserBean userBean = new UserBean();
+		userBean.setDisplayName(userEntity.getLoginName() + " (" + userEntity.getName() + ")");
+		userBean.setOrgId(userEntity.getOrgId());
+		userBean.setRootOrgId(userEntity.getRootOrgId());
+		Org rootOrg = orgRepo.findOne(userBean.getRootOrgId());
+		userBean.setRootOrgName(rootOrg.getName());
+
+		if (null != userBean.getOrgId()) {
+			Org org = orgRepo.findOne(userBean.getOrgId());
+			userBean.setOrgName(org.getName());
+		}
+
+		List<RoleBean> roleList = getUserRoles(userEntity.getId());
+		userBean.setRoleList(roleList);
+
+		resp.setUserBean(userBean);
+		return resp;
+	}
+
+	/**
+	 * 获取角色集合
+	 *
+	 * @author WANGWEI
+	 * @param userId
+	 * @return
+	 */
+	private List<RoleBean> getUserRoles(Long userId) {
+		List<UserRoleRelationEntity> relationList = userRoleRelationRepo.findAllByUserId(userId);
+		List<RoleBean> roleList = Lists.newArrayList();
+		if (CollectionUtils.isNotEmpty(relationList)) {
+			for (UserRoleRelationEntity cur : relationList) {
+				String roleCode = cur.getRoleCode();
+				RoleEntity roleEntity = roleRepo.findByCode(roleCode);
+				if (null == roleEntity) {
+					throw new StatusException("B-002002",
+							"role code is wrong. roleCode=" + roleCode);
+				}
+				RoleBean role = new RoleBean(roleEntity.getId(), roleEntity.getCode(),
+						roleEntity.getName());
+				roleList.add(role);
+			}
+		}
+		return roleList;
 	}
 
 }

+ 3 - 1
examcloud-core-basic-api/src/main/java/cn/com/qmth/examcloud/core/basic/api/bean/RoleBean.java

@@ -22,7 +22,9 @@ public class RoleBean implements JsonSerializable {
 	public RoleBean() {
 	}
 
-	public RoleBean(String roleCode, String roleName) {
+	public RoleBean(Long roleId, String roleCode, String roleName) {
+		super();
+		this.roleId = roleId;
 		this.roleCode = roleCode;
 		this.roleName = roleName;
 	}

+ 12 - 62
examcloud-core-basic-api/src/main/java/cn/com/qmth/examcloud/core/basic/api/bean/UserBean.java

@@ -15,11 +15,6 @@ public class UserBean implements JsonSerializable {
 
 	private static final long serialVersionUID = -4157068941699776174L;
 
-	/**
-	 * 用户类型
-	 */
-	private String userType;
-
 	/**
 	 * 用户ID
 	 */
@@ -34,16 +29,17 @@ public class UserBean implements JsonSerializable {
 	 * 顶级机构(学校)ID
 	 */
 	private Long rootOrgId;
-	
+
 	/**
 	 * 顶级机构(学校)名称
 	 */
 	private String rootOrgName;
-	
+
 	/**
 	 * 学习中心 ID
 	 */
 	private Long orgId;
+
 	/**
 	 * 学习中心名称
 	 */
@@ -53,28 +49,6 @@ public class UserBean implements JsonSerializable {
 	 * 角色集合
 	 */
 	private List<RoleBean> roleList;
-	
-	/**
-	 * 学号 
-	 */
-	private String studentCode;
-	/**
-	 * 学生ID
-	 */
-	private Long studentId;
-	
-	/**
-	 * 身份证号
-	 */
-	private String identityNumber;
-
-	public String getUserType() {
-		return userType;
-	}
-
-	public void setUserType(String userType) {
-		this.userType = userType;
-	}
 
 	public Long getUserId() {
 		return userId;
@@ -100,22 +74,6 @@ public class UserBean implements JsonSerializable {
 		this.rootOrgId = rootOrgId;
 	}
 
-	public List<RoleBean> getRoleList() {
-		return roleList;
-	}
-
-	public void setRoleList(List<RoleBean> roleList) {
-		this.roleList = roleList;
-	}
-
-	public Long getOrgId() {
-		return orgId;
-	}
-
-	public void setOrgId(Long orgId) {
-		this.orgId = orgId;
-	}
-
 	public String getRootOrgName() {
 		return rootOrgName;
 	}
@@ -124,20 +82,12 @@ public class UserBean implements JsonSerializable {
 		this.rootOrgName = rootOrgName;
 	}
 
-	public String getStudentCode() {
-		return studentCode;
-	}
-
-	public void setStudentCode(String studentCode) {
-		this.studentCode = studentCode;
-	}
-
-	public String getIdentityNumber() {
-		return identityNumber;
+	public Long getOrgId() {
+		return orgId;
 	}
 
-	public void setIdentityNumber(String identityNumber) {
-		this.identityNumber = identityNumber;
+	public void setOrgId(Long orgId) {
+		this.orgId = orgId;
 	}
 
 	public String getOrgName() {
@@ -148,12 +98,12 @@ public class UserBean implements JsonSerializable {
 		this.orgName = orgName;
 	}
 
-	public Long getStudentId() {
-		return studentId;
+	public List<RoleBean> getRoleList() {
+		return roleList;
 	}
 
-	public void setStudentId(Long studentId) {
-		this.studentId = studentId;
+	public void setRoleList(List<RoleBean> roleList) {
+		this.roleList = roleList;
 	}
-	
+
 }

+ 15 - 5
examcloud-core-basic-api/src/main/java/cn/com/qmth/examcloud/core/basic/api/request/AddUserReq.java

@@ -19,7 +19,9 @@ public class AddUserReq extends BaseRequest {
 
 	private String loginName;
 
-	private List<String> roleCode;
+	private String password;
+
+	private List<String> roleCodeList;
 
 	public Long getRootOrgId() {
 		return rootOrgId;
@@ -37,12 +39,20 @@ public class AddUserReq extends BaseRequest {
 		this.loginName = loginName;
 	}
 
-	public List<String> getRoleCode() {
-		return roleCode;
+	public String getPassword() {
+		return password;
+	}
+
+	public void setPassword(String password) {
+		this.password = password;
+	}
+
+	public List<String> getRoleCodeList() {
+		return roleCodeList;
 	}
 
-	public void setRoleCode(List<String> roleCode) {
-		this.roleCode = roleCode;
+	public void setRoleCodeList(List<String> roleCodeList) {
+		this.roleCodeList = roleCodeList;
 	}
 
 }

+ 10 - 0
examcloud-core-basic-api/src/main/java/cn/com/qmth/examcloud/core/basic/api/response/AddUserResp.java

@@ -13,4 +13,14 @@ public class AddUserResp extends BaseResponse {
 
 	private static final long serialVersionUID = -9143409387667411757L;
 
+	private Long userId;
+
+	public Long getUserId() {
+		return userId;
+	}
+
+	public void setUserId(Long userId) {
+		this.userId = userId;
+	}
+
 }

+ 21 - 0
examcloud-core-basic-api/src/main/java/cn/com/qmth/examcloud/core/basic/api/response/GetUserResp.java

@@ -1,9 +1,30 @@
 package cn.com.qmth.examcloud.core.basic.api.response;
 
 import cn.com.qmth.examcloud.commons.web.cloud.api.BaseResponse;
+import cn.com.qmth.examcloud.core.basic.api.bean.UserBean;
 
 public class GetUserResp extends BaseResponse {
 
 	private static final long serialVersionUID = -2113393945721693679L;
 
+	private Boolean existing;
+
+	private UserBean userBean;
+
+	public Boolean getExisting() {
+		return existing;
+	}
+
+	public void setExisting(Boolean existing) {
+		this.existing = existing;
+	}
+
+	public UserBean getUserBean() {
+		return userBean;
+	}
+
+	public void setUserBean(UserBean userBean) {
+		this.userBean = userBean;
+	}
+
 }