property.go 3.31 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
package user_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/internal/enum/property_e"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"time"
)

//用户道具
type UserProperty struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId        mysql.ID
	PropertyId    mysql.ID
	EndTime       time.Time
	Using         property_e.UserPropertyUsing
}

func GetUserPropertyOrInit(model *domain.Model, userId mysql.ID, propertyId mysql.ID) (*UserProperty, error) {
	userProperty := UserProperty{}
	if err := model.Db.Model(&UserProperty{}).Where(&UserProperty{
		UserId:     userId,
		PropertyId: propertyId,
	}).First(&userProperty).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return &UserProperty{
				Model:      model,
				UserId:     userId,
				PropertyId: propertyId,
				EndTime:    time.Now(),
			}, nil
		} else {
			return nil, err
		}
	}
	userProperty.Model = model
	return &userProperty, nil
}

//设置为使用中
func (userProperty *UserProperty) SetUsing() (*UserProperty, error) {
	if err := ResetAllUserPropertyNoUsing(userProperty.Model, userProperty.UserId); err != nil {
		return nil, err
	}
	userProperty.Using = property_e.YesUsing
	return userProperty, nil
}

//增加结束时间
func (userProperty *UserProperty) AddEndTime(t property_e.UserPropertyLogOrginType, second uint32, operateUserId mysql.ID) (*UserProperty, mysql.ID, error) {
	logId, err := addUserPropertyLog(userProperty.Model, userProperty.UserId, userProperty.PropertyId, t, property_e.AddSecond, &second, nil, operateUserId)
	if err != nil {
		return nil, 0, err
	}
	//if err := ResetAllUserPropertyNoUsing(userProperty.Model, userProperty.UserId); err != nil {
	//	return nil, logId, err
	//}
	nowTime := time.Now()
	if userProperty.EndTime.After(nowTime) {
		nowTime = userProperty.EndTime
	}
	userProperty.EndTime = nowTime.Add(time.Duration(second) * time.Second)
	return userProperty, logId, nil
}

//重置所有的座驾均为不使用状态
func ResetAllUserPropertyNoUsing(model *domain.Model, userId mysql.ID) error {
	if err := model.Db.Model(&UserProperty{}).Where(&UserProperty{
		UserId: userId,
	}).UpdateColumn("using", property_e.NoUsing).Error; err != nil {
		return err
	}
	return nil
}

//增加修改日志
func addUserPropertyLog(model *domain.Model, userId mysql.ID, propertyId mysql.ID, originType property_e.UserPropertyLogOrginType, t property_e.UserPropertyLogType, addSecond *uint32, UpdateEndTime *time.Time, operateUserId mysql.ID) (mysql.ID, error) {
	userPropertyLog := UserPropertyLog{
		UserId:        userId,
		OperateUserId: operateUserId,
		PropertyId:    propertyId,
		OriginType:    originType,
		Type:          t,
		AddSecond:     addSecond,
		UpdateEndTime: UpdateEndTime,
	}
	if err := model.Db.Create(&userPropertyLog).Error; err != nil {
		return 0, err
	}
	return userPropertyLog.ID, nil
}

func RemoveUserProperty(model *domain.Model, userId mysql.ID, propertyId mysql.ID) error {
	return model.Db.Where("user_id = ? AND property_id = ?", userId, propertyId).Delete(&UserProperty{}).Error
}

//用户道具日志
type UserPropertyLog struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId        mysql.ID
	OperateUserId mysql.ID
	PropertyId    mysql.ID
	OriginType    property_e.UserPropertyLogOrginType
	Type          property_e.UserPropertyLogType
	AddSecond     *mysql.Num
	UpdateEndTime *time.Time
}