property.go 6 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 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
package user_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-group/_const/enum/property_e"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"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 GetUserPropertyOrErr(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 {
		return nil, myerr.WrapErr(err)
	}
	userProperty.Model = model
	return &userProperty, nil
}

//获取正在使用的座驾,不存在则位nil
/*func GetUserPropertyWithUsingNil(model *domain.Model, userId mysql.ID) (*UserProperty, error) {
	userProperty := UserProperty{}
	if err := model.Db.Model(&UserProperty{}).Where(&UserProperty{
		UserId: userId,
		Using:  property_m.YesUsing,
	}).First(&userProperty).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, 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 (userProperty *UserProperty) Give(receiveUserId mysql.ID) (*UserProperty, *UserProperty, uint32, error) {
	remainSecond := userProperty.EndTime.Unix() - time.Now().Unix()
	if remainSecond <= 0 {
		return nil, nil, 0, bizerr.UserPropertyHasEnd
	}
	receiveUserProperty, err := GetUserPropertyOrInit(userProperty.Model, receiveUserId, userProperty.PropertyId)
	if err != nil {
		return nil, nil, 0, err
	}

	receiveUserProperty, _ , err = receiveUserProperty.AddEndTime(property_e.Give, uint32(remainSecond), userProperty.UserId)
	if err != nil {
		return nil, nil, 0, err
	}

	_, err = addUserPropertyLog(userProperty.Model, userProperty.UserId, userProperty.PropertyId, property_e.Give, property_e.Del, nil, nil, userProperty.UserId)
	if err != nil {
		return nil, nil, 0, err
	}
	userProperty.SetDel()
	return userProperty, receiveUserProperty, uint32(remainSecond), 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
}

func (up *UserProperty) BatchGet(db *gorm.DB, userIds []uint64) (map[uint64]uint64, error) {
	rows := make([]UserProperty, 0)
	if err := db.Model(up).
		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
}

//用户道具日志
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
}