package svc import ( "applet/app/admin/svc/order" "applet/app/db" "applet/app/db/model" "applet/app/e" "applet/app/ipad/md" "applet/app/utils" "github.com/gin-gonic/gin" "time" ) func NewOrderMakeEnd(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"] = "烘焙" commNewMake(c, args) } func NewOrderBakingEnd(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"] = "烘焙" commNewMake(c, args) } func NewOrderSortingEnd(c *gin.Context) { var args md.NewOrderSorting 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() //修改制作记录 oids := make([]string, 0) for _, v := range args.OrderInfo { oids = append(oids, v["oid"]) record := db.GetOrderGoods(sess, v["oid"], args.Gid, args.SkuId) if record.MakeDate > utils.StrToInt(time.Now().Format("20060102")) { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, v["oid"]+",未到制作时间,请勿提交")) return } if utils.StrToInt(v["num"]) > record.Num-record.SuccessNum { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, record.GoodsTitle+",数量只能提交"+utils.IntToStr(record.Num-record.SuccessNum)+"个")) return } record.SuccessNum += utils.StrToInt(v["num"]) if record.SuccessNum >= record.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 } makeRecord := db.GetOrderGoodsMakeStockByDate(sess, utils.StrToInt64(args.Gid), utils.StrToInt64(args.SkuId), record.MakeDate) if utils.StrToInt(v["num"]) > makeRecord.WaitSortingNum { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, record.GoodsTitle+",数量只能提交"+utils.IntToStr(makeRecord.WaitSortingNum)+"个")) return } makeRecord.WaitSortingNum -= utils.StrToInt(v["num"]) makeRecord.SuccessNum += utils.StrToInt(v["num"]) _, err = sess.Where("id=?", makeRecord.Id).Cols("success_num,wait_sorting_num").Update(makeRecord) 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(), Gid: record.GoodsId, SkuId: record.SkuId, SkuCode: record.SkuCode, AdminId: utils.StrToInt(args.AdminId), GoodsTitle: record.GoodsTitle, Sku: record.Sku, } var admin model.Staff sess.Where("id=?", recordFlow.AdminId).Get(&admin) recordFlow.AdminName = admin.Name one, err := sess.InsertOne(recordFlow) if one == 0 || err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试")) return } } if len(oids) > 0 { ord := db.GetOrderByIdsSess(sess, oids) if ord != nil { for _, v := range *ord { err := order.JudgePackageOrdOrdStateSess(sess, &v) if err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "请重新提交")) return } } } } sess.Commit() e.OutSuc(c, "success", nil) return } func commNewMake(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.GetOrderGoodsMakeStockById(sess, utils.StrToInt64(args["id"])) if orderGoods == nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试")) return } if orderGoods.MakeDate > utils.StrToInt(time.Now().Format("20060102")) { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "该订单未到制作时间,请勿提交")) return } WaitMakeNum := orderGoods.WaitMakeNum if state == 2 { //制作完成 if utils.StrToInt(args["num"]) > orderGoods.WaitMakeNum { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitMakeNum)+"个")) return } orderGoods.WaitMakeNum -= utils.StrToInt(args["num"]) orderGoods.WaitBakingNum += utils.StrToInt(args["num"]) if orderGoods.WaitMakeNum < 0 { orderGoods.WaitMakeNum = 0 } } if state == 3 { //烘焙完成 if utils.StrToInt(args["num"]) > orderGoods.WaitBakingNum { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitBakingNum)+"个")) return } WaitMakeNum = orderGoods.WaitBakingNum orderGoods.WaitBakingNum -= utils.StrToInt(args["num"]) orderGoods.WaitSortingNum += utils.StrToInt(args["num"]) if orderGoods.WaitBakingNum < 0 { orderGoods.WaitBakingNum = 0 } } _, err := sess.Where("id=?", orderGoods.Id).Cols("wait_make_num,wait_baking_num,wait_sorting_num").Update(orderGoods) if err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试")) return } //写入流程记录 var recordFlow = &model.OrderGoodsDoingFlow{ Num: WaitMakeNum, State: state, Memo: args["memo"], SuccessNum: utils.StrToInt(args["num"]), Time: time.Now(), Gid: orderGoods.Gid, SkuId: orderGoods.SkuId, SkuCode: orderGoods.SkuCode, AdminId: utils.StrToInt(args["admin_id"]), GoodsTitle: orderGoods.GoodsTitle, Sku: orderGoods.Sku, } var admin model.Staff sess.Where("id=?", recordFlow.AdminId).Get(&admin) recordFlow.AdminName = admin.Name one, err := sess.InsertOne(recordFlow) if one == 0 || err != nil { sess.Rollback() e.OutErr(c, 400, e.NewErr(400, "操作失败,请重试")) return } sess.Commit() e.OutSuc(c, "success", nil) return }