|
|
@@ -1,24 +1,34 @@
|
|
|
package com.zhongshu.card.server.core.service.org;
|
|
|
|
|
|
+import com.github.microservice.auth.client.constant.AuthConstant;
|
|
|
import com.github.microservice.auth.client.content.ResultState;
|
|
|
+import com.github.microservice.auth.client.model.RoleGroupModel;
|
|
|
+import com.github.microservice.auth.client.model.RoleModel;
|
|
|
+import com.github.microservice.auth.client.model.UserAuthModel;
|
|
|
+import com.github.microservice.auth.client.model.UserModel;
|
|
|
import com.github.microservice.auth.client.service.OrganizationUserService;
|
|
|
import com.github.microservice.auth.client.service.RoleService;
|
|
|
import com.github.microservice.auth.client.service.UserService;
|
|
|
+import com.github.microservice.auth.client.type.LoginType;
|
|
|
import com.github.microservice.auth.security.type.AuthType;
|
|
|
import com.zhongshu.card.client.model.org.OrganizationAddParam;
|
|
|
import com.zhongshu.card.client.model.org.OrganizationModel;
|
|
|
import com.zhongshu.card.client.ret.ResultContent;
|
|
|
import com.zhongshu.card.client.service.org.OrganizationService;
|
|
|
-import com.zhongshu.card.server.core.dao.org.OrganizationDao;
|
|
|
-import com.zhongshu.card.server.core.domain.org.Organization;
|
|
|
+import com.zhongshu.card.client.type.UserState;
|
|
|
+import com.zhongshu.card.server.core.dao.org.*;
|
|
|
+import com.zhongshu.card.server.core.dataConfig.CardDefault;
|
|
|
+import com.zhongshu.card.server.core.domain.org.*;
|
|
|
import com.zhongshu.card.server.core.util.BeanUtils;
|
|
|
-import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
/**
|
|
|
* 机构管理
|
|
|
*
|
|
|
@@ -44,6 +54,21 @@ public class OrganizationServiceImpl implements OrganizationService {
|
|
|
@Autowired
|
|
|
UserService userService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ UserCountDao userCountDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserAccountServiceImpl userAccountService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrganizationUserDao organizationUserDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RoleDao roleDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DepartmentServiceImpl departmentService;
|
|
|
+
|
|
|
public ResultContent addOrganization(OrganizationAddParam param) {
|
|
|
Organization organization = new Organization();
|
|
|
|
|
|
@@ -54,12 +79,13 @@ public class OrganizationServiceImpl implements OrganizationService {
|
|
|
@Override
|
|
|
public ResultContent initSuperOrganization() {
|
|
|
String oid = "";
|
|
|
- Organization organization = organizationDao.findByAuthType(AuthType.Platform);
|
|
|
+ AuthType authType = AuthType.Project;
|
|
|
+ Organization organization = organizationDao.findByAuthType(authType);
|
|
|
if (ObjectUtils.isEmpty(organization)) {
|
|
|
log.info("initSuperOrganization 开始初始平台结构信息");
|
|
|
com.github.microservice.auth.client.model.OrganizationModel organizationModel = new com.github.microservice.auth.client.model.OrganizationModel();
|
|
|
organizationModel.setName("全卡项目平台");
|
|
|
- organizationModel.setAuthType(AuthType.Platform);
|
|
|
+ organizationModel.setAuthType(authType);
|
|
|
organizationModel.setRemark("系统自动初始化");
|
|
|
com.github.microservice.auth.client.content.ResultContent<String>
|
|
|
resultContent = organizationService.add(organizationModel);
|
|
|
@@ -75,12 +101,143 @@ public class OrganizationServiceImpl implements OrganizationService {
|
|
|
log.info("initSuperOrganization {}", msg);
|
|
|
return ResultContent.buildFail(msg);
|
|
|
}
|
|
|
+
|
|
|
+ // 保存企业信息
|
|
|
+ organization = new Organization();
|
|
|
+ organization.setAuthType(authType);
|
|
|
+ organization.setOid(oid);
|
|
|
+ organization.setName(organizationModel.getName());
|
|
|
+ organization.setRemark(organizationModel.getRemark());
|
|
|
+ organization.setCode("000001");
|
|
|
+ organization.setContactPhone(CardDefault.SUPER_ADMIN_NAME);
|
|
|
+ organizationDao.save(organization);
|
|
|
+ // 初始用户信息
|
|
|
+ initOrganization(organization);
|
|
|
} else {
|
|
|
oid = organization.getOid();
|
|
|
}
|
|
|
return ResultContent.buildSuccess(oid);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 初始 结构用户这些信息
|
|
|
+ *
|
|
|
+ * @param organization
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent initOrganization(Organization organization) {
|
|
|
+ String userId = "";
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<UserModel> userModelResult =
|
|
|
+ userService.queryFromLoginType(LoginType.Phone, organization.getContactPhone());
|
|
|
+ if (userModelResult.getState() != ResultState.Success) {
|
|
|
+ //未注册
|
|
|
+ //TODO 权限中心: 注册用户
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<String> addModelResult = userService.add(
|
|
|
+ UserAuthModel.builder().loginType(LoginType.Phone).loginValue(organization.getContactPhone())
|
|
|
+ .passWord(CardDefault.DEFAULT_PASSWORD).build());
|
|
|
+ userId = addModelResult.getContent();
|
|
|
+ log.info("初始权限中心 用户成功:{}", userId);
|
|
|
+ } else {
|
|
|
+ userId = userModelResult.getContent().getUserId();
|
|
|
+ }
|
|
|
+
|
|
|
+ UserAccount userAccount = userCountDao.findTopByUserId(userId);
|
|
|
+ if (userAccount == null) {
|
|
|
+ // 初始企业中心的用户
|
|
|
+ userAccount = userAccountService.initUserAccountOrg(userId, organization);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!organizationUserDao.existsByOrganizationAndUser(organization, userAccount)) {
|
|
|
+ //本地: 绑定机构和用户的关系
|
|
|
+ OrganizationUser organizationUser = new OrganizationUser();
|
|
|
+ organizationUser.setCode("0000001");
|
|
|
+ organizationUser.setUser(userAccount);
|
|
|
+ organizationUser.setOrganization(organization);
|
|
|
+ organizationUser.setName(userAccount.getName());
|
|
|
+ organizationUser.setPhone(userAccount.getPhone());
|
|
|
+ organizationUser.setIsAdmin(Boolean.TRUE);
|
|
|
+ organizationUser.setState(UserState.Normal);
|
|
|
+ organizationUser.setUserId(userAccount.getUserId());
|
|
|
+ organizationUser.setAuthType(organization.getAuthType());
|
|
|
+ organizationUserDao.save(organizationUser);
|
|
|
+
|
|
|
+ // 权限中心:用户和机构的关系
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<Void> resultContent = organizationUserService.update(organization.getOid(), new String[]{userId});
|
|
|
+ if (resultContent.getState() == ResultState.Success) {
|
|
|
+ log.info("权限中心:绑定机构和用户关系成功");
|
|
|
+ } else {
|
|
|
+ log.error("权限中心:绑定机构和用户关系出错:{}", resultContent.getMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ initResource(organization, userId);
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始角色等信息
|
|
|
+ *
|
|
|
+ * @param organization
|
|
|
+ * @param userId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent initResource(Organization organization, String userId) {
|
|
|
+ UserAccount userAccount = userCountDao.findTopByUserId(userId);
|
|
|
+
|
|
|
+ String oid = organization.getOid();
|
|
|
+ // 权限中心: 角色组
|
|
|
+ RoleGroupModel admin = new RoleGroupModel();
|
|
|
+ admin.setName(CardDefault.DEFAULT_ROLE_NAME);
|
|
|
+ admin.setRemark(CardDefault.DEFAULT_ROLE_NAME);
|
|
|
+ admin.setOrganizationId(oid);
|
|
|
+ admin.setIdentity(Set.of(AuthConstant.Admin));
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<String> updateRoleGroupAdmin = roleService.updateRoleGroup(admin);
|
|
|
+ String groupId = updateRoleGroupAdmin.getContent();
|
|
|
+ log.info("初始权限中心角色组成功:{}", groupId);
|
|
|
+
|
|
|
+ Role adminRole = null;
|
|
|
+ if (!StringUtils.isEmpty(groupId)) {
|
|
|
+ Set<String> auth = Set.of(AuthConstant.Admin);
|
|
|
+ // 权限中心: 角色信息
|
|
|
+ RoleModel roleModel = new RoleModel();
|
|
|
+ roleModel.setOrganizationId(oid);
|
|
|
+ roleModel.setName(CardDefault.DEFAULT_ROLE_NAME);
|
|
|
+ roleModel.setRemark(CardDefault.DEFAULT_ROLE_NAME);
|
|
|
+ roleModel.setAuth(auth);
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<String> updateRole =
|
|
|
+ roleService.updateRole(roleModel);
|
|
|
+ roleService.addRoleToRoleGroup(groupId, new String[]{updateRole.getContent()});
|
|
|
+ log.info("权限中心:初始角色和角色组关系成功");
|
|
|
+
|
|
|
+ // 添加本地角色信息
|
|
|
+ adminRole = new Role();
|
|
|
+ adminRole.setCode("manager");
|
|
|
+ adminRole.setAuth(auth);
|
|
|
+ adminRole.setOid(oid);
|
|
|
+ adminRole.setRoleGroupId(groupId);
|
|
|
+ adminRole.setName(CardDefault.DEFAULT_ROLE_NAME);
|
|
|
+ adminRole.setRemark(CardDefault.DEFAULT_ROLE_NAME);
|
|
|
+ adminRole.setAdmin(true);
|
|
|
+ roleDao.save(adminRole);
|
|
|
+
|
|
|
+ //权限中心: 将用户添加进管理员角色组
|
|
|
+ roleService.addUserToRoleGroup(groupId, new String[]{userId});
|
|
|
+ log.info("权限中心:绑定角色和用户关系成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 本地:保存用户角色信息
|
|
|
+ if (ObjectUtils.isNotEmpty(adminRole)) {
|
|
|
+ OrganizationUser organizationUser = organizationUserDao.findTopByOrganizationAndUser(organization, userAccount);
|
|
|
+ organizationUser.setRoles(List.of(adminRole));
|
|
|
+ organizationUserDao.save(organizationUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 本地:初始顶级部门
|
|
|
+ departmentService.initOrgDepartment(organization);
|
|
|
+
|
|
|
+ log.info("-------------初始换所有成功-------------");
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
public OrganizationModel toModel(Organization entity) {
|
|
|
OrganizationModel model = new OrganizationModel();
|
|
|
if (ObjectUtils.isNotEmpty(entity)) {
|