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

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