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

119 lines
3.1 KiB

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