package wxuser

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

var (
	// 用户缓存对象
	userInfoMap      = make(map[string]*WxUserInfo)
	rwmu             sync.RWMutex
	userRecordMap    = make(map[int64]*RecordLoginOfWxUser)
	rwmu2            sync.RWMutex
	userADRecordMap  = make(map[int64]*RecordWatchADOfWxUser)
	rwmu3            sync.RWMutex
	userSeverInfoMap = make(map[string]map[int32]int64)
	rwmu4            sync.RWMutex
	userUidMap       = make(map[int64]*WxUserSeverInfo)
	rwmu5            sync.RWMutex
)

func GetUserByOpenId(openId string) (*WxUserInfo, error) {
	var userInfo *WxUserInfo
	func() *WxUserInfo {
		rwmu.RLock()
		defer rwmu.RUnlock()
		ok := true
		if userInfo, ok = userInfoMap[openId]; ok {
			return userInfo
		}
		return nil
	}()
	if userInfo != nil {
		return userInfo, nil
	}
	result := connection.GetUserDB().Where("openId = ?", openId).First(&userInfo)
	if result.Error != nil {
		return nil, result.Error
	}
	return userInfo, nil
}

// AddUser 添加用户
// AddUser 添加新的用户到数据库中。
// 参数 User: 包含用户信息的对象。
func AddUser(user *WxUserInfo) {

	//处理一些验证

	// 写入到数据库
	result := connection.GetUserDB().Create(&user) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("添加用户失败 错误信息：", result.Error.Error())
	}
}

func AddUserCache(user *WxUserInfo) {
	rwmu.Lock()
	defer rwmu.Unlock()
	userInfoMap[user.OpenId] = user
}

// 登录登出相关



// 添加用户登录登出表到缓存
func AddUserRecordCache(userrecord *RecordLoginOfWxUser) {
	rwmu2.Lock()
	defer rwmu2.Unlock()
	userRecordMap[userrecord.Uid] = userrecord
}

// 获取最新一条登录记录
func GetUserRecord(uid int64) (*RecordLoginOfWxUser, error) {
	var userRecord *RecordLoginOfWxUser
	func() *RecordLoginOfWxUser {
		rwmu2.RLock()
		defer rwmu2.RUnlock()
		ok := true
		if userRecord, ok = userRecordMap[uid]; ok {
			return userRecord
		}
		return nil
	}()
	if userRecord != nil {
		return userRecord, nil
	}
	result := connection.GetUserDB().Where("uid = ?", uid).Last(&userRecord)
	if result.Error != nil {
		return nil, result.Error
	}
	//添加缓存
	func() {
		rwmu2.Lock()
		defer rwmu2.Unlock()
		userRecordMap[userRecord.Uid] = userRecord
	}()
	return userRecord, nil
}

// 添加用户登录登出记录
func AddUserRecord(userrecord *RecordLoginOfWxUser) (int64, error) {

	//处理一些验证

	// 写入到数据库
	result := connection.GetUserDB().Create(&userrecord) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("添加用户记录失败 错误信息：", result.Error.Error())
	}
	return userrecord.ID, nil
}

// 保存添加用户登录登出记录
func SaveUserRecord(userrecord *RecordLoginOfWxUser) (int64, error) {

	//处理一些验证

	// 写入到数据库
	result := connection.GetUserDB().Save(&userrecord) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("保存用户记录失败 错误信息：", result.Error.Error())
	}
	return userrecord.ID, nil
}

// 看广告相关



// 添加用户看广告记录到缓存
func AddUserADRecordCache(userADrecord *RecordWatchADOfWxUser) {
	rwmu3.Lock()
	defer rwmu3.Unlock()
	userADRecordMap[userADrecord.Uid] = userADrecord
}

// 获取最新一条看广告记录
func GetUserADRecord(uid int64) (*RecordWatchADOfWxUser, error) {
	var userADRecord *RecordWatchADOfWxUser
	func() *RecordWatchADOfWxUser {
		rwmu3.RLock()
		defer rwmu3.RUnlock()
		ok := true
		if userADRecord, ok = userADRecordMap[uid]; ok {
			return userADRecord
		}
		return nil
	}()
	if userADRecord != nil {
		return userADRecord, nil
	}
	result := connection.GetUserDB().Where("uid = ?", uid).Last(&userADRecord)
	if result.Error != nil {
		return nil, result.Error
	}
	//添加缓存
	func() {
		rwmu3.Lock()
		defer rwmu3.Unlock()
		userADRecordMap[userADRecord.Uid] = userADRecord
	}()
	return userADRecord, nil
}

