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

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