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 {// 处方IDString 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 {// 处方IDString prescriptionId;// 处方状态String state;// 药师签章 (JSON字符串)Signature pharmacistSignature;// 药师姓名String pharmacistName;// 药师身份证String pharmacistIdentityNumber;// 第三方审方机构名称String auditName;// 第三方审方机构社会信用代码String auditId;}// 患者授权对象定义class PscrpAccredit {// 处方IDString prescriptionId;// 患者姓名String name;// 患者身份证String identityNumber;// 处方状态String state;// 患者授权信息 (数组)Accredit[] accreditInfo;}// 处方配药对象定义class PscrpDispensation {// 处方IDString prescriptionId;// 处方状态String state;// 配药机构名称String dispensingName;// 配药机构社会信用代码String dispensingId;}private static Log logger = LogFactory.getLog(SimpleChaincode.class);@Overridepublic Response init(ChaincodeStub stub) {logger.info("Init");return newSuccessResponse();}@Overridepublic Response invoke(ChaincodeStub stub) {try {logger.info("Invoke java simple chaincode");String func = stub.getFunction();List 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 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 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 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 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 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);}}