1.智能合约状态图
2.实现功能,通过背书,签收机制实现任务在各部门流转
3.相关sdk
- Chaincode接口 {Init,Invoke}方法
- ChaincodeStubInterface接口
1.GetTxTimestamp
2.CreateCompositeKey
3.GetQueryResult
4.GetHistoryForKey
5.SetEvent
6.GetStateByPartialCompositeKey
7.GetFunctionAndParameters
8.PutState
9.DelState
10.GetStateByRange
4.代码架构图
5.开发环境(centos7.4,goland)
6.global.go代码
1 package global 2 3 import "github.com/hyperledger/fabric/common/flogging" 4 5 const ( 6 PrescriptionBlockchain_State_NewPublish ="NewPublish" 7 PrescriptionBlockchain_State_EndrWaitSign ="EndrWaitSign" 8 PrescriptionBlockchain_state_EndrSigned ="EndrSigned" 9 HospitalNO_DayTime_PrescriptionID_IndexName ="hospitalno~daytime-prescriptionid" 10 PatientID_PrescriptionID_IndexName ="patientid~prescriptionid" 11 State_PrescriptionID_IndexName ="state~prescriptionid" 12 PendingReceivePrescriptionDeptID_PrescriptionID_IndexName="pendingreceiveprescriptiondeptid~prescriptionid" 13 ReceivingPrescriptionDeptID_PrescriptionID_IndexName="receivingprescriptiondeptid~prescriptionid" 14 OpenPrescriptionDeptID_PrescriptionID_IndexName="openprescriptiondeptid~prescriptionid" 15 ) 16 var( 17 Logger =flogging.MustGetLogger("PrescriptionChaincode") 18 )
7.prescriptionentity.go代码
1 package model 2 3 type PrescriptionBlockchain struct { 4 PrescriptionID string `json:"PrescriptionID"` 5 HospitalNO string `json:"HospitalNO"` 6 PatientID string `json:"PatientID"` 7 GenerationTime string `json:"GenerationTime"` 8 PrescriptionSummary string `json:"PrescriptionSummary"` 9 OpenPrescriptionDeptID string `json:"OpenPrescriptionDeptID"` 10 OpenPrescriptionDoctorID string `json:"OpenPrescriptionDoctorID"` 11 PendingReceivePrescriptionDeptID string `json:"PendingReceivePrescriptionDeptID"` 12 PendingReceivePrescriptionWorkID string `json:"PendingReceivePrescriptionWorkID"` 13 ReceivingPrescriptionDeptID string `json:"ReceivingPrescriptionDeptID"` 14 ReceivingPrescriptionWorkID string `json:"ReceivingPrescriptionWorkID"` 15 State string `json:"State"` 16 History []HistoryItem `json:"History"` 17 } 18 type HistoryItem struct { 19 TxId string `json:"TxId"` 20 PrescriptionBlockchain PrescriptionBlockchain `json:"PrescriptionBlockchain"` 21 } 22 type ChaincodeRet struct { 23 Code int // 0 success otherwise 1 24 Des string //description 25 }
8.prescriptioninvoke.go代码
1 package operation 2 3 import ( 4 "github.com/hyperledger/fabric/core/chaincode/shim" 5 "github.com/hyperledger/fabric/protos/peer" 6 "fmt" 7 "github.com/prescriptionserver/chaincode/go/prescription/model" 8 "encoding/json" 9 "github.com/prescriptionserver/chaincode/go/prescription/global" 10 "time" 11 ) 12 13 type PrescriptionInvoke struct { 14 Stub shim.ChaincodeStubInterface 15 Args []string 16 } 17 func (p *PrescriptionInvoke) Publish() peer.Response { 18 19 if len(p.Args) != 1{ 20 return shim.Error("Incorrect number of arguments. Expecting 1") 21 } 22 fmt.Println("- start publish prescription") 23 var pbcn model.PrescriptionBlockchain 24 err := json.Unmarshal([]byte(p.Args[0]), &pbcn) 25 if err != nil{ 26 return shim.Error("PrescriptionChaincode Invoke publish unmarshal failed") 27 } 28 pq := PrescriptionQuery{ 29 Stub:p.Stub, 30 } 31 _, exists := pq.GetPbcnByPid(pbcn.PrescriptionID) 32 if exists{ 33 res := GetRetString(1, 34 "PrescriptionChaincode Invoke publish failed : the PrescriptionID has exist ") 35 shim.Error(res) 36 } 37 timestamp, err := p.Stub.GetTxTimestamp() 38 if err != nil{ 39 res := GetRetString(1,"PrescriptionChaincode Invoke publish failed :get time stamp failed") 40 return shim.Error(res) 41 } 42 global.Logger.Error("%s",timestamp) 43 dayTime :=time.Now().Format("2010-11-11") 44 pbcn.State=global.PrescriptionBlockchain_State_NewPublish 45 if !p.put(pbcn){ 46 res :=GetRetString(1,"PrescriptionChaincode Invoke publish put prescription failed") 47 return shim.Error(res) 48 } 49 hospitalNODayTimePrescriptionIDIndexKey,err :=p.Stub.CreateCompositeKey(global.HospitalNO_DayTime_PrescriptionID_IndexName, 50 []string{pbcn.HospitalNO,dayTime,pbcn.PrescriptionID}) 51 if err != nil{ 52 res:=GetRetString(1,"PrescriptionChaincode Invoke publish create"+ 53 " hospitalno_daytime_prescriptionid composite key failed") 54 return shim.Error(res) 55 } 56 err = p.Stub.PutState(hospitalNODayTimePrescriptionIDIndexKey, 57 []byte(time.Now().Format("2017-11-20 12:56:56"))) 58 if err != nil{ 59 res :=GetRetString(1,"PrescriptionChaincode Invoke publish put"+ 60 " hospitalno_daytime_prescriptionid search table failed") 61 return shim.Error(res) 62 } 63 patientIDPrescriptionIDIndexKey,err:=p.Stub.CreateCompositeKey(global.PatientID_PrescriptionID_IndexName, 64 []string{pbcn.PatientID,pbcn.PrescriptionID}) 65 if err != nil{ 66 res:=GetRetString(1,"PrescriptionChaincode Invoke publish"+ 67 " create patientid_prescriptionid composite key failed") 68 return shim.Error(res) 69 } 70 err = p.Stub.PutState(patientIDPrescriptionIDIndexKey,[]byte{0x00}) 71 if err != nil{ 72 res :=GetRetString(1,"PrescriptionChaincode Invoke publish put"+ 73 " patientid_prescriptionid search table failed") 74 return shim.Error(res) 75 } 76 statePrescriptionIDIndexKey,err := p.Stub.CreateCompositeKey(global.State_PrescriptionID_IndexName, 77 []string{pbcn.State,pbcn.PrescriptionID}) 78 if err != nil{ 79 res:=GetRetString(1,"PrescriptionChaincode Invoke publish"+ 80 " create state_prescriptionid composite key failed") 81 return shim.Error(res) 82 } 83 err = p.Stub.PutState(statePrescriptionIDIndexKey,[]byte{0x00}) 84 if err != nil{ 85 res :=GetRetString(1,"PrescriptionChaincode Invoke publish put"+ 86 " state_prescriptionid search table failed") 87 return shim.Error(res) 88 } 89 openPrescriptionDeptIDPrescriptionIDIndexKey,err :=p.Stub.CreateCompositeKey(global.OpenPrescriptionDeptID_PrescriptionID_IndexName, 90 []string{pbcn.OpenPrescriptionDeptID,pbcn.PrescriptionID}) 91 if err != nil{ 92 res :=GetRetString(1,"PrescriptionChaincode Invoke publish"+ 93 "create openprescriptiondeptid_prescriptionid composite key failed") 94 return shim.Error(res) 95 } 96 err = p.Stub.PutState(openPrescriptionDeptIDPrescriptionIDIndexKey,[]byte{0x00}) 97 if err != nil{ 98 res:=GetRetString(1,"PrescriptionChaincode Invoke putlish put"+ 99 "openprescriptiondeptid_prescriptionid composite key failed") 100 return shim.Error(res) 101 } 102 res :=GetRetByte(0,"invoke publish success") 103 return shim.Success(res) 104 } 105 func (p *PrescriptionInvoke) put(pbcn model.PrescriptionBlockchain) bool { 106 bytes, err := json.Marshal(pbcn) 107 if err != nil{ 108 return false 109 } 110 err = p.Stub.PutState(pbcn.PrescriptionID,bytes) 111 if err != nil{ 112 return false 113 } 114 return true 115 } 116 func (p *PrescriptionInvoke) Endorse() peer.Response { 117 if len(p.Args)<3{ 118 res:=GetRetString(1,"PrescriptionChaincode Invoke endorse args<3") 119 return shim.Error(res) 120 } 121 pq := PrescriptionQuery{ 122 Stub:p.Stub, 123 } 124 pbcn, b := pq.GetPbcnByPid(p.Args[0]) 125 if !b{ 126 res:=GetRetString(1,"PrescriptionChaincode Invoke endorse get prescription error") 127 return shim.Error(res) 128 } 129 if pbcn.OpenPrescriptionDoctorID == p.Args[1]{ 130 res:=GetRetString(1,"PrescriptionChaincode Invoke endorse failed:"+ 131 "PendingReceivePrescriptionID should not be same with OpenPrescriptionDoctorID") 132 return shim.Error(res) 133 } 134 if pbcn.ReceivingPrescriptionWorkID==p.Args[1]{ 135 res:=GetRetString(1,"PrescriptionChaincode Invoke endorse failed:"+ 136 "PendingReceivePrescriptionID should not be same with ReceivingPrescriptionWorkID") 137 return shim.Error(res) 138 } 139 pbcn.PendingReceivePrescriptionDeptID=p.Args[1] 140 pbcn.PendingReceivePrescriptionWorkID=p.Args[2] 141 pbcn.State=global.PrescriptionBlockchain_State_EndrWaitSign 142 pbcn.ReceivingPrescriptionDeptID="" 143 pbcn.ReceivingPrescriptionWorkID="" 144 b=p.put(pbcn) 145 if !b{ 146 res:=GetRetString(1,"PrescriptionChaincode Invoke endorse put error") 147 return shim.Error(res) 148 } 149 pendingReceivePrescriptionDeptIDPrescriptionIDIndexKey,err:=p.Stub.CreateCompositeKey(global.PendingReceivePrescriptionDeptID_PrescriptionID_IndexName, 150 []string{pbcn.PendingReceivePrescriptionDeptID,pbcn.PrescriptionID}) 151 if err != nil{ 152 res:=GetRetString(1,"PrescriptionChaincode Invoke endorse"+ 153 " create pendingreceiveprescriptiondeptid_prescriptionid composite key failed") 154 return shim.Error(res) 155 } 156 err = p.Stub.PutState(pendingReceivePrescriptionDeptIDPrescriptionIDIndexKey,[]byte{0x00}) 157 if err != nil { 158 res :=GetRetString(1,"PrescriptionChaincode Invoke endorse"+ 159 " put PendingReceivePrescriptionDeptIDPrescriptionIDIndexKey error") 160 return shim.Error(res) 161 } 162 res:=GetRetByte(0,"invoke endorse success") 163 return shim.Success(res) 164 } 165 func (p *PrescriptionInvoke) Accept() peer.Response { 166 if len(p.Args) <3{ 167 res:=GetRetString(1,"PrescriptionChaincode Invoke accept args<3") 168 return shim.Error(res) 169 } 170 pq:= PrescriptionQuery{ 171 Stub:p.Stub, 172 } 173 pbcn,b:=pq.GetPbcnByPid(p.Args[0]) 174 if !b{ 175 res:=GetRetString(1,"PrescriptionChaincode Invoke accept get prescription error") 176 return shim.Error(res) 177 } 178 if pbcn.PendingReceivePrescriptionDeptID != p.Args[1]{ 179 res:=GetRetString(1,"PrescriptionChaincode Invoke accept error:"+ 180 "PendingReceivePrescriptionDeptID is not consistent with ReceivingPrescriptionDeptID") 181 return shim.Error(res) 182 } 183 if pbcn.PendingReceivePrescriptionWorkID != p.Args[2]{ 184 res:=GetRetString(1,"PrescriptionChaincode Invoke accept error:"+ 185 "PendingReceivePrescriptionWorkID is not consistent with ReceivingPrescriptionWorkID") 186 return shim.Error(res) 187 } 188 pbcn.PendingReceivePrescriptionDeptID="" 189 pbcn.PendingReceivePrescriptionWorkID="" 190 pbcn.ReceivingPrescriptionDeptID=p.Args[1] 191 pbcn.ReceivingPrescriptionWorkID=p.Args[2] 192 pbcn.State=global.PrescriptionBlockchain_state_EndrSigned 193 b = p.put(pbcn) 194 if !b{ 195 res :=GetRetString(1,"PrescriptionChaincode Invoke accept put error") 196 return shim.Error(res) 197 } 198 receivingPrescriptionDeptIDIndexKey,err:=p.Stub.CreateCompositeKey(global.ReceivingPrescriptionDeptID_PrescriptionID_IndexName, 199 []string{pbcn.ReceivingPrescriptionDeptID,pbcn.PrescriptionID}) 200 if err != nil{ 201 res:=GetRetString(1,"PrescriptionChaincode Invoke accept"+ 202 " create receivingprescriptiondeptid_prescriptionid composite key failed") 203 return shim.Error(res) 204 } 205 err=p.Stub.PutState(receivingPrescriptionDeptIDIndexKey,[]byte{0x00}) 206 if err != nil{ 207 res :=GetRetString(1,"PrescriptionChaincode Invoke accept"+ 208 " put ReceivingPrescriptionDeptIDPrescriptionIDIndexKey error") 209 return shim.Error(res) 210 } 211 res:=GetRetByte(0,"invoke accept success") 212 return shim.Success(res) 213 }
9.prescriptionquery.go代码
1 package operation 2 3 import ( 4 "github.com/prescriptionserver/chaincode/go/prescription/model" 5 "github.com/hyperledger/fabric/core/chaincode/shim" 6 "encoding/json" 7 "github.com/hyperledger/fabric/protos/peer" 8 "github.com/prescriptionserver/chaincode/go/prescription/global" 9 "bytes" 10 ) 11 12 type PrescriptionQuery struct { 13 Stub shim.ChaincodeStubInterface 14 Args []string 15 } 16 17 func (p *PrescriptionQuery) GetPbcnByPid(pid string)(model.PrescriptionBlockchain,bool) { 18 var pbcn model.PrescriptionBlockchain 19 bytes, err := p.Stub.GetState(pid) 20 if bytes == nil { 21 return pbcn, false 22 } 23 err =json.Unmarshal(bytes,&pbcn) 24 if err != nil{ 25 return pbcn,false 26 } 27 return pbcn,true 28 } 29 func (p *PrescriptionQuery) GetPbcnsByOpdid() peer.Response { 30 if len(p.Args) != 1{ 31 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyopdid args!=1") 32 return shim.Error(res) 33 } 34 pspnsIterator,err :=p.Stub.GetStateByPartialCompositeKey(global.OpenPrescriptionDeptID_PrescriptionID_IndexName, 35 []string{p.Args[0]}) 36 if err != nil{ 37 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyopdid"+ 38 "getstatebypartialcompositekey error") 39 return shim.Error(res) 40 } 41 defer pspnsIterator.Close() 42 var pbcns []model.PrescriptionBlockchain 43 for pspnsIterator.HasNext() { 44 kv,err:=pspnsIterator.Next() 45 if err != nil{ 46 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyopdid iterator.next error") 47 return shim.Error(res) 48 } 49 _, compositeKey, err := p.Stub.SplitCompositeKey(kv.Key) 50 if err != nil{ 51 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyopdid"+ 52 "split composite key error") 53 return shim.Error(res) 54 } 55 if len(compositeKey) <2{ 56 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyopdid"+ 57 "composite keys len<2") 58 return shim.Error(res) 59 } 60 pbcn, b := p.GetPbcnByPid(compositeKey[1]) 61 if !b{ 62 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyopdid getpbcnbypid error") 63 return shim.Error(res) 64 } 65 pbcns =append(pbcns,pbcn) 66 } 67 pbcndata,err :=json.Marshal(pbcns) 68 if err != nil{ 69 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyopdid json marshal error") 70 return shim.Error(res) 71 } 72 return shim.Success(pbcndata) 73 } 74 func (p *PrescriptionQuery) GetPbcnsByPrpdid() peer.Response { 75 if len(p.Args) != 1{ 76 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyprpdid args!=1") 77 return shim.Error(res) 78 } 79 pspnsIterator,err :=p.Stub.GetStateByPartialCompositeKey( 80 global.PendingReceivePrescriptionDeptID_PrescriptionID_IndexName, 81 []string{p.Args[0]}) 82 if err != nil{ 83 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyprpdid"+ 84 "getstatebypartialcompositekey error") 85 return shim.Error(res) 86 } 87 defer pspnsIterator.Close() 88 var pbcns []model.PrescriptionBlockchain 89 for pspnsIterator.HasNext() { 90 kv,err:=pspnsIterator.Next() 91 if err != nil{ 92 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyprpdid iterator.next error") 93 return shim.Error(res) 94 } 95 _, compositeKey, err := p.Stub.SplitCompositeKey(kv.Key) 96 if err != nil{ 97 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyprpdid"+ 98 "split composite key error") 99 return shim.Error(res) 100 } 101 if len(compositeKey) <2{ 102 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyprpdid"+ 103 "composite keys len<2") 104 return shim.Error(res) 105 } 106 pbcn, b := p.GetPbcnByPid(compositeKey[1]) 107 if !b{ 108 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyprpdid getpbcnbypid error") 109 return shim.Error(res) 110 } 111 pbcns =append(pbcns,pbcn) 112 } 113 pbcndata,err :=json.Marshal(pbcns) 114 if err != nil{ 115 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyprpdid json marshal error") 116 return shim.Error(res) 117 } 118 return shim.Success(pbcndata) 119 } 120 func (p *PrescriptionQuery) GetPbcnsByState() peer.Response { 121 if len(p.Args) != 1{ 122 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbystate args!=1") 123 return shim.Error(res) 124 } 125 pspnsIterator,err :=p.Stub.GetStateByPartialCompositeKey(global.State_PrescriptionID_IndexName, 126 []string{p.Args[0]}) 127 if err != nil{ 128 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbystate"+ 129 "getstatebypartialcompositekey error") 130 return shim.Error(res) 131 } 132 defer pspnsIterator.Close() 133 var pbcns []model.PrescriptionBlockchain 134 for pspnsIterator.HasNext() { 135 kv,err:=pspnsIterator.Next() 136 if err != nil{ 137 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbystate iterator.next error") 138 return shim.Error(res) 139 } 140 _, compositeKey, err := p.Stub.SplitCompositeKey(kv.Key) 141 if err != nil{ 142 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbystate"+ 143 "split composite key error") 144 return shim.Error(res) 145 } 146 if len(compositeKey) <2{ 147 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbystate"+ 148 "composite keys len<2") 149 return shim.Error(res) 150 } 151 pbcn, b := p.GetPbcnByPid(compositeKey[1]) 152 if !b{ 153 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbystate getpbcnbypid error") 154 return shim.Error(res) 155 } 156 pbcns =append(pbcns,pbcn) 157 } 158 pbcndata,err :=json.Marshal(pbcns) 159 if err != nil{ 160 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbystate json marshal error") 161 return shim.Error(res) 162 } 163 return shim.Success(pbcndata) 164 } 165 func (p *PrescriptionQuery) GetPbcnsByPtnid() peer.Response { 166 if len(p.Args) != 1{ 167 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyptnid args!=1") 168 return shim.Error(res) 169 } 170 pspnsIterator,err :=p.Stub.GetStateByPartialCompositeKey(global.PatientID_PrescriptionID_IndexName, 171 []string{p.Args[0]}) 172 if err != nil{ 173 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyptnid"+ 174 "getstatebypartialcompositekey error") 175 return shim.Error(res) 176 } 177 defer pspnsIterator.Close() 178 var pbcns []model.PrescriptionBlockchain 179 for pspnsIterator.HasNext() { 180 kv,err:=pspnsIterator.Next() 181 if err != nil{ 182 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyptnid iterator.next error") 183 return shim.Error(res) 184 } 185 _, compositeKey, err := p.Stub.SplitCompositeKey(kv.Key) 186 if err != nil{ 187 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyptnid"+ 188 "split composite key error") 189 return shim.Error(res) 190 } 191 if len(compositeKey) <2{ 192 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyptnid"+ 193 "composite keys len<2") 194 return shim.Error(res) 195 } 196 pbcn, b := p.GetPbcnByPid(compositeKey[1]) 197 if !b{ 198 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyptnid getpbcnbypid error") 199 return shim.Error(res) 200 } 201 pbcns =append(pbcns,pbcn) 202 } 203 pbcndata,err :=json.Marshal(pbcns) 204 if err != nil{ 205 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyptnid json marshal error") 206 return shim.Error(res) 207 } 208 return shim.Success(pbcndata) 209 } 210 func (p *PrescriptionQuery) GetPbcnsByRpdid() peer.Response { 211 if len(p.Args) != 1{ 212 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyrpdid args!=1") 213 return shim.Error(res) 214 } 215 pspnsIterator,err :=p.Stub.GetStateByPartialCompositeKey( 216 global.ReceivingPrescriptionDeptID_PrescriptionID_IndexName, 217 []string{p.Args[0]}) 218 if err != nil{ 219 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyrpdid"+ 220 "getstatebypartialcompositekey error") 221 return shim.Error(res) 222 } 223 defer pspnsIterator.Close() 224 var pbcns []model.PrescriptionBlockchain 225 for pspnsIterator.HasNext() { 226 kv,err:=pspnsIterator.Next() 227 if err != nil{ 228 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyrpdid iterator.next error") 229 return shim.Error(res) 230 } 231 _, compositeKey, err := p.Stub.SplitCompositeKey(kv.Key) 232 if err != nil{ 233 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyrpdid"+ 234 "split composite key error") 235 return shim.Error(res) 236 } 237 if len(compositeKey) <2{ 238 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyrpdid"+ 239 "composite keys len<2") 240 return shim.Error(res) 241 } 242 pbcn, b := p.GetPbcnByPid(compositeKey[1]) 243 if !b{ 244 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyrpdid getpbcnbypid error") 245 return shim.Error(res) 246 } 247 pbcns =append(pbcns,pbcn) 248 } 249 pbcndata,err :=json.Marshal(pbcns) 250 if err != nil{ 251 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyrpdid json marshal error") 252 return shim.Error(res) 253 } 254 return shim.Success(pbcndata) 255 } 256 func (p *PrescriptionQuery) GetPbcnsByHndt() peer.Response { 257 if len(p.Args) !=2{ 258 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyhndt args !=2") 259 return shim.Error(res) 260 } 261 pspnsIterator,err :=p.Stub.GetStateByPartialCompositeKey(global.HospitalNO_DayTime_PrescriptionID_IndexName, 262 []string{p.Args[0],p.Args[1]}) 263 if err != nil{ 264 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyhndt"+ 265 "getstatebypartialcompositekey error") 266 return shim.Error(res) 267 } 268 defer pspnsIterator.Close() 269 var pbcns []model.PrescriptionBlockchain 270 for pspnsIterator.HasNext() { 271 kv,err:=pspnsIterator.Next() 272 if err != nil{ 273 res:=GetRetString(1,"PrescriptionChaincode query getpbcnsbyhndt iterator.next error") 274 return shim.Error(res) 275 } 276 _, compositeKey, err := p.Stub.SplitCompositeKey(kv.Key) 277 if err != nil{ 278 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyhndt"+ 279 "split composite key error") 280 return shim.Error(res) 281 } 282 if len(compositeKey) <3{ 283 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyhndt"+ 284 "composite keys len<3") 285 return shim.Error(res) 286 } 287 pbcn, b := p.GetPbcnByPid(compositeKey[2]) 288 if !b{ 289 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyhndt getpbcnbypid error") 290 return shim.Error(res) 291 } 292 pbcns =append(pbcns,pbcn) 293 } 294 pbcndata,err :=json.Marshal(pbcns) 295 if err != nil{ 296 res :=GetRetString(1,"PrescriptionChaincode query getpbcnsbyhndt json marshal error") 297 return shim.Error(res) 298 } 299 return shim.Success(pbcndata) 300 } 301 func (p *PrescriptionQuery) GetHistoryByPid() peer.Response { 302 if len(p.Args) != 1{ 303 res :=GetRetString(1,"PrescriptionChaincode query gethistorybypid args!=1") 304 return shim.Error(res) 305 } 306 pbcn, b := p.GetPbcnByPid(p.Args[0]) 307 if !b{ 308 res :=GetRetString(1,"PrescriptionChaincode query gethistorybypid get prescription error") 309 return shim.Error(res) 310 } 311 historyIterator, err := p.Stub.GetHistoryForKey(p.Args[0]) 312 if err != nil{ 313 res:=GetRetString(1,"PrescriptionChaincode query gethistorybypid"+ 314 "get history for key error") 315 return shim.Error(res) 316 } 317 defer historyIterator.Close() 318 var historyItems []model.HistoryItem 319 var hispbcn model.PrescriptionBlockchain 320 for historyIterator.HasNext(){ 321 hisData, err := historyIterator.Next() 322 if err != nil{ 323 res :=GetRetString(1,"PrescriptionChaincode query gethistorybypid"+ 324 "history iterator next error") 325 return shim.Error(res) 326 } 327 hisItem :=model.HistoryItem{ 328 TxId:hisData.TxId, 329 } 330 if hisData.Value ==nil{ 331 hisItem.PrescriptionBlockchain=model.PrescriptionBlockchain{} 332 }else { 333 err = json.Unmarshal(hisData.Value, &hispbcn) 334 if err != nil { 335 res := GetRetString(1, "PrescriptionChaincode query gethistorybypid"+ 336 "json unmarshal error") 337 return shim.Error(res) 338 } 339 hisItem.PrescriptionBlockchain=hispbcn 340 } 341 historyItems = append(historyItems,hisItem) 342 } 343 pbcn.History=historyItems 344 pbcndata,err :=json.Marshal(pbcn) 345 if err != nil{ 346 res := GetRetString(1, "PrescriptionChaincode query gethistorybypid"+ 347 "json marshal error") 348 return shim.Error(res) 349 } 350 return shim.Success(pbcndata) 351 } 352 func (p *PrescriptionQuery) GetPbcnByPrescriptionID() peer.Response { 353 pbcn,b:=p.GetPbcnByPid(p.Args[0]) 354 if !b{ 355 res :=GetRetString(1,"PrescriptionChaincode query getpbcnbyprescriptionid get pbcn error") 356 return shim.Error(res) 357 } 358 pbcnData, err := json.Marshal(pbcn) 359 if err != nil{ 360 res :=GetRetString(1,"PrescriptionChaincode query getpbcnbyprescriptionid json marshal error") 361 return shim.Error(res) 362 } 363 return shim.Success(pbcnData) 364 } 365 func (p *PrescriptionQuery) GetResultByQueryString() peer.Response { 366 if len(p.Args) != 1{ 367 res :=GetRetString(1,"PrescriptionChaincode query getresultbyquerystring args!=1") 368 return shim.Error(res) 369 } 370 resultsIterator, err := p.Stub.GetQueryResult(p.Args[0]) 371 if err != nil { 372 res :=GetRetString(1,"PrescriptionChaincode query getresultbyquerystring"+ 373 "get query result error") 374 return shim.Error(res) 375 } 376 defer resultsIterator.Close() 377 378 var datas bytes.Buffer 379 datas.WriteString("[") 380 381 arrayState := false 382 for resultsIterator.HasNext() { 383 queryData, err := resultsIterator.Next() 384 if err != nil { 385 res :=GetRetString(1,"PrescriptionChaincode query getresultbyquerystring"+ 386 "results iterator next error") 387 return shim.Error(res) 388 } 389 if arrayState == true { 390 datas.WriteString(",") 391 } 392 datas.WriteString("{\"Key\":") 393 datas.WriteString("\"") 394 datas.WriteString(queryData.Key) 395 datas.WriteString("\"") 396 397 datas.WriteString(", \"Record\":") 398 // Record is a JSON object, so we write as-is 399 datas.WriteString(string(queryData.Value)) 400 datas.WriteString("}") 401 arrayState = true 402 } 403 datas.WriteString("]") 404 405 return shim.Success(datas.Bytes()) 406 }
10.prescriptionchaincode.go代码
1 package main 2 3 import ( 4 "github.com/hyperledger/fabric/core/chaincode/shim" 5 "log" 6 "github.com/hyperledger/fabric/protos/peer" 7 "github.com/prescriptionserver/chaincode/go/prescription/global" 8 "github.com/prescriptionserver/chaincode/go/prescription/operation" 9 ) 10 type PrescriptionChaincode struct { 11 12 } 13 14 func (p PrescriptionChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response { 15 return shim.Success(nil) 16 } 17 func (p *PrescriptionChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response { 18 function,args := stub.GetFunctionAndParameters() 19 global.Logger.Info("%s%s","PrescriptionChaincode function=",function) 20 global.Logger.Info("%s%s","PrescriptionChaincode args=",args) 21 pi :=operation.PrescriptionInvoke{ 22 Stub:stub, 23 Args:args, 24 } 25 pq := operation.PrescriptionQuery{ 26 Stub:stub, 27 Args:args, 28 } 29 if function == "publish" { 30 return pi.Publish() 31 } else if function == "endorse" { 32 return pi.Endorse() 33 } else if function == "accept" { 34 return pi.Accept() 35 } 36 37 if function == "getpbcnbypid" { 38 return pq.GetPbcnByPrescriptionID() 39 } else if function == "getpbcnsbyopdid" { 40 return pq.GetPbcnsByOpdid() 41 } else if function == "getpbcnsbyprpdid" { 42 return pq.GetPbcnsByPrpdid() 43 }else if function == "getpbcnsbystate" { 44 return pq.GetPbcnsByState() 45 }else if function == "getpbcnsbyptnid" { 46 return pq.GetPbcnsByPtnid() 47 }else if function == "getpbcnsbyrpdid" { 48 return pq.GetPbcnsByRpdid() 49 }else if function == "getpbcnsbyhndt" { 50 return pq.GetPbcnsByHndt() 51 }else if function == "gethistorybypid" { 52 return pq.GetHistoryByPid() 53 }else if function == "getresultbyquerystring" { 54 return pq.GetResultByQueryString() 55 } 56 57 res := operation.GetRetString(1,"PrescriptionChaincode Unkown method!") 58 global.Logger.Info("%s",res) 59 global.Logger.Infof("%s",res) 60 return shim.Error(res) 61 } 62 63 64 func main() { 65 if err := shim.Start(new (PrescriptionChaincode));err != nil{ 66 log.Printf("error starting prescription chaincode: %v",err) 67 } 68 }
go client sdk的相关开发以后有时间补全吧,chaincode sdk相关的内容有时间了再补