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

207 lines
5.6 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 OrderMakeBegin(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"] = "1"
  18. args["str"] = "制作"
  19. commMake(c, args)
  20. }
  21. func OrderMakeEnd(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"] = "2"
  28. args["str"] = "烘焙"
  29. commMake(c, args)
  30. }
  31. func OrderBakingEnd(c *gin.Context) {
  32. var args map[string]string
  33. if err := c.ShouldBindJSON(&args); err != nil {
  34. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  35. return
  36. }
  37. args["state"] = "3"
  38. args["str"] = "烘焙"
  39. commMake(c, args)
  40. }
  41. func OrderSortingEnd(c *gin.Context) {
  42. var args md.OrderSorting
  43. if err := c.ShouldBindJSON(&args); err != nil {
  44. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  45. return
  46. }
  47. eg := db.Db
  48. sess := eg.NewSession()
  49. defer sess.Close()
  50. sess.Begin()
  51. order := db.GetOrderGoodsByIdSess(sess, args.Oid)
  52. if order == nil {
  53. sess.Rollback()
  54. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  55. return
  56. }
  57. if order.State == 4 {
  58. sess.Rollback()
  59. e.OutErr(c, 400, e.NewErr(400, "订单已分拣完成"))
  60. return
  61. }
  62. //修改制作记录
  63. for _, v := range args.GoodsInfo {
  64. record := db.GetOrderGoodsByRecordId(eg, v["id"])
  65. record.SuccessNum = utils.StrToInt(v["num"])
  66. record.State = 4
  67. record.AdminId = utils.StrToInt(args.AdminId)
  68. record.Time = time.Now()
  69. _, err := sess.Where("id=?", record.Id).Cols("success_num,state,time").Update(record)
  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. RecordId: record.RecordId,
  83. AdminId: utils.StrToInt(args.AdminId),
  84. }
  85. one, err := sess.InsertOne(recordFlow)
  86. if one == 0 || err != nil {
  87. sess.Rollback()
  88. e.OutErr(c, 400, e.NewErr(400, "开始制作失败,请重试"))
  89. return
  90. }
  91. }
  92. //判断大订单 要不要改状态
  93. count, _ := sess.Where("oid=?", order.Oid).Count(&model.OrderGoods{})
  94. countEnd, _ := sess.Where("oid=? and state=?", order.Oid, 4).Count(&model.OrderGoods{})
  95. if countEnd == count {
  96. _, err := sess.Where("oid=?", order.Oid).Cols("state").Update(&model.Order{State: 4})
  97. if err != nil {
  98. sess.Rollback()
  99. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  100. return
  101. }
  102. }
  103. sess.Commit()
  104. e.OutSuc(c, "success", nil)
  105. return
  106. }
  107. func commMake(c *gin.Context, args map[string]string) {
  108. eg := db.Db
  109. sess := eg.NewSession()
  110. defer sess.Close()
  111. sess.Begin()
  112. state := utils.StrToInt(args["state"])
  113. orderGoods := db.GetOrderGoodsByIdSess(sess, args["id"])
  114. if orderGoods == nil {
  115. sess.Rollback()
  116. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  117. return
  118. }
  119. order := db.GetOrderByIdSess(sess, utils.Int64ToStr(orderGoods.Oid))
  120. if order == nil {
  121. sess.Rollback()
  122. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  123. return
  124. }
  125. record := db.GetOrderGoodsByRecordId(eg, args["id"])
  126. if orderGoods.State >= state {
  127. sess.Rollback()
  128. e.OutErr(c, 400, e.NewErr(400, "该商品已在"+args["str"]+"中"))
  129. return
  130. }
  131. if record == nil {
  132. record = &model.OrderGoodsDoing{
  133. Oid: orderGoods.Oid,
  134. GoodsId: orderGoods.GoodsId,
  135. SkuId: orderGoods.SkuId,
  136. Num: orderGoods.Num,
  137. State: state,
  138. SkuCode: orderGoods.SkuCode,
  139. GoodsTitle: orderGoods.GoodsTitle,
  140. Sku: orderGoods.Sku,
  141. Time: time.Now(),
  142. RecordId: orderGoods.Id,
  143. EnterpriseId: order.EnterpriseId,
  144. AdminId: utils.StrToInt(args["admin_id"]),
  145. }
  146. one, err := sess.InsertOne(record)
  147. if one == 0 || err != nil {
  148. sess.Rollback()
  149. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  150. return
  151. }
  152. }
  153. record.SuccessNum = utils.StrToInt(args["num"])
  154. record.Memo = args["memo"]
  155. record.State = state
  156. record.AdminId = utils.StrToInt(args["admin_id"])
  157. record.Time = time.Now()
  158. _, err := sess.Where("id=?", record.Id).Cols("success_num,memo,state,time").Update(record)
  159. if err != nil {
  160. sess.Rollback()
  161. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  162. return
  163. }
  164. //写入流程记录
  165. var recordFlow = &model.OrderGoodsDoingFlow{
  166. Num: orderGoods.Num,
  167. State: state,
  168. Memo: args["memo"],
  169. SuccessNum: utils.StrToInt(args["num"]),
  170. Time: time.Now(),
  171. RecordId: record.RecordId,
  172. AdminId: utils.StrToInt(args["admin_id"]),
  173. }
  174. one, err := sess.InsertOne(recordFlow)
  175. if one == 0 || err != nil {
  176. sess.Rollback()
  177. e.OutErr(c, 400, e.NewErr(400, "开始制作失败,请重试"))
  178. return
  179. }
  180. orderGoods.State = state
  181. update, err := sess.Where("id=?", orderGoods.Id).Cols("state").Update(orderGoods)
  182. if update == 0 || err != nil {
  183. sess.Rollback()
  184. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  185. return
  186. }
  187. //判断大订单 要不要改状态
  188. count, _ := sess.Where("oid=?", orderGoods.Oid).Count(&model.OrderGoods{})
  189. countEnd, _ := sess.Where("oid=? and state=?", orderGoods.Oid, state).Count(&model.OrderGoods{})
  190. if countEnd == count {
  191. _, err = sess.Where("oid=?", orderGoods.Oid).Cols("state").Update(&model.Order{State: state})
  192. if err != nil {
  193. sess.Rollback()
  194. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  195. return
  196. }
  197. }
  198. sess.Commit()
  199. e.OutSuc(c, "success", nil)
  200. return
  201. }