// 添加用户看广告记录到数据库
func AddUserADRecord(userADrecord *RecordWatchADOfWxUser) (int64, error) {

	//处理一些验证

	// 写入到数据库
	result := connection.GetUserDB().Create(&userADrecord) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("添加用户看广告记录失败 错误信息：", result.Error.Error())
	}
	return userADrecord.ID, nil
}

// 保存用户看广告记录到数据库
func SaveUserADRecord(userADrecord *RecordWatchADOfWxUser) (int64, error) {

	//处理一些验证

	// 写入到数据库
	result := connection.GetUserDB().Save(&userADrecord) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("保存用户看广告记录失败 错误信息：", result.Error.Error())
	}
	return userADrecord.ID, nil
}

// 区服相关



// 添加用户的区服信息到缓存 以openid为key的map
func AddUserSeverInfoCache(openid string, severid int32, uid int64) {
	rwmu4.Lock()
	defer rwmu4.Unlock()
	newMap := make(map[int32]int64)
	newMap[severid] = uid
	if _, ok := userSeverInfoMap[openid]; !ok {
		userSeverInfoMap[openid] = newMap
		return
	}
	if _, ok := userSeverInfoMap[openid][severid]; !ok {
		userSeverInfoMap[openid][severid] = uid
	}
}

// 添加用户的区服信息到缓存 以uid为key
func AddUserUidCache(wxUserSeverInfo *WxUserSeverInfo) {
	rwmu5.Lock()
	defer rwmu5.Unlock()
	userUidMap[wxUserSeverInfo.Uid] = wxUserSeverInfo
}

// 通过uid来获取用户的区服信息
func GetUserSeverInfoByUid(uid int64) (*WxUserSeverInfo, error) {
	var userSeverInfoRecord *WxUserSeverInfo
	func() *WxUserSeverInfo {
		rwmu5.RLock()
		defer rwmu5.RUnlock()
		ok := true
		if userSeverInfoRecord, ok = userUidMap[uid]; ok {
			return userSeverInfoRecord
		}
		return nil
	}()
	if userSeverInfoRecord == nil {
		result := connection.GetUserDB().Where("uid = ?", uid).First(&userSeverInfoRecord)
		if result.Error != nil {
			return nil, result.Error
		}
	}
	func() {
		rwmu5.Lock()
		defer rwmu5.Unlock()
		userUidMap[uid] = userSeverInfoRecord
	}()
	return userSeverInfoRecord, nil
}

// 通过openid和severid来获取uid
func GetUserUid(openId string, severId int32) (int64, error) {
	var uid int64
	func() int64 {
		rwmu4.RLock()
		defer rwmu4.RUnlock()
		ok := true
		if _, ok = userSeverInfoMap[openId]; !ok {
			return 0
		}
		if uid, ok = userSeverInfoMap[openId][severId]; ok {
			return uid
		}
		return 0
	}()
	if uid == 0 {
		var userSeverInfoRecord *WxUserSeverInfo
		result := connection.GetUserDB().Where("openId = ? AND severId = ?", openId, severId).First(&userSeverInfoRecord)
		if result.Error != nil {
			return 0, result.Error
		}
		if userSeverInfoRecord.Uid == 0 {
			userSeverInfoRecord.Uid = userSeverInfoRecord.ID + 100000000
			SaveUserSeverInfo(userSeverInfoRecord)
		}
		return userSeverInfoRecord.Uid, nil
	}
	return uid, nil
}

// 添加用户区服信息到数据库
func AddUserSeverInfo(userSeverInfo *WxUserSeverInfo) (int64, error) {

	//处理一些验证

	// 写入到数据库
	result := connection.GetUserDB().Create(&userSeverInfo) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("添加用户区服记录失败 错误信息：", result.Error.Error())
	}
	return userSeverInfo.ID, nil
}

// 保存用户区服信息到数据库
func SaveUserSeverInfo(userSeverInfo *WxUserSeverInfo) (int64, error) {

	//处理一些验证

	// 写入到数据库
	result := connection.GetUserDB().Save(&userSeverInfo) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("保存用户区服记录失败 错误信息：", result.Error.Error())
	}
	return userSeverInfo.ID, nil
}

// 查询severid是否存在，即是否为新服
func CheckSeverID(severid int32) bool {
	var userSeverList *WxUserSeverList
	result := connection.GetUserDB().Where("severId = ?", severid).First(&userSeverList)
	if result.Error != nil {
		return false
	}
	return true
}

// 添加新的severid到服务器列表
func AddUserSeverList(userSeverList *WxUserSeverList) (int64, error) {

	//处理一些验证

	// 写入到数据库
	result := connection.GetUserDB().Create(&userSeverList) // 通过数据的指针来创建
	if result.Error != nil {
		logUtilPlus.ErrorLog("添加用户区服列表失败 错误信息：", result.Error.Error())
	}
	return userSeverList.ID, nil
}
