面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

177 lines
4.7 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/ipad/md"
  7. "applet/app/utils"
  8. "github.com/gin-gonic/gin"
  9. "time"
  10. )
  11. func OrderMakeBegin(c *gin.Context) {
  12. var args map[string]string
  13. if err := c.ShouldBindJSON(&args); err != nil {
  14. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  15. return
  16. }
  17. args["state"] = "1"
  18. args["str"] = "制作"
  19. commMake(c, args)
  20. }
  21. func OrderMakeEnd(c *gin.Context) {
  22. var args map[string]string
  23. if err := c.ShouldBindJSON(&args); err != nil {
  24. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  25. return
  26. }
  27. args["state"] = "2"
  28. args["str"] = "烘焙"
  29. commMake(c, args)
  30. }
  31. func OrderBakingEnd(c *gin.Context) {
  32. var args map[string]string
  33. if err := c.ShouldBindJSON(&args); err != nil {
  34. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  35. return
  36. }
  37. args["state"] = "3"
  38. args["str"] = "烘焙"
  39. commMake(c, args)
  40. }
  41. func OrderSortingEnd(c *gin.Context) {
  42. var args md.OrderSorting
  43. if err := c.ShouldBindJSON(&args); err != nil {
  44. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  45. return
  46. }
  47. eg := db.Db
  48. sess := eg.NewSession()
  49. defer sess.Close()
  50. sess.Begin()
  51. order := db.GetOrderByIdSess(sess, args.Oid)
  52. if order == nil {
  53. sess.Rollback()
  54. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  55. return
  56. }
  57. if order.State == 4 {
  58. sess.Rollback()
  59. e.OutErr(c, 400, e.NewErr(400, "订单已分拣完成"))
  60. return
  61. }
  62. //修改制作记录
  63. for _, v := range args.GoodsInfo {
  64. record := db.GetOrderGoodsByIdSess(sess, v["id"])
  65. record.SuccessNum = utils.StrToInt(v["num"])
  66. record.State = 4
  67. record.AdminId = utils.StrToInt(args.AdminId)
  68. record.Time = time.Now()
  69. _, err := sess.Where("id=?", record.Id).Cols("success_num,state,time").Update(record)
  70. if err != nil {
  71. sess.Rollback()
  72. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  73. return
  74. }
  75. //写入流程记录
  76. var recordFlow = &model.OrderGoodsDoingFlow{
  77. Num: record.Num,
  78. State: 4,
  79. Memo: "",
  80. SuccessNum: utils.StrToInt(v["num"]),
  81. Time: time.Now(),
  82. RecordId: record.Id,
  83. AdminId: utils.StrToInt(args.AdminId),
  84. }
  85. one, err := sess.InsertOne(recordFlow)
  86. if one == 0 || err != nil {
  87. sess.Rollback()
  88. e.OutErr(c, 400, e.NewErr(400, "开始制作失败,请重试"))
  89. return
  90. }
  91. }
  92. //判断大订单 要不要改状态
  93. count, _ := sess.Where("oid=?", order.Oid).Count(&model.OrderGoods{})
  94. countEnd, _ := sess.Where("oid=? and state=?", order.Oid, 4).Count(&model.OrderGoods{})
  95. if countEnd == count {
  96. _, err := sess.Where("oid=?", order.Oid).Cols("state").Update(&model.Order{State: 4})
  97. if err != nil {
  98. sess.Rollback()
  99. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  100. return
  101. }
  102. }
  103. sess.Commit()
  104. e.OutSuc(c, "success", nil)
  105. return
  106. }
  107. func commMake(c *gin.Context, args map[string]string) {
  108. eg := db.Db
  109. sess := eg.NewSession()
  110. defer sess.Close()
  111. sess.Begin()
  112. state := utils.StrToInt(args["state"])
  113. orderGoods := db.GetOrderGoodsByIdSess(sess, args["id"])
  114. if orderGoods == nil {
  115. sess.Rollback()
  116. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  117. return
  118. }
  119. order := db.GetOrderByIdSess(sess, utils.Int64ToStr(orderGoods.Oid))
  120. if order == nil {
  121. sess.Rollback()
  122. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  123. return
  124. }
  125. if orderGoods.State >= state {
  126. sess.Rollback()
  127. e.OutErr(c, 400, e.NewErr(400, "该商品已在"+args["str"]+"中"))
  128. return
  129. }
  130. orderGoods.SuccessNum = utils.StrToInt(args["num"])
  131. orderGoods.Memo = args["memo"]
  132. orderGoods.State = state
  133. orderGoods.AdminId = utils.StrToInt(args["admin_id"])
  134. orderGoods.Time = time.Now()
  135. _, err := sess.Where("id=?", orderGoods.Id).Cols("success_num,memo,state,time").Update(orderGoods)
  136. if err != nil {
  137. sess.Rollback()
  138. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  139. return
  140. }
  141. //写入流程记录
  142. var recordFlow = &model.OrderGoodsDoingFlow{
  143. Num: orderGoods.Num,
  144. State: state,
  145. Memo: args["memo"],
  146. SuccessNum: utils.StrToInt(args["num"]),
  147. Time: time.Now(),
  148. RecordId: orderGoods.Id,
  149. AdminId: utils.StrToInt(args["admin_id"]),
  150. }
  151. one, err := sess.InsertOne(recordFlow)
  152. if one == 0 || err != nil {
  153. sess.Rollback()
  154. e.OutErr(c, 400, e.NewErr(400, "开始制作失败,请重试"))
  155. return
  156. }
  157. //判断大订单 要不要改状态
  158. count, _ := sess.Where("oid=?", orderGoods.Oid).Count(&model.OrderGoods{})
  159. countEnd, _ := sess.Where("oid=? and state=?", orderGoods.Oid, state).Count(&model.OrderGoods{})
  160. if countEnd == count {
  161. _, err = sess.Where("oid=?", orderGoods.Oid).Cols("state").Update(&model.Order{State: state})
  162. if err != nil {
  163. sess.Rollback()
  164. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  165. return
  166. }
  167. }
  168. sess.Commit()
  169. e.OutSuc(c, "success", nil)
  170. return
  171. }