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

193 lines
5.5 KiB

  1. package svc
  2. import (
  3. "applet/app/admin/svc/order"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/e"
  7. "applet/app/ipad/md"
  8. "applet/app/utils"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. )
  12. func NewOrderMakeEnd(c *gin.Context) {
  13. var args map[string]string
  14. if err := c.ShouldBindJSON(&args); err != nil {
  15. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  16. return
  17. }
  18. args["state"] = "2"
  19. args["str"] = "烘焙"
  20. commNewMake(c, args)
  21. }
  22. func NewOrderBakingEnd(c *gin.Context) {
  23. var args map[string]string
  24. if err := c.ShouldBindJSON(&args); err != nil {
  25. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  26. return
  27. }
  28. args["state"] = "3"
  29. args["str"] = "烘焙"
  30. commNewMake(c, args)
  31. }
  32. func NewOrderSortingEnd(c *gin.Context) {
  33. var args md.NewOrderSorting
  34. if err := c.ShouldBindJSON(&args); err != nil {
  35. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  36. return
  37. }
  38. eg := db.Db
  39. sess := eg.NewSession()
  40. defer sess.Close()
  41. sess.Begin()
  42. //修改制作记录
  43. oids := make([]string, 0)
  44. for _, v := range args.OrderInfo {
  45. oids = append(oids, v["oid"])
  46. record := db.GetOrderGoods(sess, v["oid"], args.Gid, args.SkuId)
  47. if record.MakeDate > utils.StrToInt(time.Now().Format("20060102")) {
  48. sess.Rollback()
  49. e.OutErr(c, 400, e.NewErr(400, v["oid"]+",未到制作时间,请勿提交"))
  50. return
  51. }
  52. record.SuccessNum += utils.StrToInt(v["num"])
  53. if record.SuccessNum >= record.Num {
  54. record.State = 4
  55. }
  56. record.AdminId = utils.StrToInt(args.AdminId)
  57. record.Time = time.Now()
  58. _, err := sess.Where("id=?", record.Id).Cols("success_num,state,time").Update(record)
  59. if err != nil {
  60. sess.Rollback()
  61. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  62. return
  63. }
  64. makeRecord := db.GetOrderGoodsMakeStockByDate(sess, utils.StrToInt64(args.Gid), utils.StrToInt64(args.SkuId), record.MakeDate)
  65. if utils.StrToInt(v["num"]) > makeRecord.WaitSortingNum {
  66. sess.Rollback()
  67. e.OutErr(c, 400, e.NewErr(400, record.GoodsTitle+",数量只能提交"+utils.IntToStr(makeRecord.WaitSortingNum)+"个"))
  68. return
  69. }
  70. makeRecord.WaitSortingNum -= utils.StrToInt(v["num"])
  71. makeRecord.SuccessNum += utils.StrToInt(v["num"])
  72. _, err = sess.Where("id=?", makeRecord.Id).Cols("success_num,wait_sorting_num").Update(makeRecord)
  73. if err != nil {
  74. sess.Rollback()
  75. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  76. return
  77. }
  78. //写入流程记录
  79. var recordFlow = &model.OrderGoodsDoingFlow{
  80. Num: record.Num,
  81. State: 4,
  82. Memo: "",
  83. SuccessNum: utils.StrToInt(v["num"]),
  84. Time: time.Now(),
  85. Gid: record.GoodsId,
  86. SkuId: record.SkuId,
  87. SkuCode: record.SkuCode,
  88. AdminId: utils.StrToInt(args.AdminId),
  89. GoodsTitle: record.GoodsTitle,
  90. Sku: record.Sku,
  91. }
  92. var admin model.Staff
  93. sess.Where("id=?", recordFlow.AdminId).Get(&admin)
  94. recordFlow.AdminName = admin.Name
  95. one, err := sess.InsertOne(recordFlow)
  96. if one == 0 || err != nil {
  97. sess.Rollback()
  98. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  99. return
  100. }
  101. }
  102. sess.Commit()
  103. if len(oids) > 0 {
  104. ord := db.GetOrderByIds(eg, oids)
  105. if ord != nil {
  106. for _, v := range *ord {
  107. order.JudgePackageOrdOrdState(&v)
  108. }
  109. }
  110. }
  111. e.OutSuc(c, "success", nil)
  112. return
  113. }
  114. func commNewMake(c *gin.Context, args map[string]string) {
  115. eg := db.Db
  116. sess := eg.NewSession()
  117. defer sess.Close()
  118. sess.Begin()
  119. state := utils.StrToInt(args["state"])
  120. orderGoods := db.GetOrderGoodsMakeStockById(sess, utils.StrToInt64(args["id"]))
  121. if orderGoods == nil {
  122. sess.Rollback()
  123. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  124. return
  125. }
  126. if orderGoods.MakeDate > utils.StrToInt(time.Now().Format("20060102")) {
  127. sess.Rollback()
  128. e.OutErr(c, 400, e.NewErr(400, "该订单未到制作时间,请勿提交"))
  129. return
  130. }
  131. WaitMakeNum := orderGoods.WaitMakeNum
  132. if state == 2 { //制作完成
  133. if utils.StrToInt(args["num"]) > orderGoods.WaitMakeNum {
  134. sess.Rollback()
  135. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitMakeNum)+"个"))
  136. return
  137. }
  138. orderGoods.WaitMakeNum -= utils.StrToInt(args["num"])
  139. orderGoods.WaitBakingNum += utils.StrToInt(args["num"])
  140. if orderGoods.WaitMakeNum < 0 {
  141. orderGoods.WaitMakeNum = 0
  142. }
  143. }
  144. if state == 3 { //烘焙完成
  145. if utils.StrToInt(args["num"]) > orderGoods.WaitBakingNum {
  146. sess.Rollback()
  147. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitBakingNum)+"个"))
  148. return
  149. }
  150. WaitMakeNum = orderGoods.WaitBakingNum
  151. orderGoods.WaitBakingNum -= utils.StrToInt(args["num"])
  152. orderGoods.WaitSortingNum += utils.StrToInt(args["num"])
  153. if orderGoods.WaitBakingNum < 0 {
  154. orderGoods.WaitBakingNum = 0
  155. }
  156. }
  157. _, err := sess.Where("id=?", orderGoods.Id).Cols("wait_make_num,wait_baking_num,wait_sorting_num").Update(orderGoods)
  158. if err != nil {
  159. sess.Rollback()
  160. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  161. return
  162. }
  163. //写入流程记录
  164. var recordFlow = &model.OrderGoodsDoingFlow{
  165. Num: WaitMakeNum,
  166. State: state,
  167. Memo: args["memo"],
  168. SuccessNum: utils.StrToInt(args["num"]),
  169. Time: time.Now(),
  170. Gid: orderGoods.Gid,
  171. SkuId: orderGoods.SkuId,
  172. SkuCode: orderGoods.SkuCode,
  173. AdminId: utils.StrToInt(args["admin_id"]),
  174. GoodsTitle: orderGoods.GoodsTitle,
  175. Sku: orderGoods.Sku,
  176. }
  177. var admin model.Staff
  178. sess.Where("id=?", recordFlow.AdminId).Get(&admin)
  179. recordFlow.AdminName = admin.Name
  180. one, err := sess.InsertOne(recordFlow)
  181. if one == 0 || err != nil {
  182. sess.Rollback()
  183. e.OutErr(c, 400, e.NewErr(400, "操作失败,请重试"))
  184. return
  185. }
  186. sess.Commit()
  187. e.OutSuc(c, "success", nil)
  188. return
  189. }