go_dreamfactory/modules/gourmet/api_createorder.go
2022-09-01 18:55:02 +08:00

149 lines
3.8 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
szTime map[int32]int32 // 记录每个类型的订单耗时 key 是技能组type value 订单耗时
)
szTime = make(map[int32]int32, 0)
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 k, v := range _gourmet.Skill {
var _time int32
// 计算出需要的时间
_skillCfg := this.module.configure.GetGourmetConfigData(k, v)
_time += _skillCfg.Needtime
// 高效制作技能
for k, v := range _gourmet.SpecialSkill {
specalSkill := this.module.configure.GetGourmetConfigData(k, v)
_time += specalSkill.Needtime
}
szTime[k] = _time
}
if !utils.IsToday(_gourmet.Ctime) { // 跨天了
_gourmet.Ctime = time.Now().Unix()
_gourmet.OrderCostTime = 0
}
for _, order := range req.Order {
if order.FoodCount == 0 {
continue
}
if v, ok := szTime[order.FoodType]; ok {
costTime += v * order.FoodCount
}
}
// 校验时间
cfgCom := this.module.configure.GetGlobalConf() // 获取总的下单时长
if cfgCom == nil {
return
}
_gourmet.OrderCostTime += costTime
if cfgCom.Gourmet < _gourmet.OrderCostTime { // 大于总时长是不允许的
code = pb.ErrorCode_GourmetMoreOrderTime
return
}
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)
}
}
}
// 重新计算时间
for _, v := range _gourmet.Foods {
if v.FoodCount == 0 {
continue
}
if v1, ok := szTime[v.FoodType]; ok {
v.CookTime = v1 * v.FoodCount
costTime += v.CookTime
}
}
if _gourmet.CookingFood != nil && _gourmet.CookingFood.ETime == 0 {
_gourmet.CookingFood = nil
}
if _gourmet.CookingFood == nil {
if _gourmet.Ctime == 0 {
_gourmet.Ctime = time.Now().Unix()
}
for _, v := range _gourmet.Foods {
if v.FoodCount > 0 {
v.FoodCount--
v.CookTime = v.FoodCount * szTime[v.FoodType]
// 获取生产时间
_gourmet.CookingFood = &pb.Cooking{
FoodType: v.FoodType,
ETime: time.Now().Unix() + int64(szTime[v.FoodType]),
STime: time.Now().Unix(),
}
if v.FoodCount == 0 {
v.CookTime = 0
}
break
}
}
}
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 // 正在做的
mapData["ctime"] = _gourmet.Ctime
code = this.module.ModifyGourmetData(session.GetUserId(), mapData)
session.SendMsg(string(this.module.GetType()), GourmetCreateOrderResp, &pb.GourmetCreateOrderResp{Data: _gourmet})
return
}