|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- 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 utils.StrToInt(v["num"]) == 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))
- 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 { //制作完成
- orderGoods.WaitMakeNum -= utils.StrToInt(args["num"])
- orderGoods.WaitBakingNum += utils.StrToInt(args["num"])
- if orderGoods.WaitMakeNum < 0 {
- orderGoods.WaitMakeNum = 0
- }
- }
- if state == 3 { //烘焙完成
- 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
- }
|