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

161 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. State: 1,
  51. }
  52. one, err := sess.InsertOne(&order)
  53. if one == 0 || err != nil {
  54. sess.Rollback()
  55. e.OutErr(c, 400, e.NewErr(400, "下单失败"))
  56. return
  57. }
  58. var orderGoods = make([]model.OrderGoods, 0)
  59. goodsTotal := make(map[int64]int)
  60. skuIds := make([]int64, 0)
  61. gids := make([]int64, 0)
  62. for _, v := range req.GoodsInfo {
  63. skuIds = append(skuIds, utils.StrToInt64(v.SkuId))
  64. gids = append(gids, utils.StrToInt64(v.GoodsId))
  65. }
  66. skuMap := db.GetSkuMore(db.Db, skuIds)
  67. goodsMap := db.GetGoodsMore(db.Db, gids)
  68. for _, v := range req.GoodsInfo {
  69. tmp := model.OrderGoods{
  70. Oid: order.Oid,
  71. GoodsId: utils.StrToInt64(v.GoodsId),
  72. GoodsTitle: goodsMap[utils.StrToInt64(v.GoodsId)].Title,
  73. SkuId: utils.StrToInt64(v.SkuId),
  74. SkuCode: skuMap[utils.StrToInt64(v.SkuId)].SkuCode,
  75. Sku: skuMap[utils.StrToInt64(v.SkuId)].Sku,
  76. Num: utils.StrToInt(v.Num),
  77. Price: skuPrice[v.SkuId],
  78. EnterpriseId: utils.StrToInt(req.EnterpriseId),
  79. State: 1,
  80. }
  81. goodsTotal[tmp.GoodsId] += tmp.Num
  82. orderGoods = append(orderGoods, tmp)
  83. err = Total(sess, tmp)
  84. if err != nil {
  85. sess.Rollback()
  86. e.OutErr(c, 400, e.NewErr(400, "下单失败"))
  87. return
  88. }
  89. }
  90. for k, v := range goodsTotal {
  91. sql := `UPDATE goods SET sale=sale-%d WHERE goods_id=%d`
  92. sql = fmt.Sprintf(sql, v, k)
  93. db.QueryNativeStringSess(sess, sql)
  94. }
  95. one, err = sess.Insert(&orderGoods)
  96. if one == 0 || err != nil {
  97. sess.Rollback()
  98. e.OutErr(c, 400, e.NewErr(400, "下单失败"))
  99. return
  100. }
  101. sess.Commit()
  102. svc.SysCfgSet(c, "has_new_order", "1", "")
  103. e.OutSuc(c, "success", nil)
  104. return
  105. }
  106. func commCalc(c *gin.Context, req md.GoodsPayParam) (float64, map[string]string) {
  107. skuPrice := make(map[string]string, 0)
  108. skuIds := make([]int64, 0)
  109. gids := make([]string, 0)
  110. for _, v := range req.GoodsInfo {
  111. skuIds = append(skuIds, utils.StrToInt64(v.SkuId))
  112. gids = append(gids, v.GoodsId)
  113. }
  114. if len(skuIds) == 0 {
  115. return 0, skuPrice
  116. }
  117. engine := db.Db
  118. skuSchemeMap := goods.GetSkuScheme(engine, req.EnterpriseId, gids)
  119. skuMap := db.GetSkuMore(engine, skuIds)
  120. //计算价格 TODO 对应方案的价格怎么算
  121. var amount float64 = 0
  122. for _, v := range req.GoodsInfo {
  123. sku, ok := skuMap[utils.StrToInt64(v.SkuId)]
  124. if ok == false {
  125. return 0, skuPrice
  126. }
  127. //方案的价格
  128. skuScheme, ok := skuSchemeMap[sku.SkuId]
  129. if ok {
  130. sku.Price = skuScheme.Price
  131. }
  132. skuPrice[v.SkuId] = sku.Price
  133. amount += utils.StrToFloat64(sku.Price) * utils.StrToFloat64(v.Num)
  134. }
  135. return amount, skuPrice
  136. }
  137. // 统计数量
  138. func Total(sess *xorm.Session, tmp model.OrderGoods) error {
  139. stock := db.GetOrderGoodsMakeStock(sess, tmp.GoodsId, tmp.SkuId)
  140. if stock == nil {
  141. return errors.New("下单失败")
  142. }
  143. stock.Sku = tmp.Sku
  144. stock.SkuCode = tmp.SkuCode
  145. stock.GoodsTitle = tmp.GoodsTitle
  146. stock.WaitMakeNum += tmp.Num
  147. update, err := sess.Where("id=?", stock.Id).Cols("sku,sku_code,goods_title,wait_make_num").Update(stock)
  148. if update == 0 || err != nil {
  149. return errors.New("下单失败")
  150. }
  151. return nil
  152. }