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

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