order.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package service
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "go-common/app/admin/main/vip/model"
  7. vipmol "go-common/app/service/main/vip/model"
  8. "go-common/library/ecode"
  9. "github.com/google/uuid"
  10. "github.com/pkg/errors"
  11. )
  12. //OrderList order list.
  13. func (s *Service) OrderList(c context.Context, arg *model.ArgPayOrder) (res []*model.PayOrder, count int64, err error) {
  14. if count, err = s.dao.OrderCount(c, arg); err != nil {
  15. err = errors.WithStack(err)
  16. return
  17. }
  18. if res, err = s.dao.OrderList(c, arg); err != nil {
  19. err = errors.WithStack(err)
  20. }
  21. return
  22. }
  23. //Refund refund order.
  24. func (s *Service) Refund(c context.Context, orderNo, username string, refundAmount float64) (err error) {
  25. var (
  26. order *model.PayOrder
  27. )
  28. if order, err = s.dao.SelOrder(c, orderNo); err != nil {
  29. err = errors.WithStack(err)
  30. return
  31. }
  32. if order == nil {
  33. err = ecode.VipOrderNoErr
  34. return
  35. }
  36. if order.Status != model.SUCCESS {
  37. err = ecode.VipOrderStatusErr
  38. return
  39. }
  40. if order.ToMid > 0 {
  41. err = ecode.VipOrderToMidErr
  42. return
  43. }
  44. if order.AppID == vipmol.EleAppID {
  45. return ecode.VipEleOrderCanNotReFundErr
  46. }
  47. if order.Money < refundAmount {
  48. err = ecode.VipOrderMoneyErr
  49. return
  50. }
  51. if order.Money < refundAmount+order.RefundAmount {
  52. err = ecode.VipOrderMoneyErr
  53. return
  54. }
  55. var timefmt = time.Now().Format("20060102150405")
  56. if len(order.ThirdTradeNo) <= 0 || order.ThirdTradeNo[0:len(timefmt)] == timefmt {
  57. err = ecode.VipOldOrderErr
  58. return
  59. }
  60. refundID := strings.Replace(uuid.New().String(), "-", "", -1)[4:20]
  61. if err = s.dao.PayRefund(c, order, refundAmount, refundID); err != nil {
  62. err = errors.WithStack(err)
  63. return
  64. }
  65. olog := new(model.PayOrderLog)
  66. olog.OrderNo = order.OrderNo
  67. olog.Mid = order.Mid
  68. olog.RefundID = refundID
  69. olog.Status = model.REFUNDING
  70. olog.Operator = username
  71. olog.RefundAmount = refundAmount
  72. if err = s.dao.AddPayOrderLog(c, olog); err != nil {
  73. err = errors.WithStack(err)
  74. }
  75. return
  76. }