Sfoglia il codice sorgente

改造新增租户相关逻辑实现,租户与用户实现类划分改造

tags/v2.4.1
xiaoying 1 anno fa
parent
commit
af8b96c538
14 ha cambiato i file con 773 aggiunte e 680 eliminazioni
  1. +8
    -19
      tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/TenantController.java
  2. +0
    -24
      tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/UserController.java
  3. +3
    -1
      tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/OidcTenantDto.java
  4. +32
    -1
      tuoheng_oidc_admin/src/main/java/com/tuoheng/model/param/CreateClientTenantDto.java
  5. +0
    -39
      tuoheng_oidc_admin/src/main/java/com/tuoheng/service/ClientUserSevice.java
  6. +50
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/service/TenantService.java
  7. +1
    -596
      tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/ClientUserServiceImpl.java
  8. +679
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/TenantServiceImpl.java
  9. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/controller/TenantController.class
  10. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/controller/UserController.class
  11. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/model/dto/OidcTenantDto.class
  12. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/model/param/CreateClientTenantDto.class
  13. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/service/ClientUserSevice.class
  14. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/service/impl/ClientUserServiceImpl.class

+ 8
- 19
tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/TenantController.java Vedi File

@@ -7,6 +7,7 @@ import com.tuoheng.model.param.CreateClientTenantDto;
import com.tuoheng.model.query.TenantQuery;
import com.tuoheng.service.ClientUserSevice;
import com.tuoheng.service.CurrentUser;
import com.tuoheng.service.TenantService;
import com.tuoheng.until.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,25 +24,13 @@ import org.springframework.web.bind.annotation.*;
public class TenantController {

@Autowired
private ClientUserSevice clientUserSevice;
private TenantService tenantService;

@PostMapping("/create")
@PostMapping("/createTenant")
public JsonResult createClientTenant(@RequestBody CreateClientTenantDto createClientTenantDto,
@CurrentUser LoginUser loginUser) {
return clientUserSevice.createClientTenant(createClientTenantDto, loginUser);
return tenantService.createClientTenant(createClientTenantDto, loginUser);
}

/**
* 新增业务平台的租户基本数据
*
* @param dto
* @return
*/
@PostMapping("/add")
public JsonResult add(@RequestBody OidcTenantDto dto, @CurrentUser LoginUser loginUser) {
return clientUserSevice.addTenant(dto, loginUser);
}

/**
* 查询租户列表
*
@@ -49,7 +38,7 @@ public class TenantController {
*/
@GetMapping("/list")
public JsonResult list(TenantQuery query) {
return clientUserSevice.findTenants(query);
return tenantService.findTenants(query);
}

/**
@@ -61,7 +50,7 @@ public class TenantController {
*/
@PostMapping("/edit")
public JsonResult edit(@RequestBody OidcTenantDto dto, @CurrentUser LoginUser loginUser) {
return clientUserSevice.editTenant(dto, loginUser);
return tenantService.editTenant(dto, loginUser);

}

@@ -72,7 +61,7 @@ public class TenantController {
*/
@PostMapping("/delete")
public JsonResult delete(@RequestBody OidcTenantDto dto, @CurrentUser LoginUser loginUser) {
return clientUserSevice.deleteTenant(dto, loginUser);
return tenantService.deleteTenant(dto, loginUser);
}

/**
@@ -84,6 +73,6 @@ public class TenantController {
*/
@GetMapping("/getRoleList")
public JsonResult getRoleList(String clientId, @CurrentUser LoginUser loginUser) {
return clientUserSevice.getRoleList(clientId, loginUser);
return tenantService.getRoleList(clientId, loginUser);
}
}

+ 0
- 24
tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/UserController.java Vedi File

@@ -48,28 +48,4 @@ public class UserController {
@CurrentUser LoginUser loginUser){
return clientUserSevice.updateUserClientRole(updateUserClientRoleDto, loginUser);
}

/**
* 更新 用户/租户对应关系的系统or角色id
* @param createClientUserDto
* @param loginUser
* @return
*/
@PostMapping("/updateAuthorities")
public JsonResult updateAuthorities(@RequestBody @Validated CreateClientUserDto createClientUserDto,
@CurrentUser LoginUser loginUser){
return clientUserSevice.updateAuthorities(createClientUserDto, loginUser);

}

/**
* 查询对应租户下的所有用户数据
* @param query
* @return
*/
@GetMapping("/findUserList")
public JsonResult findUserList(UserQuery query){
return clientUserSevice.findUserList(query);
}

}

+ 3
- 1
tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/OidcTenantDto.java Vedi File

@@ -3,6 +3,8 @@ package com.tuoheng.model.dto;
import com.tuoheng.model.param.ClientRoleDto;
import lombok.Data;

import java.util.List;

/**
* oidc-新增租户第三方
* @Author xiaoying
@@ -25,7 +27,7 @@ public class OidcTenantDto {
/**
* 平台集合
*/
private ClientRoleDto clientRoleDto;
private List<ClientRoleDto> clientRoleDtos;
/**
* 密码
*/

+ 32
- 1
tuoheng_oidc_admin/src/main/java/com/tuoheng/model/param/CreateClientTenantDto.java Vedi File

@@ -24,5 +24,36 @@ public class CreateClientTenantDto {

@NotNull(message = "clientRoleDtoList can not be null!")
private List<ClientRoleDto> clientRoleDtoList;

/**
* 租户名称
*/
private String tenantName;
/**
* 租户code
*/
private String tenantCode;
/**
* 省级编码
*/
private String provinceCode;
/**
* 省份名称
*/
private String provinceName;
/**
*市区编号
*/
private String cityCode;
/**
* 市区名称
*/
private String cityName;
/**
* 区县编号
*/
private String districtCode;
/**
* 区县名称
*/
private String districtName;
}

+ 0
- 39
tuoheng_oidc_admin/src/main/java/com/tuoheng/service/ClientUserSevice.java Vedi File

@@ -21,47 +21,8 @@ public interface ClientUserSevice {

JsonResult createClientUser(CreateClientUserDto createClientUserDto, LoginUser loginUser);

JsonResult createClientTenant(CreateClientTenantDto createClientTenantDto, LoginUser loginUser);

JsonResult updateUserPassword(UpdateUserPassDto updateUserPassDto, LoginUser loginUser);

JsonResult updateUserClientRole(UpdateUserClientRoleDto updateUserClientRoleDto, LoginUser loginUser);

/**
* 新增业务平台的租户基本数据
* @param dto
* @return
*/
JsonResult addTenant(OidcTenantDto dto,LoginUser loginUser);

JsonResult findTenants(TenantQuery query);

JsonResult editTenant(OidcTenantDto dto, LoginUser loginUser);
/**
* 删除租户(逻辑删除)
* @return
*/
JsonResult deleteTenant(OidcTenantDto dto, LoginUser loginUser);
/**
* 更新 用户/租户对应关系的系统or角色id
* @param createClientUserDto
* @param loginUser
* @return
*/
JsonResult updateAuthorities(CreateClientUserDto createClientUserDto, LoginUser loginUser);
/**
* 查询对应租户下的所有用户数据
* @param query
* @return
*/
JsonResult findUserList(UserQuery query);
/**
* 查询对应业务平台的所有角色
*
* @param clientId 平台标识
* @param loginUser
* @return
*/
JsonResult getRoleList(String clientId, LoginUser loginUser);

}

