附近小店
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.

svc_store.go 1.9 KiB

3 months ago
3 months ago
2 months ago
3 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/svc"
  6. "applet/app/utils"
  7. "applet/app/utils/cache"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. )
  12. func StoreOrderConfirm(c *gin.Context) {
  13. var arg map[string]string
  14. if err := c.ShouldBindJSON(&arg); err != nil {
  15. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  16. return
  17. }
  18. // 加锁 防止并发提取
  19. mutexKey := fmt.Sprintf("%s:team.StoreOrderConfirm:%s", c.GetString("mid"), arg["oid"])
  20. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  21. if err != nil {
  22. e.OutErr(c, e.ERR, err)
  23. return
  24. }
  25. if withdrawAvailable != "OK" {
  26. e.OutErr(c, e.ERR, e.NewErr(400000, "请求过于频繁,请稍后再试"))
  27. return
  28. }
  29. sess := svc.MasterDb(c).NewSession()
  30. defer sess.Close()
  31. sess.Begin()
  32. order := db.GetOrder(sess, arg["oid"])
  33. if order == nil {
  34. sess.Rollback()
  35. e.OutErr(c, 400, e.NewErr(400, "订单不存在"))
  36. return
  37. }
  38. if order.State != 1 {
  39. sess.Rollback()
  40. e.OutErr(c, 400, e.NewErr(400, "订单不能确认"))
  41. return
  42. }
  43. order.State = 2
  44. order.UpdateAt = time.Now()
  45. order.ConfirmAt = time.Now()
  46. update, err := sess.Where("id=?", order.Id).Cols("state,confirm_at,update_at").Update(order)
  47. if update == 0 || err != nil {
  48. sess.Rollback()
  49. e.OutErr(c, 400, e.NewErr(400, "订单确认失败"))
  50. return
  51. }
  52. //给商家余额
  53. money := utils.StrToFloat64(order.Commission)
  54. if order.ParentUid > 0 {
  55. money = utils.StrToFloat64(order.Amount) - utils.StrToFloat64(order.AgentCommission)
  56. }
  57. if order.StoreType == 1 {
  58. money = utils.StrToFloat64(order.Amount) - utils.StrToFloat64(order.PlatformCommission)
  59. }
  60. bools := svc.MoneyCheck(c, sess, order.StoreUid, order.ParentUid, order.StoreType, 0, 1, money, "订单核销", order.Oid)
  61. if bools == false {
  62. sess.Rollback()
  63. e.OutErr(c, 400, e.NewErr(400, "订单确认失败"))
  64. return
  65. }
  66. sess.Commit()
  67. e.OutSuc(c, "success", nil)
  68. return
  69. }