From a51728a14baff1391603f4d1bbd7c1fcca933f9a Mon Sep 17 00:00:00 2001 From: gong <1157756119@qq.com> Date: Tue, 28 Oct 2025 14:30:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/copy_data.go | 78 +++++++++++++++++++++++++++++++++--------------- db/db_manager.go | 2 ++ db_config.yml | 3 +- main.go | 7 +++++ 4 files changed, 65 insertions(+), 25 deletions(-) diff --git a/db/copy_data.go b/db/copy_data.go index 284e30c..c1a19fc 100644 --- a/db/copy_data.go +++ b/db/copy_data.go @@ -22,17 +22,20 @@ func copyOrders(db *gorm.DB, timeNow time.Time) { db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.OrdersCopy1{}) - _execOrders(db, start, 0) + var totalCount int64 = 0 + db.Debug().Model(&data.Orders{}).Where("create_time >= ?", deleteTime).Count(&totalCount) + + _execOrders(db, start, 0, totalCount) db.Debug().Where("create_time < ?", start).Delete(&data.Orders{}) } -func _execOrders(db *gorm.DB, timeNow string, index int) { +func _execOrders(db *gorm.DB, timeNow string, index int, totalCount int64) { var orders []*data.Orders db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&orders) - fmt.Printf("index: %d, timeNow: %s ,orders count: %d\n", index, timeNow, len(orders)) + fmt.Printf("index: %d, timeNow: %s ,orders count: %d, totalCount: %d\n", index, timeNow, len(orders), totalCount) if len(orders) == 0 { return } @@ -46,7 +49,9 @@ func _execOrders(db *gorm.DB, timeNow string, index int) { db.Create(©List) - _execOrders(db, timeNow, index+1) + //time.Sleep(1500 * time.Millisecond) + + _execOrders(db, timeNow, index+1, totalCount) } func copyPayDetails(db *gorm.DB, timeNow time.Time) { @@ -62,17 +67,20 @@ func copyPayDetails(db *gorm.DB, timeNow time.Time) { db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.PayDetailsCopy1{}) - _execPayDetails(db, start, 0) + var totalCount int64 = 0 + db.Debug().Model(&data.PayDetails{}).Where("create_time >= ?", deleteTime).Count(&totalCount) + + _execPayDetails(db, start, 0, totalCount) db.Debug().Where("create_time < ?", start).Delete(&data.PayDetails{}) } -func _execPayDetails(db *gorm.DB, timeNow string, index int) { +func _execPayDetails(db *gorm.DB, timeNow string, index int, totalCount int64) { var details []*data.PayDetails db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&details) - fmt.Printf("index: %d, timeNow: %s ,pay_details count: %d\n", index, timeNow, len(details)) + fmt.Printf("index: %d, timeNow: %s ,pay_details count: %d, totalCount: %d\n", index, timeNow, len(details), totalCount) if len(details) == 0 { return } @@ -86,7 +94,9 @@ func _execPayDetails(db *gorm.DB, timeNow string, index int) { db.Create(©List) - _execPayDetails(db, timeNow, index+1) + //time.Sleep(1500 * time.Millisecond) + + _execPayDetails(db, timeNow, index+1, totalCount) } func copyUserMoneyDetails(db *gorm.DB, timeNow time.Time) { @@ -102,17 +112,20 @@ func copyUserMoneyDetails(db *gorm.DB, timeNow time.Time) { db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.UserMoneyDetailsCopy1{}) - _execUserMoneyDetails(db, start, 0) + var totalCount int64 = 0 + db.Debug().Model(&data.UserMoneyDetails{}).Where("create_time >= ?", deleteTime).Count(&totalCount) + + _execUserMoneyDetails(db, start, 0, totalCount) db.Debug().Where("create_time < ?", start).Delete(&data.UserMoneyDetails{}) } -func _execUserMoneyDetails(db *gorm.DB, timeNow string, index int) { +func _execUserMoneyDetails(db *gorm.DB, timeNow string, index int, totalCount int64) { var details []*data.UserMoneyDetails db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&details) - fmt.Printf("index: %d, timeNow: %s ,pay_details count: %d\n", index, timeNow, len(details)) + fmt.Printf("index: %d, timeNow: %s ,user_money_details count: %d, totalCount: %d\n", index, timeNow, len(details), totalCount) if len(details) == 0 { return } @@ -126,7 +139,9 @@ func _execUserMoneyDetails(db *gorm.DB, timeNow string, index int) { db.Create(©List) - _execUserMoneyDetails(db, timeNow, index+1) + //time.Sleep(1500 * time.Millisecond) + + _execUserMoneyDetails(db, timeNow, index+1, totalCount) } func copyCourseCollect(db *gorm.DB, timeNow time.Time) { @@ -142,17 +157,20 @@ func copyCourseCollect(db *gorm.DB, timeNow time.Time) { db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.CourseCollectCopy1{}) - _execCourseCollect(db, start, 0) + var totalCount int64 = 0 + db.Debug().Model(&data.CourseCollect{}).Where("create_time >= ?", deleteTime).Count(&totalCount) + + _execCourseCollect(db, start, 0, totalCount) db.Debug().Where("create_time < ?", start).Delete(&data.CourseCollect{}) } -func _execCourseCollect(db *gorm.DB, timeNow string, index int) { +func _execCourseCollect(db *gorm.DB, timeNow string, index int, totalCount int64) { var collects []*data.CourseCollect db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&collects) - fmt.Printf("index: %d, timeNow: %s ,course_collect count: %d\n", index, timeNow, len(collects)) + fmt.Printf("index: %d, timeNow: %s ,course_collect count: %d, totalCount: %d\n", index, timeNow, len(collects), totalCount) if len(collects) == 0 { return } @@ -166,7 +184,9 @@ func _execCourseCollect(db *gorm.DB, timeNow string, index int) { db.Create(©List) - _execCourseCollect(db, timeNow, index+1) + //time.Sleep(1500 * time.Millisecond) + + _execCourseCollect(db, timeNow, index+1, totalCount) } func copyCourseUser(db *gorm.DB, timeNow time.Time) { @@ -182,17 +202,20 @@ func copyCourseUser(db *gorm.DB, timeNow time.Time) { db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.CourseUserCopy1{}) - _execCourseUser(db, start, 0) + var totalCount int64 = 0 + db.Debug().Model(&data.CourseUser{}).Where("create_time >= ?", deleteTime).Count(&totalCount) + + _execCourseUser(db, start, 0, totalCount) db.Debug().Where("create_time < ?", start).Delete(&data.CourseUser{}) } -func _execCourseUser(db *gorm.DB, timeNow string, index int) { +func _execCourseUser(db *gorm.DB, timeNow string, index int, totalCount int64) { var users []*data.CourseUser db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&users) - fmt.Printf("index: %d, timeNow: %s ,course_user count: %d\n", index, timeNow, len(users)) + fmt.Printf("index: %d, timeNow: %s ,course_user count: %d, totalCount: %d\n", index, timeNow, len(users), totalCount) if len(users) == 0 { return } @@ -206,7 +229,9 @@ func _execCourseUser(db *gorm.DB, timeNow string, index int) { db.Create(©List) - _execCourseUser(db, timeNow, index+1) + //time.Sleep(1500 * time.Millisecond) + + _execCourseUser(db, timeNow, index+1, totalCount) } func copyDiscSpinningRecord(db *gorm.DB, timeNow time.Time) { @@ -222,17 +247,20 @@ func copyDiscSpinningRecord(db *gorm.DB, timeNow time.Time) { db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.DiscSpinningRecordCopy1{}) - _execDiscSpinningRecord(db, start, 0) + var totalCount int64 = 0 + db.Debug().Model(&data.DiscSpinningRecord{}).Where("create_time >= ?", deleteTime).Count(&totalCount) + + _execDiscSpinningRecord(db, start, 0, totalCount) db.Debug().Where("create_time < ?", start).Delete(&data.DiscSpinningRecord{}) } -func _execDiscSpinningRecord(db *gorm.DB, timeNow string, index int) { +func _execDiscSpinningRecord(db *gorm.DB, timeNow string, index int, totalCount int64) { var records []*data.DiscSpinningRecord db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&records) - fmt.Printf("index: %d, timeNow: %s ,disc_spinning_record count: %d\n", index, timeNow, len(records)) + fmt.Printf("index: %d, timeNow: %s ,disc_spinning_record count: %d, totalCount: %d\n", index, timeNow, len(records), totalCount) if len(records) == 0 { return } @@ -246,7 +274,9 @@ func _execDiscSpinningRecord(db *gorm.DB, timeNow string, index int) { db.Create(©List) - _execDiscSpinningRecord(db, timeNow, index+1) + //time.Sleep(1500 * time.Millisecond) + + _execDiscSpinningRecord(db, timeNow, index+1, totalCount) } // CopyStruct 使用反射复制结构体 diff --git a/db/db_manager.go b/db/db_manager.go index 7fffe32..51a08a0 100644 --- a/db/db_manager.go +++ b/db/db_manager.go @@ -54,6 +54,8 @@ func DisconnectDB() { } _ = s.Close() } + + fmt.Println("Disconnect to DB success") } func _loadConfig() { diff --git a/db_config.yml b/db_config.yml index e0b07b5..3c658ff 100644 --- a/db_config.yml +++ b/db_config.yml @@ -13,7 +13,8 @@ dev: - duanju-4 prod: - host: rm-2vc4acq5369l0y612.mysql.cn-chengdu.rds.aliyuncs.com + host: rm-2vc4acq5369l0y612mo.mysql.cn-chengdu.rds.aliyuncs.com +# host: rm-2vc4acq5369l0y612.mysql.cn-chengdu.rds.aliyuncs.com name: video_user password: VideoUser@1 port: 3306 diff --git a/main.go b/main.go index 80ba195..e914ca3 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( ) func main() { + _testDbConn() //_startCopyData() c := cron.New() _, err := c.AddFunc("0 4 * * *", func() { @@ -25,6 +26,12 @@ func main() { select {} } +func _testDbConn() { + db.ConnectDB() + time.Sleep(10 * time.Second) + db.DisconnectDB() +} + func _startCopyData() { db.ConnectDB() db.CopyData()