package admin

import (
	"common/connection"
	"goutil/logUtilPlus"
)

// AddAdmin 添加管理员
// AddAdmin 添加新的管理员到数据库中。
// 参数 admin: 包含管理员信息的对象。
// 返回值: 插入操作影响的行数和可能发生的错误。
func AddAdmin(admin *Admin) (int64, error) {

	//处理一些验证

	// 写入到数据库
	result := connection.Create(connection.GetAdminDB(), &admin, 0) // 通过数据的指针来创建

	if result.Error != nil {
		logUtilPlus.ErrorLog("添加管理员失败 错误信息：", result.Error.Error())
	}
	return admin.ID, nil
}

// GetAdminByID 根据管理员ID获取管理员信息
func GetAdminByID(adminID int64) (*Admin, error) {
	var admin Admin
	result := connection.GetAdminDB().First(&admin, adminID)
	if result.Error != nil {
		return nil, result.Error
	}
	return &admin, nil
}

// 管理员登录
func Login(account string, password string) (*Admin, error) {
	var admin Admin
	result := connection.GetAdminDB().Where("account = ? AND password = ?", account, password).First(&admin)
	if result.Error != nil {
		return nil, result.Error
	}
	return &admin, nil
}

// 查询玩家首次登录登出记录
func GetUserFirstRecord(uid int64) (*RecordLoginOfWxUser, error) {
	var userRecord *RecordLoginOfWxUser
	result := connection.GetUserDB().Where("uid = ?", uid).First(&userRecord)
	if result.Error != nil {
		return nil, result.Error
	}
	return userRecord, nil
}

// 查询玩家最新登录登出记录
func GetUserLastRecord(uid int64) (*RecordLoginOfWxUser, error) {
	var userRecord *RecordLoginOfWxUser
	result := connection.GetUserDB().Where("uid = ?", uid).Last(&userRecord)
	if result.Error != nil {
		return nil, result.Error
	}
	return userRecord, nil
}

// 计算某日某服的玩家在线总时长
func GetUserTotalPlayTimes(date int64, severId int32) int64 {
	var totalPlayTime int64
	connection.GetUserDB().Table("recordloginofwxuser").Where("recorddate = ? AND severid = ?", date, severId).Select("SUM(playtimes)").Scan(&totalPlayTime)
	return totalPlayTime
}

// 计算某日某服的玩家上线总人数
func GetUserTotalPlayerNum(date int64, severId int32) int64 {
	var count int64
	connection.GetUserDB().Table("recordloginofwxuser").Where("recorddate = ? AND severid = ? AND isfirstlogin = ?", date, severId, 1).Count(&count)
	return count
}

// 获取区服列表
func GetUserSeverList() ([]*WxUserSeverList, error) {
	var userSeverList []*WxUserSeverList
	result := connection.GetUserDB().Find(&userSeverList)
	if result.Error != nil {
		logUtilPlus.ErrorLog("查询用户区服列表失败 错误信息：%s", result.Error.Error())
		return nil, result.Error
	}
	return userSeverList, nil
}

// 添加玩家的平均在线时长数据到数据库
func AddRecordAveragePlayTimes(recordAveragePlayTimes *RecordAveragePlayTimes) (int64, error) {
	connection.CheckTableExists(connection.GetAdminDB(), &RecordAveragePlayTimes{})
	result := connection.GetAdminDB().Create(&recordAveragePlayTimes) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("添加平均在线时长记录失败 错误信息：%s", result.Error)
	}
	return recordAveragePlayTimes.ID, nil
}

// 获取某日某服的玩家平均在线时长记录和总上线人数
func GetRecordAveragePlayTimes(date int64, severId int32) (int64, int64, error) {
	var recordAveragePlayTimes *RecordAveragePlayTimes
	result := connection.GetAdminDB().Where("recorddate = ? AND severid = ?", date, severId).First(&recordAveragePlayTimes)
	if result.Error != nil {
		logUtilPlus.ErrorLog("查询平均在线时长记录失败 错误信息：%s", result.Error)
		return 0, 0, result.Error
	}
	return recordAveragePlayTimes.AveragePlayTimes, recordAveragePlayTimes.PlayerNum, nil
}

// 计算某日某服的玩家平均看广告次数
func GetUserAvgWatchADNum(date int64, severId int32) float32 {
	var avgWatchADNum float32
	connection.GetUserDB().Table("recordwatchadofwxuser").Where("recorddate = ? AND severid = ?", date, severId).Select("AVG(watchadnum)").Scan(&avgWatchADNum)
	return avgWatchADNum
}

// 计算某日某服玩家的看广告总人数
func GetUserWatchADPlayerNum(date int64, severId int32) int64 {
	var count int64
	connection.GetUserDB().Table("recordwatchadofwxuser").Where("recorddate = ? AND severid = ?", date, severId).Count(&count)
	return count
}

// 添加玩家的平均看广告次数到数据库
func AddRecordAverageWatchADNum(recordAverageWatchADNum *RecordAverageWatchADNum) (int64, error) {
	result := connection.GetAdminDB().Create(&recordAverageWatchADNum) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("添加平均看广告次数记录失败 错误信息：%s", result.Error)
	}
	return recordAverageWatchADNum.ID, nil
}

// 获取某日某服玩家的平均看广告次数和总人数
func GetRecordAverageWatchADNum(date int64, severId int32) (float32, int64, error) {
	var recordAverageWatchADNum *RecordAverageWatchADNum
	result := connection.GetAdminDB().Where("recorddate = ? AND severid = ?", date, severId).First(&recordAverageWatchADNum)
	if result.Error != nil {
		logUtilPlus.ErrorLog("查询平均看广告次数记录失败 错误信息：%s", result.Error)
		return 0, 0, result.Error
	}
	return recordAverageWatchADNum.AverageWatchADNum, recordAverageWatchADNum.PlayerNum, nil
}
