-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
243 lines (211 loc) · 7.21 KB
/
client.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
package cockroach
import (
"fmt"
"log"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/stanxii/iccrawler/crawlerSZLC/request"
)
// Although there are many cockroachdb clients with Go, I still want to implement one by myself.
// Because we only need some very simple usages.
type Client struct {
DB *gorm.DB
// ctx context.Context
}
func NewClient(dbURL string) *Client {
c := new(Client)
db, err := setupDatabase(dbURL)
if err != nil {
log.Fatal("error connecting to the database", err.Error())
return nil
}
c.DB = db
fmt.Println("now will create table cockpark")
//if table not exist create table
hasTable := c.DB.HasTable(&CockPartNumber{})
fmt.Println("hasTable not hasTable. == .", hasTable)
if !hasTable {
fmt.Println("Table not exist. create it.")
c.DB.CreateTable(&CockPartNumber{})
} else {
fmt.Println("Table exist. true it.")
}
return c
}
func setupDatabase(dbURL string) (*gorm.DB, error) {
fmt.Println("XXdbstring path", dbURL)
// Open connection to server and create a database.
db, err := gorm.Open("postgres", dbURL)
if err != nil {
fmt.Println("Cockroach db open error. Xdbstring path", dbURL)
return db, err
}
// Allow a maximum of concurrency+1 connections to the database.
db.DB().SetMaxOpenConns(100 + 1)
db.DB().SetMaxIdleConns(100 + 1)
return db, nil
}
//CSVPartNumber csvpart
type CockPartNumber struct {
ID int64
Part string `gorm:"column:pro_sno"`
Cat string `gorm:"column:pro_cat"`
Imag string `gorm:"column:pro_img"`
Promaf string `gorm:"column:pro_maf"`
BaseSalePrice float64 `gorm:"column:base_sale_price"`
Stock int64 `gorm:"column:stock"`
SupPart string `gorm:"column:suppart"`
Moq int64 `gorm:"column:moq"`
Mbuy float64 `gorm:"column:mbuy"`
Currency string `gorm:"column:currency"`
KuCunDi string `gorm:"column:kucundi"`
CunHuoWeiZhi string `gorm:"column:kucunweizi"`
BasePurchasePrice float64 `gorm:"column:base_pur_price"`
PiHao string `gorm:"column:pihao"`
Package string `gorm:"column:pkg"`
Spq int64 `gorm:"column:spq"`
FutureCN string `gorm:"column:futurecn"`
futureHK string `gorm:"column:futurehk"`
Pname string `gorm:"column:pname"`
PurchaseNum1 int64 `gorm:"column:purnum1"`
PurchaseNum2 int64 `gorm:"column:purnum2"`
PurchaseNum3 int64 `gorm:"column:purnum3"`
PurchaseNum4 int64 `gorm:"column:purnum4"`
PurchaseNum5 int64 `gorm:"column:purnum5"`
PurchaseNum6 int64 `gorm:"column:purnum6"`
PurchaseNum7 int64 `gorm:"column:purnum7"`
PurchaseNum8 int64 `gorm:"column:purnum8"`
PurchaseNum9 int64 `gorm:"column:purnum9"`
PurchaseNum10 int64 `gorm:"column:purnum10"`
SalesUnitPrice1 float64 `gorm:"column:saluprice1"`
SalesUnitPrice2 float64 `gorm:"column:saluprice2"`
SalesUnitPrice3 float64 `gorm:"column:saluprice3"`
SalesUnitPrice4 float64 `gorm:"column:saluprice4"`
SalesUnitPrice5 float64 `gorm:"column:saluprice5"`
SalesUnitPrice6 float64 `gorm:"column:saluprice6"`
SalesUnitPrice7 float64 `gorm:"column:saluprice7"`
SalesUnitPrice8 float64 `gorm:"column:saluprice8"`
SalesUnitPrice9 float64 `gorm:"column:saluprice9"`
SalesUnitPrice10 float64 `gorm:"column:saluprice10"`
PurchaseUnitPrice1 float64 `gorm:"column:purunit_price1"`
PurchaseUnitPrice2 float64 `gorm:"column:purunit_price2"`
PurchaseUnitPrice3 float64 `gorm:"column:purunit_price3"`
PurchaseUnitPrice4 float64 `gorm:"column:purunit_price4"`
PurchaseUnitPrice5 float64 `gorm:"column:purunit_price5"`
PurchaseUnitPrice6 float64 `gorm:"column:purunit_price6"`
PurchaseUnitPrice7 float64 `gorm:"column:purunit_price7"`
PurchaseUnitPrice8 float64 `gorm:"column:purunit_price8"`
PurchaseUnitPrice9 float64 `gorm:"column:purunit_price9"`
PurchaseUnitPrice10 float64 `gorm:"column:purunit_price10"`
Comments string `gorm:"column:comments"`
Description string `gorm:"column:pro_desc"`
SaleChannel string `gorm:"column:sale_channel"`
ProductDetail string `gorm:"column:pro_detail"`
Datasheet string `gorm:"column:datasheet"`
Rosh string `gorm:"column:rosh"`
StockBiaoStock string `gorm:"column:stock_biao"`
NotUse1 string `gorm:"column:not_use1"`
NotUse2 string `gorm:"column:not_use2"`
PartEN string `gorm:"column:parten"`
PromatEN string `gorm:"column:promaten"`
FutureEN string `gorm:"column:futureen"`
DescriptionEN string `gorm:"column:descen"`
LUptime time.Time `gorm:"column:luptime"`
}
func (c CockPartNumber) TableName() string {
return "t_pro_sell"
}
func (c *Client) Close() {
c.DB.Close()
}
func convertReqToCock(in request.PartNumber, o *CockPartNumber) error {
o.Part = in.Part
o.Comments = in.Keyword
o.Promaf = in.Promaf
o.Stock = in.Stock
o.Cat = in.Cat
o.ProductDetail = in.Detail
o.Package = in.Pkg
o.Description = in.Desc
//非数字
// pattern := `[\\d+$]`
for i, v := range in.Steps {
if i < 10 {
if 0 == i {
o.PurchaseNum1 = v
} else if 1 == i {
o.PurchaseNum2 = v
} else if 2 == i {
o.PurchaseNum3 = v
} else if 3 == i {
o.PurchaseNum4 = v
} else if 4 == i {
o.PurchaseNum5 = v
} else if 5 == i {
o.PurchaseNum6 = v
} else if 6 == i {
o.PurchaseNum7 = v
} else if 7 == i {
o.PurchaseNum8 = v
} else if 8 == i {
o.PurchaseNum9 = v
} else if 9 == i {
o.PurchaseNum10 = v
}
}
}
for i, v := range in.Prices {
if i < 10 {
if 0 == i {
o.PurchaseUnitPrice1 = v
} else if 1 == i {
o.PurchaseUnitPrice2 = v
} else if 2 == i {
o.PurchaseUnitPrice3 = v
} else if 3 == i {
o.PurchaseUnitPrice4 = v
} else if 4 == i {
o.PurchaseUnitPrice5 = v
} else if 5 == i {
o.PurchaseUnitPrice6 = v
} else if 6 == i {
o.PurchaseUnitPrice7 = v
} else if 7 == i {
o.PurchaseUnitPrice8 = v
} else if 8 == i {
o.PurchaseUnitPrice9 = v
} else if 9 == i {
o.PurchaseUnitPrice10 = v
}
}
}
//set timezone = server local setting. china time.
local, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println("tim.loadLocation error", err)
}
now := time.Now()
fmt.Println("get localtimXXXe", now)
o.LUptime = now.In(local)
return nil
}
func (c *Client) DoSave(p request.PartNumber) error {
defer func() {
if err := recover(); err != nil {
fmt.Println("recoverd in DoUpdatePrice")
}
}()
pt := CockPartNumber{}
convertReqToCock(p, &pt)
// if c.DB.Where("pro_sno = ? AND pro_maf=? AND luptime=?", pt.Part, pt.Promaf, pt.LUptime).First(&CockPartNumber{}).RecordNotFound() {
// fmt.Println("data alread exist is db", pt)
// return errors.New("dat had already exist.")
// }
fmt.Println("<<<>>> now will sav to cockroach", pt)
c.DB.Create(&pt)
//update
// fmt.Printf("XXXX Update new itme = %+v\n", pt)
return nil
}
//do stan