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

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