property.go 4.24 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/resource/mysql"
	"gorm.io/gorm"
	"hilo-user/_const/enum/property_e"
	"hilo-user/myerr"
	"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, myerr.WrapErr(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, myerr.WrapErr(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 myerr.WrapErr(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, myerr.WrapErr(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
hujiebin's avatar
hujiebin committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

func (userProperty *UserProperty) BatchGet(db *gorm.DB, userIds []uint64) (map[uint64]uint64, error) {
	rows := make([]UserProperty, 0)
	if err := db.Model(userProperty).
		Where("end_time > NOW() AND user_id IN ?", userIds).Order("id DESC").Find(&rows).Error; err != nil {
		return nil, err
	}
	result := make(map[uint64]uint64, 0)
	tmp := make(map[uint64]uint64, 0)
	for _, i := range rows {
		if _, ok := result[i.UserId]; !ok {
			if i.Using == property_e.YesUsing {
				// using = true且id最大,就确定是当前使用的
				result[i.UserId] = i.PropertyId
			} else if _, ok := tmp[i.UserId]; !ok {
				// using = false且id最大,先记下,因为不知道还有没有using=true的
				tmp[i.UserId] = i.PropertyId
			}
		}
	}
	for k, v := range tmp {
		// result中没有的,就采用tmp保存的
		if _, ok := result[k]; !ok {
			result[k] = v
		}
	}
	return result, nil
}