互动
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.

165 lines
5.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package pay_service
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/ouxuanserver/osmanthuswine/src/core"
  6. "hudongzhuanjia/logger"
  7. "hudongzhuanjia/models"
  8. im_service "hudongzhuanjia/services/im"
  9. "time"
  10. )
  11. //处理支付成功之后的回调问题
  12. // 直播红包
  13. func HandleSuccess(param *OrderDelayQueueParam) (err error) {
  14. var exist bool
  15. if param.Order.GoodType == 4 { // 直播商品
  16. customerOrder := new(models.CustomerOrder)
  17. if customerOrder.Status == 1 {
  18. return errors.New("商品已经支付")
  19. }
  20. _, err := customerOrder.UpdateStatusBy(param.Order.OutTradeNo, 0, 1)
  21. if err != nil {
  22. return errors.New("更新状态发送错误")
  23. }
  24. activity := new(models.Activity)
  25. exist, err = models.GetById(activity, param.Order.ActivityId)
  26. if err != nil || !exist {
  27. return errors.New("互动信息异常")
  28. }
  29. gift := new(models.OrderGift)
  30. exist, err = gift.GetByActivityId(param.Order.ActivityId)
  31. if err != nil {
  32. return err
  33. }
  34. prize := new(models.UserPrize)
  35. prize.UserId = param.Order.UserId
  36. prize.ActivityId = param.Order.ActivityId
  37. prize.RehearsalId = activity.RehearsalId
  38. prize.ActivityName = activity.Name
  39. prize.PrizeName = gift.GiftName
  40. prize.PrizeImg = gift.GiftPicUrl
  41. prize.PrizeType = 4
  42. prize.IsDelete = false
  43. prize.CreatedAt = time.Now()
  44. prize.UpdatedAt = time.Now()
  45. if exist {
  46. if gift.Num < 0 {
  47. } else if gift.Num > 0 {
  48. count, err := new(models.CustomerOrder).
  49. Count(activity.Id, activity.RehearsalId, customerOrder.CreatedAt)
  50. if err != nil {
  51. err = fmt.Errorf("订单人数统计失败: err->%v, out_trade_no->%v", err, param.Order.OutTradeNo)
  52. return err
  53. }
  54. if gift.Num <= int(count) { // 人数足够
  55. return nil
  56. }
  57. }
  58. // gift.Num = 0 and
  59. _, err = prize.Add()
  60. if err != nil {
  61. err = fmt.Errorf("奖品添加失败: err->%v, out_trade_no->%v", err, param.Order.OutTradeNo)
  62. return err
  63. }
  64. }
  65. } else if param.Order.GoodType == 3 { // 直播红包
  66. info := new(models.LiveRedEnvelopeRule)
  67. _, err = info.UpdateStatusByOutTradeNo(param.Order.OutTradeNo, 1)
  68. if err != nil {
  69. err = fmt.Errorf("状态改变出现错误: err->%v, out_trade_no->%v", err, param.Order.OutTradeNo)
  70. logger.Error(err.Error())
  71. return
  72. }
  73. exist, err = info.GetByOutTradeNo(param.Order.OutTradeNo)
  74. if err != nil || !exist || info.Status != 1 {
  75. err = fmt.Errorf("直播红包信息异常: err-> %v, exist->%v, status->%v, out_trade_no->%v",
  76. err, exist, info.Status, param.Order.OutTradeNo)
  77. logger.Error(err.Error())
  78. return
  79. }
  80. user := new(models.User)
  81. exist, err = models.GetById(user, info.UserId)
  82. if err != nil || !exist {
  83. err = fmt.Errorf("直播红包用户信息异常: err-> %v, exist->%v, status->%v, out_trade_no->%v",
  84. err, exist, info.Status, param.Order.OutTradeNo)
  85. logger.Error(err.Error())
  86. return
  87. }
  88. err = im_service.SendGroupCustomMessage(info.UserId, param.Order.ActivityId, im_service.NoticeLiveRedPackStart,
  89. map[string]interface{}{
  90. "live_red_envelope_rule_id": info.Id,
  91. "prompt": info.Prompt,
  92. "timestamp": time.Now().Unix(),
  93. })
  94. if err != nil {
  95. err = fmt.Errorf("发送腾讯im信息出现错误: err->%v, info_id->%v, out_trade_no->%v",
  96. err, info.Id, param.Order.OutTradeNo)
  97. logger.Error(err.Error())
  98. }
  99. } else if param.Order.GoodType == 2 { // 打赏
  100. history := new(models.RewardHistory)
  101. exist, err = history.GetByOutTradeNo(param.Order.OutTradeNo)
  102. if err != nil || !exist || history.Status != -1 {
  103. err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
  104. logger.Error(err.Error())
  105. return
  106. }
  107. _, err = history.UpdateStatus(history.Id, 0)
  108. if err != nil {
  109. err = fmt.Errorf("打赏状态发送变化: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
  110. logger.Error(err.Error())
  111. }
  112. } else if param.Order.GoodType == 1 { // 霸屏
  113. history := new(models.BullyScreenHistory)
  114. exist, err = history.GetByOutTradeNo(param.Order.OutTradeNo)
  115. if err != nil || !exist || history.Status != -1 {
  116. err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
  117. logger.Error(err.Error())
  118. return
  119. }
  120. _, err = history.UpdateStatus(history.Id, 0)
  121. if err != nil {
  122. err = fmt.Errorf("霸屏状态发送变化: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
  123. logger.Error(err.Error())
  124. }
  125. }
  126. return
  127. }
  128. func HandleFailed(param *OrderDelayQueueParam) (err error) {
  129. var exist bool
  130. // 退还库存
  131. if param.Order.GoodType == 4 {
  132. order := new(models.CustomerOrder)
  133. exist, err = order.GetByOutTradeNO(param.Order.OutTradeNo)
  134. if err != nil || !exist {
  135. err = fmt.Errorf("不存在客户订单: err-> %v, out_trade_no->%v", err, param.Order.OutTradeNo)
  136. logger.Error(err.Error())
  137. return
  138. }
  139. subs := make([]*models.CustomerOrderSub, 0)
  140. err = core.GetXormAuto().Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
  141. if err != nil {
  142. err = fmt.Errorf("不存在客户子订单: err-> %v, out_trade_no->%v", err, param.Order.OutTradeNo)
  143. }
  144. for _, sub := range subs {
  145. _, err = core.GetXormAuto().Where("id=?", sub.GoodsId).
  146. Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{})
  147. if err != nil {
  148. err = fmt.Errorf("库存返回失败: err->%v, out_trade_no->%v, sub_id->%v", err, param.Order.OutTradeNo, sub.Id)
  149. }
  150. }
  151. }
  152. return
  153. }