Browse Source

完成租户更新实现

tags/v2.4.1
xiaoying 1 year ago
parent
commit
14afd6bd52
8 changed files with 105 additions and 170 deletions
  1. +17
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/config/WebConfig.java
  2. +0
    -27
      tuoheng_oidc_admin/src/main/java/com/tuoheng/config/WebMVCConfig.java
  3. +2
    -2
      tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/TenantController.java
  4. +4
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/model/param/CreateClientTenantDto.java
  5. +2
    -2
      tuoheng_oidc_admin/src/main/java/com/tuoheng/service/TenantService.java
  6. +74
    -138
      tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/TenantServiceImpl.java
  7. +6
    -1
      tuoheng_oidc_admin/src/main/resources/mapper/TenantMapper.xml
  8. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/config/WebConfig.class

+ 17
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/config/WebConfig.java View File

@@ -1,6 +1,8 @@
package com.tuoheng.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@@ -23,4 +25,19 @@ public class WebConfig implements WebMvcConfigurer {
argumentResolvers.add(loginUserHandler);
}

/**
* 扩展mvc框架的消息转换器
*
* @param converters 转换器列表集合
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建消息转换器对象
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//设置自定义的对象转换器
converter.setObjectMapper(new OptimizationObjectMapper());
// 通过设置索引,让自己的转换器放在最前面,否则默认的jackson转换器会在前面,用不上自己配置的转换器
converters.add(0, converter);
}

}

+ 0
- 27
tuoheng_oidc_admin/src/main/java/com/tuoheng/config/WebMVCConfig.java View File

@@ -1,27 +0,0 @@
package com.tuoheng.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
public class WebMVCConfig extends WebMvcConfigurationSupport {
/**
* 扩展mvc框架的消息转换器
*
* @param converters 转换器列表集合
*/
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建消息转换器对象
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//设置自定义的对象转换器
converter.setObjectMapper(new OptimizationObjectMapper());
// 通过设置索引,让自己的转换器放在最前面,否则默认的jackson转换器会在前面,用不上自己配置的转换器
converters.add(0, converter);
}
}

+ 2
- 2
tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/TenantController.java View File

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

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


+ 4
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/model/param/CreateClientTenantDto.java View File

