121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package smithy
|
|
|
|
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.SmithyCreateOrderReq) (code pb.ErrorCode) {
|
|
if len(req.Order) == 0 {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
///美食城创建订单
|
|
func (this *apiComp) CreateOrder(session comm.IUserSession, req *pb.SmithyCreateOrderReq) (code pb.ErrorCode, data proto.Message) {
|
|
var (
|
|
res []*cfg.Gameatn
|
|
costTime int32
|
|
)
|
|
|
|
code = this.CreateOrderCheck(session, req)
|
|
if code != pb.ErrorCode_Success {
|
|
return // 参数校验失败直接返回
|
|
}
|
|
_smithy, err := this.module.modelSmithy.getSmithyList(session.GetUserId())
|
|
if err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
_skillCfg := this.module.configure.GetSmithyStoveConfigData(_smithy.StoveLv)
|
|
needTime := _skillCfg.Time // 订单需要的时间
|
|
for _, order := range req.Order {
|
|
if order.Count == 0 {
|
|
continue
|
|
}
|
|
costTime += needTime * order.Count
|
|
}
|
|
if _smithy.Orders == nil { // 队列数据为nil 直接将订单数据给ta
|
|
_smithy.Orders = req.Order
|
|
} else {
|
|
for _, v := range req.Order {
|
|
bFound := false
|
|
for _, v1 := range _smithy.Orders {
|
|
if v.DeskType == v1.DeskType {
|
|
v1.Count += v.Count // 加对应的数量
|
|
bFound = true
|
|
break
|
|
}
|
|
}
|
|
if !bFound {
|
|
_smithy.Orders = append(_smithy.Orders, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
if _smithy.Clang == nil || (_smithy.Clang != nil && _smithy.Clang.ETime == 0) {
|
|
if _smithy.Ctime == 0 {
|
|
_smithy.Ctime = time.Now().Unix()
|
|
}
|
|
if !utils.IsToday(_smithy.Ctime) {
|
|
_smithy.Ctime = time.Now().Unix()
|
|
_smithy.OrderCostTime = 0
|
|
}
|
|
for _, v := range _smithy.Orders {
|
|
if v.Count > 0 {
|
|
v.Count--
|
|
// 获取生产时间
|
|
|
|
_smithy.Clang = &pb.Clang{
|
|
DeskType: v.DeskType,
|
|
ETime: time.Now().Unix() + int64(needTime),
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
}
|
|
// 計算耗時
|
|
for _, v := range _smithy.Orders {
|
|
if v.Count > 0 {
|
|
v.NeedTime = needTime * v.Count
|
|
}
|
|
}
|
|
if _smithy.Clang != nil && _smithy.Clang.ETime == 0 {
|
|
_smithy.Clang = nil
|
|
}
|
|
// 获取总的下单时长
|
|
cfgCom := this.module.configure.GetGlobalConf()
|
|
if cfgCom == nil {
|
|
return
|
|
}
|
|
if cfgCom.SmithyMaxtime < _smithy.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["orders"] = _smithy.Orders
|
|
mapData["orderCostTime"] = _smithy.OrderCostTime
|
|
mapData["clang"] = _smithy.Clang // 正在做的
|
|
code = this.module.ModifySmithyData(session.GetUserId(), mapData)
|
|
|
|
session.SendMsg(string(this.module.GetType()), SmithyCreateOrderResp, &pb.SmithyCreateOrderResp{Data: _smithy})
|
|
return
|
|
}
|