package jobs

import (
	"fmt"
	"gadmin/config"
	model2 "gadmin/internal/gorm/model"
	"gadmin/internal/gorm/query"
	"github.com/jinzhu/now"
	"github.com/sirupsen/logrus"
	"gorm.io/gen"
	"log"
	"time"
)

var SuppleOrderAt = new(jSuppleOrderAt)

type jSuppleOrderAt struct {
}

func (j *jSuppleOrderAt) Run() {
	logrus.Info("jSuppleOrderAt Run.....")
	j.handle()
}

func (j *jSuppleOrderAt) handle() {
	var (
		results []*model2.PayOrderAndriod
		m       = query.Use(config.GDB1).PayOrderAndriod
	)
	err := m.Select(m.ID, m.BillNo).
		FindInBatches(&results, 1000, func(tx gen.Dao, batch int) error {
			for _, order := range results {
				payAt, _ := now.ParseInLocation(time.Local, fmt.Sprintf("%s-%s-%s %s:%s:%s", order.BillNo[0:4], order.BillNo[4:6], order.BillNo[6:8], order.BillNo[8:10], order.BillNo[10:12], order.BillNo[12:14]))
				_, err := query.Use(config.GDB1).PayOrderAndriod.Where(m.ID.Eq(order.ID)).Updates(&model2.PayOrderAndriod{
					CreateTime: payAt,
				})
				if err != nil {
					logrus.Panicf("SuppleOrderAt err:%+v", err)
				}

			}
			return nil
		})

	if err != nil {
		panic(err)
	} else {
		log.Println("stat SuppleOrderAt finished")
	}
}
