feat:文件上传的功能开发
parent
a6a83e5bd5
commit
8f948dece3
@ -1,8 +1,11 @@
|
||||
package com.yau.digitalrmb.issuance.domain.repository;
|
||||
|
||||
import com.yau.digitalrmb.issuance.domain.model.CentralBankCurrencyVault;
|
||||
import com.yau.digitalrmb.issuance.domain.model.IssuanceOwner;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CentralBankCurrencyVaultRepository {
|
||||
Optional<CentralBankCurrencyVault> findByCurrency(String currency);
|
||||
/** 查询当前实训主体的央行发行库余额;首次访问时按其原始值初始化。 */
|
||||
Optional<CentralBankCurrencyVault> findByCurrency(IssuanceOwner owner, String currency);
|
||||
}
|
||||
|
||||
@ -1,10 +1,15 @@
|
||||
package com.yau.digitalrmb.issuance.domain.repository;
|
||||
|
||||
import com.yau.digitalrmb.issuance.domain.model.CentralBankInstitutionAccount;
|
||||
import com.yau.digitalrmb.issuance.domain.model.IssuanceOwner;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CentralBankInstitutionAccountRepository {
|
||||
Optional<CentralBankInstitutionAccount> findByBankCode(String bankCode);
|
||||
Optional<CentralBankInstitutionAccount> findByBankCodeForUpdate(String bankCode);
|
||||
void updateReserveBalance(String bankCode, java.math.BigDecimal reserveBalance);
|
||||
/** 查询当前实训主体的准备金账户;首次访问时按其原始值初始化。 */
|
||||
Optional<CentralBankInstitutionAccount> findByBankCode(IssuanceOwner owner, String bankCode);
|
||||
|
||||
Optional<CentralBankInstitutionAccount> findByBankCodeForUpdate(IssuanceOwner owner, String bankCode);
|
||||
|
||||
void updateReserveBalance(IssuanceOwner owner, String bankCode, java.math.BigDecimal reserveBalance);
|
||||
}
|
||||
|
||||
@ -1,18 +1,81 @@
|
||||
package com.yau.digitalrmb.issuance.infrastructure.persistence.repository;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.yau.digitalrmb.issuance.domain.model.CentralBankCurrencyVault;
|
||||
import com.yau.digitalrmb.issuance.domain.model.IssuanceAssetDefaults;
|
||||
import com.yau.digitalrmb.issuance.domain.model.IssuanceOwner;
|
||||
import com.yau.digitalrmb.issuance.domain.repository.CentralBankCurrencyVaultRepository;
|
||||
import com.yau.digitalrmb.issuance.infrastructure.persistence.entity.CentralBankCurrencyVaultEntity;
|
||||
import com.yau.digitalrmb.issuance.infrastructure.persistence.mapper.CentralBankCurrencyVaultMapper;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public class MybatisCentralBankCurrencyVaultRepository implements CentralBankCurrencyVaultRepository {
|
||||
private final CentralBankCurrencyVaultMapper mapper;
|
||||
public MybatisCentralBankCurrencyVaultRepository(CentralBankCurrencyVaultMapper mapper) { this.mapper = mapper; }
|
||||
public Optional<CentralBankCurrencyVault> findByCurrency(String currency) {
|
||||
CentralBankCurrencyVaultEntity entity = mapper.selectById(currency);
|
||||
return entity == null ? Optional.<CentralBankCurrencyVault>empty() : Optional.of(new CentralBankCurrencyVault(entity.getCurrency(), entity.getAvailableBalance()));
|
||||
|
||||
public MybatisCentralBankCurrencyVaultRepository(CentralBankCurrencyVaultMapper mapper) {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<CentralBankCurrencyVault> findByCurrency(IssuanceOwner owner, String currency) {
|
||||
CentralBankCurrencyVaultEntity entity = select(owner, currency);
|
||||
if (entity == null) {
|
||||
entity = initialize(owner, currency);
|
||||
}
|
||||
return entity == null ? Optional.<CentralBankCurrencyVault>empty()
|
||||
: Optional.of(new CentralBankCurrencyVault(entity.getCurrency(), entity.getAvailableBalance()));
|
||||
}
|
||||
|
||||
private CentralBankCurrencyVaultEntity select(IssuanceOwner owner, String currency) {
|
||||
return mapper.selectOne(new LambdaQueryWrapper<CentralBankCurrencyVaultEntity>()
|
||||
.eq(CentralBankCurrencyVaultEntity::getUserId, owner.getUserId())
|
||||
.eq(CentralBankCurrencyVaultEntity::getSchoolId, owner.getSchoolId())
|
||||
.eq(CentralBankCurrencyVaultEntity::getClassId, owner.getClassId())
|
||||
.eq(CentralBankCurrencyVaultEntity::getCurrency, currency)
|
||||
.eq(CentralBankCurrencyVaultEntity::getDeleted, 0L)
|
||||
.orderByAsc(CentralBankCurrencyVaultEntity::getId)
|
||||
.last("LIMIT 1"));
|
||||
}
|
||||
|
||||
/** 首次实训时按原始值落一条该学生专属的发行库记录;并发下若已被其他请求建好则直接复用。 */
|
||||
private CentralBankCurrencyVaultEntity initialize(IssuanceOwner owner, String currency) {
|
||||
CentralBankCurrencyVaultEntity entity = new CentralBankCurrencyVaultEntity();
|
||||
entity.setUserId(owner.getUserId());
|
||||
entity.setSchoolId(owner.getSchoolId());
|
||||
entity.setClassId(owner.getClassId());
|
||||
entity.setCurrency(currency);
|
||||
entity.setAvailableBalance(IssuanceAssetDefaults.INITIAL_VAULT_BALANCE);
|
||||
try {
|
||||
mapper.insert(entity);
|
||||
} catch (DuplicateKeyException duplicate) {
|
||||
return select(owner, currency);
|
||||
}
|
||||
convergeDuplicates(owner, currency);
|
||||
CentralBankCurrencyVaultEntity current = select(owner, currency);
|
||||
return current == null ? entity : current;
|
||||
}
|
||||
|
||||
/** 该表不使用唯一键:并发首次初始化可能插出多条有效行,只保留 id 最小的一条,其余逻辑删除。 */
|
||||
private void convergeDuplicates(IssuanceOwner owner, String currency) {
|
||||
List<CentralBankCurrencyVaultEntity> rows = mapper.selectList(
|
||||
new LambdaQueryWrapper<CentralBankCurrencyVaultEntity>()
|
||||
.select(CentralBankCurrencyVaultEntity::getId)
|
||||
.eq(CentralBankCurrencyVaultEntity::getUserId, owner.getUserId())
|
||||
.eq(CentralBankCurrencyVaultEntity::getSchoolId, owner.getSchoolId())
|
||||
.eq(CentralBankCurrencyVaultEntity::getClassId, owner.getClassId())
|
||||
.eq(CentralBankCurrencyVaultEntity::getCurrency, currency)
|
||||
.eq(CentralBankCurrencyVaultEntity::getDeleted, 0L)
|
||||
.orderByAsc(CentralBankCurrencyVaultEntity::getId));
|
||||
for (int index = 1; index < rows.size(); index++) {
|
||||
mapper.update(null, new LambdaUpdateWrapper<CentralBankCurrencyVaultEntity>()
|
||||
.setSql("deleted = id")
|
||||
.eq(CentralBankCurrencyVaultEntity::getId, rows.get(index).getId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,96 @@
|
||||
package com.yau.digitalrmb.corporatewallet.application;
|
||||
|
||||
import com.yau.digitalrmb.corporatewallet.domain.CorporateWalletApplication;
|
||||
import com.yau.digitalrmb.corporatewallet.domain.CorporateWalletApplicationRepository;
|
||||
import com.yau.digitalrmb.corporatewallet.domain.RemoteWalletApplication;
|
||||
import com.yau.digitalrmb.corporatewallet.domain.RemoteWalletApplicationRepository;
|
||||
import com.yau.digitalrmb.institutionidentity.application.InstitutionKeySubject;
|
||||
import com.yau.digitalrmb.institutionidentity.infrastructure.SmInstitutionIdentityCryptography;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CorporateWalletServiceTest {
|
||||
|
||||
private static final InstitutionKeySubject SUBJECT = new InstitutionKeySubject("corp-user", 1001L, 2001L);
|
||||
private static final String DIGEST = "C2DBCCFE515B4570EABAD62B74385CF70CBF1CBC5ED7C7F7F58BEEE8722B8BB6";
|
||||
|
||||
@Mock private CorporateWalletApplicationRepository repository;
|
||||
@Mock private RemoteWalletApplicationRepository remoteRepository;
|
||||
@Mock private SmInstitutionIdentityCryptography cryptography;
|
||||
@Mock private SalaryScoreService scoreService;
|
||||
@InjectMocks private CorporateWalletService service;
|
||||
|
||||
@Test
|
||||
void outputWalletPrefixesGeneratedWalletIdWithCorp() {
|
||||
CorporateWalletApplication app = CorporateWalletApplication.create(
|
||||
SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId());
|
||||
app.setCreditCode("91440101MA5XXXXXX");
|
||||
app.computeDigest("SM3", DIGEST);
|
||||
when(repository.findLatest(SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId()))
|
||||
.thenReturn(Optional.of(app));
|
||||
when(remoteRepository.findActivatedByCreditCode("91440101MA5XXXXXX",
|
||||
SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId()))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
CorporateWalletResult result = service.outputWallet(SUBJECT);
|
||||
|
||||
assertThat(result.getWalletId()).isEqualTo("CORP_" + DIGEST);
|
||||
assertThat(app.getWalletId()).isEqualTo("CORP_" + DIGEST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void outputWalletNormalizesHistoricalWalletIdWithoutPrefix() {
|
||||
CorporateWalletApplication app = CorporateWalletApplication.create(
|
||||
SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId());
|
||||
app.setCreditCode("91440101MA5XXXXXX");
|
||||
app.computeDigest("SM3", DIGEST);
|
||||
app.activateWallet(DIGEST, "20261031120000");
|
||||
when(repository.findLatest(SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId()))
|
||||
.thenReturn(Optional.of(app));
|
||||
|
||||
CorporateWalletResult result = service.outputWallet(SUBJECT);
|
||||
|
||||
assertThat(result.getWalletId()).isEqualTo("CORP_" + DIGEST);
|
||||
assertThat(app.getWalletId()).isEqualTo("CORP_" + DIGEST);
|
||||
}
|
||||
|
||||
@Test
|
||||
void outputWalletKeepsSharedRemoteWalletIdPrefixed() {
|
||||
CorporateWalletApplication app = CorporateWalletApplication.create(
|
||||
SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId());
|
||||
app.setCorpName("创新科技公司");
|
||||
app.setCreditCode("91440101MA5XXXXXX");
|
||||
app.setLegalPerson("张三");
|
||||
app.setApplyTime("20261031120000");
|
||||
app.computeDigest("SM3", DIGEST);
|
||||
|
||||
RemoteWalletApplication remote = RemoteWalletApplication.create(
|
||||
SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId());
|
||||
remote.setCorpName("创新科技公司");
|
||||
remote.setCreditCode("91440101MA5XXXXXX");
|
||||
remote.setLegalPerson("张三");
|
||||
remote.setPhone("13800138000");
|
||||
remote.setApplyTime("20261031120000");
|
||||
remote.activateWallet(DIGEST, "20261031121000");
|
||||
|
||||
when(repository.findLatest(SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId()))
|
||||
.thenReturn(Optional.of(app));
|
||||
when(remoteRepository.findActivatedByCreditCode("91440101MA5XXXXXX",
|
||||
SUBJECT.getUserId(), SUBJECT.getSchoolId(), SUBJECT.getClassId()))
|
||||
.thenReturn(Optional.of(remote));
|
||||
|
||||
CorporateWalletResult result = service.outputWallet(SUBJECT);
|
||||
|
||||
assertThat(result.getWalletId()).isEqualTo("CORP_" + DIGEST);
|
||||
assertThat(remote.getWalletId()).isEqualTo("CORP_" + DIGEST);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue