github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/game/internal/redpacketmatch_resultdata.go (about)

     1  package internal
     2  
     3  import (
     4  	"encoding/json"
     5  	"game/circle"
     6  
     7  	"github.com/name5566/leaf/log"
     8  	"gopkg.in/mgo.v2/bson"
     9  )
    10  
    11  type RedPacketMatchResultData struct {
    12  	ID            bson.ObjectId `bson:"_id"`
    13  	UserID        int
    14  	RedPacketType int     // 红包种类(元): 1、5、10、50
    15  	RedPacket     float64 // 红包奖励
    16  	Taken         bool    // 奖励是否被领取
    17  	Handling      bool    // 处理中
    18  	CreatedAt     int64
    19  	UpdatedAt     int64
    20  	CardCode      string //红包码
    21  }
    22  
    23  func saveRedPacketMatchResultData(resultData *RedPacketMatchResultData) {
    24  	temp := &struct {
    25  		UserID        int
    26  		RedPacketType int     // 红包种类(元): 1、5、10、50
    27  		RedPacket     float64 // 红包奖励
    28  		Taken         bool    // 是否领取
    29  		CreatedAt     int64
    30  		CardCode      string //  红包码
    31  	}{}
    32  	temp.UserID = resultData.UserID
    33  	temp.RedPacketType = resultData.RedPacketType
    34  	temp.RedPacket = resultData.RedPacket
    35  	temp.Taken = resultData.Taken
    36  	temp.CreatedAt = resultData.CreatedAt
    37  	temp.CardCode = ""
    38  	skeleton.Go(func() {
    39  		db := mongoDB.Ref()
    40  		defer mongoDB.UnRef(db)
    41  		if temp.RedPacket > 0 {
    42  			temp1 := &struct {
    43  				Code string
    44  				Data string
    45  			}{}
    46  			r := new(circle.RedPacketCodeInfo)
    47  			r.Sum = float64(temp.RedPacket)
    48  			param, _ := json.Marshal(r)
    49  			json.Unmarshal(circle.DoRequestRepacketCode(string(param)), temp1)
    50  			log.Release("玩家用户Id:%v请求%v红包码:%v", temp.UserID, temp.RedPacket, temp1.Data)
    51  			temp.CardCode = temp1.Data
    52  		}
    53  		err := db.DB(DB).C("redpacketmatchresult").Insert(temp)
    54  		if err != nil {
    55  			log.Error("insert redpacketmatchresult data error: %v", err)
    56  		}
    57  	}, nil)
    58  }
    59  
    60  func updateRedPacketMatchResultData(id bson.ObjectId, update interface{}, cb func()) {
    61  	skeleton.Go(func() {
    62  		db := mongoDB.Ref()
    63  		defer mongoDB.UnRef(db)
    64  		_, err := db.DB(DB).C("redpacketmatchresult").UpsertId(id, update)
    65  		if err != nil {
    66  			log.Error("upsert redpacketmatchresult %v data error: %v", id, err)
    67  		}
    68  	}, func() {
    69  		if cb != nil {
    70  			cb()
    71  		}
    72  	})
    73  }