-
Notifications
You must be signed in to change notification settings - Fork 0
/
fruit-info-contract.go
750 lines (687 loc) · 27.7 KB
/
fruit-info-contract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
* SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
"realChainCode/util"
"time"
)
// FruitInfoContract contract for managing CRUD for FruitInfo
type FruitInfoContract struct {
contractapi.Contract
}
// FruitInfoExists returns true when asset with given ID exists in world state
func (c *FruitInfoContract) FruitInfoExists(ctx contractapi.TransactionContextInterface, fruitInfoID string) (bool, error) {
data, err := ctx.GetStub().GetState(fruitInfoID)
if err != nil {
return false, err
}
return data != nil, nil
}
// CreateFruitInfo creates a new instance of FruitInfo
func (c *FruitInfoContract) CreateFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
if len(args) != 29 {
return fmt.Errorf("collect args num should be 29, but now is %d", len(args))
}
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if exists {
return fmt.Errorf("The asset %s already exists", fruitInfoID)
}
var collectInfo CollectInfo
fruitInfo := new(FruitInfo)
fruitInfo.ID = fruitInfoID
collectInfo.CollectID = args[0]
collectInfo.Type = args[1]
collectInfo.Name = args[2]
collectInfo.GermplasmName = args[3]
collectInfo.GermplasmNameEn = args[4]
collectInfo.SectionName = args[5]
collectInfo.GenericName = args[6]
collectInfo.ScientificName = args[7]
collectInfo.ResourceType = args[8]
collectInfo.CollectMethod = args[9]
collectInfo.GermplasmSource = args[10]
collectInfo.SourceCountry = args[11]
collectInfo.SourceProvince = args[12]
collectInfo.Source = args[13]
collectInfo.SourceOrg = args[14]
collectInfo.OriginCountry = args[15]
collectInfo.OriginPlace = args[16]
collectInfo.CollectPlaceLongitude = args[17]
collectInfo.CollectPlaceLatitude = args[18]
collectInfo.CollectPlaceAltitude = args[19]
collectInfo.CollectPlaceSoilType = args[20]
collectInfo.CollectPlaceEcologyType = args[21]
collectInfo.CollectMaterialType = args[22]
collectInfo.CollectPeople = args[23]
collectInfo.CollectUnit = args[24]
collectInfo.CollectTime = args[25]
collectInfo.SpeciesName = args[26]
collectInfo.Image = args[27]
collectInfo.CollectRemark = args[28]
collectInfo.CollectHash = util.GetSHA256String(args)
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.CollectInfo = collectInfo
fruitInfo.Status = "0"
fruitInfo.IsContradict = "0"
fruitInfo.IsDeleted = "0"
fruitInfo.IsLoaded = "0"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) RejectCreate(ctx contractapi.TransactionContextInterface, fruitInfoID string) error {
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not reject createinfo, because the info is already loaded")
}
if fruitInfo.Status != "0" {
return fmt.Errorf("Could not reject create, because the status is %s, but we except 0. %s", fruitInfo.Status, err)
}
createTime, _ := ctx.GetStub().GetTxTimestamp()
fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.IsContradict = "1"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) ModifyCreateFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
if len(args) != 29 {
return fmt.Errorf("collect args num should be 29, but now is %d", len(args))
}
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not modify createInfo, because the info is already loaded")
}
var collectInfo CollectInfo
collectInfo.CollectID = args[0]
collectInfo.Type = args[1]
collectInfo.Name = args[2]
collectInfo.GermplasmName = args[3]
collectInfo.GermplasmNameEn = args[4]
collectInfo.SectionName = args[5]
collectInfo.GenericName = args[6]
collectInfo.ScientificName = args[7]
collectInfo.ResourceType = args[8]
collectInfo.CollectMethod = args[9]
collectInfo.GermplasmSource = args[10]
collectInfo.SourceCountry = args[11]
collectInfo.SourceProvince = args[12]
collectInfo.Source = args[13]
collectInfo.SourceOrg = args[14]
collectInfo.OriginCountry = args[15]
collectInfo.OriginPlace = args[16]
collectInfo.CollectPlaceLongitude = args[17]
collectInfo.CollectPlaceLatitude = args[18]
collectInfo.CollectPlaceAltitude = args[19]
collectInfo.CollectPlaceSoilType = args[20]
collectInfo.CollectPlaceEcologyType = args[21]
collectInfo.CollectMaterialType = args[22]
collectInfo.CollectPeople = args[23]
collectInfo.CollectUnit = args[24]
collectInfo.CollectTime = args[25]
collectInfo.SpeciesName = args[26]
collectInfo.Image = args[27]
collectInfo.CollectRemark = args[28]
collectInfo.CollectHash = util.GetSHA256String(args)
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.CollectInfo = collectInfo
fruitInfo.Status = "0"
fruitInfo.IsContradict = "0"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) SaveFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
if len(args) != 14 {
return fmt.Errorf("collect args num should be 14, but now is %d", len(args))
}
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not add saveInfo, because the info is already loaded")
}
if fruitInfo.Status != "0" {
return fmt.Errorf("we expect status from 0 to 1, but the status now is %s", fruitInfo.Status)
}
var saveInfo SaveInfo
saveInfo.MainPreference = args[0]
saveInfo.MainUse = args[1]
saveInfo.PreservationFacility = args[2]
saveInfo.GermplasmType = args[3]
saveInfo.SaveQuantity = args[4]
saveInfo.MeasuringUnit = args[5]
saveInfo.SaveUnit = args[6]
saveInfo.SaveVault = args[7]
saveInfo.SavePlace = args[8]
saveInfo.WarehousingYear = args[9]
saveInfo.SaveProperty = args[10]
saveInfo.ResourceDescription = args[11]
saveInfo.ResourceRemark = args[12]
saveInfo.GermplasmImage = args[13]
saveInfo.SaveHash = util.GetSHA256String(args)
fruitInfo.SaveInfo = saveInfo
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.Status = "1"
fruitInfo.IsContradict = "0"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) RejectSave(ctx contractapi.TransactionContextInterface, fruitInfoID string) error {
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not reject saveInfo, because the info is already loaded")
}
if fruitInfo.Status != "1" {
return fmt.Errorf("Could not reject save, because the status is %s, but we except 1.", fruitInfo.Status)
}
createTime, _ := ctx.GetStub().GetTxTimestamp()
fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.IsContradict = "1"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) ModifySaveFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
if len(args) != 14 {
return fmt.Errorf("collect args num should be 14, but now is %d", len(args))
}
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not modify saveInfo, because the info is already loaded")
}
var saveInfo SaveInfo
saveInfo.MainPreference = args[0]
saveInfo.MainUse = args[1]
saveInfo.PreservationFacility = args[2]
saveInfo.GermplasmType = args[3]
saveInfo.SaveQuantity = args[4]
saveInfo.MeasuringUnit = args[5]
saveInfo.SaveUnit = args[6]
saveInfo.SaveVault = args[7]
saveInfo.SavePlace = args[8]
saveInfo.WarehousingYear = args[9]
saveInfo.SaveProperty = args[10]
saveInfo.ResourceDescription = args[11]
saveInfo.ResourceRemark = args[12]
saveInfo.GermplasmImage = args[13]
saveInfo.SaveHash = util.GetSHA256String(args)
fruitInfo.SaveInfo = saveInfo
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.Status = "1"
fruitInfo.IsContradict = "0"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) EnterFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
if len(args) != 6 {
return fmt.Errorf("collect args num should be 6, but now is %d", len(args))
}
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not add enterinfo, because the info is already loaded")
}
if fruitInfo.Status != "1" {
return fmt.Errorf("we expect status from 1 to 2, but the status now is %s", fruitInfo.Status)
}
var enterInfo EnterInfo
enterInfo.Certifier = args[0]
enterInfo.CertifyOrg = args[1]
enterInfo.CertifyPlace = args[2]
enterInfo.CertifyYear = args[3]
enterInfo.OperationRange = args[4]
enterInfo.EnterRemark = args[5]
enterInfo.EnterHash = util.GetSHA256String(args)
fruitInfo.EnterInfo = enterInfo
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.Status = "2"
fruitInfo.IsContradict = "0"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) RejectEnter(ctx contractapi.TransactionContextInterface, fruitInfoID string) error {
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not reject enterinfo, because the info is already loaded")
}
if fruitInfo.Status != "2" {
return fmt.Errorf("Could not reject enter, because the status is %s, but we except 2.", fruitInfo.Status)
}
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.IsContradict = "1"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) ModifyEnterFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
if len(args) != 6 {
return fmt.Errorf("collect args num should be 6, but now is %d", len(args))
}
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not modify enterInfo, because the info is already loaded")
}
var enterInfo EnterInfo
enterInfo.Certifier = args[0]
enterInfo.CertifyOrg = args[1]
enterInfo.CertifyPlace = args[2]
enterInfo.CertifyYear = args[3]
enterInfo.OperationRange = args[4]
enterInfo.EnterRemark = args[5]
enterInfo.EnterHash = util.GetSHA256String(args)
fruitInfo.EnterInfo = enterInfo
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.Status = "2"
fruitInfo.IsContradict = "0"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) ShareFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
if len(args) != 7 {
return fmt.Errorf("collect args num should be 7, but now is %d", len(args))
}
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not add shareinfo, because the info is already loaded")
}
if fruitInfo.Status != "2" {
return fmt.Errorf("we expect status from 2 to 3, but the status now is %s", fruitInfo.Status)
}
var shareInfo ShareInfo
shareInfo.ShareObj = args[0]
shareInfo.ContactInfo = args[1]
shareInfo.ShareMode = args[2]
shareInfo.ShareUse = args[3]
shareInfo.ShareNum = args[4]
shareInfo.ShareBeginTime = args[5]
shareInfo.ShareEndTime = args[6]
shareInfo.ShareHash = util.GetSHA256String(args)
fruitInfo.ShareInfo = shareInfo
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.Status = "3"
fruitInfo.IsContradict = "0"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) RejectShare(ctx contractapi.TransactionContextInterface, fruitInfoID string) error {
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not reject shareinfo, because the info is already loaded")
}
if fruitInfo.Status != "3" {
return fmt.Errorf("Could not reject share, because the status is %s, but we except 3.", fruitInfo.Status)
}
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.IsContradict = "1"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) ModifyShareFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
if len(args) != 7 {
return fmt.Errorf("collect args num should be 7, but now is %d", len(args))
}
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not modify shareInfo, because the info is already loaded")
}
var shareInfo ShareInfo
shareInfo.ShareObj = args[0]
shareInfo.ContactInfo = args[1]
shareInfo.ShareMode = args[2]
shareInfo.ShareUse = args[3]
shareInfo.ShareNum = args[4]
shareInfo.ShareBeginTime = args[5]
shareInfo.ShareEndTime = args[6]
shareInfo.ShareHash = util.GetSHA256String(args)
fruitInfo.ShareInfo = shareInfo
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.Status = "3"
fruitInfo.IsContradict = "0"
bytes, _ := json.Marshal(fruitInfo)
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
func (c *FruitInfoContract) LoadFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string) error {
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("could not load, because the info is already loaded")
}
if fruitInfo.Status != "3" {
return fmt.Errorf("we expect status from 3 to 4, but the status now is %s", fruitInfo.Status)
}
//createTime, _ := ctx.GetStub().GetTxTimestamp()
//fruitInfo.CreateTime = time.Unix(createTime.GetSeconds(), int64(createTime.GetNanos())).Local().Format("2006-01-02 15:04:05")
//fruitInfo.TxId = ctx.GetStub().GetTxID()
fruitInfo.IsLoaded = "1"
bytes, _ := json.Marshal(fruitInfo)
//ctx.GetStub().GetTxID()
return ctx.GetStub().PutState(fruitInfoID, bytes)
}
// SetCollectInfo retrieves an instance of FruitInfo from the world state and set collection information for it
//func (c *FruitInfoContract) SetCollectInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
// if len(args) != 11 {
// return fmt.Errorf("collect args num should be 11, but now is %d", len(args))
// }
// exists, err := c.FruitInfoExists(ctx, fruitInfoID)
// if err != nil {
// return fmt.Errorf("Could not read from world state. %s", err)
// } else if !exists {
// return fmt.Errorf("The asset %s does not exist", fruitInfoID)
// }
// var collectInfo CollectInfo
// collectInfo.CollectPlaceLongitude = args[0]
// collectInfo.CollectPlaceLatitude = args[1]
// collectInfo.CollectPlaceAltitude = args[2]
// collectInfo.CollectPlaceSoilType = args[3]
// collectInfo.CollectPlaceEcologyType = args[4]
// collectInfo.CollectMaterialType = args[5]
// collectInfo.CollectPeople = args[6]
// collectInfo.CollectUnit = args[7]
// collectInfo.CollectTime = args[8]
// collectInfo.SpeciesName = args[9]
// collectInfo.Image = args[10]
// fruitInfo := new(FruitInfo)
// fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
// fruitInfo.CollectInfo = collectInfo
// bytes, _ := json.Marshal(fruitInfo)
// return ctx.GetStub().PutState(fruitInfoID, bytes)
//}
// SetSpeciesInfo retrieves an instance of FruitInfo from the world state and set species information for it
//func (c *FruitInfoContract) SetSpeciesInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
// if len(args) != 10 {
// return fmt.Errorf("speciesInfo args num should be 10, but now is %d", len(args))
// }
// exists, err := c.FruitInfoExists(ctx, fruitInfoID)
// if err != nil {
// return fmt.Errorf("could not read from world state. %s", err)
// } else if !exists {
// return fmt.Errorf("the asset %s does not exist", fruitInfoID)
// }
// var speciesInfo SpeciesInfo
// speciesInfo.Type = args[0]
// speciesInfo.Name = args[1]
// speciesInfo.GermplasmName = args[2]
// speciesInfo.GermplasmNameEn = args[3]
// speciesInfo.SectionName = args[4]
// speciesInfo.GenericName = args[5]
// speciesInfo.ScientificName = args[6]
// speciesInfo.ResourceType = args[7]
// speciesInfo.CollectMethod = args[8]
// speciesInfo.GermplasmSource = args[9]
// fruitInfo := new(FruitInfo)
// fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
// fruitInfo.SpeciesInfo = speciesInfo
// bytes, _ := json.Marshal(fruitInfo)
// return ctx.GetStub().PutState(fruitInfoID, bytes)
//}
// SetSourceInfo retrieves an instance of FruitInfo from the world state and set source information for it
//func (c *FruitInfoContract) SetSourceInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string, args []string) error {
// if len(args) != 6 {
// return fmt.Errorf("speciesInfo args num should be 6, but now is %d", len(args))
// }
// exists, err := c.FruitInfoExists(ctx, fruitInfoID)
// if err != nil {
// return fmt.Errorf("could not read from world state. %s", err)
// } else if !exists {
// return fmt.Errorf("the asset %s does not exist", fruitInfoID)
// }
// var sourceInfo SourceInfo
// sourceInfo.SourceCountry = args[0]
// sourceInfo.SourceProvince = args[1]
// sourceInfo.Source = args[2]
// sourceInfo.SourceOrg = args[3]
// sourceInfo.OriginCountry = args[4]
// sourceInfo.OriginPlace = args[5]
// fruitInfo := new(FruitInfo)
// fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
// fruitInfo.SourceInfo = sourceInfo
// bytes, _ := json.Marshal(fruitInfo)
// return ctx.GetStub().PutState(fruitInfoID, bytes)
//}
// UpdateProcessID retrieves an instance of FruitInfo from the world state and update its processID
//func (c *FruitInfoContract) UpdateProcessID(ctx contractapi.TransactionContextInterface, fruitInfoID string, processID string) error {
// exists, err := c.FruitInfoExists(ctx, fruitInfoID)
// if err != nil {
// return fmt.Errorf("Could not read from world state. %s", err)
// } else if !exists {
// return fmt.Errorf("The asset %s does not exist", fruitInfoID)
// }
// fruitInfo := new(FruitInfo)
// fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
// fruitInfo.ProcessInstanceID = processID
// bytes, _ := json.Marshal(fruitInfo)
// return ctx.GetStub().PutState(fruitInfoID, bytes)
//}
// UpdateCollectID retrieves an instance of FruitInfo from the world state and update its collectID
//func (c *FruitInfoContract) UpdateCollectID(ctx contractapi.TransactionContextInterface, fruitInfoID string, collectID string) error {
// exists, err := c.FruitInfoExists(ctx, fruitInfoID)
// if err != nil {
// return fmt.Errorf("Could not read from world state. %s", err)
// } else if !exists {
// return fmt.Errorf("The asset %s does not exist", fruitInfoID)
// }
// fruitInfo := new(FruitInfo)
// fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
// fruitInfo.CollectID = collectID
// bytes, _ := json.Marshal(fruitInfo)
// return ctx.GetStub().PutState(fruitInfoID, bytes)
//}
// ReadFruitInfo retrieves an instance of FruitInfo from the world state
func (c *FruitInfoContract) ReadFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string) (*FruitInfo, error) {
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return nil, fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return nil, fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
bytes, _ := ctx.GetStub().GetState(fruitInfoID)
fruitInfo := new(FruitInfo)
err = json.Unmarshal(bytes, fruitInfo)
if err != nil {
return nil, fmt.Errorf("Could not unmarshal world state data to type FruitInfo")
}
return fruitInfo, nil
}
// ReadFruitInfoByRange retrieves an instance of FruitInfo from the world state by range
func (c *FruitInfoContract) ReadFruitInfoByRange(ctx contractapi.TransactionContextInterface, fruitInfoIDStart string, fruitInfoIDEnd string) ([]*FruitInfo, error) {
resultsIterator, err := ctx.GetStub().GetStateByRange(fruitInfoIDStart, fruitInfoIDEnd)
if err != nil {
return nil, err
}
defer func(resultsIterator shim.StateQueryIteratorInterface) {
err := resultsIterator.Close()
if err != nil {
return
}
}(resultsIterator)
var fruitInfos []*FruitInfo
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, fmt.Errorf("ite.next dont exists, %s", err)
}
fruitInfo := new(FruitInfo)
err = json.Unmarshal(queryResponse.Value, fruitInfo)
if err != nil {
return nil, fmt.Errorf("unmarshl error, %s", err)
}
fruitInfos = append(fruitInfos, fruitInfo)
}
return fruitInfos, nil
}
// ReadHistory retrieves an instance of FruitInfo from the world state and return its alter history
func (c *FruitInfoContract) ReadHistory(ctx contractapi.TransactionContextInterface, fruitInfoID string) ([]*FruitInfo, error) {
historyIterator, err := ctx.GetStub().GetHistoryForKey(fruitInfoID)
if err != nil {
return nil, fmt.Errorf("can not get history for key, %s", err)
}
defer func(historyIterator shim.HistoryQueryIteratorInterface) {
err := historyIterator.Close()
if err != nil {
return
}
}(historyIterator)
var fruitInfos []*FruitInfo
//var records []HistoryQueryResult
for historyIterator.HasNext() {
queryResponse, err := historyIterator.Next()
if err != nil {
return nil, fmt.Errorf("ite.next dont exists, %s", err)
}
var fruitInfo FruitInfo
if len(queryResponse.Value) > 0 {
err = json.Unmarshal(queryResponse.Value, &fruitInfo)
if err != nil {
return nil, fmt.Errorf("unmarshl error, %s", err)
}
} else {
fruitInfo = FruitInfo{
ID: fruitInfoID,
IsDeleted: "1",
}
}
//timestamp, err := ptypes.Timestamp(queryResponse.Timestamp)
//if err != nil {
// return nil, err
//}
//record := HistoryQueryResult{
// TxId: queryResponse.TxId,
// Timestamp: timestamp,
// Record: &fruitInfo,
// IsDelete: queryResponse.IsDelete,
//}
//records = append(records, record)
fruitInfos = append(fruitInfos, &fruitInfo)
}
return fruitInfos, nil
}
// DeleteFruitInfo deletes an instance of FruitInfo from the world state
func (c *FruitInfoContract) DeleteFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string) error {
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
fruitInfo := new(FruitInfo)
fruitInfo, _ = c.ReadFruitInfo(ctx, fruitInfoID)
if fruitInfo.IsLoaded != "0" {
return fmt.Errorf("you cannot delete info which is already loaded")
}
return ctx.GetStub().DelState(fruitInfoID)
}
// ProgrammerDeleteFruitInfo programmer delete the whole info
func (c *FruitInfoContract) ProgrammerDeleteFruitInfo(ctx contractapi.TransactionContextInterface, fruitInfoID string) error {
exists, err := c.FruitInfoExists(ctx, fruitInfoID)
if err != nil {
return fmt.Errorf("Could not read from world state. %s", err)
} else if !exists {
return fmt.Errorf("The asset %s does not exist", fruitInfoID)
}
return ctx.GetStub().DelState(fruitInfoID)
}