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;
// 數(shù)量
int drugNumber;
}
// 簽章對象定義
class Signature {
// 簽章機構(gòu)
String signCompany;
// 簽章內(nèi)容
String signInfo;
// 簽章時間
String signTime;
// 存證信息
String certificateInfo;
}
// 患者授權(quán)對象定義
class Accredit {
// 被授權(quán)機構(gòu)名稱
String toAccreditCompany;
// 被授權(quán)機構(gòu)社會信用代碼
String toAccreditCompanyId;
// 被授權(quán)機構(gòu)分支機構(gòu)
String toAccreditCompanyBranch;
// 授權(quán)開始時間
String beginTime;
// 授權(quán)結(jié)束時間
String endTime;
// 授權(quán)狀態(tài)(0-未用;1-使用;9-取消)
int accreditState;
}
// 處方登記對象定義
class Prescription {
// 處方ID
String prescriptionId;
// 開具時間
String signTime;
// 醫(yī)生姓名
String dName;
// 醫(yī)生身份證
String dIdentityNumber;
// 醫(yī)院名稱
String hospitalName;
// 醫(yī)院社會信用代碼
String hospitalId;
// 患者姓名
String name;
// 患者身份證
String identityNumber;
// 處方狀態(tài)
String state;
// 診斷信息
String diagnosticInfo;
// 藥品信息 (數(shù)組)
Drug[] drugInfo;
// 醫(yī)生簽章 (JSON字符串)
Signature doctorSignature;
// 醫(yī)院簽章 (JSON字符串)
Signature hospitaSignature;
// 藥師簽章 (JSON字符串)
Signature pharmacistSignature;
// 患者授權(quán)信息 (數(shù)組)
Accredit[] accreditInfo;
// 藥師姓名
String pharmacistName;
// 藥師身份證
String pharmacistIdentityNumber;
// 第三方審方機構(gòu)名稱
String auditName;
// 第三方審方機構(gòu)社會信用代碼
String auditId;
// 配藥機構(gòu)名稱
String dispensingName;
// 配藥機構(gòu)社會信用代碼
String dispensingId;
}
// 處方審核對象定義
class PscrpAudit {
// 處方ID
String prescriptionId;
// 處方狀態(tài)
String state;
// 藥師簽章 (JSON字符串)
Signature pharmacistSignature;
// 藥師姓名
String pharmacistName;
// 藥師身份證
String pharmacistIdentityNumber;
// 第三方審方機構(gòu)名稱
String auditName;
// 第三方審方機構(gòu)社會信用代碼
String auditId;
}
// 患者授權(quán)對象定義
class PscrpAccredit {
// 處方ID
String prescriptionId;
// 患者姓名
String name;
// 患者身份證
String identityNumber;
// 處方狀態(tài)
String state;
// 患者授權(quán)信息 (數(shù)組)
Accredit[] accreditInfo;
}
// 處方配藥對象定義
class PscrpDispensation {
// 處方ID
String prescriptionId;
// 處方狀態(tài)
String state;
// 配藥機構(gòu)名稱
String dispensingName;
// 配藥機構(gòu)社會信用代碼
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);
}
}
/**
* 登記處方信息
* 參數(shù)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();
}
/**
* 處方審核
* 參數(shù)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();
}
/**
* 患者授權(quán)
* 參數(shù)1:患者授權(quán)對象
*/
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();
}
/**
* 處方配藥
* 參數(shù)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;
}
/*
查詢處方信息
參數(shù)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);
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-775363.html
文章來源:http://www.zghlxwxcb.cn/news/detail-775363.html
到了這里,關(guān)于華為區(qū)塊鏈開發(fā),處方流轉(zhuǎn)合約Java代碼示例的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!