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

131 lines
3.5 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/utils"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "time"
  12. )
  13. func GoodsPayCalcAmount(c *gin.Context) {
  14. var req md.GoodsPayParam
  15. if err := c.ShouldBindJSON(&req); err != nil {
  16. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  17. return
  18. }
  19. amount, _ := commCalc(c, req)
  20. res := map[string]string{
  21. "amount": utils.Float64ToStr(amount),
  22. }
  23. e.OutSuc(c, res, nil)
  24. return
  25. }
  26. func GoodsPayCreate(c *gin.Context) {
  27. var req md.GoodsPayParam
  28. if err := c.ShouldBindJSON(&req); err != nil {
  29. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  30. return
  31. }
  32. amount, skuPrice := commCalc(c, req)
  33. if amount <= 0 {
  34. e.OutErr(c, 400, e.NewErr(400, "价格计算错误,下单失败"))
  35. return
  36. }
  37. sess := db.Db.NewSession()
  38. defer sess.Close()
  39. sess.Begin()
  40. sess.Commit()
  41. var order = model.Order{
  42. Oid: utils.StrToInt64(utils.OrderUUID(utils.StrToInt(req.EnterpriseId))),
  43. CreateAt: time.Now(),
  44. EnterpriseId: utils.StrToInt(req.EnterpriseId),
  45. BuyInfo: utils.SerializeStr(req.BuyInfo),
  46. BuyPhone: req.BuyInfo.Phone,
  47. Amount: utils.Float64ToStr(amount),
  48. }
  49. one, err := sess.InsertOne(&order)
  50. if one == 0 || err != nil {
  51. sess.Rollback()
  52. e.OutErr(c, 400, e.NewErr(400, "下单失败"))
  53. return
  54. }
  55. var orderGoods = make([]model.OrderGoods, 0)
  56. goodsTotal := make(map[int64]int)
  57. skuIds := make([]int64, 0)
  58. gids := make([]int64, 0)
  59. for _, v := range req.GoodsInfo {
  60. skuIds = append(skuIds, utils.StrToInt64(v.SkuId))
  61. gids = append(gids, utils.StrToInt64(v.GoodsId))
  62. }
  63. skuMap := db.GetSkuMore(db.Db, skuIds)
  64. goodsMap := db.GetGoodsMore(db.Db, gids)
  65. for _, v := range req.GoodsInfo {
  66. tmp := model.OrderGoods{
  67. Oid: order.Oid,
  68. GoodsId: utils.StrToInt64(v.GoodsId),
  69. GoodsTitle: goodsMap[utils.StrToInt64(v.GoodsId)].Title,
  70. SkuId: utils.StrToInt64(v.SkuId),
  71. SkuCode: skuMap[utils.StrToInt64(v.SkuId)].SkuCode,
  72. Sku: skuMap[utils.StrToInt64(v.SkuId)].Sku,
  73. Num: utils.StrToInt(v.Num),
  74. Price: skuPrice[v.SkuId],
  75. EnterpriseId: utils.StrToInt(req.EnterpriseId),
  76. }
  77. goodsTotal[tmp.GoodsId] += tmp.Num
  78. orderGoods = append(orderGoods, tmp)
  79. }
  80. for k, v := range goodsTotal {
  81. sql := `UPDATE goods SET sale=sale-%d WHERE goods_id=%d`
  82. sql = fmt.Sprintf(sql, v, k)
  83. db.QueryNativeStringSess(sess, sql)
  84. }
  85. one, err = sess.Insert(&orderGoods)
  86. if one == 0 || err != nil {
  87. sess.Rollback()
  88. e.OutErr(c, 400, e.NewErr(400, "下单失败"))
  89. return
  90. }
  91. e.OutSuc(c, "success", nil)
  92. return
  93. }
  94. func commCalc(c *gin.Context, req md.GoodsPayParam) (float64, map[string]string) {
  95. skuPrice := make(map[string]string, 0)
  96. skuIds := make([]int64, 0)
  97. gids := make([]string, 0)
  98. for _, v := range req.GoodsInfo {
  99. skuIds = append(skuIds, utils.StrToInt64(v.SkuId))
  100. gids = append(gids, v.GoodsId)
  101. }
  102. if len(skuIds) == 0 {
  103. return 0, skuPrice
  104. }
  105. engine := db.Db
  106. skuSchemeMap := goods.GetSkuScheme(engine, req.EnterpriseId, gids)
  107. skuMap := db.GetSkuMore(engine, skuIds)
  108. //计算价格 TODO 对应方案的价格怎么算
  109. var amount float64 = 0
  110. for _, v := range req.GoodsInfo {
  111. sku, ok := skuMap[utils.StrToInt64(v.SkuId)]
  112. if ok == false {
  113. return 0, skuPrice
  114. }
  115. //方案的价格
  116. skuScheme, ok := skuSchemeMap[sku.SkuId]
  117. if ok {
  118. sku.Price = skuScheme.Price
  119. }
  120. skuPrice[v.SkuId] = sku.Price
  121. amount += utils.StrToFloat64(sku.Price) * utils.StrToFloat64(v.Num)
  122. }
  123. return amount, skuPrice
  124. }