面包店
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.
 
 
 
 
 

171 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 NewOrderMakeEnd(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"] = "2"
  18. args["str"] = "烘焙"
  19. commNewMake(c, args)
  20. }
  21. func NewOrderBakingEnd(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"] = "3"
  28. args["str"] = "烘焙"
  29. commNewMake(c, args)
  30. }
  31. func NewOrderSortingEnd(c *gin.Context) {
  32. var args md.NewOrderSorting
  33. if err := c.ShouldBindJSON(&args); err != nil {
  34. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  35. return
  36. }
  37. eg := db.Db
  38. sess := eg.NewSession()
  39. defer sess.Close()
  40. sess.Begin()
  41. //修改制作记录
  42. for _, v := range args.OrderInfo {
  43. record := db.GetOrderGoods(sess, v["oid"], args.Gid, args.SkuId)
  44. record.SuccessNum += utils.StrToInt(v["num"])
  45. if record.SuccessNum >= record.Num {
  46. record.State = 4
  47. }
  48. record.AdminId = utils.StrToInt(args.AdminId)
  49. record.Time = time.Now()
  50. _, err := sess.Where("id=?", record.Id).Cols("success_num,state,time").Update(record)
  51. if err != nil {
  52. sess.Rollback()
  53. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  54. return
  55. }
  56. makeRecord := db.GetOrderGoodsMakeStock(sess, utils.StrToInt64(args.Gid), utils.StrToInt64(args.SkuId))
  57. if utils.StrToInt(v["num"]) > makeRecord.WaitSortingNum {
  58. sess.Rollback()
  59. e.OutErr(c, 400, e.NewErr(400, record.GoodsTitle+",数量只能提交"+utils.IntToStr(makeRecord.WaitSortingNum)+"个"))
  60. return
  61. }
  62. makeRecord.WaitSortingNum -= utils.StrToInt(v["num"])
  63. makeRecord.SuccessNum += utils.StrToInt(v["num"])
  64. _, err = sess.Where("id=?", makeRecord.Id).Cols("success_num,wait_sorting_num").Update(makeRecord)
  65. if err != nil {
  66. sess.Rollback()
  67. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  68. return
  69. }
  70. //写入流程记录
  71. var recordFlow = &model.OrderGoodsDoingFlow{
  72. Num: record.Num,
  73. State: 4,
  74. Memo: "",
  75. SuccessNum: utils.StrToInt(v["num"]),
  76. Time: time.Now(),
  77. Gid: record.GoodsId,
  78. SkuId: record.SkuId,
  79. SkuCode: record.SkuCode,
  80. AdminId: utils.StrToInt(args.AdminId),
  81. GoodsTitle: record.GoodsTitle,
  82. Sku: record.Sku,
  83. }
  84. one, err := sess.InsertOne(recordFlow)
  85. if one == 0 || err != nil {
  86. sess.Rollback()
  87. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  88. return
  89. }
  90. }
  91. sess.Commit()
  92. e.OutSuc(c, "success", nil)
  93. return
  94. }
  95. func commNewMake(c *gin.Context, args map[string]string) {
  96. eg := db.Db
  97. sess := eg.NewSession()
  98. defer sess.Close()
  99. sess.Begin()
  100. state := utils.StrToInt(args["state"])
  101. orderGoods := db.GetOrderGoodsMakeStock(sess, utils.StrToInt64(args["gid"]), utils.StrToInt64(args["sku_id"]))
  102. if orderGoods == nil {
  103. sess.Rollback()
  104. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  105. return
  106. }
  107. WaitMakeNum := orderGoods.WaitMakeNum
  108. if state == 2 { //制作完成
  109. if utils.StrToInt(args["num"]) > orderGoods.WaitMakeNum {
  110. sess.Rollback()
  111. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitMakeNum)+"个"))
  112. return
  113. }
  114. orderGoods.WaitMakeNum -= utils.StrToInt(args["num"])
  115. orderGoods.WaitBakingNum += utils.StrToInt(args["num"])
  116. if orderGoods.WaitMakeNum < 0 {
  117. orderGoods.WaitMakeNum = 0
  118. }
  119. }
  120. if state == 3 { //烘焙完成
  121. if utils.StrToInt(args["num"]) > orderGoods.WaitBakingNum {
  122. sess.Rollback()
  123. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitBakingNum)+"个"))
  124. return
  125. }
  126. WaitMakeNum = orderGoods.WaitBakingNum
  127. orderGoods.WaitBakingNum -= utils.StrToInt(args["num"])
  128. orderGoods.WaitSortingNum += utils.StrToInt(args["num"])
  129. if orderGoods.WaitBakingNum < 0 {
  130. orderGoods.WaitBakingNum = 0
  131. }
  132. }
  133. _, err := sess.Where("id=?", orderGoods.Id).Cols("wait_make_num,wait_baking_num,wait_sorting_num").Update(orderGoods)
  134. if err != nil {
  135. sess.Rollback()
  136. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  137. return
  138. }
  139. //写入流程记录
  140. var recordFlow = &model.OrderGoodsDoingFlow{
  141. Num: WaitMakeNum,
  142. State: state,
  143. Memo: args["memo"],
  144. SuccessNum: utils.StrToInt(args["num"]),
  145. Time: time.Now(),
  146. Gid: orderGoods.Gid,
  147. SkuId: orderGoods.SkuId,
  148. SkuCode: orderGoods.SkuCode,
  149. AdminId: utils.StrToInt(args["admin_id"]),
  150. GoodsTitle: orderGoods.GoodsTitle,
  151. Sku: orderGoods.Sku,
  152. }
  153. one, err := sess.InsertOne(recordFlow)
  154. if one == 0 || err != nil {
  155. sess.Rollback()
  156. e.OutErr(c, 400, e.NewErr(400, "操作失败,请重试"))
  157. return
  158. }
  159. sess.Commit()
  160. e.OutSuc(c, "success", nil)
  161. return
  162. }