添加第三方账号信息

This commit is contained in:
2024-03-20 17:02:22 +08:00
parent 13ac8d0b33
commit 3f90b799bc
9 changed files with 461 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package cn.lihongjie.coal.thirdAccount.controller;
import cn.lihongjie.coal.annotation.OrgScope;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.thirdAccount.dto.CreateThirdAccountDto;
import cn.lihongjie.coal.thirdAccount.dto.ThirdAccountDto;
import cn.lihongjie.coal.thirdAccount.dto.UpdateThirdAccountDto;
import cn.lihongjie.coal.thirdAccount.service.ThirdAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/thirdAccount")
@SysLog(module = "第三方账号管理")
@Slf4j
@OrgScope
public class ThirdAccountController {
@Autowired private ThirdAccountService service;
@PostMapping("/create")
public ThirdAccountDto create(@RequestBody CreateThirdAccountDto request) {
return this.service.create(request);
}
@PostMapping("/update")
public ThirdAccountDto update(@RequestBody UpdateThirdAccountDto request) {
return this.service.update(request);
}
@PostMapping("/delete")
public Object delete(@RequestBody IdRequest request) {
this.service.delete(request);
return true;
}
@PostMapping("/getById")
public ThirdAccountDto getById(@RequestBody IdRequest request) {
return this.service.getById(request.getId());
}
@PostMapping("/list")
public Page<ThirdAccountDto> list(@RequestBody CommonQuery request) {
return this.service.list(request);
}
}

View File

@@ -0,0 +1,53 @@
package cn.lihongjie.coal.thirdAccount.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;
import java.time.LocalDateTime;
@Data
public class CreateThirdAccountDto extends OrgCommonDto {
@Comment("账号类型")
private String accountType;
@Formula(
"(select i.name\n"
+ "from t_dictionary d,\n"
+ " t_dictionary_item i\n"
+ "where d.id = i.dictionary_id\n"
+ " and d.code = 'thirdAccount.type'\n"
+ " and i.code = account_type)")
private String accountTypeName;
@Comment("用户名")
private String username;
@Comment("密码")
private String password;
@Comment("token")
private String token;
@Comment("token过期时间(分钟)")
private Long tokenExpireMinute;
@Comment("token过期时间")
private LocalDateTime tokenExpireTime;
@Comment("token下次刷新时间")
private LocalDateTime tokenNextRefreshTime;
@Comment("token刷新时间")
private LocalDateTime tokenRefreshTime;
@Comment("token刷新次数")
private Integer tokenRefreshCount;
@Comment("token刷新错误信息")
private String tokenRefreshError;
}

View File

@@ -0,0 +1,52 @@
package cn.lihongjie.coal.thirdAccount.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;
import java.time.LocalDateTime;
@Data
public class ThirdAccountDto extends OrgCommonDto {
@Comment("账号类型")
private String accountType;
@Formula(
"(select i.name\n"
+ "from t_dictionary d,\n"
+ " t_dictionary_item i\n"
+ "where d.id = i.dictionary_id\n"
+ " and d.code = 'thirdAccount.type'\n"
+ " and i.code = account_type)")
private String accountTypeName;
@Comment("用户名")
private String username;
@Comment("密码")
private String password;
@Comment("token")
private String token;
@Comment("token过期时间(分钟)")
private Long tokenExpireMinute;
@Comment("token过期时间")
private LocalDateTime tokenExpireTime;
@Comment("token下次刷新时间")
private LocalDateTime tokenNextRefreshTime;
@Comment("token刷新时间")
private LocalDateTime tokenRefreshTime;
@Comment("token刷新次数")
private Integer tokenRefreshCount;
@Comment("token刷新错误信息")
private String tokenRefreshError;
}

View File

