-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (160 loc) · 4 KB
/
main.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
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/liuyibao/data-split/model"
"fmt"
"bytes"
"sync"
"log"
)
var DbGymSecond *sql.DB
var DbGym *sql.DB
var totalRecord int
const SPAN = 10000
func init() {
var err error
DbGymSecond, err = sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/ran_gym_second?maxAllowedPacket=0&interpolateParams=true")
if err != nil {
panic(err)
}
DbGym, err = sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/ran_gym?maxAllowedPacket=0&interpolateParams=true")
if err != nil {
panic(err)
}
log.SetPrefix("TRACE: ")
log.SetFlags(log.Ldate | log.Lmicroseconds)
}
func main() {
var maxId, offset uint32
err := DbGym.QueryRow("SELECT MAX(id) FROM shop_member_follow_history_new").Scan(&maxId);
if err != nil {
panic(err)
}
for offset=0; offset<maxId; offset+=SPAN {
modelList := readData(offset, SPAN)
groups := groupData(modelList)
var wg sync.WaitGroup
wg.Add(len(groups))
for index,value := range groups {
go writeData(&wg, index, value)
}
wg.Wait()
totalRecord += len(modelList)
log.Println("offset:", offset, "span:", offset+SPAN, "查询到数量:", len(modelList), "累计插入总数:", totalRecord)
}
}
func readData(offset uint32, span uint32) (modelList []model.ShopMemberFollowHistory) {
rows, err := DbGym.Query("SELECT * FROM shop_member_follow_history_new WHERE id>? AND id<=?", offset, offset+span)
if err != nil {
panic(err)
}
for rows.Next() {
m := model.ShopMemberFollowHistory{}
err = rows.Scan(
&m.Id,
&m.MemberId,
&m.Category,
&m.Type,
&m.ContactPurpose,
&m.ContactStatus,
&m.ContactResult,
&m.OrderType,
&m.OrderStatus,
&m.SalesId,
&m.VisitTime,
&m.ReceiveTime,
&m.LeaveTime,
&m.Duration,
&m.ShopId,
&m.CabinetId,
&m.CabinetStatus,
&m.ParentId,
&m.CourseCatId,
&m.OrderId,
&m.ContractId,
&m.Unit,
&m.FailReason,
&m.NextContact,
&m.Content,
&m.AssignContent,
&m.Text,
&m.Uid,
&m.StaffId,
&m.UpdatedTime,
&m.CreatedTime,
&m.ReceiveSalesId,
&m.SpaceId,
)
if err != nil {
panic(err)
}
modelList = append(modelList, m)
}
return
}
func groupData(modelList []model.ShopMemberFollowHistory) (map[string][]model.ShopMemberFollowHistory) {
groups := make(map[string][]model.ShopMemberFollowHistory)
for _, m := range modelList {
mod := fmt.Sprintf("%02d", m.Uid%100)
groups[mod] = append(groups[mod], m)
}
return groups
}
func writeData(wg *sync.WaitGroup, suffix string, modelList []model.ShopMemberFollowHistory) {
defer wg.Done()
insertSql := "INSERT INTO shop_member_follow_history_" + suffix + "(id,member_id,category,type,contact_purpose,contact_status,contact_result,order_type,order_status,sales_id,visit_time,receive_time,leave_time,duration,shop_id,cabinet_id,cabinet_status,parent_id,course_cat_id,order_id,contract_id,unit,fail_reason,next_contact,content,assign_content,text,uid,staff_id,updated_time,created_time,receive_sales_id,space_id) VALUES"
var buf bytes.Buffer
buf.WriteString(insertSql)
buf.WriteString("(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
for i:=1;i<len(modelList);i++ {
buf.WriteString(",(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
}
var args []interface{}
for _,m := range modelList {
args = append(args,
m.Id,
m.MemberId,
m.Category,
m.Type,
m.ContactPurpose,
m.ContactStatus,
m.ContactResult,
m.OrderType,
m.OrderStatus,
m.SalesId,
m.VisitTime,
m.ReceiveTime,
m.LeaveTime,
m.Duration,
m.ShopId,
m.CabinetId,
m.CabinetStatus,
m.ParentId,
m.CourseCatId,
m.OrderId,
m.ContractId,
m.Unit,
m.FailReason,
m.NextContact,
m.Content,
m.AssignContent,
m.Text,
m.Uid,
m.StaffId,
m.UpdatedTime,
m.CreatedTime,
m.ReceiveSalesId,
m.SpaceId,
)
}
res, err := DbGymSecond.Exec(buf.String(), args...)
if err != nil {
panic(err)
}
_, err = res.RowsAffected()
if err != nil {
panic(err)
}
//fmt.Println("插入数量", rowCnt)
}