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

264 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
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.ArchId = activity.ArchId
  67. prize.PrizeName = gift.GiftName
  68. prize.PrizeImg = gift.GiftPicUrl
  69. prize.PrizeType = 4
  70. prize.IsDelete = false
  71. prize.Status = 1
  72. if gift.Num > 0 {
  73. count, err := new(models.CustomerOrder).
  74. Count(activity.Id, activity.RehearsalId, customerOrder.CreatedAt, activity.ArchId)
  75. if err != nil {
  76. err = fmt.Errorf("订单人数统计失败: err->%v, out_trade_no->%v", err, order.OutTradeNo)
  77. logger.Error(err)
  78. return err
  79. }
  80. if gift.Num <= int(count) { // 人数足够
  81. return nil
  82. }
  83. }
  84. _, err = prize.Add()
  85. if err != nil {
  86. err = fmt.Errorf("奖品添加失败: err->%v, out_trade_no->%v", err, order.OutTradeNo)
  87. logger.Error(err)
  88. return err
  89. }
  90. customerOrder.UserPrizeId = prize.Id
  91. _, err = models.Update(customerOrder.Id, customerOrder, "user_prize_id")
  92. if err != nil {
  93. err = fmt.Errorf("奖品记录添加失败: err-> %v, out_trade_no-> %v, prize_id->%v", err, order.OutTradeNo, prize.Id)
  94. logger.Error(err)
  95. return err
  96. }
  97. } else if order.GoodType == 3 { // 直播红包
  98. info := new(models.LiveRedEnvelopeRule)
  99. _, err := info.UpdateStatusByOutTradeNo(order.OutTradeNo, 1)
  100. if err != nil {
  101. err = fmt.Errorf("状态改变出现错误: err->%v, out_trade_no->%v", err, order.OutTradeNo)
  102. logger.Error(err.Error())
  103. return err
  104. }
  105. exist, err := info.GetByOutTradeNo(order.OutTradeNo)
  106. if err != nil || !exist || info.Status != 1 {
  107. err = fmt.Errorf("直播红包信息异常: err-> %v, exist->%v, status->%v, out_trade_no->%v",
  108. err, exist, info.Status, order.OutTradeNo)
  109. logger.Error(err.Error())
  110. return err
  111. }
  112. go im_service.SendGroupCustomMessage(info.UserId, order.ActivityId, im_service.NoticeLiveRedPackStart,
  113. map[string]interface{}{
  114. "live_red_envelope_rule_id": info.Id,
  115. "prompt": info.Prompt,
  116. "timestamp": time.Now().Unix(),
  117. })
  118. } else if order.GoodType == 2 { // 打赏
  119. history := new(models.RewardHistory)
  120. exist, err := history.GetByOutTradeNo(order.OutTradeNo)
  121. if err != nil || !exist || history.Status != -1 {
  122. err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, order.OutTradeNo)
  123. logger.Error(err.Error())
  124. return err
  125. }
  126. _, err = history.UpdateStatus(history.Id, 0)
  127. if err != nil {
  128. err = fmt.Errorf("打赏状态发送变化: err->%v, out_trade_no %v", err, order.OutTradeNo)
  129. logger.Error(err.Error())
  130. return err
  131. }
  132. } else if order.GoodType == 1 { // 霸屏
  133. history := new(models.BullyScreenHistory)
  134. exist, err := history.GetByOutTradeNo(order.OutTradeNo)
  135. if err != nil || !exist || history.Status != -1 {
  136. err = fmt.Errorf("打赏历史异常: err->%v, out_trade_no %v", err, order.OutTradeNo)
  137. logger.Error(err.Error())
  138. return err
  139. }
  140. _, err = history.UpdateStatus(history.Id, 0)
  141. if err != nil {
  142. err = fmt.Errorf("霸屏状态发送变化: err->%v, out_trade_no %v", err, order.OutTradeNo)
  143. logger.Error(err.Error())
  144. }
  145. }
  146. return nil
  147. }
  148. // 处理商品订单超时的状态
  149. func HandleGoodOrder() error {
  150. orders, err := models.GetExpiredAtLiveCustomerOrder()
  151. if err != nil {
  152. err = fmt.Errorf("获取过期订单信息错误原因: %v", err)
  153. }
  154. for _, v := range orders {
  155. if v.Type != 1 || v.Status != 0 {
  156. continue
  157. }
  158. res, err := OrderQuery(v.OutTradeNo)
  159. if err != nil {
  160. err = fmt.Errorf("订单查询出现错误: %v, out_trade_no: %v", err, v.OutTradeNo)
  161. return err
  162. }
  163. if res["trade_state"] != define.CODE_TRADE_NOTPAY {
  164. err = fmt.Errorf("订单非未支付订单: %v, out_trade_no: %v", res["trade_state"], v.OutTradeNo)
  165. return err
  166. }
  167. err = Close(v.OutTradeNo)
  168. if err != nil {
  169. return err
  170. }
  171. v.Status = 9
  172. v.CancelTime = time.Now()
  173. _, err = models.Update(v.Id, v, "status", "cancel_time")
  174. if err != nil {
  175. return err
  176. }
  177. subs, err := models.GetSubOrdersByOrderNo(v.OrderNo)
  178. if err != nil {
  179. return err
  180. }
  181. for _, sub := range subs {
  182. _, err = new(models.CustomerGoods).IncrStockById(sub.GoodsId, sub.GoodsNum)
  183. if err != nil {
  184. return err
  185. }
  186. }
  187. }
  188. return nil
  189. }
  190. // 处理打赏
  191. func HandleReward() error {
  192. result, err := models.GetExpireRewardHistory()
  193. if err != nil {
  194. err = fmt.Errorf("获取过期打赏错误原因: %v", err)
  195. return err
  196. }
  197. for _, v := range result {
  198. if v.RehearsalId != 0 && v.Type == 0 { // 欧轩互动h5彩排
  199. continue
  200. }
  201. _, err := Refund("欧轩互动-打赏过期退款", v.OutTradeNo)
  202. if err != nil {
  203. err = fmt.Errorf("欧轩互动打赏过期退款错误: %v, out_trade_no: %v", err, v.OutTradeNo)
  204. return err
  205. }
  206. v.Status = 4
  207. _, err = models.Update(v.Id, v, "status")
  208. if err != nil {
  209. err = fmt.Errorf("欧轩互动打赏过期状态改变错误: %v, out_trade_no: %v", err, v.OutTradeNo)
  210. return err
  211. }
  212. }
  213. return nil
  214. }
  215. func HandleCancelOrder(outTradeNo string) error {
  216. return models.Commit(func(session *xorm.Session) error {
  217. order := new(models.CustomerOrder)
  218. exist, err := session.Where("is_delete=0 and status=0 and out_trade_no=?",
  219. outTradeNo).Get(order)
  220. if err != nil {
  221. return err
  222. }
  223. if !exist || order.Status != 0 {
  224. return errors.New("客户订单异常")
  225. }
  226. order.Status = 9
  227. order.CancelTime = time.Now()
  228. _, err = session.Where("is_delete=0 and out_trade_no=?",
  229. outTradeNo).Cols("status, cancel_time").Update(order)
  230. if err != nil {
  231. return err
  232. }
  233. subs := make([]*models.CustomerOrderSub, 0)
  234. err = session.Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
  235. if err != nil {
  236. return err
  237. }
  238. for _, sub := range subs {
  239. _, err = session.Where("id=?", sub.GoodsId).
  240. Incr("stock", sub.GoodsNum).Update(&models.CustomerGoods{})
  241. if err != nil {
  242. return err
  243. }
  244. }
  245. return nil
  246. })
  247. }