https://wheart.cn/
package org.hyperledger.fabric.example;
import java.util.List;
import com.google.gson.Gson;
import com.google.protobuf.ByteString;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperledger.fabric.shim.ChaincodeBase;
import org.hyperledger.fabric.shim.ChaincodeStub;
import static java.nio.charset.StandardCharsets.UTF_8;
public class SimpleChaincode extends ChaincodeBase {
// 药品对象定义
class Drug {
// 药品名称
String drugName;
// 用法
String drugUsage;
// 用量
String drugAmount;
// 频率
String drugRate;
// 数量
int drugNumber;
}
// 签章对象定义
class Signature {
// 签章机构
String signCompany;
// 签章内容
String signInfo;
// 签章时间
String signTime;
// 存证信息
String certificateInfo;
}
// 患者授权对象定义
class Accredit {
// 被授权机构名称
String toAccreditCompany;
// 被授权机构社会信用代码
String toAccreditCompanyId;
// 被授权机构分支机构
String toAccreditCompanyBranch;
// 授权开始时间
String beginTime;
// 授权结束时间
String endTime;
// 授权状态(0-未用;1-使用;9-取消)
int accreditState;
}
// 处方登记对象定义
class Prescription {
// 处方ID
String prescriptionId;
// 开具时间
String signTime;
// 医生姓名
String dName;
// 医生身份证
String dIdentityNumber;
// 医院名称
String hospitalName;
// 医院社会信用代码
String hospitalId;
// 患者姓名
String name;
// 患者身份证
String identityNumber;
// 处方状态
String state;
// 诊断信息
String diagnosticInfo;
// 药品信息 (数组)
Drug[] drugInfo;
// 医生签章 (JSON字符串)
Signature doctorSignature;
// 医院签章 (JSON字符串)
Signature hospitaSignature;
// 药师签章 (JSON字符串)
Signature pharmacistSignature;
// 患者授权信息 (数组)
Accredit[] accreditInfo;
// 药师姓名
String pharmacistName;
// 药师身份证
String pharmacistIdentityNumber;
// 第三方审方机构名称
String auditName;
// 第三方审方机构社会信用代码
String auditId;
// 配药机构名称
String dispensingName;
// 配药机构社会信用代码
String dispensingId;
}
// 处方审核对象定义
class PscrpAudit {
// 处方ID
String prescriptionId;
// 处方状态
String state;
// 药师签章 (JSON字符串)
Signature pharmacistSignature;
// 药师姓名
String pharmacistName;
// 药师身份证
String pharmacistIdentityNumber;
// 第三方审方机构名称
String auditName;
// 第三方审方机构社会信用代码
String auditId;
}
// 患者授权对象定义
class PscrpAccredit {
// 处方ID
String prescriptionId;
// 患者姓名
String name;
// 患者身份证
String identityNumber;
// 处方状态
String state;
// 患者授权信息 (数组)
Accredit[] accreditInfo;
}
// 处方配药对象定义
class PscrpDispensation {
// 处方ID
String prescriptionId;
// 处方状态
String state;
// 配药机构名称
String dispensingName;
// 配药机构社会信用代码
String dispensingId;
}
private static Log logger = LogFactory.getLog(SimpleChaincode.class);
@Override
public Response init(ChaincodeStub stub) {
logger.info("Init");
return newSuccessResponse();
}
@Override
public Response invoke(ChaincodeStub stub) {
try {
logger.info("Invoke java simple chaincode");
String func = stub.getFunction();
List<String> params = stub.getParameters();
if (func.equals("register")) {
return register(stub, params);
} else if (func.equals("audit")) {
return audit(stub, params);
} else if (func.equals("accredit")) {
return accredit(stub, params);
} else if (func.equals("dispense")) {
return dispense(stub, params);
} else if (func.equals("query")) {
return query(stub, params);
}
return newErrorResponse("Invalid invoke function name.");
} catch (Throwable e) {
return newErrorResponse(e);
}
}
/**
* 登记处方信息
* 参数1:处方登记对象
*/
private Response register(ChaincodeStub stub, List<String> args) {
if (args.size() != 1) {
return newErrorResponse("Incorrect number of arguments. Expecting 1");
}
String prescriptionMsg = args.get(0);
Prescription prescription;
Gson gson = new Gson();
prescription = gson.fromJson(prescriptionMsg, Prescription.class);
String prescriptionId = prescription.prescriptionId;
if (prescriptionId.equals("")) {
logger.info("The arguments is invalid - " + prescriptionMsg + ", no prescriptionId.");
return newErrorResponse("The arguments is invalid - " + prescriptionMsg + ", no prescriptionId.");
}
stub.putState(prescriptionId, ByteString.copyFrom(prescriptionMsg, UTF_8).toByteArray());
return newSuccessResponse();
}
/**
* 处方审核
* 参数1:处方审核对象
*/
private Response audit(ChaincodeStub stub, List<String> args) {
if (args.size() != 1) {
return newErrorResponse("Incorrect number of arguments. Expecting 1");
}
String pscrpAuditMsg = args.get(0);
PscrpAudit pscrpAudit;
Gson gson = new Gson();
pscrpAudit = gson.fromJson(pscrpAuditMsg, PscrpAudit.class);
String prescriptionId = pscrpAudit.prescriptionId;
if (prescriptionId.equals("")) {
logger.info("The arguments is invalid - " + pscrpAuditMsg + ", no prescriptionId.");
return newErrorResponse("The arguments is invalid - " + pscrpAuditMsg + ", no prescriptionId.");
}
Prescription prescription = getPrescription(stub, prescriptionId);
String state = prescription.state;
if (!state.equals("register")) {
logger.info("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
return newErrorResponse("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
}
prescription.state = pscrpAudit.state;
prescription.pharmacistSignature = pscrpAudit.pharmacistSignature;
prescription.pharmacistName = pscrpAudit.pharmacistName;
prescription.pharmacistIdentityNumber = pscrpAudit.pharmacistIdentityNumber;
prescription.auditName = pscrpAudit.auditName;
prescription.auditId = pscrpAudit.auditId;
String prescriptionStr = gson.toJson(prescription);
stub.putState(prescriptionId, ByteString.copyFrom(prescriptionStr, UTF_8).toByteArray());
return newSuccessResponse();
}
/**
* 患者授权
* 参数1:患者授权对象
*/
private Response accredit(ChaincodeStub stub, List<String> args) {
if (args.size() != 1) {
return newErrorResponse("Incorrect number of arguments. Expecting 1");
}
String pscrpAccreditMsg = args.get(0);
Gson gson = new Gson();
PscrpAccredit pscrpAccredit = gson.fromJson(pscrpAccreditMsg, PscrpAccredit.class);
String prescriptionId = pscrpAccredit.prescriptionId;
if (prescriptionId.equals("")) {
logger.info("The arguments is invalid - " + pscrpAccreditMsg + ", no prescriptionId.");
return newErrorResponse("The arguments is invalid - " + pscrpAccreditMsg + ", no prescriptionId.");
}
Prescription prescription = getPrescription(stub, prescriptionId);
String state = prescription.state;
if (!state.equals("audit")) {
logger.info("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
return newErrorResponse("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
}
prescription.state = pscrpAccredit.state;
prescription.name = pscrpAccredit.name;
prescription.identityNumber = pscrpAccredit.identityNumber;
prescription.accreditInfo = pscrpAccredit.accreditInfo;
String prescriptionStr = gson.toJson(prescription);
stub.putState(prescriptionId, ByteString.copyFrom(prescriptionStr, UTF_8).toByteArray());
return newSuccessResponse();
}
/**
* 处方配药
* 参数1:处方配药对象
*/
private Response dispense(ChaincodeStub stub, List<String> args) {
if (args.size() != 1) {
return newErrorResponse("Incorrect number of arguments. Expecting 1");
}
String pscrpDispenseMsg = args.get(0);
Gson gson = new Gson();
PscrpDispensation pscrpDispense = gson.fromJson(pscrpDispenseMsg, PscrpDispensation.class);
String prescriptionId = pscrpDispense.prescriptionId;
if (prescriptionId.equals("")) {
logger.info("The arguments is invalid - " + pscrpDispenseMsg + ", no prescriptionId.");
return newErrorResponse("The arguments is invalid - " + pscrpDispenseMsg + ", no prescriptionId.");
}
Prescription prescription = getPrescription(stub, prescriptionId);
String state = prescription.state;
if (!state.equals("accredit")) {
logger.info("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
return newErrorResponse("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
}
prescription.state = pscrpDispense.state;
prescription.dispensingName = pscrpDispense.dispensingName;
prescription.dispensingId = pscrpDispense.dispensingId;
String prescriptionStr = gson.toJson(prescription);
stub.putState(prescriptionId, ByteString.copyFrom(prescriptionStr, UTF_8).toByteArray());
return newSuccessResponse();
}
private Prescription getPrescription(ChaincodeStub stub, String prescriptionId) {
Prescription prescription;
String prescriptionStr = stub.getStringState(prescriptionId);
Gson gson = new Gson();
prescription = gson.fromJson(prescriptionStr, Prescription.class);
return prescription;
}
/*
查询处方信息
参数1:处方登记对象
*/
private Response query(ChaincodeStub stub, List<String> args) {
if (args.size() != 1) {
return newErrorResponse("Incorrect number of arguments. Expecting 1");
}
String prescriptionId = args.get(0);
Prescription prescription = getPrescription(stub, prescriptionId);
Gson gson = new Gson();
String prescriptionStr = gson.toJson(prescription);
return newSuccessResponse(prescriptionStr, ByteString.copyFrom(prescriptionStr, UTF_8).toByteArray());
}
public static void main(String[] args) {
new SimpleChaincode().start(args);
}
}
文章来源地址https://www.toymoban.com/news/detail-775363.html
文章来源:https://www.toymoban.com/news/detail-775363.html
到了这里,关于华为区块链开发,处方流转合约Java代码示例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!