华为区块链开发,处方流转合约Java代码示例

骑猪看日落 2024-03-30 17:34 132阅读 0赞

https://wheart.cn/

  1. package org.hyperledger.fabric.example;
  2. import java.util.List;
  3. import com.google.gson.Gson;
  4. import com.google.protobuf.ByteString;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. import org.hyperledger.fabric.shim.ChaincodeBase;
  8. import org.hyperledger.fabric.shim.ChaincodeStub;
  9. import static java.nio.charset.StandardCharsets.UTF_8;
  10. public class SimpleChaincode extends ChaincodeBase {
  11. // 药品对象定义
  12. class Drug {
  13. // 药品名称
  14. String drugName;
  15. // 用法
  16. String drugUsage;
  17. // 用量
  18. String drugAmount;
  19. // 频率
  20. String drugRate;
  21. // 数量
  22. int drugNumber;
  23. }
  24. // 签章对象定义
  25. class Signature {
  26. // 签章机构
  27. String signCompany;
  28. // 签章内容
  29. String signInfo;
  30. // 签章时间
  31. String signTime;
  32. // 存证信息
  33. String certificateInfo;
  34. }
  35. // 患者授权对象定义
  36. class Accredit {
  37. // 被授权机构名称
  38. String toAccreditCompany;
  39. // 被授权机构社会信用代码
  40. String toAccreditCompanyId;
  41. // 被授权机构分支机构
  42. String toAccreditCompanyBranch;
  43. // 授权开始时间
  44. String beginTime;
  45. // 授权结束时间
  46. String endTime;
  47. // 授权状态(0-未用;1-使用;9-取消)
  48. int accreditState;
  49. }
  50. // 处方登记对象定义
  51. class Prescription {
  52. // 处方ID
  53. String prescriptionId;
  54. // 开具时间
  55. String signTime;
  56. // 医生姓名
  57. String dName;
  58. // 医生身份证
  59. String dIdentityNumber;
  60. // 医院名称
  61. String hospitalName;
  62. // 医院社会信用代码
  63. String hospitalId;
  64. // 患者姓名
  65. String name;
  66. // 患者身份证
  67. String identityNumber;
  68. // 处方状态
  69. String state;
  70. // 诊断信息
  71. String diagnosticInfo;
  72. // 药品信息 (数组)
  73. Drug[] drugInfo;
  74. // 医生签章 (JSON字符串)
  75. Signature doctorSignature;
  76. // 医院签章 (JSON字符串)
  77. Signature hospitaSignature;
  78. // 药师签章 (JSON字符串)
  79. Signature pharmacistSignature;
  80. // 患者授权信息 (数组)
  81. Accredit[] accreditInfo;
  82. // 药师姓名
  83. String pharmacistName;
  84. // 药师身份证
  85. String pharmacistIdentityNumber;
  86. // 第三方审方机构名称
  87. String auditName;
  88. // 第三方审方机构社会信用代码
  89. String auditId;
  90. // 配药机构名称
  91. String dispensingName;
  92. // 配药机构社会信用代码
  93. String dispensingId;
  94. }
  95. // 处方审核对象定义
  96. class PscrpAudit {
  97. // 处方ID
  98. String prescriptionId;
  99. // 处方状态
  100. String state;
  101. // 药师签章 (JSON字符串)
  102. Signature pharmacistSignature;
  103. // 药师姓名
  104. String pharmacistName;
  105. // 药师身份证
  106. String pharmacistIdentityNumber;
  107. // 第三方审方机构名称
  108. String auditName;
  109. // 第三方审方机构社会信用代码
  110. String auditId;
  111. }
  112. // 患者授权对象定义
  113. class PscrpAccredit {
  114. // 处方ID
  115. String prescriptionId;
  116. // 患者姓名
  117. String name;
  118. // 患者身份证
  119. String identityNumber;
  120. // 处方状态
  121. String state;
  122. // 患者授权信息 (数组)
  123. Accredit[] accreditInfo;
  124. }
  125. // 处方配药对象定义
  126. class PscrpDispensation {
  127. // 处方ID
  128. String prescriptionId;
  129. // 处方状态
  130. String state;
  131. // 配药机构名称
  132. String dispensingName;
  133. // 配药机构社会信用代码
  134. String dispensingId;
  135. }
  136. private static Log logger = LogFactory.getLog(SimpleChaincode.class);
  137. @Override
  138. public Response init(ChaincodeStub stub) {
  139. logger.info("Init");
  140. return newSuccessResponse();
  141. }
  142. @Override
  143. public Response invoke(ChaincodeStub stub) {
  144. try {
  145. logger.info("Invoke java simple chaincode");
  146. String func = stub.getFunction();
  147. List<String> params = stub.getParameters();
  148. if (func.equals("register")) {
  149. return register(stub, params);
  150. } else if (func.equals("audit")) {
  151. return audit(stub, params);
  152. } else if (func.equals("accredit")) {
  153. return accredit(stub, params);
  154. } else if (func.equals("dispense")) {
  155. return dispense(stub, params);
  156. } else if (func.equals("query")) {
  157. return query(stub, params);
  158. }
  159. return newErrorResponse("Invalid invoke function name.");
  160. } catch (Throwable e) {
  161. return newErrorResponse(e);
  162. }
  163. }
  164. /**
  165. * 登记处方信息
  166. * 参数1:处方登记对象
  167. */
  168. private Response register(ChaincodeStub stub, List<String> args) {
  169. if (args.size() != 1) {
  170. return newErrorResponse("Incorrect number of arguments. Expecting 1");
  171. }
  172. String prescriptionMsg = args.get(0);
  173. Prescription prescription;
  174. Gson gson = new Gson();
  175. prescription = gson.fromJson(prescriptionMsg, Prescription.class);
  176. String prescriptionId = prescription.prescriptionId;
  177. if (prescriptionId.equals("")) {
  178. logger.info("The arguments is invalid - " + prescriptionMsg + ", no prescriptionId.");
  179. return newErrorResponse("The arguments is invalid - " + prescriptionMsg + ", no prescriptionId.");
  180. }
  181. stub.putState(prescriptionId, ByteString.copyFrom(prescriptionMsg, UTF_8).toByteArray());
  182. return newSuccessResponse();
  183. }
  184. /**
  185. * 处方审核
  186. * 参数1:处方审核对象
  187. */
  188. private Response audit(ChaincodeStub stub, List<String> args) {
  189. if (args.size() != 1) {
  190. return newErrorResponse("Incorrect number of arguments. Expecting 1");
  191. }
  192. String pscrpAuditMsg = args.get(0);
  193. PscrpAudit pscrpAudit;
  194. Gson gson = new Gson();
  195. pscrpAudit = gson.fromJson(pscrpAuditMsg, PscrpAudit.class);
  196. String prescriptionId = pscrpAudit.prescriptionId;
  197. if (prescriptionId.equals("")) {
  198. logger.info("The arguments is invalid - " + pscrpAuditMsg + ", no prescriptionId.");
  199. return newErrorResponse("The arguments is invalid - " + pscrpAuditMsg + ", no prescriptionId.");
  200. }
  201. Prescription prescription = getPrescription(stub, prescriptionId);
  202. String state = prescription.state;
  203. if (!state.equals("register")) {
  204. logger.info("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
  205. return newErrorResponse("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
  206. }
  207. prescription.state = pscrpAudit.state;
  208. prescription.pharmacistSignature = pscrpAudit.pharmacistSignature;
  209. prescription.pharmacistName = pscrpAudit.pharmacistName;
  210. prescription.pharmacistIdentityNumber = pscrpAudit.pharmacistIdentityNumber;
  211. prescription.auditName = pscrpAudit.auditName;
  212. prescription.auditId = pscrpAudit.auditId;
  213. String prescriptionStr = gson.toJson(prescription);
  214. stub.putState(prescriptionId, ByteString.copyFrom(prescriptionStr, UTF_8).toByteArray());
  215. return newSuccessResponse();
  216. }
  217. /**
  218. * 患者授权
  219. * 参数1:患者授权对象
  220. */
  221. private Response accredit(ChaincodeStub stub, List<String> args) {
  222. if (args.size() != 1) {
  223. return newErrorResponse("Incorrect number of arguments. Expecting 1");
  224. }
  225. String pscrpAccreditMsg = args.get(0);
  226. Gson gson = new Gson();
  227. PscrpAccredit pscrpAccredit = gson.fromJson(pscrpAccreditMsg, PscrpAccredit.class);
  228. String prescriptionId = pscrpAccredit.prescriptionId;
  229. if (prescriptionId.equals("")) {
  230. logger.info("The arguments is invalid - " + pscrpAccreditMsg + ", no prescriptionId.");
  231. return newErrorResponse("The arguments is invalid - " + pscrpAccreditMsg + ", no prescriptionId.");
  232. }
  233. Prescription prescription = getPrescription(stub, prescriptionId);
  234. String state = prescription.state;
  235. if (!state.equals("audit")) {
  236. logger.info("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
  237. return newErrorResponse("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
  238. }
  239. prescription.state = pscrpAccredit.state;
  240. prescription.name = pscrpAccredit.name;
  241. prescription.identityNumber = pscrpAccredit.identityNumber;
  242. prescription.accreditInfo = pscrpAccredit.accreditInfo;
  243. String prescriptionStr = gson.toJson(prescription);
  244. stub.putState(prescriptionId, ByteString.copyFrom(prescriptionStr, UTF_8).toByteArray());
  245. return newSuccessResponse();
  246. }
  247. /**
  248. * 处方配药
  249. * 参数1:处方配药对象
  250. */
  251. private Response dispense(ChaincodeStub stub, List<String> args) {
  252. if (args.size() != 1) {
  253. return newErrorResponse("Incorrect number of arguments. Expecting 1");
  254. }
  255. String pscrpDispenseMsg = args.get(0);
  256. Gson gson = new Gson();
  257. PscrpDispensation pscrpDispense = gson.fromJson(pscrpDispenseMsg, PscrpDispensation.class);
  258. String prescriptionId = pscrpDispense.prescriptionId;
  259. if (prescriptionId.equals("")) {
  260. logger.info("The arguments is invalid - " + pscrpDispenseMsg + ", no prescriptionId.");
  261. return newErrorResponse("The arguments is invalid - " + pscrpDispenseMsg + ", no prescriptionId.");
  262. }
  263. Prescription prescription = getPrescription(stub, prescriptionId);
  264. String state = prescription.state;
  265. if (!state.equals("accredit")) {
  266. logger.info("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
  267. return newErrorResponse("Current State of the prescription is invalid, state=" + state + ", prescriptionId=" + prescriptionId);
  268. }
  269. prescription.state = pscrpDispense.state;
  270. prescription.dispensingName = pscrpDispense.dispensingName;
  271. prescription.dispensingId = pscrpDispense.dispensingId;
  272. String prescriptionStr = gson.toJson(prescription);
  273. stub.putState(prescriptionId, ByteString.copyFrom(prescriptionStr, UTF_8).toByteArray());
  274. return newSuccessResponse();
  275. }
  276. private Prescription getPrescription(ChaincodeStub stub, String prescriptionId) {
  277. Prescription prescription;
  278. String prescriptionStr = stub.getStringState(prescriptionId);
  279. Gson gson = new Gson();
  280. prescription = gson.fromJson(prescriptionStr, Prescription.class);
  281. return prescription;
  282. }
  283. /*
  284. 查询处方信息
  285. 参数1:处方登记对象
  286. */
  287. private Response query(ChaincodeStub stub, List<String> args) {
  288. if (args.size() != 1) {
  289. return newErrorResponse("Incorrect number of arguments. Expecting 1");
  290. }
  291. String prescriptionId = args.get(0);
  292. Prescription prescription = getPrescription(stub, prescriptionId);
  293. Gson gson = new Gson();
  294. String prescriptionStr = gson.toJson(prescription);
  295. return newSuccessResponse(prescriptionStr, ByteString.copyFrom(prescriptionStr, UTF_8).toByteArray());
  296. }
  297. public static void main(String[] args) {
  298. new SimpleChaincode().start(args);
  299. }
  300. }

发表评论

表情:
评论列表 (有 0 条评论,132人围观)

还没有评论,来说两句吧...

相关阅读

    相关 区块 智能合约 简介

    根据谷歌趋势数据显示,目前,程序员对智能合约编程的兴趣已经处于历史最高水平,其中中国高居全球榜首,随着区块链技术的发展,相信日后智能合约将会与我们的生活密切相关,今天就为大家介