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{}
	db.Model(order).Where("create_time < ?", timeNow.Format("2006-01-02 15:04:05")).Order("create_time asc").First(order)

	parse, err := time.Parse("2006-01-02T15:04:05Z07:00", order.CreateTime)
	if err != nil {
		fmt.Println("parse order create_time error:", err)
	}
	format := parse.Format("2006-01-02 15:04:05")
	fmt.Println("copy orders from:", format)
	db.Debug().Where("create_time >= ?", format).Delete(&data.OrdersCopy1{})

	_execOrders(db, timeNow.Format("2006-01-02 15:04:05"), 0)

	db.Debug().Where("create_time < ?", format).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{}
	db.Model(detail).Where("create_time < ?", timeNow.Format("2006-01-02 15:04:05")).Order("create_time asc").First(detail)

	parse, err := time.Parse("2006-01-02T15:04:05Z07:00", detail.CreateTime)
	if err != nil {
		fmt.Println("parse pay_details create_time error:", err)
	}
	format := parse.Format("2006-01-02 15:04:05")
	fmt.Println("copy pay_details from:", format)
	db.Debug().Where("create_time >= ?", format).Delete(&data.PayDetailsCopy1{})

	_execPayDetails(db, timeNow.Format("2006-01-02 15:04:05"), 0)

	db.Debug().Where("create_time < ?", format).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{}
	db.Model(detail).Where("create_time < ?", timeNow.Format("2006-01-02 15:04:05")).Order("create_time asc").First(detail)

	parse, err := time.Parse("2006-01-02T15:04:05Z07:00", detail.CreateTime)
	if err != nil {
		fmt.Println("parse pay_details create_time error:", err)
	}
	format := parse.Format("2006-01-02 15:04:05")
	fmt.Println("copy pay_details from:", format)
	db.Debug().Where("create_time >= ?", format).Delete(&data.UserMoneyDetailsCopy1{})

	_execUserMoneyDetails(db, timeNow.Format("2006-01-02 15:04:05"), 0)

	db.Debug().Where("create_time < ?", format).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)
}
