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

190 lines
6.2 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
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) error {
  14. if param.Order.GoodType == 4 { // 直播商品
  15. customerOrder := new(models.CustomerOrder)
  16. _, err := customerOrder.UpdateStatusBy(param.Order.OutTradeNo, 0, 1)
  17. if err != nil {
  18. return errors.New("更新状态发送错误")
  19. }
  20. customerOrder.GetByOutTradeNO(param.Order.OutTradeNo)
  21. if customerOrder.Status != 1 {
  22. return errors.New("商品订单状态信息出现异常")
  23. }
  24. subs, err := models.GetCustomerOrderSubsByOrderNos(customerOrder.OrderNo)
  25. if err != nil {
  26. logger.Error("商品子订单查询异常", err)
  27. return err
  28. }
  29. for _, sub := range subs {
  30. err = im_service.SendGroupCustomMessage(customerOrder.BuyerId, param.Order.ActivityId, im_service.NoticeLiveGoodBuy,
  31. map[string]interface{}{
  32. "goods_pic_url": sub.GoodsPicUrl,
  33. "goods_num": sub.GoodsNum,
  34. "good_name": sub.GoodName,
  35. "good_price": sub.GoodPrice,
  36. "goods_id": sub.GoodsId,
  37. "timestamp": time.Now().Unix(),
  38. })
  39. if err != nil {
  40. logger.Error("直播商品子订单信息发送异常异常", err)
  41. return err
  42. }
  43. }
  44. activity := new(models.Activity)
  45. exist, err := models.Get(activity, param.Order.ActivityId)
  46. if err != nil || !exist {
  47. return errors.New("互动信息异常")
  48. }
  49. gift := new(models.OrderGift)
  50. exist, err = gift.GetByActivityId(param.Order.ActivityId)
  51. if err != nil {
  52. err = fmt.Errorf("获取订单送礼错误: err-> %v, activity_id-> %v", err, activity.Id)
  53. logger.Error(err)
  54. return err
  55. }
  56. if !exist {
  57. return nil
  58. }
  59. prize := new(models.UserPrize)
  60. prize.UserId = param.Order.UserId
  61. prize.ActivityId = param.Order.ActivityId
  62. prize.RehearsalId = activity.RehearsalId
  63. prize.ActivityName = activity.Name
  64. prize.PrizeName = gift.GiftName
  65. prize.PrizeImg = gift.GiftPicUrl
  66. prize.PrizeType = 4
  67. prize.IsDelete = false
  68. prize.Status = 1
  69. prize.CreatedAt = time.Now()
  70. prize.UpdatedAt = time.Now()
  71. if gift.Num > 0 {
  72. count, err := new(models.CustomerOrder).
  73. Count(activity.Id, activity.RehearsalId, customerOrder.CreatedAt)
  74. if err != nil {
  75. err = fmt.Errorf("订单人数统计失败: err->%v, out_trade_no->%v", err, param.Order.OutTradeNo)
  76. logger.Error(err)
  77. return err
  78. }
  79. if gift.Num <= int(count) { // 人数足够
  80. return nil
  81. }
  82. }
  83. _, err = prize.Add()
  84. if err != nil {
  85. err = fmt.Errorf("奖品添加失败: err->%v, out_trade_no->%v", err, param.Order.OutTradeNo)
  86. logger.Error(err)
  87. return err
  88. }
  89. customerOrder.UserPrizeId = prize.Id
  90. _, err = models.Update(customerOrder, "user_prize_id")
  91. if err != nil {
  92. err = fmt.Errorf("奖品记录添加失败: err-> %v, out_trade_no-> %v, prize_id->%v", err, param.Order.OutTradeNo, prize.Id)
  93. logger.Error(err)
  94. return err
  95. }
  96. } else if param.Order.GoodType == 3 { // 直播红包
  97. info := new(models.LiveRedEnvelopeRule)
  98. _, err := info.UpdateStatusByOutTradeNo(param.Order.OutTradeNo, 1)
  99. if err != nil {
  100. err = fmt.Errorf("状态改变出现错误: err->%v, out_trade_no->%v", err, param.Order.OutTradeNo)
  101. logger.Error(err.Error())
  102. return err
  103. }
  104. exist, err := info.GetByOutTradeNo(param.Order.OutTradeNo)
  105. if err != nil || !exist || info.Status != 1 {
  106. err = fmt.Errorf("直播红包信息异常: err-> %v, exist->%v, status->%v, out_trade_no->%v",
  107. err, exist, info.Status, param.Order.OutTradeNo)
  108. logger.Error(err.Error())
  109. return err
  110. }
  111. err = im_service.SendGroupCustomMessage(info.UserId, param.Order.ActivityId, im_service.NoticeLiveRedPackStart,
  112. map[string]interface{}{
  113. "live_red_envelope_rule_id": info.Id,
  114. "prompt": info.Prompt,
  115. "timestamp": time.Now().Unix(),
  116. })
  117. if err != nil {
  118. err = fmt.Errorf("发送腾讯im信息出现错误: err->%v, info_id->%v, out_trade_no->%v",
  119. err, info.Id, param.Order.OutTradeNo)
  120. logger.Error(err.Error())
  121. return err
  122. }
  123. } else if param.Order.GoodType == 2 { // 打赏
  124. history := new(models.RewardHistory)
  125. exist, err := history.GetByOutTradeNo(param.Order.OutTradeNo)
  126. if err != nil || !exist || history.Status != -1 {
  127. err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
  128. logger.Error(err.Error())
  129. return err
  130. }
  131. _, err = history.UpdateStatus(history.Id, 0)
  132. if err != nil {
  133. err = fmt.Errorf("打赏状态发送变化: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
  134. logger.Error(err.Error())
  135. return err
  136. }
  137. } else if param.Order.GoodType == 1 { // 霸屏
  138. history := new(models.BullyScreenHistory)
  139. exist, err := history.GetByOutTradeNo(param.Order.OutTradeNo)
  140. if err != nil || !exist || history.Status != -1 {
  141. err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
  142. logger.Error(err.Error())
  143. return err
  144. }
  145. _, err = history.UpdateStatus(history.Id, 0)
  146. if err != nil {
  147. err = fmt.Errorf("霸屏状态发送变化: err->%v, out_trade_no %v", err, param.Order.OutTradeNo)
  148. logger.Error(err.Error())
  149. }
  150. }
  151. return nil
  152. }
  153. func HandleFailed(param *OrderDelayQueueParam) (err error) {
  154. var exist bool
  155. // 退还库存
  156. if param.Order.GoodType == 4 {
  157. order := new(models.CustomerOrder)
  158. exist, err = order.GetByOutTradeNO(param.Order.OutTradeNo)
  159. if err != nil || !exist {
  160. err = fmt.Errorf("不存在客户订单: err-> %v, out_trade_no->%v", err, param.Order.OutTradeNo)
  161. logger.Error(err.Error())
  162. return
  163. }
  164. subs := make([]*models.CustomerOrderSub, 0)
  165. err = core.GetXormAuto().Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
  166. if err != nil {
  167. err = fmt.Errorf("不存在客户子订单: err-> %v, out_trade_no->%v", err, param.Order.OutTradeNo)
  168. }
  169. for _, sub := range subs {
  170. _, err = core.GetXormAuto().Where("id=?", sub.GoodsId).
  171. Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{})
  172. if err != nil {
  173. err = fmt.Errorf("库存返回失败: err->%v, out_trade_no->%v, sub_id->%v", err, param.Order.OutTradeNo, sub.Id)
  174. }
  175. }
  176. }
  177. return
  178. }