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

159 lines
4.2 KiB

  1. package order
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/admin/svc/goods"
  5. "applet/app/db"
  6. "applet/app/db/model"
  7. "applet/app/e"
  8. "applet/app/svc"
  9. "applet/app/utils"
  10. "errors"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "time"
  14. "xorm.io/xorm"
  15. )
  16. func GoodsPayCalcAmount(c *gin.Context) {
  17. var req md.GoodsPayParam
  18. if err := c.ShouldBindJSON(&req); err != nil {
  19. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  20. return
  21. }
  22. amount, _ := commCalc(c, req)
  23. res := map[string]string{
  24. "amount": utils.Float64ToStr(amount),
  25. }
  26. e.OutSuc(c, res, nil)
  27. return
  28. }
  29. func GoodsPayCreate(c *gin.Context) {
  30. var req md.GoodsPayParam
  31. if err := c.ShouldBindJSON(&req); err != nil {
  32. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  33. return
  34. }
  35. amount, skuPrice := commCalc(c, req)
  36. if amount <= 0 {
  37. e.OutErr(c, 400, e.NewErr(400, "价格计算错误,下单失败"))
  38. return
  39. }
  40. sess := db.Db.NewSession()
  41. defer sess.Close()
  42. sess.Begin()
  43. var order = model.Order{
  44. Oid: utils.StrToInt64(utils.OrderUUID(utils.StrToInt(req.EnterpriseId))),
  45. CreateAt: time.Now(),
  46. EnterpriseId: utils.StrToInt(req.EnterpriseId),
  47. BuyInfo: utils.SerializeStr(req.BuyInfo),
  48. BuyPhone: req.BuyInfo.Phone,
  49. Amount: utils.Float64ToStr(amount),
  50. }
  51. one, err := sess.InsertOne(&order)
  52. if one == 0 || err != nil {
  53. sess.Rollback()
  54. e.OutErr(c, 400, e.NewErr(400, "下单失败"))
  55. return
  56. }
  57. var orderGoods = make([]model.OrderGoods, 0)
  58. goodsTotal := make(map[int64]int)
  59. skuIds := make([]int64, 0)
  60. gids := make([]int64, 0)
  61. for _, v := range req.GoodsInfo {
  62. skuIds = append(skuIds, utils.StrToInt64(v.SkuId))
  63. gids = append(gids, utils.StrToInt64(v.GoodsId))
  64. }
  65. skuMap := db.GetSkuMore(db.Db, skuIds)
  66. goodsMap := db.GetGoodsMore(db.Db, gids)
  67. for _, v := range req.GoodsInfo {
  68. tmp := model.OrderGoods{
  69. Oid: order.Oid,
  70. GoodsId: utils.StrToInt64(v.GoodsId),
  71. GoodsTitle: goodsMap[utils.StrToInt64(v.GoodsId)].Title,
  72. SkuId: utils.StrToInt64(v.SkuId),
  73. SkuCode: skuMap[utils.StrToInt64(v.SkuId)].SkuCode,
  74. Sku: skuMap[utils.StrToInt64(v.SkuId)].Sku,
  75. Num: utils.StrToInt(v.Num),
  76. Price: skuPrice[v.SkuId],
  77. EnterpriseId: utils.StrToInt(req.EnterpriseId),
  78. }
  79. goodsTotal[tmp.GoodsId] += tmp.Num
  80. orderGoods = append(orderGoods, tmp)
  81. err = Total(sess, tmp)
  82. if err != nil {
  83. sess.Rollback()
  84. e.OutErr(c, 400, e.NewErr(400, "下单失败"))
  85. return
  86. }
  87. }
  88. for k, v := range goodsTotal {
  89. sql := `UPDATE goods SET sale=sale-%d WHERE goods_id=%d`
  90. sql = fmt.Sprintf(sql, v, k)
  91. db.QueryNativeStringSess(sess, sql)
  92. }
  93. one, err = sess.Insert(&orderGoods)
  94. if one == 0 || err != nil {
  95. sess.Rollback()
  96. e.OutErr(c, 400, e.NewErr(400, "下单失败"))
  97. return
  98. }
  99. sess.Commit()
  100. svc.SysCfgSet(c, "has_new_order", "1", "")
  101. e.OutSuc(c, "success", nil)
  102. return
  103. }
  104. func commCalc(c *gin.Context, req md.GoodsPayParam) (float64, map[string]string) {
  105. skuPrice := make(map[string]string, 0)
  106. skuIds := make([]int64, 0)
  107. gids := make([]string, 0)
  108. for _, v := range req.GoodsInfo {
  109. skuIds = append(skuIds, utils.StrToInt64(v.SkuId))
  110. gids = append(gids, v.GoodsId)
  111. }
  112. if len(skuIds) == 0 {
  113. return 0, skuPrice
  114. }
  115. engine := db.Db
  116. skuSchemeMap := goods.GetSkuScheme(engine, req.EnterpriseId, gids)
  117. skuMap := db.GetSkuMore(engine, skuIds)
  118. //计算价格 TODO 对应方案的价格怎么算
  119. var amount float64 = 0
  120. for _, v := range req.GoodsInfo {
  121. sku, ok := skuMap[utils.StrToInt64(v.SkuId)]
  122. if ok == false {
  123. return 0, skuPrice
  124. }
  125. //方案的价格
  126. skuScheme, ok := skuSchemeMap[sku.SkuId]
  127. if ok {
  128. sku.Price = skuScheme.Price
  129. }
  130. skuPrice[v.SkuId] = sku.Price
  131. amount += utils.StrToFloat64(sku.Price) * utils.StrToFloat64(v.Num)
  132. }
  133. return amount, skuPrice
  134. }
  135. // 统计数量
  136. func Total(sess *xorm.Session, tmp model.OrderGoods) error {
  137. stock := db.GetOrderGoodsMakeStock(sess, tmp.GoodsId, tmp.SkuId)
  138. if stock == nil {
  139. return errors.New("下单失败")
  140. }
  141. stock.Sku = tmp.Sku
  142. stock.SkuCode = tmp.SkuCode
  143. stock.GoodsTitle = tmp.GoodsTitle
  144. stock.WaitMakeNum += tmp.Num
  145. update, err := sess.Where("id=?", stock.Id).Cols("sku,sku_code,goods_title,wait_make_num").Update(stock)
  146. if update == 0 || err != nil {
  147. return errors.New("下单失败")
  148. }
  149. return nil
  150. }