go_dreamfactory/modules/gourmet/api_createorder.go
2022-08-18 15:11:20 +08:00

126 lines
3.4 KiB
Go

package gourmet
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/utils"
"time"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) CreateOrderCheck(session comm.IUserSession, req *pb.GourmetCreateOrderReq) (code pb.ErrorCode) {
if len(req.Order) == 0 {
code = pb.ErrorCode_ReqParameterError
return
}
return
}
///美食城创建订单
func (this *apiComp) CreateOrder(session comm.IUserSession, req *pb.GourmetCreateOrderReq) (code pb.ErrorCode, data proto.Message) {
var (
res []*cfg.Gameatn
costTime int32
)
code = this.CreateOrderCheck(session, req)
if code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
_gourmet, err := this.module.modelGourmet.getGourmetList(session.GetUserId())
if err != nil {
code = pb.ErrorCode_DBError
return
}
for _, order := range req.Order {
if order.FoodCount == 0 {
continue
}
foodtype := order.FoodType
// 获取技能等级
skillLv := _gourmet.Skill[foodtype]
// 计算出需要的时间
_skillCfg := this.module.configure.GetGourmetSkillConfigData(foodtype, skillLv)
costTime += _skillCfg.Needtime * order.FoodCount
}
if _gourmet.Foods == nil { // 队列数据为nil 直接将订单数据给ta
_gourmet.Foods = req.Order
} else {
for _, v := range req.Order {
bFound := false
for _, v1 := range _gourmet.Foods {
if v.FoodType == v1.FoodType {
v1.FoodCount += v.FoodCount // 加对应的数量
bFound = true
break
}
}
if !bFound {
_gourmet.Foods = append(_gourmet.Foods, v)
}
}
}
if _gourmet.CookingFood == nil || (_gourmet.CookingFood != nil && _gourmet.CookingFood.ETime == 0) {
if _gourmet.Ctime == 0 {
_gourmet.Ctime = time.Now().Unix()
}
if !utils.IsToday(_gourmet.Ctime) {
_gourmet.Ctime = time.Now().Unix()
_gourmet.OrderCostTime = 0
}
for _, v := range _gourmet.Foods {
if v.FoodCount > 0 {
v.FoodCount--
// 获取生产时间
_skillCfg := this.module.configure.GetGourmetSkillConfigData(v.FoodType, _gourmet.Skill[v.FoodType])
_gourmet.CookingFood = &pb.Cooking{
FoodType: v.FoodType,
ETime: time.Now().Unix() + int64(_skillCfg.Needtime),
}
break
}
}
}
// 計算耗時
for _, v := range _gourmet.Foods {
if v.FoodCount > 0 {
_skillCfg := this.module.configure.GetGourmetSkillConfigData(v.FoodType, _gourmet.Skill[v.FoodType])
v.CookTime = _skillCfg.Needtime * v.FoodCount
}
}
if _gourmet.CookingFood != nil && _gourmet.CookingFood.ETime == 0 {
_gourmet.CookingFood = nil
}
// 获取总的下单时长
cfgCom := this.module.configure.GetGlobalConf()
if cfgCom == nil {
return
}
if cfgCom.Gourmet < _gourmet.OrderCostTime+costTime { // 大于总时长是不允许的
code = pb.ErrorCode_GourmetMoreOrderTime
return
}
if code = this.module.ConsumeRes(session, res, true); code != pb.ErrorCode_Success { // 消耗校验
return
}
if code = this.module.DispenseRes(session, res, true); code != pb.ErrorCode_Success { // 真正消耗
return
}
// 校验通过 写数据
mapData := make(map[string]interface{}, 0)
mapData["foods"] = _gourmet.Foods
mapData["orderCostTime"] = _gourmet.OrderCostTime
mapData["cookingFood"] = _gourmet.CookingFood // 正在做的
code = this.module.ModifyGourmetData(session.GetUserId(), mapData)
session.SendMsg(string(this.module.GetType()), GourmetCreateOrderResp, &pb.GourmetCreateOrderResp{Data: _gourmet})
return
}