diff --git a/internal/model/bag_m/user_bag.go b/internal/model/bag_m/user_bag.go new file mode 100644 index 0000000000000000000000000000000000000000..175a6297792cbd6b87ae34b0a083bc6f29aa17c9 --- /dev/null +++ b/internal/model/bag_m/user_bag.go @@ -0,0 +1,28 @@ +package bag_m + +import ( + "git.hilo.cn/hilo-common/resource/mysql" + "time" +) + +type UserBag struct { + mysql.Entity + UserId mysql.ID + ResType mysql.Type // 资源类型 1:礼物 + ResId mysql.ID + Count mysql.Num + EndTime time.Time +} + +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 +} \ No newline at end of file diff --git a/internal/model/res_m/gift.go b/internal/model/res_m/gift.go new file mode 100644 index 0000000000000000000000000000000000000000..79f1776dd0cee90c5ceced2d44bbbc8fc62f12c8 --- /dev/null +++ b/internal/model/res_m/gift.go @@ -0,0 +1,47 @@ +package res_m + +import ( + "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/resource/mysql" +) + +type ResGift struct { + mysql.Entity + Name mysql.Str + IconUrl mysql.Str + SvagUrl mysql.Str + MusicUrl mysql.Str + Column uint16 + DiamondNum mysql.Num + BeanNum mysql.Num + ReceiveDiamondNum mysql.Num + Second mysql.Num // obsolete + N mysql.Num + GroupBroadcast bool + Cp bool + Together bool + Status mysql.UserYesNo + GiftType mysql.Type +} + +// 获取所有的礼物 +func FindAllResGiftsMap(model *domain.Model) (map[mysql.ID]ResGift, error) { + res := make(map[mysql.ID]ResGift, 0) + rows := make([]ResGift, 0) + if err := model.DB().Model(ResGift{}).Find(&rows).Error; err != nil { + return nil, err + } + for i, v := range rows { + res[v.ID] = rows[i] + } + return res, nil +} + +// 获取礼物 +func FindResGift(model *domain.Model, giftId mysql.ID) (*ResGift, error) { + res := new(ResGift) + if err := model.DB().Model(ResGift{}).Where("id = ?", giftId).First(res).Error; err != nil { + return nil, err + } + return res, nil +} diff --git a/txop/bag_tx/user_bag.go b/txop/bag_tx/user_bag.go new file mode 100644 index 0000000000000000000000000000000000000000..25294ac511a6f3ab03221efaa7fee890b4839df8 --- /dev/null +++ b/txop/bag_tx/user_bag.go @@ -0,0 +1,83 @@ +package bag_tx + +import ( + "fmt" + "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/internal/enum/msg_e" + "git.hilo.cn/hilo-common/internal/model/bag_m" + "git.hilo.cn/hilo-common/internal/model/msg_m" + "git.hilo.cn/hilo-common/internal/model/res_m" + "git.hilo.cn/hilo-common/internal/model/user_m" + "git.hilo.cn/hilo-common/resource/mysql" + "gorm.io/gorm/clause" + "time" +) + +// 增加用户背包 +// param userId 用户id +// param resType 道具类型 +// param resId 道具id +// param count 增加数量 +// param day 增加天数 +// condition: +// 0.事务操作,依赖外面传进来的model +// 1.背包表 +// 2.明细表 +// return bagId +func SendUserBag(txModel *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 + // 1.背包表 + endTime := time.Now().AddDate(0, 0, day) + userBag := &bag_m.UserBag{ + UserId: userId, + ResType: resType, + ResId: resId, + Count: count, + EndTime: endTime, + } + if err := txModel.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 0, err + } + bagId = userBag.ID // 自增id + // 2.明细表 + userBagDetail := &bag_m.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 := txModel.DB().Create(userBagDetail).Error; err != nil { + return 0, err + } + // 小助手通知 + go func() { + model := domain.CreateModelContext(txModel.MyContext) + user, err := user_m.GetUser(model, userId) + if err != nil { + model.Log.Errorf("user_m.GetUser fail:%v", err) + return + } + gift, err := res_m.FindResGift(model, resId) + if err != nil { + model.Log.Errorf("res_m.FindResGift fail:%v", err) + } + if err := msg_m.NewUserRecord(model, userId, msg_e.AddUserBag, + gift.Name, 0, "", fmt.Sprintf("%d", day), + "", "", "").Persistent(); err != nil { + model.Log.Errorf("NewUserRecord fail") + } else { + if err := msg_m.SendEmasMsgAssistant(model, user.ExternalId, user.DeviceType); err != nil { + model.Log.Errorf("SendEmasMsgAssistant fail:%v", err) + } + } + }() + return bagId, nil +}