+ 50
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/service/TenantService.java Vedi File

@@ -0,0 +1,50 @@
package com.tuoheng.service;

import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.dto.OidcTenantDto;
import com.tuoheng.model.param.CreateClientTenantDto;
import com.tuoheng.model.param.CreateClientUserDto;
import com.tuoheng.model.query.TenantQuery;
import com.tuoheng.model.query.UserQuery;
import com.tuoheng.until.JsonResult;

/**
* @Author xiaoying
* @Date 2023/3/6 11:26
*/
public interface TenantService {


JsonResult findTenants(TenantQuery query);

JsonResult editTenant(OidcTenantDto dto, LoginUser loginUser);
/**
* 删除租户(逻辑删除)
* @return
*/
JsonResult deleteTenant(OidcTenantDto dto, LoginUser loginUser);
/**
* 更新 用户/租户对应关系的系统or角色id
* @param createClientUserDto
* @param loginUser
* @return
*/
JsonResult updateAuthorities(CreateClientUserDto createClientUserDto, LoginUser loginUser);
/**
* 查询对应租户下的所有用户数据
* @param query
* @return
*/
JsonResult findUserList(UserQuery query);
/**
* 查询对应业务平台的所有角色
*
* @param clientId 平台标识
* @param loginUser
* @return
*/
JsonResult getRoleList(String clientId, LoginUser loginUser);

JsonResult createClientTenant(CreateClientTenantDto createClientTenantDto, LoginUser loginUser);

}

+ 1
- 596
tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/ClientUserServiceImpl.java Vedi File

@@ -22,6 +22,7 @@ import com.tuoheng.model.vo.UserVo;
import com.tuoheng.service.ClientUserSevice;
import com.tuoheng.until.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
@@ -153,52 +154,6 @@ public class ClientUserServiceImpl implements ClientUserSevice {
clientUserRoleMapper.batchInsert(clientUserRolePoArrayList);
return JsonResult.success(userPo.getId());
}


@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult createClientTenant(CreateClientTenantDto createClientTenantDto, LoginUser loginUser) {

if (clientUserMapper.judgeCreateByUserName(createClientTenantDto.getUsername()) > 0) {
return JsonResult.error("该用户名称已存在!");
}
//1.开始判断租户对应需要的平台并进行匹对


UserPo userPo = new UserPo()
.setIsTenant(1)
.setUsername(createClientTenantDto.getUsername())
.setPassword("{bcrypt}" + new BCryptPasswordEncoder().encode(createClientTenantDto.getPassword()));
userPo.setCreateUser(loginUser.getUserId());
clientUserMapper.insertClientUser(userPo);
TenantPo tenantPo = new TenantPo()
.setUserId(userPo.getId())
.setRemark(createClientTenantDto.getRemark());
tenantMapper.insertTenant(tenantPo);
List<AuthoritiesPo> authoritiesPos = new ArrayList<>();
List<ClientUserRolePo> clientUserRolePoArrayList = new ArrayList<>();
for (ClientRoleDto clientRoleDto : createClientTenantDto.getClientRoleDtoList()) {
AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())
.setUsername(createClientTenantDto.getUsername())
.setAuthority(clientRoleDto.getClientId());
authoritiesPo.setCreateUser(loginUser.getUserId());
authoritiesPos.add(authoritiesPo);

ClientUserRolePo clientUserRolePo = new ClientUserRolePo()
.setUserId(userPo.getId())
.setClientId(clientRoleDto.getClientId())
.setRoleId(clientRoleDto.getRoleId());
clientUserRolePo.setCreateUser(loginUser.getUserId());
clientUserRolePoArrayList.add(clientUserRolePo);

}
authoritiesMapper.batchInsert(authoritiesPos);
clientUserRoleMapper.batchInsert(clientUserRolePoArrayList);
//todo:调用业务系统完成租户创建
return JsonResult.success(userPo.getId());
}

@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult updateUserPassword(UpdateUserPassDto updateUserPassDto, LoginUser loginUser) {
@@ -213,7 +168,6 @@ public class ClientUserServiceImpl implements ClientUserSevice {
clientUserMapper.updatePass(userPo);
return JsonResult.success(true);
}

@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult updateUserClientRole(UpdateUserClientRoleDto updateUserClientRoleDto, LoginUser loginUser) {
@@ -239,553 +193,4 @@ public class ClientUserServiceImpl implements ClientUserSevice {
}
return JsonResult.success(true);
}

/**
* 查询对应租户下的所有用户数据
*
* @param query
* @return
*/
@Override
public JsonResult findUserList(UserQuery query) {

//分页参数校验
query.checkParam();
//开启分页
IPage<UserPo> page = new Page<>(query.getPage(), query.getLimit());
IPage<UserVo> pageData = clientUserMapper.selectByTenantIdAndPage(query, page);
pageData.convert(x -> {
String platformCode = x.getPlatformCode();
if (platformCode.contains(HhzUrlConstant.HHZ_CLIENT)) {
x.setPlatformName(HhzUrlConstant.HHZ_NAME);
} else if (platformCode.contains(WaterWayConstant.WATERWAY_CLIENT)) {
x.setPlatformName(WaterWayConstant.WATERWAY_NAME);
} else if (platformCode.contains(FreeWayConstant.FREEWAY_CLIENT)) {
x.setPlatformName(FreeWayConstant.FREEWAY_NAME);
} else if (platformCode.contains(PilotConstant.PILOT_CLIENT)) {
x.setPlatformName(PilotConstant.PILOT_NAME);
} else if (platformCode.contains(AirportConstant.AIRPORT_CLIENT)) {
x.setPlatformName(AirportConstant.AIRPORT_NAME);
}
return x;
});
return JsonResult.success(pageData);
}

/**
* 更新 用户/租户对应关系的系统or角色id
*
* @param createClientUserDto
* @param loginUser
* @return
*/
@Override
public JsonResult updateAuthorities(CreateClientUserDto createClientUserDto, LoginUser loginUser) {

UserPo userPo = clientUserMapper.getUserByUserName(createClientUserDto.getUsername());
if (ObjectUtil.isNull(userPo)) {
return JsonResult.error("此用户不存在");
}
List<AuthoritiesPo> authoritiesPos = new ArrayList<>();
List<ClientUserRolePo> clientUserRolePoArrayList = new ArrayList<>();
for (ClientRoleDto clientRoleDto : createClientUserDto.getClientRoleDtoList()) {
AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())
.setUsername(createClientUserDto.getUsername())
.setAuthority(clientRoleDto.getClientId());
authoritiesPo.setCreateUser(loginUser.getUserId());
authoritiesPos.add(authoritiesPo);

ClientUserRolePo clientUserRolePo = new ClientUserRolePo()
.setUserId(userPo.getId())
.setClientId(clientRoleDto.getClientId())
.setRoleId(clientRoleDto.getRoleId());
clientUserRolePo.setCreateUser(loginUser.getUserId());
clientUserRolePoArrayList.add(clientUserRolePo);

}
authoritiesMapper.batchInsert(authoritiesPos);
clientUserRoleMapper.batchInsert(clientUserRolePoArrayList);

return JsonResult.success();
}

