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

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