package wxuser

import (
	"common/mytime"
	"common/resultStatus"
	"goutil/logUtilPlus"
	"time"
)

// 记录登录数据
func RecordLoginIn(uid int64, severId int32) {
	nowTime := time.Now().Unix()
	userRecord := &RecordLoginOfWxUser{}
	userRecord.IsFirstLogin = 1
	if userLastRecord, _ := GetUserRecord(uid); userLastRecord != nil {
		if userLastRecord.RecordDate == mytime.ZeroTime(nowTime, 0) {
			userRecord.IsFirstLogin = 0
		}
	}
	userRecord.LoginInTime = nowTime
	userRecord.Uid = uid
	userRecord.RecordDate = mytime.ZeroTime(nowTime, 0)
	userRecord.SeverId = severId
	AddUserRecord(userRecord)
	AddUserRecordCache(userRecord)
	//检测该服是否是新服
	if !CheckSeverID(severId) {
		//若是新服，添加服务器id到服务器列表，方便统计的时候知道有多少个服
		newUserSeverList := &WxUserSeverList{}
		newUserSeverList.SeverId = severId
		AddUserSeverList(newUserSeverList)
	}
}

// 统计登出数据
func RecordLoginOut(uid int64) resultStatus.ResultStatus {
	var userRecord *RecordLoginOfWxUser
	if userRecord, _ = GetUserRecord(uid); userRecord == nil {
		return resultStatus.PlayerNotExist
	}
	if userRecord.LoginOutTime != 0 {
		logUtilPlus.ErrorLog("PlayerIsLoginOut,uid=%d", uid)
		return resultStatus.PlayerIsLoginOut
	}
	go func() {
		nowTime := time.Now().Unix()
		if userRecord.RecordDate == mytime.ZeroTime(nowTime, 0) {
			userRecord.LoginOutTime = nowTime
			playTime := nowTime - userRecord.LoginInTime
			userRecord.PlayTimes += playTime
			SaveUserRecord(userRecord)
		} else {
			//如果跨天，要拆分成两条数据记录
			zeroTime := mytime.ZeroTime(nowTime, 0)
			userRecord.LoginOutTime = zeroTime
			playTime := zeroTime - userRecord.LoginInTime
			userRecord.PlayTimes += playTime
			SaveUserRecord(userRecord)
			newUserRecord := &RecordLoginOfWxUser{}
			newUserRecord.LoginInTime = zeroTime
			newUserRecord.LoginOutTime = nowTime
			playTime = nowTime - userRecord.LoginInTime
			newUserRecord.PlayTimes += playTime
			newUserRecord.RecordDate = mytime.ZeroTime(nowTime, 0)
			newUserRecord.Uid = uid
			newUserRecord.SeverId = userRecord.SeverId
			newUserRecord.IsFirstLogin = 1
			AddUserRecord(newUserRecord)
		}
	}()
	return resultStatus.Success
}

// 记录看广告数据
func RecordWatchAD(uid int64) resultStatus.ResultStatus {
	todayZeroTime := mytime.ZeroTime(time.Now().Unix(), 0)
	userADRecord := &RecordWatchADOfWxUser{}
	var userSeverInfoRecord *WxUserSeverInfo
	if userSeverInfoRecord, _ = GetUserSeverInfoByUid(uid); userSeverInfoRecord == nil {
		return resultStatus.PlayerNotExist
	}
	severId := userSeverInfoRecord.SeverId
	if userLastADRecord, _ := GetUserADRecord(uid); userLastADRecord != nil {
		if userLastADRecord.RecordDate == mytime.ZeroTime(todayZeroTime, 0) {
			//数据按天记录，如果为同一天，就在原来的数据上加
			userLastADRecord.WatchADNum++
			SaveUserADRecord(userLastADRecord)
			return resultStatus.Success
		}
	}
	userADRecord.RecordDate = todayZeroTime
	userADRecord.WatchADNum += 1
	userADRecord.Uid = uid
	userADRecord.SeverId = severId
	AddUserADRecord(userADRecord)
	AddUserADRecordCache(userADRecord)
	return resultStatus.Success
}

// 根据openid和severid来创建uid
func CreatUid(openId string, severId int32) int64 {
	if uid, _ := GetUserUid(openId, severId); uid != 0 {
		return uid
	}
	userSeverInfo := &WxUserSeverInfo{}
	userSeverInfo.OpenId = openId
	userSeverInfo.SeverId = severId
	ID, _ := AddUserSeverInfo(userSeverInfo)
	//uid=数据库索引+1亿，先取索引再相加，避免重复
	Uid := ID + 100000000
	AddUserSeverInfoCache(openId, severId, Uid)
	AddUserUidCache(userSeverInfo)
	go func() {
		userSeverInfo.Uid = Uid
		SaveUserSeverInfo(userSeverInfo)
	}()
	return Uid
}

// 切服时记录数据
func SwitchSever(uid int64, oldseverId, newseverId int32) (int64, resultStatus.ResultStatus) {
	var userSeverInfoRecord *WxUserSeverInfo
	if userSeverInfoRecord, _ = GetUserSeverInfoByUid(uid); userSeverInfoRecord == nil {
		return 0, resultStatus.PlayerNotExist
	}
	if userSeverInfoRecord.SeverId != oldseverId {
		return 0, resultStatus.PlayerNotMatchSever
	}
	RecordLoginOut(uid)
	uidOfNewSever := CreatUid(userSeverInfoRecord.OpenId, newseverId)
	go RecordLoginIn(uidOfNewSever, newseverId)
	return uidOfNewSever, resultStatus.Success
}