/**
* 查询对应业务平台的所有角色
*
* @param clientId 平台标识
* @param loginUser
* @return
*/
@Override
public JsonResult getRoleList(String clientId, LoginUser loginUser) {

if (ObjectUtil.isEmpty(clientId)) {
return JsonResult.error("clientId不能为空");
}
List<ClientRoleListDto> clientRoleListDtos = new ArrayList<>();

if (clientId.contains(CommonConstant.COMMA)) {
String[] clientIds = clientId.split(CommonConstant.COMMA);
for (String id : clientIds) {
ClientRoleListDto dto = getRoleListByClinetId(id, loginUser);
clientRoleListDtos.add(dto);
}
} else {
ClientRoleListDto dto = getRoleListByClinetId(clientId, loginUser);
clientRoleListDtos.add(dto);
}
return JsonResult.success(clientRoleListDtos);
}

/**
* 访问对应系统获取对应业务平台可关联的角色
*
* @param clientId
* @param loginUser
* @return
*/
private ClientRoleListDto getRoleListByClinetId(String clientId, LoginUser loginUser) {


Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getPlatformCode, clientId)
.eq(Platform::getMark, 1));
if (ObjectUtil.isNull(platform)) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "该业务平台不存在");
}
ClientRoleListDto clientRoleListDto = new ClientRoleListDto();
clientRoleListDto.setPlatformName(platform.getPlatformName());
clientRoleListDto.setClientId(platform.getPlatformCode());

String url = platform.getPlatformUrl();
//根据不同业务平台进行动态匹配 ->并修改对应标识权限
switch (platform.getPlatformCode()) {
//河湖长
case HhzUrlConstant.HHZ_CLIENT:
url = url + HhzUrlConstant.FIND_ROLE;
break;
////机场
//case AirportConstant.AIRPORT_CLIENT:
// url = url + AirportConstant.CREATE_TENANT;
// break;
////高速
//case FreeWayConstant.FREEWAY_CLIENT:
// url = url + FreeWayConstant.CREATE_TENANT;
// dto.getClientRoleDto().setClientId(FreeWayConstant.FREEWAY_CLIENT_ADMIN + CommonConstant.COMMA + FreeWayConstant.FREEWAY_CLIENT_MP);
// break;
////航道
//case WaterWayConstant.WATERWAY_CLIENT:
// url = url + WaterWayConstant.CREATE_TENANT;
// dto.getClientRoleDto().setClientId(WaterWayConstant.WATERWAY_CLIENT_ADMIN + CommonConstant.COMMA + WaterWayConstant.WATERWAY_CLIENT_MP);
// break;
////飞手
//case PilotConstant.PILOT_CLIENT:
// url = url + PilotConstant.CREATE_TENANT;
// dto.getClientRoleDto().setClientId(PilotConstant.PILOT_CLIENT + CommonConstant.COMMA + PilotConstant.PILOT_CLIENT_MP);
// break;
default:
break;
}
ParameterizedTypeReference<JsonResult<List<RoleDto>>> parameterizedTypeReference =
new ParameterizedTypeReference<JsonResult<List<RoleDto>>>() {
};
ResponseEntity<JsonResult<List<RoleDto>>> response;
org.springframework.http.HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("Authorization", "Bearer " + loginUser.getThToken());
HttpEntity httpEntity = new HttpEntity(resultRequestHeader);
try {
log.info("url:{}", url);
response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, parameterizedTypeReference);
} catch (Exception e) {
log.error("对应平台标识:{}", platform.getPlatformName());
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "获取角色列表失败!");
}
if (response == null || !response.hasBody() || response.getBody().getCode() != JsonResult.SUCCESS) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "获取角色列表响应失败!");
}
List<RoleDto> roleDtos = response.getBody().getData();

clientRoleListDto.setRoleDtoList(roleDtos);

return clientRoleListDto;

}

/**
* 新增业务平台的租户基本数据
*
* @param dto
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult addTenant(OidcTenantDto dto, LoginUser loginUser) {

if (clientUserMapper.judgeCreateByUserName(dto.getUsername()) > 0) {
return JsonResult.error("该租户账号已存在!");
}

if (ObjectUtil.isEmpty(dto.getTenantCode())) {
return JsonResult.error("租户code不能为空");
}
TTenant tTenant = tenantMapper.getByCode(dto.getTenantCode());
if (ObjectUtil.isNotNull(tTenant)) {
return JsonResult.error("该租户code已存在,请重新输入");
}
if (dto.getClientRoleDto().getClientId().contains(CommonConstant.COMMA)) {
String[] codes = dto.getClientRoleDto().getClientId().split(CommonConstant.COMMA);
for (String code : codes) {
JsonResult result = getResult(dto, code, loginUser);
if (result.getCode() != JsonResult.SUCCESS) {
return result;
}
}
} else {
JsonResult result = getResult(dto, dto.getClientRoleDto().getClientId(), loginUser);
if (result.getCode() != JsonResult.SUCCESS) {
return result;
}
}
return JsonResult.success();
}

/**
* 查询租户以及该租户对应绑定的系统等
*
* @return
*/
@Override
public JsonResult findTenants(TenantQuery query) {
//分页参数校验
query.checkParam();
//开启分页
IPage<TenantVo> page = new Page<>(query.getPage(), query.getLimit());
IPage<TenantPo> pageData = tenantMapper.findList(page, query);
pageData.convert(x -> {
TenantVo vo = Convert.convert(TenantVo.class, x);
vo.setTenantId(x.getUserId());
vo.setTenantCode(x.getCode());
vo.setTenantName(x.getName());
Long userId = x.getUserId();
List<AuthoritiesPo> poList = authoritiesMapper.selectByUserId(userId);
List<BusinessSystemVo> businessSystemVoList = new ArrayList<>();
for (AuthoritiesPo authoritiesPo : poList) {
//TODO 后期维护各个业务平台
if (authoritiesPo.getAuthority().contains("hhz")) {
businessSystemVoList.add(getbusinessSystemVo(HhzUrlConstant.HHZ_CLIENT, HhzUrlConstant.HHZ_NAME));
} else if (authoritiesPo.getAuthority().contains("airport")) {
businessSystemVoList.add(getbusinessSystemVo(AirportConstant.AIRPORT_CLIENT, AirportConstant.AIRPORT_NAME));
} else if (authoritiesPo.getAuthority().contains("waterway")) {
businessSystemVoList.add(getbusinessSystemVo(WaterWayConstant.WATERWAY_CLIENT, WaterWayConstant.WATERWAY_NAME));
} else if (authoritiesPo.getAuthority().contains("freeway")) {
businessSystemVoList.add(getbusinessSystemVo(FreeWayConstant.FREEWAY_CLIENT, FreeWayConstant.FREEWAY_NAME));
} else if (authoritiesPo.getAuthority().contains("pilot")) {
businessSystemVoList.add(getbusinessSystemVo(PilotConstant.PILOT_CLIENT, PilotConstant.PILOT_NAME));
}
}
businessSystemVoList = businessSystemVoList.stream().distinct().collect(Collectors.toList());
vo.setList(businessSystemVoList);
return vo;
});
return JsonResult.success(pageData);
}

/**
* 封装返回展示类
*
* @param client
* @param name
* @return
*/
private BusinessSystemVo getbusinessSystemVo(String client, String name) {
BusinessSystemVo businessSystemVo = new BusinessSystemVo();
businessSystemVo.setClientId(client);
businessSystemVo.setName(name);
return businessSystemVo;
}

