user_bag.go 2.76 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package bag_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
6
	"gorm.io/gorm/clause"
hujiebin's avatar
hujiebin committed
7 8 9 10 11 12 13
	"hilo-user/_const/enum/res_e"
	"time"
)

type UserBag struct {
	mysql.Entity
	UserId  mysql.ID
hujiebin's avatar
hujiebin committed
14
	ResType mysql.Type // 资源类型 1:礼物
hujiebin's avatar
hujiebin committed
15 16 17 18 19
	ResId   mysql.ID
	Count   mysql.Num
	EndTime time.Time
}

hujiebin's avatar
hujiebin committed
20 21 22 23 24 25 26 27 28 29 30 31 32
type UserBagDetail struct {
	mysql.Entity
	UserId    mysql.ID
	BagId     mysql.ID
	ResType   mysql.Type // 资源类型 1:礼物
	ResId     mysql.ID
	Count     mysql.Num
	AddReduce mysql.AddReduce
	BefNum    mysql.Num
	AftNum    mysql.Num
	Remark    mysql.Str
}

hujiebin's avatar
hujiebin committed
33 34 35 36 37 38 39 40 41 42 43
// 获取用户有效的背包
// param userId 用户id
// param resType 背包类型 1:礼物
// condition
//	1.获取end_time未到期的
//	2.数量大于0的
func GetUserValidUserBag(model *domain.Model, userId mysql.ID, resType res_e.ResUserBag) ([]*UserBag, error) {
	var res []*UserBag
	if err := model.DB().Model(UserBag{}).
		Where("end_time > ?", time.Now()).
		Where("count > 0").
hujiebin's avatar
hujiebin committed
44
		Where("res_type = ?", resType).
hujiebin's avatar
hujiebin committed
45 46
		Where("user_id = ?", userId).
		Order("id").Find(&res).Error; err != nil {
hujiebin's avatar
hujiebin committed
47 48 49 50
		return nil, err
	}
	return res, nil
}
hujiebin's avatar
hujiebin committed
51 52 53 54 55 56 57 58 59 60

// 获取指定背包
func GetUserBag(model *domain.Model, bagId mysql.ID) (*UserBag, error) {
	res := new(UserBag)
	if err := model.DB().Model(UserBag{}).
		Where("id = ?", bagId).First(res).Error; err != nil {
		return nil, err
	}
	return res, nil
}
hujiebin's avatar
hujiebin committed
61 62 63 64 65 66 67 68 69 70 71

// 增加用户背包
// param userId 用户id
// param resType 道具类型
// param resId 道具id
// param count 增加数量
// param day 增加天数
// condition:
//	0.事务操作
//  1.背包表
//  2.明细表
hujiebin's avatar
hujiebin committed
72 73 74 75
// return bagId
func AddUserBag(model *domain.Model, userId mysql.ID, resType mysql.Type, resId mysql.ID, count mysql.Num, day int, reason string) (mysql.ID, error) {
	var bagId mysql.ID
	err := model.Transaction(func(model *domain.Model) error {
hujiebin's avatar
hujiebin committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
		//  1.背包表
		endTime := time.Now().AddDate(0, 0, day)
		userBag := &UserBag{
			UserId:  userId,
			ResType: resType,
			ResId:   resId,
			Count:   count,
			EndTime: endTime,
		}
		if err := model.DB().Clauses(clause.OnConflict{
			Columns:   []clause.Column{{Name: "user_id"}, {Name: "res_type"}, {Name: "res_id"}, {Name: "end_time"}},
			DoUpdates: clause.AssignmentColumns([]string{"count"}),
		}).Create(userBag).Error; err != nil {
			return err
		}
hujiebin's avatar
hujiebin committed
91
		bagId = userBag.ID // 自增id
hujiebin's avatar
hujiebin committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		//  2.明细表
		userBagDetail := &UserBagDetail{
			UserId:    userId,
			BagId:     userBag.ID,
			ResType:   resType,
			ResId:     resId,
			Count:     count,
			AddReduce: mysql.ADD,
			BefNum:    0,     // 加背包的统一为0
			AftNum:    count, // 加背包统一为count,因为每次加几乎不可能是一样的
			Remark:    reason,
		}
		if err := model.DB().Create(userBagDetail).Error; err != nil {
			return err
		}
		return nil
	})
hujiebin's avatar
hujiebin committed
109
	return bagId, err
hujiebin's avatar
hujiebin committed
110
}