package db

import (
	"fmt"
	"github.com/jinzhu/copier"
	"gorm.io/gorm"
	"time"
	"video_data_copy/data"
)

func copyOrders(db *gorm.DB, timeNow time.Time) {
	order := &data.Orders{}
	start := timeNow.Format("2006-01-02 15:04:05")
	first := db.Debug().Where("create_time < ?", start).Order("create_time asc").First(order)
	if first.RowsAffected == 0 {
		fmt.Println("no orders data")
		return
	}

	deleteTime := order.CreateTime
	fmt.Println("copy orders from:", deleteTime)

	db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.OrdersCopy1{})

	_execOrders(db, start, 0)

	db.Debug().Where("create_time < ?", start).Delete(&data.Orders{})
}

func _execOrders(db *gorm.DB, timeNow string, index int) {
	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))
	if len(orders) == 0 {
		return
	}

	var copyList []*data.OrdersCopy1
	for _, order := range orders {
		copyOrder := &data.OrdersCopy1{}
		CopyStruct(order, copyOrder)
		copyList = append(copyList, copyOrder)
	}

	db.Create(&copyList)

	_execOrders(db, timeNow, index+1)
}

func copyPayDetails(db *gorm.DB, timeNow time.Time) {
	detail := &data.PayDetails{}
	start := timeNow.Format("2006-01-02 15:04:05")
	first := db.Model(detail).Where("create_time < ?", start).Order("create_time asc").First(detail)
	if first.RowsAffected == 0 {
		fmt.Println("no pay_details data")
		return
	}

	deleteTime := detail.CreateTime

	db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.PayDetailsCopy1{})

	_execPayDetails(db, start, 0)

	db.Debug().Where("create_time < ?", start).Delete(&data.PayDetails{})
}

func _execPayDetails(db *gorm.DB, timeNow string, index int) {
	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))
	if len(details) == 0 {
		return
	}

	var copyList []*data.PayDetailsCopy1
	for _, detail := range details {
		detailCopy := &data.PayDetailsCopy1{}
		CopyStruct(detail, detailCopy)
		copyList = append(copyList, detailCopy)
	}

	db.Create(&copyList)

	_execPayDetails(db, timeNow, index+1)
}

func copyUserMoneyDetails(db *gorm.DB, timeNow time.Time) {
	detail := &data.UserMoneyDetails{}
	start := timeNow.Format("2006-01-02 15:04:05")
	first := db.Model(detail).Where("create_time < ?", start).Order("create_time asc").First(detail)
	if first.RowsAffected == 0 {
		fmt.Println("no user_money_details data")
		return
	}

	deleteTime := detail.CreateTime

	db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.UserMoneyDetailsCopy1{})

	_execUserMoneyDetails(db, start, 0)

	db.Debug().Where("create_time < ?", start).Delete(&data.UserMoneyDetails{})
}

func _execUserMoneyDetails(db *gorm.DB, timeNow string, index int) {
	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))
	if len(details) == 0 {
		return
	}

	var copyList []*data.UserMoneyDetailsCopy1
	for _, detail := range details {
		detailCopy := &data.UserMoneyDetailsCopy1{}
		CopyStruct(detail, detailCopy)
		copyList = append(copyList, detailCopy)
	}

	db.Create(&copyList)

	_execUserMoneyDetails(db, timeNow, index+1)
}

// CopyStruct 使用反射复制结构体
func CopyStruct(src, dst interface{}) {
	_ = copier.Copy(dst, src)
}