/**
* 删除租户(逻辑删除)
*
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult deleteTenant(OidcTenantDto dto, LoginUser loginUser) {

TenantPo tenantPo = tenantMapper.selectById(dto.getId());
List<UserPo> list = clientUserMapper.selectByTenantId(tenantPo.getUserId());
if (ObjectUtil.isNotEmpty(list)) {
return JsonResult.error("该租户下含有关联用户,不能进行删除");
}
tenantPo.setEnabled(0);
tenantMapper.updateById(tenantPo);
UserPo userPo = clientUserMapper.selectByUserId(tenantPo.getUserId());
userPo.setEnabled(0);
clientUserMapper.updatePass(userPo);
if (dto.getClientRoleDto().getClientId().contains(CommonConstant.COMMA)) {
String[] codes = dto.getClientRoleDto().getClientId().split(CommonConstant.COMMA);
for (String code : codes) {
JsonResult jsonResult = deleteResult(dto, code, loginUser);
if (jsonResult.getCode() != JsonResult.SUCCESS) {
return jsonResult;
}
}
} else {
JsonResult jsonResult = deleteResult(dto, dto.getClientRoleDto().getClientId(), loginUser);
if (jsonResult.getCode() != JsonResult.SUCCESS) {
return jsonResult;
}
}
return JsonResult.success();
}

/**
* 更新业务平台租户的相关基本信息
*
* @param dto
* @param loginUser
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult editTenant(OidcTenantDto dto, LoginUser loginUser) {

if (ObjectUtil.isEmpty(dto.getTenantCode())) {
return JsonResult.error("租户code不能为空");
}
if (dto.getClientRoleDto().getClientId().contains(CommonConstant.COMMA)) {
String[] codes = dto.getClientRoleDto().getClientId().split(CommonConstant.COMMA);
for (String code : codes) {
JsonResult jsonResult = editResult(dto, code, loginUser);
if (jsonResult.getCode() != JsonResult.SUCCESS) {
return jsonResult;
}
}
} else {
JsonResult jsonResult = editResult(dto, dto.getClientRoleDto().getClientId(), loginUser);
if (jsonResult.getCode() != JsonResult.SUCCESS) {
return jsonResult;
}
}
TTenant tTenant = tenantMapper.getByCode(dto.getTenantCode());
dto.setId(tTenant.getId());

TenantPo tenantPo = new TenantPo();
tenantPo.setName(dto.getTenantName())
.setId(dto.getId())
.setCityCode(dto.getCityCode())
.setCityName(dto.getCityName())
.setProvinceName(dto.getProvinceName())
.setProvinceCode(dto.getProvinceCode())
.setDistrictCode(dto.getDistrictCode())
.setDistrictName(dto.getDistrictName());
tenantMapper.updateById(tenantPo);
//更新密码
if (ObjectUtil.isNotEmpty(dto.getPassword())) {
UserPo userPo = new UserPo();
userPo.setUsername(dto.getUsername());
dto.setPassword("{bcrypt}" + new BCryptPasswordEncoder().encode(dto.getPassword()));
userPo.setPassword(dto.getPassword());
clientUserMapper.updatePass(userPo);
}
return JsonResult.success();
}

/**
* 新增请求
*
* @param dto
* @param code
* @param loginUser
* @return
*/
private JsonResult getResult(OidcTenantDto dto, String code, LoginUser loginUser) {
Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getPlatformCode, code)
.eq(Platform::getMark, 1));
if (ObjectUtil.isNull(platform)) {
return JsonResult.error("该业务平台不存在");
}
//设置请求头
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("Authorization", "Bearer " + loginUser.getThToken());
//设置地址
String url = platform.getPlatformUrl();
//根据不同业务平台进行动态匹配 ->并修改对应标识权限
switch (platform.getPlatformCode()) {
//河湖长
case HhzUrlConstant.HHZ_CLIENT:
url = url + HhzUrlConstant.CREATE_TENANT;
dto.getClientRoleDto().setClientId(HhzUrlConstant.HHZ_CLIENT_ADMIN);
break;
//机场
case AirportConstant.AIRPORT_CLIENT:
url = url + AirportConstant.CREATE_TENANT;
break;
//高速
case FreeWayConstant.FREEWAY_CLIENT:
url = url + FreeWayConstant.CREATE_TENANT;
dto.getClientRoleDto().setClientId(FreeWayConstant.FREEWAY_CLIENT_ADMIN + CommonConstant.COMMA + FreeWayConstant.FREEWAY_CLIENT_MP);
break;
//航道
case WaterWayConstant.WATERWAY_CLIENT:
url = url + WaterWayConstant.CREATE_TENANT;
dto.getClientRoleDto().setClientId(WaterWayConstant.WATERWAY_CLIENT_ADMIN + CommonConstant.COMMA + WaterWayConstant.WATERWAY_CLIENT_MP);
break;
//飞手
case PilotConstant.PILOT_CLIENT:
url = url + PilotConstant.CREATE_TENANT;
dto.getClientRoleDto().setClientId(PilotConstant.PILOT_CLIENT + CommonConstant.COMMA + PilotConstant.PILOT_CLIENT_MP);
break;
default:
break;
}
HttpEntity httpEntity = new HttpEntity(dto, resultRequestHeader);
log.info("请求url:{}", url);
ResponseEntity<JsonResult> response;
try {
response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
} catch (Exception e) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台新增租户失败");
}
if (null == response || !response.hasBody()) {
log.error("业务平台新增租户响应失败");
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台新增租户失败");
}
if (response.getBody().getCode() != JsonResult.SUCCESS) {
log.error("业务平台新增租户响应失败,数据来源:" + platform.getPlatformName());
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), response.getBody().getMsg());
}
return JsonResult.success();
}

/**
* 更新请求
*
* @param dto
* @param code
* @param loginUser
* @return
*/
private JsonResult editResult(OidcTenantDto dto, String code, LoginUser loginUser) {
Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getPlatformCode, code)
.eq(Platform::getMark, 1));
if (ObjectUtil.isNull(platform)) {
return JsonResult.error("该业务平台不存在");
}
//设置请求头
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("Authorization", "Bearer " + loginUser.getThToken());
HttpEntity httpEntity = new HttpEntity(dto, resultRequestHeader);
//设置地址(hhz平台)
String url = platform.getPlatformUrl();
//根据不同业务平台进行动态匹配
switch (platform.getPlatformCode()) {
//河湖长
case HhzUrlConstant.HHZ_CLIENT:
url = url + HhzUrlConstant.UPDATE_TENANT;
dto.getClientRoleDto().setClientId(HhzUrlConstant.HHZ_CLIENT_ADMIN);
break;
//机场
case AirportConstant.AIRPORT_CLIENT:
url = url + AirportConstant.EDIT_TENANT;
break;
case FreeWayConstant.FREEWAY_CLIENT:
url = url + FreeWayConstant.UPDATE_TENANT;
dto.getClientRoleDto().setClientId(FreeWayConstant.FREEWAY_CLIENT_ADMIN + CommonConstant.COMMA + FreeWayConstant.FREEWAY_CLIENT_MP);
break;
case WaterWayConstant.WATERWAY_CLIENT:
url = url + WaterWayConstant.UPDATE_TENANT;
dto.getClientRoleDto().setClientId(WaterWayConstant.WATERWAY_CLIENT_ADMIN + CommonConstant.COMMA + WaterWayConstant.WATERWAY_CLIENT_MP);
break;
//飞手
case PilotConstant.PILOT_CLIENT:
url = url + PilotConstant.UPDATE_TENANT;
dto.getClientRoleDto().setClientId(PilotConstant.PILOT_CLIENT + CommonConstant.COMMA + PilotConstant.PILOT_CLIENT_MP);
break;
default:
break;
}
ResponseEntity<JsonResult> response;
try {
log.info("请求url:{}", url);
response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
} catch (Exception e) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台更新租户失败");
}
if (null == response || !response.hasBody()) {
log.error("业务平台更新租户响应失败");
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台更新租户失败");
}
if (response.getBody().getCode() != JsonResult.SUCCESS) {
log.error("业务平台更新租户响应失败,数据来源:" + platform.getPlatformName());
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), response.getBody().getMsg());
}
return JsonResult.success();
}

