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

183 lines
5.3 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. if record.MakeDate > utils.StrToInt(time.Now().Format("20060102")) {
  45. sess.Rollback()
  46. e.OutErr(c, 400, e.NewErr(400, v["oid"]+",未到制作时间,请勿提交"))
  47. return
  48. }
  49. record.SuccessNum += utils.StrToInt(v["num"])
  50. if record.SuccessNum >= record.Num {
  51. record.State = 4
  52. }
  53. record.AdminId = utils.StrToInt(args.AdminId)
  54. record.Time = time.Now()
  55. _, err := sess.Where("id=?", record.Id).Cols("success_num,state,time").Update(record)
  56. if err != nil {
  57. sess.Rollback()
  58. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  59. return
  60. }
  61. makeRecord := db.GetOrderGoodsMakeStockByDate(sess, utils.StrToInt64(args.Gid), utils.StrToInt64(args.SkuId), record.MakeDate)
  62. if utils.StrToInt(v["num"]) > makeRecord.WaitSortingNum {
  63. sess.Rollback()
  64. e.OutErr(c, 400, e.NewErr(400, record.GoodsTitle+",数量只能提交"+utils.IntToStr(makeRecord.WaitSortingNum)+"个"))
  65. return
  66. }
  67. makeRecord.WaitSortingNum -= utils.StrToInt(v["num"])
  68. makeRecord.SuccessNum += utils.StrToInt(v["num"])
  69. _, err = sess.Where("id=?", makeRecord.Id).Cols("success_num,wait_sorting_num").Update(makeRecord)
  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. Gid: record.GoodsId,
  83. SkuId: record.SkuId,
  84. SkuCode: record.SkuCode,
  85. AdminId: utils.StrToInt(args.AdminId),
  86. GoodsTitle: record.GoodsTitle,
  87. Sku: record.Sku,
  88. }
  89. var admin model.Staff
  90. sess.Where("id=?", recordFlow.AdminId).Get(&admin)
  91. recordFlow.AdminName = admin.Name
  92. one, err := sess.InsertOne(recordFlow)
  93. if one == 0 || err != nil {
  94. sess.Rollback()
  95. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  96. return
  97. }
  98. }
  99. sess.Commit()
  100. e.OutSuc(c, "success", nil)
  101. return
  102. }
  103. func commNewMake(c *gin.Context, args map[string]string) {
  104. eg := db.Db
  105. sess := eg.NewSession()
  106. defer sess.Close()
  107. sess.Begin()
  108. state := utils.StrToInt(args["state"])
  109. orderGoods := db.GetOrderGoodsMakeStockById(sess, utils.StrToInt64(args["id"]))
  110. if orderGoods == nil {
  111. sess.Rollback()
  112. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  113. return
  114. }
  115. if orderGoods.MakeDate > utils.StrToInt(time.Now().Format("20060102")) {
  116. sess.Rollback()
  117. e.OutErr(c, 400, e.NewErr(400, "该订单未到制作时间,请勿提交"))
  118. return
  119. }
  120. WaitMakeNum := orderGoods.WaitMakeNum
  121. if state == 2 { //制作完成
  122. if utils.StrToInt(args["num"]) > orderGoods.WaitMakeNum {
  123. sess.Rollback()
  124. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitMakeNum)+"个"))
  125. return
  126. }
  127. orderGoods.WaitMakeNum -= utils.StrToInt(args["num"])
  128. orderGoods.WaitBakingNum += utils.StrToInt(args["num"])
  129. if orderGoods.WaitMakeNum < 0 {
  130. orderGoods.WaitMakeNum = 0
  131. }
  132. }
  133. if state == 3 { //烘焙完成
  134. if utils.StrToInt(args["num"]) > orderGoods.WaitBakingNum {
  135. sess.Rollback()
  136. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitBakingNum)+"个"))
  137. return
  138. }
  139. WaitMakeNum = orderGoods.WaitBakingNum
  140. orderGoods.WaitBakingNum -= utils.StrToInt(args["num"])
  141. orderGoods.WaitSortingNum += utils.StrToInt(args["num"])
  142. if orderGoods.WaitBakingNum < 0 {
  143. orderGoods.WaitBakingNum = 0
  144. }
  145. }
  146. _, err := sess.Where("id=?", orderGoods.Id).Cols("wait_make_num,wait_baking_num,wait_sorting_num").Update(orderGoods)
  147. if err != nil {
  148. sess.Rollback()
  149. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  150. return
  151. }
  152. //写入流程记录
  153. var recordFlow = &model.OrderGoodsDoingFlow{
  154. Num: WaitMakeNum,
  155. State: state,
  156. Memo: args["memo"],
  157. SuccessNum: utils.StrToInt(args["num"]),
  158. Time: time.Now(),
  159. Gid: orderGoods.Gid,
  160. SkuId: orderGoods.SkuId,
  161. SkuCode: orderGoods.SkuCode,
  162. AdminId: utils.StrToInt(args["admin_id"]),
  163. GoodsTitle: orderGoods.GoodsTitle,
  164. Sku: orderGoods.Sku,
  165. }
  166. var admin model.Staff
  167. sess.Where("id=?", recordFlow.AdminId).Get(&admin)
  168. recordFlow.AdminName = admin.Name
  169. one, err := sess.InsertOne(recordFlow)
  170. if one == 0 || err != nil {
  171. sess.Rollback()
  172. e.OutErr(c, 400, e.NewErr(400, "操作失败,请重试"))
  173. return
  174. }
  175. sess.Commit()
  176. e.OutSuc(c, "success", nil)
  177. return
  178. }