|
|
package com.yau.digitalrmb.institutionidentity.domain;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
public class CurrencyGenerationRequest {
|
|
|
public enum Status {
|
|
|
PREPARED, CONCATENATED, DIGESTED, SIGNED, PACKAGED, SENT, RECEIVED
|
|
|
}
|
|
|
|
|
|
private final Long id;
|
|
|
private long identifierApplicationId;
|
|
|
private String institutionIdentifier;
|
|
|
private String fullInstitutionIdentifier;
|
|
|
private BigDecimal amount;
|
|
|
private String deliveryNodeCode;
|
|
|
private String requestTimestamp;
|
|
|
private String requestOriginalText;
|
|
|
private String requestDigest;
|
|
|
private String bankSignature;
|
|
|
private String signingKeyId;
|
|
|
private String requestMessage;
|
|
|
private Status status;
|
|
|
|
|
|
private CurrencyGenerationRequest(Long id, long identifierApplicationId, String institutionIdentifier,
|
|
|
String fullInstitutionIdentifier, BigDecimal amount, String deliveryNodeCode,
|
|
|
String requestTimestamp, String requestOriginalText, String requestDigest,
|
|
|
String bankSignature, String signingKeyId, String requestMessage, Status status) {
|
|
|
this.id = id;
|
|
|
this.identifierApplicationId = identifierApplicationId;
|
|
|
this.institutionIdentifier = institutionIdentifier;
|
|
|
this.fullInstitutionIdentifier = fullInstitutionIdentifier;
|
|
|
this.amount = amount;
|
|
|
this.deliveryNodeCode = deliveryNodeCode;
|
|
|
this.requestTimestamp = requestTimestamp;
|
|
|
this.requestOriginalText = requestOriginalText;
|
|
|
this.requestDigest = requestDigest;
|
|
|
this.bankSignature = bankSignature;
|
|
|
this.signingKeyId = signingKeyId;
|
|
|
this.requestMessage = requestMessage;
|
|
|
this.status = status;
|
|
|
}
|
|
|
|
|
|
public static CurrencyGenerationRequest prepare(long identifierApplicationId, String institutionIdentifier,
|
|
|
String fullInstitutionIdentifier, BigDecimal amount,
|
|
|
String deliveryNodeCode, String requestTimestamp) {
|
|
|
if (identifierApplicationId <= 0) {
|
|
|
throw new IllegalArgumentException("机构标识申请编号必须大于0");
|
|
|
}
|
|
|
if (institutionIdentifier == null || !institutionIdentifier.matches("ORG_[0-9A-F]{12}")) {
|
|
|
throw new IllegalArgumentException("简洁机构标识格式不正确");
|
|
|
}
|
|
|
if (fullInstitutionIdentifier == null || !fullInstitutionIdentifier.matches("[0-9A-F]{64}")) {
|
|
|
throw new IllegalArgumentException("完整机构标识格式不正确");
|
|
|
}
|
|
|
if (amount == null || amount.compareTo(new BigDecimal("0.00")) <= 0
|
|
|
|| amount.compareTo(new BigDecimal("50000.00")) > 0) {
|
|
|
throw new IllegalArgumentException("申请金额必须大于0且不超过50000.00元");
|
|
|
}
|
|
|
if (!"SYS_DC_001".equals(deliveryNodeCode) && !"SYS_DC_002".equals(deliveryNodeCode)) {
|
|
|
throw new IllegalArgumentException("投放节点编号不在允许范围内");
|
|
|
}
|
|
|
if (requestTimestamp == null || !requestTimestamp.matches("\\d{14}")) {
|
|
|
throw new IllegalArgumentException("请求时间戳格式不正确");
|
|
|
}
|
|
|
BigDecimal normalizedAmount = amount.setScale(2);
|
|
|
String originalText = institutionIdentifier + "|" + normalizedAmount.toPlainString() + "|"
|
|
|
+ deliveryNodeCode + "|" + requestTimestamp;
|
|
|
return new CurrencyGenerationRequest(null, identifierApplicationId, institutionIdentifier,
|
|
|
fullInstitutionIdentifier, normalizedAmount, deliveryNodeCode, requestTimestamp,
|
|
|
originalText, null, null, null, null, Status.CONCATENATED);
|
|
|
}
|
|
|
|
|
|
public static CurrencyGenerationRequest restore(Long id, long identifierApplicationId,
|
|
|
String institutionIdentifier, String fullInstitutionIdentifier,
|
|
|
BigDecimal amount, String deliveryNodeCode, String requestTimestamp,
|
|
|
String requestOriginalText, String requestDigest,
|
|
|
String bankSignature, String signingKeyId,
|
|
|
String requestMessage, Status status) {
|
|
|
return new CurrencyGenerationRequest(id, identifierApplicationId, institutionIdentifier,
|
|
|
fullInstitutionIdentifier, amount, deliveryNodeCode, requestTimestamp, requestOriginalText,
|
|
|
requestDigest, bankSignature, signingKeyId, requestMessage, status);
|
|
|
}
|
|
|
|
|
|
public void concatenate() {
|
|
|
requireStatus(Status.PREPARED, "当前步骤不是请求原文拼接,不能操作");
|
|
|
requestOriginalText = institutionIdentifier + "|" + amount.toPlainString() + "|"
|
|
|
+ deliveryNodeCode + "|" + requestTimestamp;
|
|
|
status = Status.CONCATENATED;
|
|
|
}
|
|
|
|
|
|
public void replaceDigestInput(long identifierApplicationId, String institutionIdentifier,
|
|
|
String fullInstitutionIdentifier, BigDecimal amount,
|
|
|
String deliveryNodeCode, String requestTimestamp, String digest) {
|
|
|
CurrencyGenerationRequest replacement = prepare(identifierApplicationId, institutionIdentifier,
|
|
|
fullInstitutionIdentifier, amount, deliveryNodeCode, requestTimestamp);
|
|
|
if (digest == null || !digest.matches("[0-9A-F]{64}")) {
|
|
|
throw new IllegalArgumentException("SM3摘要格式不正确");
|
|
|
}
|
|
|
this.identifierApplicationId = replacement.identifierApplicationId;
|
|
|
this.institutionIdentifier = replacement.institutionIdentifier;
|
|
|
this.fullInstitutionIdentifier = replacement.fullInstitutionIdentifier;
|
|
|
this.amount = replacement.amount;
|
|
|
this.deliveryNodeCode = replacement.deliveryNodeCode;
|
|
|
this.requestTimestamp = replacement.requestTimestamp;
|
|
|
this.requestOriginalText = replacement.requestOriginalText;
|
|
|
this.requestDigest = digest;
|
|
|
this.bankSignature = null;
|
|
|
this.signingKeyId = null;
|
|
|
this.requestMessage = null;
|
|
|
this.status = Status.DIGESTED;
|
|
|
}
|
|
|
|
|
|
public void recordDigest(String digest) {
|
|
|
requireStatus(Status.CONCATENATED, "当前步骤不是SM3摘要计算,不能操作");
|
|
|
if (digest == null || !digest.matches("[0-9A-F]{64}")) {
|
|
|
throw new IllegalArgumentException("SM3摘要格式不正确");
|
|
|
}
|
|
|
requestDigest = digest;
|
|
|
status = Status.DIGESTED;
|
|
|
}
|
|
|
|
|
|
public void recordSignature(String keyId, String signature) {
|
|
|
if (requestDigest == null) {
|
|
|
throw new IllegalStateException("请先完成SM3摘要计算");
|
|
|
}
|
|
|
if (keyId == null || signature == null || !signature.matches("[0-9A-F]+")) {
|
|
|
throw new IllegalArgumentException("SM2签名信息不正确");
|
|
|
}
|
|
|
signingKeyId = keyId;
|
|
|
bankSignature = signature;
|
|
|
requestMessage = null;
|
|
|
status = Status.SIGNED;
|
|
|
}
|
|
|
|
|
|
public void packageMessage(String message) {
|
|
|
if (bankSignature == null) {
|
|
|
throw new IllegalStateException("请先完成商业银行签名");
|
|
|
}
|
|
|
if (message == null || message.trim().isEmpty()) {
|
|
|
throw new IllegalArgumentException("请求报文不能为空");
|
|
|
}
|
|
|
requestMessage = message;
|
|
|
status = Status.PACKAGED;
|
|
|
}
|
|
|
|
|
|
public void send() {
|
|
|
if (requestMessage == null) {
|
|
|
throw new IllegalStateException("请先完成请求报文组装");
|
|
|
}
|
|
|
status = Status.SENT;
|
|
|
}
|
|
|
|
|
|
public void receive() {
|
|
|
if (requestMessage == null) {
|
|
|
throw new IllegalStateException("央行只能接收已发送的请求");
|
|
|
}
|
|
|
status = Status.RECEIVED;
|
|
|
}
|
|
|
|
|
|
private void requireStatus(Status expected, String message) {
|
|
|
if (status != expected) {
|
|
|
throw new IllegalStateException(message + ",当前状态:" + status.name());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Long getId() { return id; }
|
|
|
public long getIdentifierApplicationId() { return identifierApplicationId; }
|
|
|
public String getInstitutionIdentifier() { return institutionIdentifier; }
|
|
|
public String getFullInstitutionIdentifier() { return fullInstitutionIdentifier; }
|
|
|
public BigDecimal getAmount() { return amount; }
|
|
|
public String getDeliveryNodeCode() { return deliveryNodeCode; }
|
|
|
public String getRequestTimestamp() { return requestTimestamp; }
|
|
|
public String getRequestOriginalText() { return requestOriginalText; }
|
|
|
public String getRequestDigest() { return requestDigest; }
|
|
|
public String getBankSignature() { return bankSignature; }
|
|
|
public String getSigningKeyId() { return signingKeyId; }
|
|
|
public String getRequestMessage() { return requestMessage; }
|
|
|
public Status getStatus() { return status; }
|
|
|
}
|