@@ -0,0 +1,52 @@
package cn.lihongjie.coal.thirdAccount.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;
import java.time.LocalDateTime;
@Data
public class UpdateThirdAccountDto extends OrgCommonDto {
@Comment("账号类型")
private String accountType;
@Formula(
"(select i.name\n"
+ "from t_dictionary d,\n"
+ " t_dictionary_item i\n"
+ "where d.id = i.dictionary_id\n"
+ " and d.code = 'thirdAccount.type'\n"
+ " and i.code = account_type)")
private String accountTypeName;
@Comment("用户名")
private String username;
@Comment("密码")
private String password;
@Comment("token")
private String token;
@Comment("token过期时间(分钟)")
private Long tokenExpireMinute;
@Comment("token过期时间")
private LocalDateTime tokenExpireTime;
@Comment("token下次刷新时间")
private LocalDateTime tokenNextRefreshTime;
@Comment("token刷新时间")
private LocalDateTime tokenRefreshTime;
@Comment("token刷新次数")
private Integer tokenRefreshCount;
@Comment("token刷新错误信息")
private String tokenRefreshError;
}

View File

@@ -0,0 +1,56 @@
package cn.lihongjie.coal.thirdAccount.entity;
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import jakarta.persistence.Entity;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;
import java.time.LocalDateTime;
@Data
@Entity
public class ThirdAccountEntity extends OrgCommonEntity {
@Comment("账号类型")
private String accountType;
@Formula(
"(select i.name\n"
+ "from t_dictionary d,\n"
+ " t_dictionary_item i\n"
+ "where d.id = i.dictionary_id\n"
+ " and d.code = 'thirdAccount.type'\n"
+ " and i.code = account_type)")
private String accountTypeName;
@Comment("用户名")
private String username;
@Comment("密码")
private String password;
@Comment("token")
private String token;
@Comment("token过期时间(分钟)")
private Long tokenExpireMinute;
@Comment("token过期时间")
private LocalDateTime tokenExpireTime;
@Comment("token下次刷新时间")
private LocalDateTime tokenNextRefreshTime;
@Comment("token刷新时间")
private LocalDateTime tokenRefreshTime;
@Comment("token刷新次数")
private Integer tokenRefreshCount;
@Comment("token刷新错误信息")
private String tokenRefreshError;
}

View File

@@ -0,0 +1,23 @@
package cn.lihongjie.coal.thirdAccount.mapper;
import cn.lihongjie.coal.base.mapper.BaseMapper;
import cn.lihongjie.coal.base.mapper.CommonEntityMapper;
import cn.lihongjie.coal.base.mapper.CommonMapper;
import cn.lihongjie.coal.thirdAccount.dto.CreateThirdAccountDto;
import cn.lihongjie.coal.thirdAccount.dto.ThirdAccountDto;
import cn.lihongjie.coal.thirdAccount.dto.UpdateThirdAccountDto;
import cn.lihongjie.coal.thirdAccount.entity.ThirdAccountEntity;
import org.mapstruct.Mapper;
import org.mapstruct.control.DeepClone;
@Mapper(
componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING,
uses = {CommonMapper.class, CommonEntityMapper.class},
mappingControl = DeepClone.class)
public interface ThirdAccountMapper
extends BaseMapper<
ThirdAccountEntity,
ThirdAccountDto,
CreateThirdAccountDto,
UpdateThirdAccountDto> {}

View File

@@ -0,0 +1,9 @@
package cn.lihongjie.coal.thirdAccount.repository;
import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.thirdAccount.entity.ThirdAccountEntity;
import org.springframework.stereotype.Repository;
@Repository
public interface ThirdAccountRepository extends BaseRepository<ThirdAccountEntity> {}

View File

@@ -0,0 +1,145 @@
package cn.lihongjie.coal.thirdAccount.service;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.organization.entity.OrganizationEntity;
import cn.lihongjie.coal.organization.service.OrganizationService;
import cn.lihongjie.coal.thirdAccount.dto.CreateThirdAccountDto;
import cn.lihongjie.coal.thirdAccount.dto.ThirdAccountDto;
import cn.lihongjie.coal.thirdAccount.dto.UpdateThirdAccountDto;
import cn.lihongjie.coal.thirdAccount.entity.ThirdAccountEntity;
import cn.lihongjie.coal.thirdAccount.mapper.ThirdAccountMapper;
import cn.lihongjie.coal.thirdAccount.repository.ThirdAccountRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Slf4j
@Transactional
public class ThirdAccountService extends BaseService<ThirdAccountEntity, ThirdAccountRepository> {
@Autowired OrganizationService organizationService;
@Autowired private ThirdAccountRepository repository;
@Autowired private ThirdAccountMapper mapper;
@Autowired private ConversionService conversionService;
@Autowired private DbFunctionService dbFunctionService;
public ThirdAccountDto create(CreateThirdAccountDto request) {
ThirdAccountEntity entity = mapper.toEntity(request);
this.repository.save(entity);
return getById(entity.getId());
}
public ThirdAccountDto update(UpdateThirdAccountDto request) {
ThirdAccountEntity entity = this.repository.get(request.getId());
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
return getById(entity.getId());
}
public void delete(IdRequest request) {
this.repository.deleteAllById(request.getIds());
}
public ThirdAccountDto getById(String id) {
ThirdAccountEntity entity = repository.get(id);
return mapper.toDto(entity);
}
public Page<ThirdAccountDto> list(CommonQuery query) {
Page<ThirdAccountEntity> page =
repository.findAll(
query.specification(conversionService),
PageRequest.of(
query.getPageNo(),
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
}
public void refresh() {
List<OrganizationEntity> all = organizationService.findAll();
all.forEach(this::refresh);
}
private void refresh(OrganizationEntity organizationEntity) {
this.repository.findByOrganizationId(organizationEntity.getId()).forEach(this::refresh);
}
private void refresh(ThirdAccountEntity thirdAccountEntity) {
if (thirdAccountEntity.getTokenNextRefreshTime() == null
|| thirdAccountEntity
.getTokenNextRefreshTime()
.isBefore(java.time.LocalDateTime.now())) {
log.info("refresh token for {}", thirdAccountEntity.getId());
try {
thirdAccountEntity.setToken(genToken(thirdAccountEntity));
thirdAccountEntity.setTokenRefreshTime(java.time.LocalDateTime.now());
thirdAccountEntity.setTokenRefreshError(null);
thirdAccountEntity.setTokenRefreshCount(
ObjectUtils.defaultIfNull(thirdAccountEntity.getTokenRefreshCount(), 0)
+ 1);
thirdAccountEntity.setTokenNextRefreshTime(
java.time.LocalDateTime.now()
.plusMinutes(
(long) (thirdAccountEntity.getTokenExpireMinute() * 0.8)));
thirdAccountEntity.setTokenExpireTime(
java.time.LocalDateTime.now()
.plusMinutes(thirdAccountEntity.getTokenExpireMinute()));
} catch (Exception e) {
log.warn("refresh token error", e);
thirdAccountEntity.setTokenRefreshTime(java.time.LocalDateTime.now());
thirdAccountEntity.setTokenRefreshError(ExceptionUtils.getStackTrace(e));
}
this.repository.save(thirdAccountEntity);
}
}
private String genToken(ThirdAccountEntity thirdAccountEntity) {
switch (thirdAccountEntity.getAccountType()) {
case "1":
return "";
case "2":
return "";
case "3":
return "";
case "4":
return "";
case "5":
return "";
default:
throw new IllegalArgumentException(
"unknown account type " + thirdAccountEntity.getAccountType());
}
}
}

View File

@@ -0,0 +1,17 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.thirdAccount.controller.ThirdAccountController
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc
def controller = ioc.getBean(ThirdAccountController.class)
return controller.list(new CommonQuery())