/**
* 删除请求
*
* @param dto
* @param code
* @param loginUser
* @return
*/
private JsonResult deleteResult(OidcTenantDto dto, String code, LoginUser loginUser) {
Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getPlatformCode, code)
.eq(Platform::getMark, 1));
if (ObjectUtil.isNull(platform)) {
return JsonResult.error("该业务平台不存在");
}
//设置请求头
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("Authorization", "Bearer " + loginUser.getThToken());
HttpEntity httpEntity = new HttpEntity(dto, resultRequestHeader);
//设置地址(hhz平台)
String url = platform.getPlatformUrl();
//根据不同业务平台进行动态匹配
switch (platform.getPlatformCode()) {
//河湖长
case HhzUrlConstant.HHZ_CLIENT:
url = url + HhzUrlConstant.DELETE_TENANT;
break;
//机场
case AirportConstant.AIRPORT_CLIENT:
url = url + AirportConstant.DELETE_TENANT + CommonConstant.SLASH + dto.getTenantCode();
break;
case FreeWayConstant.FREEWAY_CLIENT:
url = url + FreeWayConstant.DELETE_TENANT;
break;
case WaterWayConstant.WATERWAY_CLIENT:
url = url + WaterWayConstant.DELETE_TENANT;
break;
//飞手
case PilotConstant.PILOT_CLIENT:
url = url + PilotConstant.DELETE_TENANT;
break;
default:
break;
}
log.info("请求url:{}", url);
ResponseEntity<JsonResult> response;
try {
response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
} catch (Exception e) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台删除租户失败");
}
if (null == response || !response.hasBody()) {
log.error("业务平台删除租户响应失败");
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台删除租户失败");
}
if (response.getBody().getCode() != JsonResult.SUCCESS) {
log.error("业务平台删除租户响应失败,数据来源:" + platform.getPlatformName());
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), response.getBody().getMsg());
}
return JsonResult.success();
}
}

+ 679
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/TenantServiceImpl.java Vedi File

@@ -0,0 +1,679 @@
package com.tuoheng.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.common.ServiceException;
import com.tuoheng.constant.*;
import com.tuoheng.mapper.*;
import com.tuoheng.model.dto.*;
import com.tuoheng.model.param.*;
import com.tuoheng.model.po.AuthoritiesPo;
import com.tuoheng.model.po.ClientUserRolePo;
import com.tuoheng.model.po.TenantPo;
import com.tuoheng.model.po.UserPo;
import com.tuoheng.model.query.TenantQuery;
import com.tuoheng.model.query.UserQuery;
import com.tuoheng.model.vo.BusinessSystemVo;
import com.tuoheng.model.vo.TenantVo;
import com.tuoheng.model.vo.UserVo;
import com.tuoheng.service.TenantService;
import com.tuoheng.until.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @Author xiaoying
* @Date 2023/3/6 11:26
*/
@Service
@Slf4j
public class TenantServiceImpl implements TenantService {

@Autowired
private ClientUserMapper clientUserMapper;

@Autowired
private TenantMapper tenantMapper;

@Autowired
private AuthoritiesMapper authoritiesMapper;

@Autowired
private ClientUserRoleMapper clientUserRoleMapper;

@Autowired
private RestTemplate restTemplate;

@Autowired
private PlatformMapper platformMapper;
@Autowired
private Oauth2RegisteredClientMapper oauth2RegisteredClientMapper;


@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult createClientTenant(CreateClientTenantDto createClientTenantDto, LoginUser loginUser) {

if (clientUserMapper.judgeCreateByUserName(createClientTenantDto.getUsername()) > 0) {
return JsonResult.error("该用户名称已存在!");
}
UserPo userPo = new UserPo()
.setIsTenant(1)
.setUsername(createClientTenantDto.getUsername())
.setPassword("{bcrypt}" + new BCryptPasswordEncoder().encode(createClientTenantDto.getPassword()));
userPo.setCreateUser(loginUser.getUserId());
clientUserMapper.insertClientUser(userPo);
TenantPo tenantPo = new TenantPo();
BeanUtils.copyProperties(createClientTenantDto, tenantPo);
tenantPo.setName(createClientTenantDto.getTenantName())
.setCode(createClientTenantDto.getTenantCode())
.setUserId(userPo.getId());
tenantMapper.insertTenant(tenantPo);
//1.开始判断租户对应需要的平台并进行匹对
List<ClientRoleDto> clientRoleDtoList = getClientRoleDtos(createClientTenantDto);
List<AuthoritiesPo> authoritiesPos = new ArrayList<>();
List<ClientUserRolePo> clientUserRolePoArrayList = new ArrayList<>();
//添加租户对应相关信息
for (ClientRoleDto clientRoleDto : clientRoleDtoList) {
AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())
.setUsername(createClientTenantDto.getUsername())
.setAuthority(clientRoleDto.getClientId());
authoritiesPo.setCreateUser(loginUser.getUserId());
authoritiesPos.add(authoritiesPo);

ClientUserRolePo clientUserRolePo = new ClientUserRolePo()
.setUserId(userPo.getId())
.setClientId(clientRoleDto.getClientId())
.setRoleId(clientRoleDto.getRoleId());
clientUserRolePo.setCreateUser(loginUser.getUserId());
clientUserRolePoArrayList.add(clientUserRolePo);

}
authoritiesMapper.batchInsert(authoritiesPos);
clientUserRoleMapper.batchInsert(clientUserRolePoArrayList);
//todo:调用业务系统完成租户创建
List<ClientRoleDto> list = createClientTenantDto.getClientRoleDtoList();
for (ClientRoleDto clientRoleDto : list) {
log.info("参数:{}",clientRoleDto.toString());
JsonResult result = getResult(createClientTenantDto, clientRoleDto.getClientId(),loginUser);
if (JsonResult.SUCCESS != result.getCode()) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), result.getMsg());
}
}

return JsonResult.success(userPo.getId());
}