@@ -14,6 +14,10 @@ import java.util.List;
*/
@Data
public class CreateClientTenantDto {
/**
* 租户id
*/
private Long id;

@NotEmpty(message = "username can not be empty!")
private String username;

+ 2
- 2
tuoheng_oidc_admin/src/main/java/com/tuoheng/service/TenantService.java View File

@@ -17,12 +17,12 @@ public interface TenantService {

JsonResult findTenants(TenantQuery query);

JsonResult editTenant(OidcTenantDto dto, LoginUser loginUser);
JsonResult editTenant(CreateClientTenantDto dto, LoginUser loginUser);
/**
* 删除租户(逻辑删除)
* @return
*/
JsonResult deleteTenant(OidcTenantDto dto, LoginUser loginUser);
JsonResult deleteTenant(CreateClientTenantDto dto, LoginUser loginUser);
/**
* 更新 用户/租户对应关系的系统or角色id
* @param createClientUserDto

+ 74
- 138
tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/TenantServiceImpl.java View File

@@ -54,6 +54,8 @@ import java.util.stream.Collectors;
@Slf4j
public class TenantServiceImpl implements TenantService {

//默认密码
private static final String passward = "thjs2023";
@Autowired
private ClientUserMapper clientUserMapper;

@@ -88,7 +90,7 @@ public class TenantServiceImpl implements TenantService {
}
//创建租户创建成功后的返回实体类
CreateTenantVo vo = new CreateTenantVo();
vo.setPassword(createClientTenantDto.getPassword());
vo.setPassword(passward);
vo.setTenantName(createClientTenantDto.getTenantName());
vo.setUsername(createClientTenantDto.getUsername());
//code复用username
@@ -184,7 +186,6 @@ public class TenantServiceImpl implements TenantService {
log.info("异常信息捕获,平台标识:{}", clientRoleDto.getClientId());
log.info("异常信息:{}", e.getMessage());
}

}
log.info("业务系统完成创建");
return JsonResult.success(vo);
@@ -379,10 +380,11 @@ public class TenantServiceImpl implements TenantService {
return JsonResult.error("租户id不能为空");
}
CreateClientTenantDto dto = new CreateClientTenantDto();
dto.setId(query.getId());
//查询出租户的相关信息
Long tenantId = query.getId();
//项目相关信息
TenantItem tenantItem = tenantItemMapper.selectOne(Wrappers.<TenantItem>lambdaQuery().eq(TenantItem::getTenantId,tenantId));
TenantItem tenantItem = tenantItemMapper.selectOne(Wrappers.<TenantItem>lambdaQuery().eq(TenantItem::getTenantId, tenantId));
dto.setTenantItem(tenantItem);
//租户基本信息
TenantPo tenantPo = tenantMapper.selectById(tenantId);
@@ -397,10 +399,10 @@ public class TenantServiceImpl implements TenantService {
ClientRoleDto clientRoleDto = null;
for (ClientUserRolePo clientUserRolePo : clientUserRolePos) {

clientRoleDto =new ClientRoleDto();
clientRoleDto = new ClientRoleDto();
//此处 暂时格式都为 tuoheng-hhz-web 等格式 如后续维护需要变更形式则此处代码需要更改
clientRoleDto.setRoleId(clientUserRolePo.getRoleId());
clientRoleDto.setClientId(clientUserRolePo.getClientId().substring(CommonConstant.ZERO,clientUserRolePo.getClientId().lastIndexOf(CommonConstant.BARS)));
clientRoleDto.setClientId(clientUserRolePo.getClientId().substring(CommonConstant.ZERO, clientUserRolePo.getClientId().lastIndexOf(CommonConstant.BARS)));
TenantEmploy tenantEmploy = tenantEmployMapper.selectOne(Wrappers.<TenantEmploy>lambdaQuery()
.eq(TenantEmploy::getTenantId, tenantId)
.eq(TenantEmploy::getPlatformCode, clientRoleDto.getClientId()));
@@ -436,32 +438,11 @@ public class TenantServiceImpl implements TenantService {
*/
@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;
// }
//}
public JsonResult deleteTenant(CreateClientTenantDto dto, LoginUser loginUser) {
//通知业务系统进行删除
for (ClientRoleDto clientRoleDto : dto.getClientRoleDtoList()) {
deleteResult(dto, clientRoleDto.getClientId(), loginUser);
}
return JsonResult.success();
}

@@ -474,46 +455,67 @@ public class TenantServiceImpl implements TenantService {
*/
@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);
//}
public JsonResult editTenant(CreateClientTenantDto dto, LoginUser loginUser) {

dto.setTenantCode(dto.getUsername());
//默认密码
dto.setPassword(passward);
//符合先删后增逻辑 则直接将原有租户已经对应的关联业务数据清除
JsonResult jsonResult = deleteTenant(dto, loginUser);
if (jsonResult.getCode() != JsonResult.SUCCESS) {
return jsonResult;
}
log.info("租户原始数据删除完毕");
//删除原本租户关联的业务实例相关表
List<TenantEmploy> tenantEmploys = tenantEmployMapper.selectList(Wrappers.<TenantEmploy>lambdaQuery()
.eq(TenantEmploy::getTenantId, dto.getId()));
tenantEmploys.forEach(t -> tenantEmployMapper.deleteById(t));
List<ClientRoleDto> list = dto.getClientRoleDtoList();
//新增租户关联业务服务实例相关
for (ClientRoleDto clientRoleDto : list) {
TenantEmploy tenantEmploy = new TenantEmploy();
tenantEmploy.setDescription(clientRoleDto.getDescription());
tenantEmploy.setTenantId(dto.getId());
tenantEmploy.setServiceId(clientRoleDto.getServiceId());
tenantEmploy.setPlatformCode(clientRoleDto.getClientId());
tenantEmployMapper.insert(tenantEmploy);
}
log.info("租户更新的业务服务实例表更新完毕");
//更新租户原有数据基本
TenantPo tenantPo = new TenantPo();
BeanUtils.copyProperties(dto, tenantPo);
tenantPo.setName(dto.getTenantName())
.setCode(dto.getTenantCode())
.setId(dto.getId());
tenantMapper.updateById(tenantPo);
log.info("租户基本信息更新完毕");
TenantItem tenantItem = dto.getTenantItem();
tenantItemMapper.updateById(tenantItem);
log.info("租户项目相关信息更新完毕");

//todo:调用业务系统完成租户创建
for (ClientRoleDto clientRoleDto : list) {
try {
log.info("参数:{}", clientRoleDto.toString());
JsonResult result = getResult(dto, clientRoleDto.getClientId(), loginUser);
if (JsonResult.SUCCESS != result.getCode()) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), result.getMsg());
}
} catch (Exception e) {
//e.printStackTrace();
//事务并不会回滚
log.info("异常信息捕获,平台标识:{}", clientRoleDto.getClientId());
log.info("异常信息:{}", e.getMessage());
}
}
//更新密码
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();
}

@@ -580,72 +582,6 @@ public class TenantServiceImpl implements TenantService {
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();
}

/**
* 删除请求
*
@@ -654,7 +590,7 @@ public class TenantServiceImpl implements TenantService {
* @param loginUser
* @return
*/
private JsonResult deleteResult(OidcTenantDto dto, String code, LoginUser loginUser) {
private JsonResult deleteResult(CreateClientTenantDto dto, String code, LoginUser loginUser) {
Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getPlatformCode, code)
.eq(Platform::getMark, 1));

+ 6
- 1
tuoheng_oidc_admin/src/main/resources/mapper/TenantMapper.xml View File

@@ -27,7 +27,12 @@
city_code =#{cityCode},
city_name=#{cityName},
district_code =#{districtCode},
district_name =#{districtName}
district_name =#{districtName},
customer =#{customer},
customer_phone =#{customerPhone},
adress =#{adress},
lng =#{lng},
lat =#{lat},
</set>
where id = #{id}
</update>

BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/config/WebConfig.class View File


Loading…
Cancel
Save