package svc import ( "applet/app/db" "applet/app/db/model" "applet/app/e" "applet/app/ipad/md" "applet/app/utils" "github.com/gin-gonic/gin" "time" ) func OrderMakeBegin(c *gin.Context) { var args map[string]string if err := c.ShouldBindJSON(&args); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } args["state"] = "1" args["str"] = "制作" commMake(c, args) } func OrderMakeEnd(c *gin.Context) { var args map[string]string if err := c.ShouldBindJSON(&args); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } args["state"] = "2" args["str"] = "烘焙" commMake(c, args) } func OrderBakingEnd(c *gin.Context) { var args map[string]string if err := c.ShouldBindJSON(&args); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } args["state"] = "3" args["str"] = "烘焙" commMake(c, args) } func OrderSortingEnd(c *gin.Context) { var args md.OrderSorting if err := c.ShouldBindJSON(&args); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } eg := db.Db sess := eg.NewSession() defer sess.Close() sess.Begin() order := db.GetOrderGoodsByIdSess(sess, args.Oid) if order == nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试")) return } if order.State == 4 { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "订单已分拣完成")) return } //修改制作记录 for _, v := range args.GoodsInfo { record := db.GetOrderGoodsByRecordId(eg, v["id"]) record.SuccessNum = utils.StrToInt(v["num"]) record.State = 4 record.AdminId = utils.StrToInt(args.AdminId) record.Time = time.Now() _, err := sess.Where("id=?", record.Id).Cols("success_num,state,time").Update(record) if err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试")) return } //写入流程记录 var recordFlow = &model.OrderGoodsDoingFlow{ Num: record.Num, State: 4, Memo: "", SuccessNum: utils.StrToInt(v["num"]), Time: time.Now(), RecordId: record.RecordId, AdminId: utils.StrToInt(args.AdminId), } one, err := sess.InsertOne(recordFlow) if one == 0 || err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "开始制作失败,请重试")) return } } //判断大订单 要不要改状态 count, _ := sess.Where("oid=?", order.Oid).Count(&model.OrderGoods{}) countEnd, _ := sess.Where("oid=? and state=?", order.Oid, 4).Count(&model.OrderGoods{}) if countEnd == count { _, err := sess.Where("oid=?", order.Oid).Cols("state").Update(&model.Order{State: 4}) if err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试")) return } } sess.Commit() e.OutSuc(c, "success", nil) return } func commMake(c *gin.Context, args map[string]string) { eg := db.Db sess := eg.NewSession() defer sess.Close() sess.Begin() state := utils.StrToInt(args["state"]) orderGoods := db.GetOrderGoodsByIdSess(sess, args["id"]) if orderGoods == nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试")) return } order := db.GetOrderByIdSess(sess, utils.Int64ToStr(orderGoods.Oid)) if order == nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试")) return } record := db.GetOrderGoodsByRecordId(eg, args["id"]) if orderGoods.State >= state { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "该商品已在"+args["str"]+"中")) return } if record == nil { record = &model.OrderGoodsDoing{ Oid: orderGoods.Oid, GoodsId: orderGoods.GoodsId, SkuId: orderGoods.SkuId, Num: orderGoods.Num, State: state, SkuCode: orderGoods.SkuCode, GoodsTitle: orderGoods.GoodsTitle, Sku: orderGoods.Sku, Time: time.Now(), RecordId: orderGoods.Id, EnterpriseId: order.EnterpriseId, AdminId: utils.StrToInt(args["admin_id"]), } one, err := sess.InsertOne(record) if one == 0 || err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试")) return } } record.SuccessNum = utils.StrToInt(args["num"]) record.Memo = args["memo"] record.State = state record.AdminId = utils.StrToInt(args["admin_id"]) record.Time = time.Now() _, err := sess.Where("id=?", record.Id).Cols("success_num,memo,state,time").Update(record) if err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试")) return } //写入流程记录 var recordFlow = &model.OrderGoodsDoingFlow{ Num: orderGoods.Num, State: state, Memo: args["memo"], SuccessNum: utils.StrToInt(args["num"]), Time: time.Now(), RecordId: record.RecordId, AdminId: utils.StrToInt(args["admin_id"]), } one, err := sess.InsertOne(recordFlow) if one == 0 || err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "开始制作失败,请重试")) return } orderGoods.State = state update, err := sess.Where("id=?", orderGoods.Id).Cols("state").Update(&orderGoods) if update == 0 || err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试")) return } //判断大订单 要不要改状态 count, _ := sess.Where("oid=?", orderGoods.Oid).Count(&model.OrderGoods{}) countEnd, _ := sess.Where("oid=? and state=?", orderGoods.Oid, state).Count(&model.OrderGoods{}) if countEnd == count { _, err = sess.Where("oid=?", orderGoods.Oid).Cols("state").Update(&model.Order{State: state}) if err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试")) return } } sess.Commit() e.OutSuc(c, "success", nil) return }