/**
* 匹配对应业务系统对应的标识,并封装返回方便后续操作
*
* @param createClientTenantDto
* @return
*/
private List<ClientRoleDto> getClientRoleDtos(CreateClientTenantDto createClientTenantDto) {
List<ClientRoleDto> clientRoleDtoList = new ArrayList<>();
//遍历获取参数进项封装
ClientRoleDto clientRoleDto = null;
ClientRoleDto clientRoleDto2 = null;
for (ClientRoleDto dto : createClientTenantDto.getClientRoleDtoList()) {

switch (dto.getClientId()) {
//暂时河湖长,动态匹配其他平台
case HhzUrlConstant.HHZ_CLIENT:
clientRoleDto = new ClientRoleDto();
clientRoleDto2 = new ClientRoleDto();
clientRoleDto.setClientId(HhzUrlConstant.HHZ_CLIENT_MP);
clientRoleDto2.setClientId(HhzUrlConstant.HHZ_CLIENT_ADMIN);
clientRoleDto.setRoleId(dto.getRoleId());
clientRoleDto2.setRoleId(dto.getRoleId());
clientRoleDtoList.add(clientRoleDto);
clientRoleDtoList.add(clientRoleDto2);
break;
////暂时河湖长,动态匹配其他平台
//case FreeWayConstant.HHZ_CLIENT:
// dto.setClientId(HhzUrlConstant.HHZ_CLIENT_ADMIN);
// clientRoleDtoList.add(dto);
// clientRoleDto.setClientId(HhzUrlConstant.HHZ_CLIENT_MP);
// clientRoleDto.setRoleId(dto.getRoleId());
// clientRoleDtoList.add(clientRoleDto);
// break;
////暂时河湖长,动态匹配其他平台
//case HhzUrlConstant.HHZ_CLIENT:
// dto.setClientId(HhzUrlConstant.HHZ_CLIENT_ADMIN);
// clientRoleDtoList.add(dto);
// clientRoleDto.setClientId(HhzUrlConstant.HHZ_CLIENT_MP);
// clientRoleDto.setRoleId(dto.getRoleId());
// clientRoleDtoList.add(clientRoleDto);
// break;
default:
break;
}

}
return clientRoleDtoList;
}
/**
* 查询对应租户下的所有用户数据
*
* @param query
* @return
*/
@Override
public JsonResult findUserList(UserQuery query) {

//分页参数校验
query.checkParam();
//开启分页
IPage<UserPo> page = new Page<>(query.getPage(), query.getLimit());
IPage<UserVo> pageData = clientUserMapper.selectByTenantIdAndPage(query, page);
pageData.convert(x -> {
String platformCode = x.getPlatformCode();
if (platformCode.contains(HhzUrlConstant.HHZ_CLIENT)) {
x.setPlatformName(HhzUrlConstant.HHZ_NAME);
} else if (platformCode.contains(WaterWayConstant.WATERWAY_CLIENT)) {
x.setPlatformName(WaterWayConstant.WATERWAY_NAME);
} else if (platformCode.contains(FreeWayConstant.FREEWAY_CLIENT)) {
x.setPlatformName(FreeWayConstant.FREEWAY_NAME);
} else if (platformCode.contains(PilotConstant.PILOT_CLIENT)) {
x.setPlatformName(PilotConstant.PILOT_NAME);
} else if (platformCode.contains(AirportConstant.AIRPORT_CLIENT)) {
x.setPlatformName(AirportConstant.AIRPORT_NAME);
}
return x;
});
return JsonResult.success(pageData);
}

/**
* 更新 用户/租户对应关系的系统or角色id
*
* @param createClientUserDto
* @param loginUser
* @return
*/
@Override
public JsonResult updateAuthorities(CreateClientUserDto createClientUserDto, LoginUser loginUser) {

UserPo userPo = clientUserMapper.getUserByUserName(createClientUserDto.getUsername());
if (ObjectUtil.isNull(userPo)) {
return JsonResult.error("此用户不存在");
}
List<AuthoritiesPo> authoritiesPos = new ArrayList<>();
List<ClientUserRolePo> clientUserRolePoArrayList = new ArrayList<>();
for (ClientRoleDto clientRoleDto : createClientUserDto.getClientRoleDtoList()) {
AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())
.setUsername(createClientUserDto.getUsername())
.setAuthority(clientRoleDto.getClientId());
authoritiesPo.setCreateUser(loginUser.getUserId());
authoritiesPos.add(authoritiesPo);

ClientUserRolePo clientUserRolePo = new ClientUserRolePo()
.setUserId(userPo.getId())
.setClientId(clientRoleDto.getClientId())
.setRoleId(clientRoleDto.getRoleId());
clientUserRolePo.setCreateUser(loginUser.getUserId());
clientUserRolePoArrayList.add(clientUserRolePo);

}
authoritiesMapper.batchInsert(authoritiesPos);
clientUserRoleMapper.batchInsert(clientUserRolePoArrayList);

return JsonResult.success();
}

/**
* 查询对应业务平台的所有角色
*
* @param clientId 平台标识
* @param loginUser
* @return
*/
@Override
public JsonResult getRoleList(String clientId, LoginUser loginUser) {

if (ObjectUtil.isEmpty(clientId)) {
return JsonResult.error("clientId不能为空");
}
List<ClientRoleListDto> clientRoleListDtos = new ArrayList<>();

if (clientId.contains(CommonConstant.COMMA)) {
String[] clientIds = clientId.split(CommonConstant.COMMA);
for (String id : clientIds) {
ClientRoleListDto dto = getRoleListByClinetId(id, loginUser);
clientRoleListDtos.add(dto);
}
} else {
ClientRoleListDto dto = getRoleListByClinetId(clientId, loginUser);
clientRoleListDtos.add(dto);
}
return JsonResult.success(clientRoleListDtos);
}

/**
* 访问对应系统获取对应业务平台可关联的角色
*
* @param clientId
* @param loginUser
* @return
*/
private ClientRoleListDto getRoleListByClinetId(String clientId, LoginUser loginUser) {


Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getPlatformCode, clientId)
.eq(Platform::getMark, 1));
if (ObjectUtil.isNull(platform)) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "该业务平台不存在");
}
ClientRoleListDto clientRoleListDto = new ClientRoleListDto();
clientRoleListDto.setPlatformName(platform.getPlatformName());
clientRoleListDto.setClientId(platform.getPlatformCode());

String url = platform.getPlatformUrl();
//根据不同业务平台进行动态匹配 ->并修改对应标识权限
switch (platform.getPlatformCode()) {
//河湖长
case HhzUrlConstant.HHZ_CLIENT:
url = url + HhzUrlConstant.FIND_ROLE;
break;
////机场
//case AirportConstant.AIRPORT_CLIENT:
// url = url + AirportConstant.CREATE_TENANT;
// break;
////高速
//case FreeWayConstant.FREEWAY_CLIENT:
// url = url + FreeWayConstant.CREATE_TENANT;
// dto.getClientRoleDto().setClientId(FreeWayConstant.FREEWAY_CLIENT_ADMIN + CommonConstant.COMMA + FreeWayConstant.FREEWAY_CLIENT_MP);
// break;
////航道
//case WaterWayConstant.WATERWAY_CLIENT:
// url = url + WaterWayConstant.CREATE_TENANT;
// dto.getClientRoleDto().setClientId(WaterWayConstant.WATERWAY_CLIENT_ADMIN + CommonConstant.COMMA + WaterWayConstant.WATERWAY_CLIENT_MP);
// break;
////飞手
//case PilotConstant.PILOT_CLIENT:
// url = url + PilotConstant.CREATE_TENANT;
// dto.getClientRoleDto().setClientId(PilotConstant.PILOT_CLIENT + CommonConstant.COMMA + PilotConstant.PILOT_CLIENT_MP);
// break;
default:
break;
}
ParameterizedTypeReference<JsonResult<List<RoleDto>>> parameterizedTypeReference =
new ParameterizedTypeReference<JsonResult<List<RoleDto>>>() {
};
ResponseEntity<JsonResult<List<RoleDto>>> response;
org.springframework.http.HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("Authorization", "Bearer " + loginUser.getThToken());
HttpEntity httpEntity = new HttpEntity(resultRequestHeader);
try {
log.info("url:{}", url);
response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, parameterizedTypeReference);
} catch (Exception e) {
log.error("对应平台标识:{}", platform.getPlatformName());
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "获取角色列表失败!");
}
if (response == null || !response.hasBody() || response.getBody().getCode() != JsonResult.SUCCESS) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "获取角色列表响应失败!");
}
List<RoleDto> roleDtos = response.getBody().getData();

