block_chain.go
6.4 KB
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
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/blockchain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
"strconv"
"time"
)
// 区块链服务
type BlockChainService struct {
}
// 数据上链
func (blockChainService *BlockChainService) UpChain(upChainCommand *command.UpChainCommand) (interface{}, error) {
if err := upChainCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
upChain := &domain.UpChain{
Source: upChainCommand.Source,
PrimaryId: upChainCommand.PrimaryId,
IssueId: upChainCommand.IssueId,
Data: upChainCommand.Data,
UpChainStatus: 2,
CreatedAt: time.Now(),
}
// 1. 查重
upChainRepository, _, _ := factory.FastPgUpChain(transactionContext, 0)
// 可溯源数据,可重复上传,可以追溯历史修改记录
if len(upChain.IssueId) == 0 {
if item, err := upChainRepository.FindOne(map[string]interface{}{"source": upChain.Source, "primaryId": upChain.PrimaryId}); err == nil && item != nil {
return nil, fmt.Errorf("duplicate message %v %v", upChain.Source, upChain.PrimaryId)
}
}
// 2.上链
bc := &blockchain.BSNBlockChain{
PublicPem: []byte(blockchain.PubPem),
Host: blockchain.Host,
PublicKey: blockchain.PubKey,
PrivatePem: blockchain.PriK,
EnableDebugLog: true,
}
options := blockchain.NewUpToChainOptions(upChain.Source, upChain.PrimaryId, upChain.Data).WithInnerPrimaryIssueId(upChain.IssueId)
upToChainResponse, e := bc.UpToChain(options)
if e != nil || upToChainResponse == nil {
upChain.UpFail()
if e != nil {
log.Logger.Error("up chain err:" + e.Error())
}
} else {
upChain.UpSuccess(string(*upToChainResponse))
}
// 3.保存记录
if upChain, err = upChainRepository.Save(upChain); err != nil {
return nil, err
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return upChain, nil
}
// 区块链列表
func (blockChainService *BlockChainService) ListBlockChain(listBlockChain *query.ListBlockChainQuery) (interface{}, error) {
if err := listBlockChain.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
upChainRepository, _, _ := factory.FastPgUpChain(transactionContext, 0)
queryOptions := utils.ObjectToMap(listBlockChain)
_, upChains, err := upChainRepository.Find(queryOptions)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
response := dto.NewBlockChains(upChains)
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return response, nil
}
func (blockChainService *BlockChainService) GetBlockChainToken(listBlockChain *query.GetBlockChainTokenQuery) (interface{}, error) {
if err := listBlockChain.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
upChainRepository, _, _ := factory.FastPgUpChain(transactionContext, 0)
upChain, err := upChainRepository.FindOne(map[string]interface{}{"upChainId": listBlockChain.UpChainId})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var request = blockchain.GetTokenRequest{
Type: listBlockChain.Type,
ShowValue: true,
}
bsn := newBSNBlockChain()
switch listBlockChain.Type {
case blockchain.QueryByHashId:
request.TsTxId = upChain.Hash
case blockchain.QueryByIssueId:
request.TsTxId = upChain.Hash
request.IssueId = upChain.IssueId
default:
return nil, application.ThrowError(application.ARG_ERROR, "unknown type "+strconv.Itoa(listBlockChain.Type))
}
getTokenResponse, err := bsn.GetToken(&request)
if err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.BUSINESS_ERROR, "不存在")
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"token": getTokenResponse.Token,
"browseUrl": fmt.Sprintf("%v?token=%v", blockchain.BlockBrowserAddress, getTokenResponse.Token),
}, nil
}
func newBSNBlockChain() *blockchain.BSNBlockChain {
// 2.上链
bc := &blockchain.BSNBlockChain{
PublicPem: []byte(blockchain.PubPem),
Host: blockchain.Host,
PublicKey: blockchain.PubKey,
PrivatePem: blockchain.PriK,
EnableDebugLog: true,
}
return bc
}
func NewBlockChainService(options map[string]interface{}) *BlockChainService {
newBlockChainService := &BlockChainService{}
return newBlockChainService
}