面包店
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

319 righe
8.7 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.GetOrderByIdSess(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.GetOrderGoodsByIdSess(sess, 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. Gid: record.GoodsId,
  83. SkuId: record.SkuId,
  84. SkuCode: record.SkuCode,
  85. AdminId: utils.StrToInt(args.AdminId),
  86. }
  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. //判断大订单 要不要改状态
  95. count, _ := sess.Where("oid=?", order.Oid).Count(&model.OrderGoods{})
  96. countEnd, _ := sess.Where("oid=? and state=?", order.Oid, 4).Count(&model.OrderGoods{})
  97. if countEnd == count {
  98. _, err := sess.Where("oid=?", order.Oid).Cols("state").Update(&model.Order{State: 4})
  99. if err != nil {
  100. sess.Rollback()
  101. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  102. return
  103. }
  104. }
  105. sess.Commit()
  106. e.OutSuc(c, "success", nil)
  107. return
  108. }
  109. func commMake(c *gin.Context, args map[string]string) {
  110. eg := db.Db
  111. sess := eg.NewSession()
  112. defer sess.Close()
  113. sess.Begin()
  114. state := utils.StrToInt(args["state"])
  115. orderGoods := db.GetOrderGoodsByIdSess(sess, args["id"])
  116. if orderGoods == nil {
  117. sess.Rollback()
  118. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  119. return
  120. }
  121. order := db.GetOrderByIdSess(sess, utils.Int64ToStr(orderGoods.Oid))
  122. if order == nil {
  123. sess.Rollback()
  124. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  125. return
  126. }
  127. if orderGoods.State >= state {
  128. sess.Rollback()
  129. e.OutErr(c, 400, e.NewErr(400, "该商品已在"+args["str"]+"中"))
  130. return
  131. }
  132. orderGoods.SuccessNum = utils.StrToInt(args["num"])
  133. orderGoods.Memo = args["memo"]
  134. orderGoods.State = state
  135. orderGoods.AdminId = utils.StrToInt(args["admin_id"])
  136. orderGoods.Time = time.Now()
  137. _, err := sess.Where("id=?", orderGoods.Id).Cols("success_num,memo,state,time").Update(orderGoods)
  138. if err != nil {
  139. sess.Rollback()
  140. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  141. return
  142. }
  143. //写入流程记录
  144. var recordFlow = &model.OrderGoodsDoingFlow{
  145. Num: orderGoods.Num,
  146. State: state,
  147. Memo: args["memo"],
  148. SuccessNum: utils.StrToInt(args["num"]),
  149. Time: time.Now(),
  150. Gid: orderGoods.GoodsId,
  151. SkuId: orderGoods.SkuId,
  152. SkuCode: orderGoods.SkuCode,
  153. AdminId: utils.StrToInt(args["admin_id"]),
  154. }
  155. one, err := sess.InsertOne(recordFlow)
  156. if one == 0 || err != nil {
  157. sess.Rollback()
  158. e.OutErr(c, 400, e.NewErr(400, "开始制作失败,请重试"))
  159. return
  160. }
  161. //判断大订单 要不要改状态
  162. count, _ := sess.Where("oid=?", orderGoods.Oid).Count(&model.OrderGoods{})
  163. countEnd, _ := sess.Where("oid=? and state=?", orderGoods.Oid, state).Count(&model.OrderGoods{})
  164. if countEnd == count {
  165. _, err = sess.Where("oid=?", orderGoods.Oid).Cols("state").Update(&model.Order{State: state})
  166. if err != nil {
  167. sess.Rollback()
  168. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  169. return
  170. }
  171. }
  172. sess.Commit()
  173. e.OutSuc(c, "success", nil)
  174. return
  175. }
  176. func NewOrderMakeEnd(c *gin.Context) {
  177. var args map[string]string
  178. if err := c.ShouldBindJSON(&args); err != nil {
  179. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  180. return
  181. }
  182. args["state"] = "2"
  183. args["str"] = "烘焙"
  184. commNewMake(c, args)
  185. }
  186. func NewOrderBakingEnd(c *gin.Context) {
  187. var args map[string]string
  188. if err := c.ShouldBindJSON(&args); err != nil {
  189. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  190. return
  191. }
  192. args["state"] = "3"
  193. args["str"] = "烘焙"
  194. commNewMake(c, args)
  195. }
  196. func NewOrderSortingEnd(c *gin.Context) {
  197. var args md.NewOrderSorting
  198. if err := c.ShouldBindJSON(&args); err != nil {
  199. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  200. return
  201. }
  202. eg := db.Db
  203. sess := eg.NewSession()
  204. defer sess.Close()
  205. sess.Begin()
  206. //修改制作记录
  207. for _, v := range args.OrderInfo {
  208. record := db.GetOrderGoods(sess, v["oid"], args.Gid, args.SkuId)
  209. record.SuccessNum = utils.StrToInt(v["num"])
  210. if utils.StrToInt(v["num"]) == record.Num {
  211. record.State = 4
  212. }
  213. record.AdminId = utils.StrToInt(args.AdminId)
  214. record.Time = time.Now()
  215. _, err := sess.Where("id=?", record.Id).Cols("success_num,state,time").Update(record)
  216. if err != nil {
  217. sess.Rollback()
  218. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  219. return
  220. }
  221. makeRecord := db.GetOrderGoodsMakeStock(sess, utils.StrToInt64(args.Gid), utils.StrToInt64(args.SkuId))
  222. makeRecord.WaitSortingNum -= utils.StrToInt(v["num"])
  223. makeRecord.SuccessNum += utils.StrToInt(v["num"])
  224. _, err = sess.Where("id=?", makeRecord.Id).Cols("success_num,wait_sorting_num").Update(makeRecord)
  225. if err != nil {
  226. sess.Rollback()
  227. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  228. return
  229. }
  230. //写入流程记录
  231. var recordFlow = &model.OrderGoodsDoingFlow{
  232. Num: record.Num,
  233. State: 4,
  234. Memo: "",
  235. SuccessNum: utils.StrToInt(v["num"]),
  236. Time: time.Now(),
  237. Gid: record.GoodsId,
  238. SkuId: record.SkuId,
  239. SkuCode: record.SkuCode,
  240. AdminId: utils.StrToInt(args.AdminId),
  241. }
  242. one, err := sess.InsertOne(recordFlow)
  243. if one == 0 || err != nil {
  244. sess.Rollback()
  245. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  246. return
  247. }
  248. }
  249. sess.Commit()
  250. e.OutSuc(c, "success", nil)
  251. return
  252. }
  253. func commNewMake(c *gin.Context, args map[string]string) {
  254. eg := db.Db
  255. sess := eg.NewSession()
  256. defer sess.Close()
  257. sess.Begin()
  258. state := utils.StrToInt(args["state"])
  259. orderGoods := db.GetOrderGoodsMakeStock(sess, utils.StrToInt64(args["gid"]), utils.StrToInt64(args["sku_id"]))
  260. if orderGoods == nil {
  261. sess.Rollback()
  262. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  263. return
  264. }
  265. WaitMakeNum := orderGoods.WaitMakeNum
  266. if state == 2 { //制作完成
  267. orderGoods.WaitMakeNum -= utils.StrToInt(args["num"])
  268. orderGoods.WaitBakingNum += utils.StrToInt(args["num"])
  269. if orderGoods.WaitMakeNum < 0 {
  270. orderGoods.WaitMakeNum = 0
  271. }
  272. }
  273. if state == 3 { //烘焙完成
  274. WaitMakeNum = orderGoods.WaitBakingNum
  275. orderGoods.WaitBakingNum -= utils.StrToInt(args["num"])
  276. orderGoods.WaitSortingNum += utils.StrToInt(args["num"])
  277. if orderGoods.WaitBakingNum < 0 {
  278. orderGoods.WaitBakingNum = 0
  279. }
  280. }
  281. _, err := sess.Where("id=?", orderGoods.Id).Cols("wait_make_num,wait_baking_num,wait_sorting_num").Update(orderGoods)
  282. if err != nil {
  283. sess.Rollback()
  284. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  285. return
  286. }
  287. //写入流程记录
  288. var recordFlow = &model.OrderGoodsDoingFlow{
  289. Num: WaitMakeNum,
  290. State: state,
  291. Memo: args["memo"],
  292. SuccessNum: utils.StrToInt(args["num"]),
  293. Time: time.Now(),
  294. Gid: orderGoods.Gid,
  295. SkuId: orderGoods.SkuId,
  296. SkuCode: orderGoods.SkuCode,
  297. AdminId: utils.StrToInt(args["admin_id"]),
  298. }
  299. one, err := sess.InsertOne(recordFlow)
  300. if one == 0 || err != nil {
  301. sess.Rollback()
  302. e.OutErr(c, 400, e.NewErr(400, "操作失败,请重试"))
  303. return
  304. }
  305. sess.Commit()
  306. e.OutSuc(c, "success", nil)
  307. return
  308. }