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

265 lines
7.7 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
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/xormplus/xorm"
  6. "hudongzhuanjia/logger"
  7. "hudongzhuanjia/models"
  8. "hudongzhuanjia/services/im"
  9. "hudongzhuanjia/utils"
  10. "hudongzhuanjia/utils/define"
  11. "time"
  12. )
  13. func init() {
  14. //go loopOrder()
  15. go utils.HandleTicker(30*time.Minute, HandleReward) // 打赏24小时退款
  16. go utils.HandleTicker(30*time.Second, HandleGoodOrder) // 订单关闭信息
  17. }
  18. //处理支付成功之后的回调问题
  19. func HandleSuccess(order *models.UserOrder) error {
  20. if order.GoodType == 4 { // 直播商品
  21. customerOrder := new(models.CustomerOrder)
  22. _, err := customerOrder.UpdateStatusBy(order.OutTradeNo, 0, 1)
  23. if err != nil {
  24. return errors.New("更新状态发送错误")
  25. }
  26. customerOrder.GetByOutTradeNO(order.OutTradeNo)
  27. if customerOrder.Status != 1 {
  28. return errors.New("商品订单状态信息出现异常")
  29. }
  30. subs, err := models.GetCustomerOrderSubsByOrderNos(customerOrder.OrderNo)
  31. if err != nil {
  32. logger.Error("商品子订单查询异常", err)
  33. return err
  34. }
  35. for _, sub := range subs {
  36. go im_service.SendGroupCustomMessage(customerOrder.BuyerId, order.ActivityId, im_service.NoticeLiveGoodBuy,
  37. map[string]interface{}{
  38. "goods_pic_url": sub["goods_pic_url"],
  39. "goods_num": sub["goods_num"],
  40. "good_name": sub["good_name"],
  41. "good_price": sub["good_price"],
  42. "goods_id": sub["goods_id"],
  43. "timestamp": time.Now().Unix(),
  44. })
  45. }
  46. activity := new(models.Activity)
  47. exist, err := models.Get(activity, order.ActivityId)
  48. if err != nil || !exist {
  49. return errors.New("互动信息异常")
  50. }
  51. gift := new(models.OrderGift)
  52. exist, err = gift.GetByActivityId(order.ActivityId)
  53. if err != nil {
  54. err = fmt.Errorf("获取订单送礼错误: err-> %v, activity_id-> %v", err, activity.Id)
  55. logger.Error(err)
  56. return err
  57. }
  58. if !exist {
  59. return nil
  60. }
  61. prize := new(models.UserPrize)
  62. prize.UserId = order.UserId
  63. prize.ActivityId = order.ActivityId
  64. prize.RehearsalId = activity.RehearsalId
  65. prize.ActivityName = activity.Name
  66. prize.PrizeName = gift.GiftName
  67. prize.PrizeImg = gift.GiftPicUrl
  68. prize.PrizeType = 4
  69. prize.IsDelete = false
  70. prize.Status = 1
  71. prize.CreatedAt = time.Now()
  72. prize.UpdatedAt = time.Now()
  73. if gift.Num > 0 {
  74. count, err := new(models.CustomerOrder).
  75. Count(activity.Id, activity.RehearsalId, customerOrder.CreatedAt)
  76. if err != nil {
  77. err = fmt.Errorf("订单人数统计失败: err->%v, out_trade_no->%v", err, order.OutTradeNo)
  78. logger.Error(err)
  79. return err
  80. }
  81. if gift.Num <= int(count) { // 人数足够
  82. return nil
  83. }
  84. }
  85. _, err = prize.Add()
  86. if err != nil {
  87. err = fmt.Errorf("奖品添加失败: err->%v, out_trade_no->%v", err, order.OutTradeNo)
  88. logger.Error(err)
  89. return err
  90. }
  91. customerOrder.UserPrizeId = prize.Id
  92. _, err = models.Update(customerOrder.Id, customerOrder, "user_prize_id")
  93. if err != nil {
  94. err = fmt.Errorf("奖品记录添加失败: err-> %v, out_trade_no-> %v, prize_id->%v", err, order.OutTradeNo, prize.Id)
  95. logger.Error(err)
  96. return err
  97. }
  98. } else if order.GoodType == 3 { // 直播红包
  99. info := new(models.LiveRedEnvelopeRule)
  100. _, err := info.UpdateStatusByOutTradeNo(order.OutTradeNo, 1)
  101. if err != nil {
  102. err = fmt.Errorf("状态改变出现错误: err->%v, out_trade_no->%v", err, order.OutTradeNo)
  103. logger.Error(err.Error())
  104. return err
  105. }
  106. exist, err := info.GetByOutTradeNo(order.OutTradeNo)
  107. if err != nil || !exist || info.Status != 1 {
  108. err = fmt.Errorf("直播红包信息异常: err-> %v, exist->%v, status->%v, out_trade_no->%v",
  109. err, exist, info.Status, order.OutTradeNo)
  110. logger.Error(err.Error())
  111. return err
  112. }
  113. go im_service.SendGroupCustomMessage(info.UserId, order.ActivityId, im_service.NoticeLiveRedPackStart,
  114. map[string]interface{}{
  115. "live_red_envelope_rule_id": info.Id,
  116. "prompt": info.Prompt,
  117. "timestamp": time.Now().Unix(),
  118. })
  119. } else if order.GoodType == 2 { // 打赏
  120. history := new(models.RewardHistory)
  121. exist, err := history.GetByOutTradeNo(order.OutTradeNo)
  122. if err != nil || !exist || history.Status != -1 {
  123. err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, order.OutTradeNo)
  124. logger.Error(err.Error())
  125. return err
  126. }
  127. _, err = history.UpdateStatus(history.Id, 0)
  128. if err != nil {
  129. err = fmt.Errorf("打赏状态发送变化: err->%v, out_trade_no %v", err, order.OutTradeNo)
  130. logger.Error(err.Error())
  131. return err
  132. }
  133. } else if order.GoodType == 1 { // 霸屏
  134. history := new(models.BullyScreenHistory)
  135. exist, err := history.GetByOutTradeNo(order.OutTradeNo)
  136. if err != nil || !exist || history.Status != -1 {
  137. err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, order.OutTradeNo)
  138. logger.Error(err.Error())
  139. return err
  140. }
  141. _, err = history.UpdateStatus(history.Id, 0)
  142. if err != nil {
  143. err = fmt.Errorf("霸屏状态发送变化: err->%v, out_trade_no %v", err, order.OutTradeNo)
  144. logger.Error(err.Error())
  145. }
  146. }
  147. return nil
  148. }
  149. // 处理商品订单超时的状态
  150. func HandleGoodOrder() error {
  151. orders, err := models.GetExpiredAtLiveCustomerOrder()
  152. if err != nil {
  153. err = fmt.Errorf("获取过期订单信息错误原因: %v", err)
  154. }
  155. for _, v := range orders {
  156. if v.Type != 1 || v.Status != 0 {
  157. continue
  158. }
  159. res, err := OrderQuery(v.OutTradeNo)
  160. if err != nil {
  161. err = fmt.Errorf("订单查询出现错误: %v, out_trade_no: %v", err, v.OutTradeNo)
  162. return err
  163. }
  164. if res["trade_state"] != define.CODE_TRADE_NOTPAY {
  165. err = fmt.Errorf("订单非未支付订单: %v, out_trade_no: %v", res["trade_state"], v.OutTradeNo)
  166. return err
  167. }
  168. err = Close(v.OutTradeNo)
  169. if err != nil {
  170. return err
  171. }
  172. v.Status = 9
  173. v.CancelTime = time.Now()
  174. _, err = models.Update(v.Id, v, "status", "cancel_time")
  175. if err != nil {
  176. return err
  177. }
  178. subs, err := models.GetSubOrdersByOrderNo(v.OrderNo)
  179. if err != nil {
  180. return err
  181. }
  182. for _, sub := range subs {
  183. _, err = new(models.CustomerGoods).IncrStockById(sub.GoodsId, sub.GoodsNum)
  184. if err != nil {
  185. return err
  186. }
  187. }
  188. }
  189. return nil
  190. }
  191. // 处理打赏
  192. func HandleReward() error {
  193. result, err := models.GetExpireRewardHistory()
  194. if err != nil {
  195. err = fmt.Errorf("获取过期打赏错误原因: %v", err)
  196. return err
  197. }
  198. for _, v := range result {
  199. if v.RehearsalId != 0 && v.Type == 0 { // 欧轩互动h5彩排
  200. continue
  201. }
  202. _, err := Refund("欧轩互动-打赏过期退款", v.OutTradeNo)
  203. if err != nil {
  204. err = fmt.Errorf("欧轩互动打赏过期退款错误: %v, out_trade_no: %v", err, v.OutTradeNo)
  205. return err
  206. }
  207. v.Status = 4
  208. _, err = models.Update(v.Id, v, "status")
  209. if err != nil {
  210. err = fmt.Errorf("欧轩互动打赏过期状态改变错误: %v, out_trade_no: %v", err, v.OutTradeNo)
  211. return err
  212. }
  213. }
  214. return nil
  215. }
  216. func HandleCancelOrder(outTradeNo string) error {
  217. return models.Commit(func(session *xorm.Session) error {
  218. order := new(models.CustomerOrder)
  219. exist, err := session.Where("is_delete=0 and status=0 and out_trade_no=?",
  220. outTradeNo).Get(order)
  221. if err != nil {
  222. return err
  223. }
  224. if !exist || order.Status != 0 {
  225. return errors.New("客户订单异常")
  226. }
  227. order.Status = 9
  228. order.CancelTime = time.Now()
  229. _, err = session.Where("is_delete=0 and out_trade_no=?",
  230. outTradeNo).Cols("status, cancel_time").Update(order)
  231. if err != nil {
  232. return err
  233. }
  234. subs := make([]*models.CustomerOrderSub, 0)
  235. err = session.Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
  236. if err != nil {
  237. return err
  238. }
  239. for _, sub := range subs {
  240. _, err = session.Where("id=?", sub.GoodsId).
  241. Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{})
  242. if err != nil {
  243. return err
  244. }
  245. }
  246. return nil
  247. })
  248. }