clientRoleListDto.setRoleDtoList(roleDtos);

return clientRoleListDto;

}
/**
* 查询租户以及该租户对应绑定的系统等
*
* @return
*/
@Override
public JsonResult findTenants(TenantQuery query) {
//分页参数校验
query.checkParam();
//开启分页
IPage<TenantVo> page = new Page<>(query.getPage(), query.getLimit());
IPage<TenantPo> pageData = tenantMapper.findList(page, query);
pageData.convert(x -> {
TenantVo vo = Convert.convert(TenantVo.class, x);
vo.setTenantId(x.getUserId());
vo.setTenantCode(x.getCode());
vo.setTenantName(x.getName());
Long userId = x.getUserId();
List<AuthoritiesPo> poList = authoritiesMapper.selectByUserId(userId);
List<BusinessSystemVo> businessSystemVoList = new ArrayList<>();
for (AuthoritiesPo authoritiesPo : poList) {
//TODO 后期维护各个业务平台
if (authoritiesPo.getAuthority().contains("hhz")) {
businessSystemVoList.add(getbusinessSystemVo(HhzUrlConstant.HHZ_CLIENT, HhzUrlConstant.HHZ_NAME));
} else if (authoritiesPo.getAuthority().contains("airport")) {
businessSystemVoList.add(getbusinessSystemVo(AirportConstant.AIRPORT_CLIENT, AirportConstant.AIRPORT_NAME));
} else if (authoritiesPo.getAuthority().contains("waterway")) {
businessSystemVoList.add(getbusinessSystemVo(WaterWayConstant.WATERWAY_CLIENT, WaterWayConstant.WATERWAY_NAME));
} else if (authoritiesPo.getAuthority().contains("freeway")) {
businessSystemVoList.add(getbusinessSystemVo(FreeWayConstant.FREEWAY_CLIENT, FreeWayConstant.FREEWAY_NAME));
} else if (authoritiesPo.getAuthority().contains("pilot")) {
businessSystemVoList.add(getbusinessSystemVo(PilotConstant.PILOT_CLIENT, PilotConstant.PILOT_NAME));
}
}
businessSystemVoList = businessSystemVoList.stream().distinct().collect(Collectors.toList());
vo.setList(businessSystemVoList);
return vo;
});
return JsonResult.success(pageData);
}

/**
* 封装返回展示类
*
* @param client
* @param name
* @return
*/
private BusinessSystemVo getbusinessSystemVo(String client, String name) {
BusinessSystemVo businessSystemVo = new BusinessSystemVo();
businessSystemVo.setClientId(client);
businessSystemVo.setName(name);
return businessSystemVo;
}

/**
* 删除租户(逻辑删除)
*
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult deleteTenant(OidcTenantDto dto, LoginUser loginUser) {
//
//TenantPo tenantPo = tenantMapper.selectById(dto.getId());
//List<UserPo> list = clientUserMapper.selectByTenantId(tenantPo.getUserId());
//if (ObjectUtil.isNotEmpty(list)) {
// return JsonResult.error("该租户下含有关联用户,不能进行删除");
//}
//tenantPo.setEnabled(0);
//tenantMapper.updateById(tenantPo);
//UserPo userPo = clientUserMapper.selectByUserId(tenantPo.getUserId());
//userPo.setEnabled(0);
//clientUserMapper.updatePass(userPo);
//if (dto.getClientRoleDto().getClientId().contains(CommonConstant.COMMA)) {
// String[] codes = dto.getClientRoleDto().getClientId().split(CommonConstant.COMMA);
// for (String code : codes) {
// JsonResult jsonResult = deleteResult(dto, code, loginUser);
// if (jsonResult.getCode() != JsonResult.SUCCESS) {
// return jsonResult;
// }
// }
//} else {
// JsonResult jsonResult = deleteResult(dto, dto.getClientRoleDto().getClientId(), loginUser);
// if (jsonResult.getCode() != JsonResult.SUCCESS) {
// return jsonResult;
// }
//}
return JsonResult.success();
}

/**
* 更新业务平台租户的相关基本信息
*
* @param dto
* @param loginUser
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult editTenant(OidcTenantDto dto, LoginUser loginUser) {
//
//if (ObjectUtil.isEmpty(dto.getTenantCode())) {
// return JsonResult.error("租户code不能为空");
//}
//if (dto.getClientRoleDto().getClientId().contains(CommonConstant.COMMA)) {
// String[] codes = dto.getClientRoleDto().getClientId().split(CommonConstant.COMMA);
// for (String code : codes) {
// JsonResult jsonResult = editResult(dto, code, loginUser);
// if (jsonResult.getCode() != JsonResult.SUCCESS) {
// return jsonResult;
// }
// }
//} else {
// JsonResult jsonResult = editResult(dto, dto.getClientRoleDto().getClientId(), loginUser);
// if (jsonResult.getCode() != JsonResult.SUCCESS) {
// return jsonResult;
// }
//}
//TTenant tTenant = tenantMapper.getByCode(dto.getTenantCode());
//dto.setId(tTenant.getId());
//
//TenantPo tenantPo = new TenantPo();
//tenantPo.setName(dto.getTenantName())
// .setId(dto.getId())
// .setCityCode(dto.getCityCode())
// .setCityName(dto.getCityName())
// .setProvinceName(dto.getProvinceName())
// .setProvinceCode(dto.getProvinceCode())
// .setDistrictCode(dto.getDistrictCode())
// .setDistrictName(dto.getDistrictName());
//tenantMapper.updateById(tenantPo);
////更新密码
//if (ObjectUtil.isNotEmpty(dto.getPassword())) {
// UserPo userPo = new UserPo();
// userPo.setUsername(dto.getUsername());
// dto.setPassword("{bcrypt}" + new BCryptPasswordEncoder().encode(dto.getPassword()));
// userPo.setPassword(dto.getPassword());
// clientUserMapper.updatePass(userPo);
//}
return JsonResult.success();
}

/**
* 新增请求
*
* @param dto
* @param loginUser
* @return
*/
private JsonResult getResult(CreateClientTenantDto dto,String code, LoginUser loginUser) {
Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getPlatformCode,code)
.eq(Platform::getMark, 1));
if (ObjectUtil.isNull(platform)) {
return JsonResult.error("该业务平台不存在");
}
//设置请求头
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("Authorization", "Bearer " + loginUser.getThToken());
//设置地址
String url = platform.getPlatformUrl();
//根据不同业务平台进行动态匹配 ->并修改对应标识权限
switch (platform.getPlatformCode()) {
//河湖长
case HhzUrlConstant.HHZ_CLIENT:
url = url + HhzUrlConstant.CREATE_TENANT;
//dto.setClientId(HhzUrlConstant.HHZ_CLIENT_ADMIN);
break;
//机场
case AirportConstant.AIRPORT_CLIENT:
url = url + AirportConstant.CREATE_TENANT;
break;
//高速
case FreeWayConstant.FREEWAY_CLIENT:
url = url + FreeWayConstant.CREATE_TENANT;
//dto.setClientId(FreeWayConstant.FREEWAY_CLIENT_ADMIN + CommonConstant.COMMA + FreeWayConstant.FREEWAY_CLIENT_MP);
break;
//航道
case WaterWayConstant.WATERWAY_CLIENT:
url = url + WaterWayConstant.CREATE_TENANT;
//dto.setClientId(WaterWayConstant.WATERWAY_CLIENT_ADMIN + CommonConstant.COMMA + WaterWayConstant.WATERWAY_CLIENT_MP);
break;
//飞手
case PilotConstant.PILOT_CLIENT:
url = url + PilotConstant.CREATE_TENANT;
//dto.setClientId(PilotConstant.PILOT_CLIENT + CommonConstant.COMMA + PilotConstant.PILOT_CLIENT_MP);
break;
default:
break;
}
HttpEntity httpEntity = new HttpEntity(dto, resultRequestHeader);
log.info("请求url:{}", url);
ResponseEntity<JsonResult> response;
try {
response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
} catch (Exception e) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台新增租户失败");
}
if (null == response || !response.hasBody()) {
log.error("业务平台新增租户响应失败");
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台新增租户失败");
}
if (response.getBody().getCode() != JsonResult.SUCCESS) {
log.error("业务平台新增租户响应失败,数据来源:" + platform.getPlatformName());
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), response.getBody().getMsg());
}
return JsonResult.success();
}

