package service

import (
	"blazing/cool"
	"blazing/modules/blazing/model"
	"encoding/json"
	"time"
)

func Exec[T cool.UserModel, F any](userid uint32, s *cool.Service, processFunc func(F) F) bool {
	//todo待测试
	var player T

	m1 := cool.DBM(s.Model).Where("player_id", userid)
	m1.Scan(&player)
	// 方法2：使用反射获取
	// 获取反射值对象

	ttt := player

	//fmt.Println(dataField.Interface().(string))
	var tt F
	err := json.Unmarshal([]byte(ttt.GetData()), &tt)
	if err != nil {
		panic(err)
	}
	tt1 := processFunc(tt)
	tmep, err := json.Marshal(tt1)
	if err != nil {
		panic(err)
	}

	ttt.SetData(string(tmep))
	m1.Save(player)
	return false
}

// 获取任务信息
func (s *UserService) Task(id uint32, t func(*model.TaskEX) bool) {
	var gg model.TaskEX
	m1 := cool.DBM(s.task.Model).
		Where("player_id", s.userid).
		Where("task_id", id)
	err := m1.Scan(&gg)
	tre := t(&gg)
	if !tre { //不需要更新
		return
	}
	if err != nil {
		m1.Insert(gg)
	} else {
		m1.Update(gg)
	}

}

// IsToday 判断给定时间是否是今天
func IsToday(t time.Time) bool {
	// 获取当前时间
	now := time.Now()

	// 比较年、月、日是否相同
	return t.Year() == now.Year() &&
		t.Month() == now.Month() &&
		t.Day() == now.Day()
}
