2024-12-06 23:02:19 +08:00

190 lines
5.0 KiB
Go

package model
import (
"errors"
"f5"
"main/constant"
"q5"
"strings"
"gorm.io/gorm"
)
type User struct {
Idx int64 `gorm:"column:idx;AUTO_INCREMENT"`
AccountId string `gorm:"column:account_id;primaryKey"`
Uid string `gorm:"column:uid"`
Gid string `gorm:"column:gid"`
OpenId string `gorm:"column:openid"`
Version int32 `gorm:"column:version"`
Avatar string `gorm:"column:avatar"`
NickName string `gorm:"column:nickname"`
Invited string `gorm:"column:invited"`
Ext string `gorm:"column:ext"`
Plat int32 `gorm:"column:plat"`
PlatVip int32 `gorm:"column:plat_vip"`
Score int64 `gorm:"column:score"`
Dice int32 `gorm:"column:dice"`
SpecDice int32 `gorm:"column:spec_dice"`
LastPresentDiceTime int32 `gorm:"column:last_present_dice_time"`
CurrGrid int32 `gorm:"column:curr_grid"`
awardGrids string `gorm:"column:award_grids"`
CreateTime int32 `gorm:"column:createtime;<-:create"`
ModifyTime int32 `gorm:"column:modifytime"`
}
func (this *User) TableName() string {
return "t_user"
}
func (this *User) UpdateFields(fields []string) error {
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Model(this).Select(
fields).Updates(this); result.Error != nil {
return result.Error
}
return nil
}
func (this *User) Find(accountId string, nowTime int64) (error, bool) {
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Table(this.TableName()).Take(
this, "account_id = ?", accountId); result.Error != nil &&
!errors.Is(result.Error, gorm.ErrRecordNotFound) {
return result.Error, false
} else {
if result.RowsAffected > 0 {
if this.Dice < constant.PRESENT_DICE_LIMIT && this.LastPresentDiceTime+constant.PRESENT_DICE_LOOP < int32(nowTime) {
present := (int32(nowTime) - this.LastPresentDiceTime) / constant.PRESENT_DICE_LOOP
if this.Dice+present < constant.PRESENT_DICE_LIMIT {
this.Dice += present
this.LastPresentDiceTime += present * constant.PRESENT_DICE_LOOP
} else {
this.Dice = constant.PRESENT_DICE_LIMIT
this.LastPresentDiceTime = q5.ToInt32(nowTime)
}
if err := this.UpdateFields([]string{"dice", "last_present_dice_time"}); err != nil {
return err, false
}
}
}
return nil, result.RowsAffected > 0
}
}
func (this *User) Create() error {
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Create(this); result.Error != nil {
return result.Error
}
return nil
}
func (this *User) AddDice(num int32) error {
if num < 1 {
return errors.New("")
}
return this.DecDice(-num)
}
func (this *User) DecDice(num int32) error {
if this.Dice < num {
return errors.New("")
}
oldDice := this.Dice
this.Dice -= num
updatepresentTime := false
if (num < 0 && oldDice < constant.PRESENT_DICE_LIMIT && this.Dice >= constant.PRESENT_DICE_LIMIT) ||
(num > 0 && oldDice >= constant.PRESENT_DICE_LIMIT && this.Dice < constant.PRESENT_DICE_LIMIT) {
this.LastPresentDiceTime = int32(f5.GetApp().GetRealSeconds())
updatepresentTime = true
}
updatefileds := []string{"dice"}
if updatepresentTime {
updatefileds = append(updatefileds, "last_present_dice_time")
}
if err := this.UpdateFields(updatefileds); err != nil {
this.Dice = oldDice
return err
}
return nil
}
func (this *User) AddSpecDice(num int32) error {
this.SpecDice += num
if err := this.UpdateFields([]string{"spec_dice"}); err != nil {
return err
}
return nil
}
func (this *User) DecSpecDice(num int32) error {
if this.SpecDice < num {
return errors.New("")
}
oldSpecDice := this.SpecDice
this.SpecDice -= num
if err := this.UpdateFields([]string{"spec_dice"}); err != nil {
this.SpecDice = oldSpecDice
return err
}
return nil
}
func (this *User) DecScore(num int32) error {
if this.Score < int64(num) {
return errors.New("")
}
oldScore := this.Score
this.Score -= int64(num)
if err := this.UpdateFields([]string{"score"}); err != nil {
this.Score = oldScore
return err
}
return nil
}
func (this *User) AddScore(score int32) error {
if score <= 0 {
return nil
}
oldScore := this.Score
this.Score += int64(score)
if err := this.UpdateFields([]string{"score"}); err != nil {
this.Score = oldScore
return err
}
return nil
}
func (this *User) UpdateName() error {
return this.UpdateFields([]string{"nickname"})
}
func (this *User) AddAwardGrid(gridid int32) error {
awardGridsList := this.GetAwardGrids()
if len(awardGridsList) > 0 {
this.awardGrids += ","
}
this.awardGrids += q5.SafeToString(gridid)
return this.UpdateFields([]string{"award_grids"})
}
func (this *User) ClearAwardGrid() error {
this.awardGrids = ""
return this.UpdateFields([]string{"award_grids"})
}
func (this *User) GetAwardGrids() []int32 {
gridstr := strings.Split(this.awardGrids, ",")
awardGridsList := []int32{}
for _, item := range gridstr {
if item != "" {
awardGridsList = append(awardGridsList, q5.SafeToInt32(item))
}
}
return awardGridsList
}