package svc import ( "errors" "net/rpc" "github.com/globalsign/mgo" "github.com/globalsign/mgo/bson" "mongo.games.com/game/common" "mongo.games.com/game/dbproxy/mongo" "mongo.games.com/game/model" "mongo.games.com/goserver/core/logger" ) func ItemLogsCollection(plt string) *mongo.Collection { s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, model.ItemLogDBName) if s != nil { c_itemlog, first := s.DB().C(model.ItemLogCollName) if first { c_itemlog.EnsureIndex(mgo.Index{Key: []string{"logtype"}, Background: true, Sparse: true}) c_itemlog.EnsureIndex(mgo.Index{Key: []string{"itemid"}, Background: true, Sparse: true}) c_itemlog.EnsureIndex(mgo.Index{Key: []string{"createts"}, Background: true, Sparse: true}) c_itemlog.EnsureIndex(mgo.Index{Key: []string{"-createts"}, Background: true, Sparse: true}) c_itemlog.EnsureIndex(mgo.Index{Key: []string{"typeid"}, Background: true, Sparse: true}) c_itemlog.EnsureIndex(mgo.Index{Key: []string{"gameid"}, Background: true, Sparse: true}) c_itemlog.EnsureIndex(mgo.Index{Key: []string{"gamefreeid"}, Background: true, Sparse: true}) c_itemlog.EnsureIndex(mgo.Index{Key: []string{"snid", "logtype", "itemid", "typeid"}, Background: true, Sparse: true}) } return c_itemlog } return nil } type ItemLogSvc struct { } func (svc *ItemLogSvc) InsertItemLog(log *model.ItemLog, ret *bool) (err error) { clog := ItemLogsCollection(log.Platform) if clog == nil { return } err = clog.Insert(log) if err != nil { logger.Logger.Warn("InsertItemLog error:", err) return } *ret = true return } // GetItemCount 获取v卡兑换消耗数量 func GetItemCount(platform string, snid, id int32, tp int) (count int64) { c := ItemLogsCollection(platform) if c == nil { return 0 } var err error var swapN int64 var costN int64 type m struct { Count int64 `bson:"count"` } tc := new(m) // 兑换返还 err = c.Pipe([]bson.M{ {"$match": bson.M{"snid": snid, "logtype": 0, "itemid": id, "typeid": common.GainWay_Exchange}}, {"$group": bson.M{"_id": nil, "count": bson.M{"$sum": "$count"}}}, }).AllowDiskUse().One(tc) if err != nil && !errors.Is(err, mgo.ErrNotFound) { logger.Logger.Warn("GetItemCount swapN error:", err) return 0 } swapN = tc.Count // 消耗总数量 tc = new(m) err = c.Pipe([]bson.M{ {"$match": bson.M{"snid": snid, "logtype": 1, "itemid": id, "typeid": common.GainWay_Exchange}}, {"$group": bson.M{"_id": nil, "count": bson.M{"$sum": "$count"}}}, }).AllowDiskUse().One(tc) if err != nil && !errors.Is(err, mgo.ErrNotFound) { logger.Logger.Warn("GetItemCount costN error:", err) return 0 } costN = tc.Count if tp == 0 { // 获得数量 = 获得总数量 - 兑换返还 - 比赛返回 } else { // 消耗数量 = 消耗总数量 - 兑换返还 count = costN - swapN } if count < 0 { count = 0 } return count } func (svc *ItemLogSvc) GetItemCount(req *model.ItemCountParam, count *int64) error { *count = GetItemCount(req.Platform, req.Snid, req.Id, req.Tp) return nil } func init() { rpc.Register(new(ItemLogSvc)) }