|
- 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.GetOrderByIdSess(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.GetOrderGoodsByIdSess(sess, 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(),
- Gid: record.GoodsId,
- SkuId: record.SkuId,
- SkuCode: record.SkuCode,
- 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
- }
- if orderGoods.State >= state {
- sess.Rollback()
- e.OutErr(c, 400, e.NewErr(400, "该商品已在"+args["str"]+"中"))
- return
- }
- orderGoods.SuccessNum = utils.StrToInt(args["num"])
- orderGoods.Memo = args["memo"]
- orderGoods.State = state
- orderGoods.AdminId = utils.StrToInt(args["admin_id"])
- orderGoods.Time = time.Now()
- _, err := sess.Where("id=?", orderGoods.Id).Cols("success_num,memo,state,time").Update(orderGoods)
- 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(),
- Gid: orderGoods.GoodsId,
- SkuId: orderGoods.SkuId,
- SkuCode: orderGoods.SkuCode,
- 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
- }
- //判断大订单 要不要改状态
- 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
- }
- 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()
- //修改制作记录
- for _, v := range args.OrderInfo {
- record := db.GetOrderGoods(sess, v["oid"], args.Gid, args.SkuId)
- 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.GetOrderGoodsMakeStock(sess, utils.StrToInt64(args.Gid), utils.StrToInt64(args.SkuId))
- 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),
- }
- 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
- }
-
- 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.GetOrderGoodsMakeStock(sess, utils.StrToInt64(args["gid"]), utils.StrToInt64(args["sku_id"]))
-
- if orderGoods == nil {
- 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"]),
- }
- 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
- }
|