/**
* 更新请求
*
* @param dto
* @param code
* @param loginUser
* @return
*/
private JsonResult editResult(OidcTenantDto dto, String code, LoginUser loginUser) {
//Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
// .eq(Platform::getPlatformCode, code)
// .eq(Platform::getMark, 1));
//if (ObjectUtil.isNull(platform)) {
// return JsonResult.error("该业务平台不存在");
//}
////设置请求头
//HttpHeaders resultRequestHeader = new HttpHeaders();
//resultRequestHeader.add("Authorization", "Bearer " + loginUser.getThToken());
//HttpEntity httpEntity = new HttpEntity(dto, resultRequestHeader);
////设置地址(hhz平台)
//String url = platform.getPlatformUrl();
////根据不同业务平台进行动态匹配
//switch (platform.getPlatformCode()) {
// //河湖长
// case HhzUrlConstant.HHZ_CLIENT:
// url = url + HhzUrlConstant.UPDATE_TENANT;
// dto.getClientRoleDto().setClientId(HhzUrlConstant.HHZ_CLIENT_ADMIN);
// break;
// //机场
// case AirportConstant.AIRPORT_CLIENT:
// url = url + AirportConstant.EDIT_TENANT;
// break;
// case FreeWayConstant.FREEWAY_CLIENT:
// url = url + FreeWayConstant.UPDATE_TENANT;
// dto.getClientRoleDto().setClientId(FreeWayConstant.FREEWAY_CLIENT_ADMIN + CommonConstant.COMMA + FreeWayConstant.FREEWAY_CLIENT_MP);
// break;
// case WaterWayConstant.WATERWAY_CLIENT:
// url = url + WaterWayConstant.UPDATE_TENANT;
// dto.getClientRoleDto().setClientId(WaterWayConstant.WATERWAY_CLIENT_ADMIN + CommonConstant.COMMA + WaterWayConstant.WATERWAY_CLIENT_MP);
// break;
// //飞手
// case PilotConstant.PILOT_CLIENT:
// url = url + PilotConstant.UPDATE_TENANT;
// dto.getClientRoleDto().setClientId(PilotConstant.PILOT_CLIENT + CommonConstant.COMMA + PilotConstant.PILOT_CLIENT_MP);
// break;
// default:
// break;
//}
//ResponseEntity<JsonResult> response;
//try {
// log.info("请求url:{}", url);
// response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
//} catch (Exception e) {
// throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台更新租户失败");
//}
//if (null == response || !response.hasBody()) {
// log.error("业务平台更新租户响应失败");
// throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台更新租户失败");
//}
//if (response.getBody().getCode() != JsonResult.SUCCESS) {
// log.error("业务平台更新租户响应失败,数据来源:" + platform.getPlatformName());
// throw new ServiceException(HttpStatus.BAD_REQUEST.value(), response.getBody().getMsg());
//}
return JsonResult.success();
}

/**
* 删除请求
*
* @param dto
* @param code
* @param loginUser
* @return
*/
private JsonResult deleteResult(OidcTenantDto dto, String code, LoginUser loginUser) {
Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getPlatformCode, code)
.eq(Platform::getMark, 1));
if (ObjectUtil.isNull(platform)) {
return JsonResult.error("该业务平台不存在");
}
//设置请求头
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("Authorization", "Bearer " + loginUser.getThToken());
HttpEntity httpEntity = new HttpEntity(dto, resultRequestHeader);
//设置地址(hhz平台)
String url = platform.getPlatformUrl();
//根据不同业务平台进行动态匹配
switch (platform.getPlatformCode()) {
//河湖长
case HhzUrlConstant.HHZ_CLIENT:
url = url + HhzUrlConstant.DELETE_TENANT;
break;
//机场
case AirportConstant.AIRPORT_CLIENT:
url = url + AirportConstant.DELETE_TENANT + CommonConstant.SLASH + dto.getTenantCode();
break;
case FreeWayConstant.FREEWAY_CLIENT:
url = url + FreeWayConstant.DELETE_TENANT;
break;
case WaterWayConstant.WATERWAY_CLIENT:
url = url + WaterWayConstant.DELETE_TENANT;
break;
//飞手
case PilotConstant.PILOT_CLIENT:
url = url + PilotConstant.DELETE_TENANT;
break;
default:
break;
}
log.info("请求url:{}", url);
ResponseEntity<JsonResult> response;
try {
response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
} catch (Exception e) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台删除租户失败");
}
if (null == response || !response.hasBody()) {
log.error("业务平台删除租户响应失败");
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台删除租户失败");
}
if (response.getBody().getCode() != JsonResult.SUCCESS) {
log.error("业务平台删除租户响应失败,数据来源:" + platform.getPlatformName());
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), response.getBody().getMsg());
}
return JsonResult.success();
}

}

BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/controller/TenantController.class Vedi File


BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/controller/UserController.class Vedi File


BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/model/dto/OidcTenantDto.class Vedi File


BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/model/param/CreateClientTenantDto.class Vedi File


BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/service/ClientUserSevice.class Vedi File


BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/service/impl/ClientUserServiceImpl.class Vedi File


Loading…
Annulla
Salva