面包店
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
5.0 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. var admin model.Staff
  85. sess.Where("id=?", recordFlow.AdminId).Get(&admin)
  86. recordFlow.AdminName = admin.Name
  87. one, err := sess.InsertOne(recordFlow)
  88. if one == 0 || err != nil {
  89. sess.Rollback()
  90. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  91. return
  92. }
  93. }
  94. sess.Commit()
  95. e.OutSuc(c, "success", nil)
  96. return
  97. }
  98. func commNewMake(c *gin.Context, args map[string]string) {
  99. eg := db.Db
  100. sess := eg.NewSession()
  101. defer sess.Close()
  102. sess.Begin()
  103. state := utils.StrToInt(args["state"])
  104. orderGoods := db.GetOrderGoodsMakeStock(sess, utils.StrToInt64(args["gid"]), utils.StrToInt64(args["sku_id"]))
  105. if orderGoods == nil {
  106. sess.Rollback()
  107. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  108. return
  109. }
  110. WaitMakeNum := orderGoods.WaitMakeNum
  111. if state == 2 { //制作完成
  112. if utils.StrToInt(args["num"]) > orderGoods.WaitMakeNum {
  113. sess.Rollback()
  114. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitMakeNum)+"个"))
  115. return
  116. }
  117. orderGoods.WaitMakeNum -= utils.StrToInt(args["num"])
  118. orderGoods.WaitBakingNum += utils.StrToInt(args["num"])
  119. if orderGoods.WaitMakeNum < 0 {
  120. orderGoods.WaitMakeNum = 0
  121. }
  122. }
  123. if state == 3 { //烘焙完成
  124. if utils.StrToInt(args["num"]) > orderGoods.WaitBakingNum {
  125. sess.Rollback()
  126. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitBakingNum)+"个"))
  127. return
  128. }
  129. WaitMakeNum = orderGoods.WaitBakingNum
  130. orderGoods.WaitBakingNum -= utils.StrToInt(args["num"])
  131. orderGoods.WaitSortingNum += utils.StrToInt(args["num"])
  132. if orderGoods.WaitBakingNum < 0 {
  133. orderGoods.WaitBakingNum = 0
  134. }
  135. }
  136. _, err := sess.Where("id=?", orderGoods.Id).Cols("wait_make_num,wait_baking_num,wait_sorting_num").Update(orderGoods)
  137. if err != nil {
  138. sess.Rollback()
  139. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  140. return
  141. }
  142. //写入流程记录
  143. var recordFlow = &model.OrderGoodsDoingFlow{
  144. Num: WaitMakeNum,
  145. State: state,
  146. Memo: args["memo"],
  147. SuccessNum: utils.StrToInt(args["num"]),
  148. Time: time.Now(),
  149. Gid: orderGoods.Gid,
  150. SkuId: orderGoods.SkuId,
  151. SkuCode: orderGoods.SkuCode,
  152. AdminId: utils.StrToInt(args["admin_id"]),
  153. GoodsTitle: orderGoods.GoodsTitle,
  154. Sku: orderGoods.Sku,
  155. }
  156. var admin model.Staff
  157. sess.Where("id=?", recordFlow.AdminId).Get(&admin)
  158. recordFlow.AdminName = admin.Name
  159. one, err := sess.InsertOne(recordFlow)
  160. if one == 0 || err != nil {
  161. sess.Rollback()
  162. e.OutErr(c, 400, e.NewErr(400, "操作失败,请重试"))
  163. return
  164. }
  165. sess.Commit()
  166. e.OutSuc(c, "success", nil)
  167. return
  168. }