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

svc_order_make.go 9.3 KiB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
7 months ago
7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 record.SuccessNum >= 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. if utils.StrToInt(v["num"]) > makeRecord.WaitSortingNum {
  223. sess.Rollback()
  224. e.OutErr(c, 400, e.NewErr(400, record.GoodsTitle+",数量只能提交"+utils.IntToStr(makeRecord.WaitSortingNum)+"个"))
  225. return
  226. }
  227. makeRecord.WaitSortingNum -= utils.StrToInt(v["num"])
  228. makeRecord.SuccessNum += utils.StrToInt(v["num"])
  229. _, err = sess.Where("id=?", makeRecord.Id).Cols("success_num,wait_sorting_num").Update(makeRecord)
  230. if err != nil {
  231. sess.Rollback()
  232. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  233. return
  234. }
  235. //写入流程记录
  236. var recordFlow = &model.OrderGoodsDoingFlow{
  237. Num: record.Num,
  238. State: 4,
  239. Memo: "",
  240. SuccessNum: utils.StrToInt(v["num"]),
  241. Time: time.Now(),
  242. Gid: record.GoodsId,
  243. SkuId: record.SkuId,
  244. SkuCode: record.SkuCode,
  245. AdminId: utils.StrToInt(args.AdminId),
  246. }
  247. one, err := sess.InsertOne(recordFlow)
  248. if one == 0 || err != nil {
  249. sess.Rollback()
  250. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  251. return
  252. }
  253. }
  254. sess.Commit()
  255. e.OutSuc(c, "success", nil)
  256. return
  257. }
  258. func commNewMake(c *gin.Context, args map[string]string) {
  259. eg := db.Db
  260. sess := eg.NewSession()
  261. defer sess.Close()
  262. sess.Begin()
  263. state := utils.StrToInt(args["state"])
  264. orderGoods := db.GetOrderGoodsMakeStock(sess, utils.StrToInt64(args["gid"]), utils.StrToInt64(args["sku_id"]))
  265. if orderGoods == nil {
  266. sess.Rollback()
  267. e.OutErr(c, 400, e.NewErr(400, "订单查找失败,请重试"))
  268. return
  269. }
  270. WaitMakeNum := orderGoods.WaitMakeNum
  271. if state == 2 { //制作完成
  272. if utils.StrToInt(args["num"]) > orderGoods.WaitMakeNum {
  273. sess.Rollback()
  274. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitMakeNum)+"个"))
  275. return
  276. }
  277. orderGoods.WaitMakeNum -= utils.StrToInt(args["num"])
  278. orderGoods.WaitBakingNum += utils.StrToInt(args["num"])
  279. if orderGoods.WaitMakeNum < 0 {
  280. orderGoods.WaitMakeNum = 0
  281. }
  282. }
  283. if state == 3 { //烘焙完成
  284. if utils.StrToInt(args["num"]) > orderGoods.WaitBakingNum {
  285. sess.Rollback()
  286. e.OutErr(c, 400, e.NewErr(400, "数量只能提交"+utils.IntToStr(orderGoods.WaitBakingNum)+"个"))
  287. return
  288. }
  289. WaitMakeNum = orderGoods.WaitBakingNum
  290. orderGoods.WaitBakingNum -= utils.StrToInt(args["num"])
  291. orderGoods.WaitSortingNum += utils.StrToInt(args["num"])
  292. if orderGoods.WaitBakingNum < 0 {
  293. orderGoods.WaitBakingNum = 0
  294. }
  295. }
  296. _, err := sess.Where("id=?", orderGoods.Id).Cols("wait_make_num,wait_baking_num,wait_sorting_num").Update(orderGoods)
  297. if err != nil {
  298. sess.Rollback()
  299. e.OutErr(c, 400, e.NewErr(400, "提交失败,请重试"))
  300. return
  301. }
  302. //写入流程记录
  303. var recordFlow = &model.OrderGoodsDoingFlow{
  304. Num: WaitMakeNum,
  305. State: state,
  306. Memo: args["memo"],
  307. SuccessNum: utils.StrToInt(args["num"]),
  308. Time: time.Now(),
  309. Gid: orderGoods.Gid,
  310. SkuId: orderGoods.SkuId,
  311. SkuCode: orderGoods.SkuCode,
  312. AdminId: utils.StrToInt(args["admin_id"]),
  313. }
  314. one, err := sess.InsertOne(recordFlow)
  315. if one == 0 || err != nil {
  316. sess.Rollback()
  317. e.OutErr(c, 400, e.NewErr(400, "操作失败,请重试"))
  318. return
  319. }
  320. sess.Commit()
  321. e.OutSuc(c, "success", nil)
  322. return
  323. }