76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package shop
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"math"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
//参数校验
|
|
func (this *apiComp) BuyCheck(session comm.IUserSession, req *pb.ShopBuyReq) (code pb.ErrorCode) {
|
|
|
|
return
|
|
}
|
|
|
|
///获取用户商品列表
|
|
func (this *apiComp) Buy(session comm.IUserSession, req *pb.ShopBuyReq) (code pb.ErrorCode, data proto.Message) {
|
|
var (
|
|
err error
|
|
ok bool
|
|
conf *cfg.Game_shopitemData
|
|
shopitem *pb.DBShopItem
|
|
need []*cfg.Game_atn
|
|
)
|
|
defer func() {
|
|
if code == pb.ErrorCode_Success {
|
|
session.SendMsg(string(this.module.GetType()), "buy", &pb.ShopBuyResp{IsSucc: true})
|
|
} else {
|
|
session.SendMsg(string(this.module.GetType()), "buy", &pb.ShopBuyResp{IsSucc: false})
|
|
}
|
|
}()
|
|
if conf, err = this.module.configure.GetShopItemsConfigure(req.GoodsId); err != nil {
|
|
code = pb.ErrorCode_SystemError
|
|
return
|
|
}
|
|
if shopitem, ok = this.module.modelShopItems.QueryUserShopDataByGoodId(session.GetUserId(), req.GoodsId); !ok { //没有购买记录
|
|
shopitem = &pb.DBShopItem{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Uid: session.GetUserId(),
|
|
GoodsId: conf.Key,
|
|
BuyNum: 0,
|
|
LastBuyTime: 0,
|
|
}
|
|
}
|
|
need = make([]*cfg.Game_atn, len(conf.Need))
|
|
for i, v := range conf.Need {
|
|
need[i] = &cfg.Game_atn{
|
|
A: v.A,
|
|
T: v.T,
|
|
N: int32(math.Ceil(float64(v.N) * float64(conf.Sale) / float64(1000))),
|
|
}
|
|
}
|
|
|
|
if code = this.module.ConsumeRes(session, conf.Need, true); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
if code = this.module.DispenseRes(session, conf.Iteminfo, true); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
shopitem.BuyNum++
|
|
shopitem.LastBuyTime = time.Now().Unix()
|
|
if !ok {
|
|
this.module.modelShopItems.AddList(session.GetUserId(), shopitem.Id, shopitem)
|
|
} else {
|
|
this.module.modelShopItems.ChangeList(session.GetUserId(), shopitem.Id, map[string]interface{}{
|
|
"buynum": shopitem.BuyNum,
|
|
"lastbuytime": shopitem.LastBuyTime,
|
|
})
|
|
}
|
|
return
|
|
}
|