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

272 lines
7.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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "github.com/xormplus/xorm"
  6. "hudongzhuanjia/models"
  7. "hudongzhuanjia/services/im"
  8. "hudongzhuanjia/utils"
  9. "hudongzhuanjia/utils/define"
  10. "time"
  11. )
  12. func init() {
  13. go utils.HandleTicker(30*time.Minute, HandleReward) // 打赏24小时退款
  14. go utils.HandleTicker(30*time.Second, HandleGoodOrder) // 订单关闭信息
  15. }
  16. //处理支付成功之后的回调问题
  17. func HandleSuccess(order *models.UserOrder) error {
  18. if order.GoodType == 4 { // 直播商品
  19. // 可以使用事务进行控制
  20. activity := new(models.Activity)
  21. exist, err := models.Get(activity, order.ActivityId)
  22. if err != nil || !exist {
  23. return errors.New("互动信息异常")
  24. }
  25. s := core.GetXormAuto().NewSession()
  26. defer s.Close()
  27. err = s.Begin()
  28. if err != nil {
  29. s.Rollback()
  30. return err
  31. }
  32. cOrder := models.CustomerOrder{}
  33. exist, err = s.Where("is_delete=0 and out_trade_no=?", order.OutTradeNo).Get(cOrder)
  34. if err != nil || !exist || cOrder.Status != 0 {
  35. s.Rollback()
  36. return errors.New("客户订单异常")
  37. }
  38. cOrder.Status = 1
  39. _, err = s.Where("out_trade_no=?", order.OutTradeNo).Cols("").Update(&cOrder)
  40. if err != nil {
  41. s.Rollback()
  42. return errors.New("订单状态异常")
  43. }
  44. //subs := make([]map[string]string, 0)
  45. subs, err := models.GetCustomerOrderSubsByOrderId(cOrder.Id)
  46. //err = core.GetXormAuto().Table(new(models.CustomerOrderSub)).Alias("s").
  47. // Select("s.order_no, s.goods_id, s.goods_num, s.good_name, s.good_price, g.goods_pic_url").
  48. // Join("left", new(models.CustomerGoods).Alias("g"), "g.id=s.goods_id").
  49. // Where("s.is_delete=0 and s.order_=?", cOrder.Id).Find(&subs)
  50. if err != nil {
  51. s.Rollback()
  52. return err
  53. }
  54. gift := models.OrderGift{}
  55. exist, err = s.Where("is_delete=0 and order_gift_switch=? "+"and activity_id=?",
  56. define.StatusOpen, activity.Id).Get(&gift)
  57. if err != nil {
  58. s.Rollback()
  59. return err
  60. }
  61. if exist {
  62. prize := new(models.UserPrize)
  63. prize.UserId = order.UserId
  64. prize.ActivityId = order.ActivityId
  65. prize.RehearsalId = activity.RehearsalId
  66. prize.CustomerOrderId = cOrder.Id
  67. prize.ActivityName = activity.Name
  68. prize.ArchId = activity.ArchId
  69. prize.PrizeName = gift.GiftName
  70. prize.PrizeImg = gift.GiftPicUrl
  71. prize.PrizeType = 4
  72. prize.IsDelete = false
  73. prize.Status = 1
  74. if gift.Num <= 0 { // 不限制
  75. _, err = s.InsertOne(prize)
  76. if err != nil {
  77. s.Rollback()
  78. return err
  79. }
  80. cOrder.UserPrizeId = prize.Id
  81. _, err := s.ID(cOrder.Id).NoAutoCondition().Cols("user_prize_id").Update(&cOrder)
  82. if err != nil {
  83. s.Rollback()
  84. return err
  85. }
  86. } else if gift.Num > 0 {
  87. count, err := s.Where("activity_id=? and rehearsal_id=? and arch_id=? and is_delete=0",
  88. activity.Id, activity.RehearsalId, activity.ArchId).NoAutoCondition().Count(&gift)
  89. if err != nil {
  90. s.Rollback()
  91. return err
  92. }
  93. if gift.Num > int(count) { // 足够
  94. _, err = s.InsertOne(prize)
  95. if err != nil {
  96. s.Rollback()
  97. return err
  98. }
  99. cOrder.UserPrizeId = prize.Id
  100. _, err := s.ID(cOrder.Id).NoAutoCondition().Cols("user_prize_id").Update(&cOrder)
  101. if err != nil {
  102. s.Rollback()
  103. return err
  104. }
  105. }
  106. }
  107. }
  108. err = s.Commit()
  109. if err != nil {
  110. s.Rollback()
  111. return err
  112. }
  113. for _, sub := range subs {
  114. go im_service.SendGroupCustomMessage(cOrder.BuyerId, order.ActivityId, im_service.NoticeLiveGoodBuy,
  115. map[string]interface{}{
  116. "goods_pic_url": sub["goods_pic_url"],
  117. "goods_num": sub["goods_num"],
  118. "good_name": sub["good_name"],
  119. "good_price": sub["good_price"],
  120. "goods_id": sub["goods_id"],
  121. "timestamp": time.Now().Unix(),
  122. })
  123. }
  124. } else if order.GoodType == 3 { // 直播红包
  125. info := new(models.LiveRedEnvelopeRule)
  126. _, err := info.UpdateStatusByOutTradeNo(order.OutTradeNo, 1)
  127. if err != nil {
  128. return err
  129. }
  130. exist, err := info.GetByOutTradeNo(order.OutTradeNo)
  131. if err != nil || !exist || info.Status != 1 {
  132. return errors.New("订单状态异常")
  133. }
  134. go im_service.SendGroupCustomMessage(info.UserId, order.ActivityId, im_service.NoticeLiveRedPackStart,
  135. map[string]interface{}{
  136. "live_red_envelope_rule_id": info.Id,
  137. "prompt": info.Prompt,
  138. "timestamp": time.Now().Unix(),
  139. })
  140. } else if order.GoodType == 2 { // 打赏
  141. history := new(models.RewardHistory)
  142. exist, err := history.GetByOutTradeNo(order.OutTradeNo)
  143. if err != nil || !exist || history.Status != -1 {
  144. return errors.New("订单状态异常")
  145. }
  146. _, err = history.UpdateStatus(history.Id, 0)
  147. if err != nil {
  148. return err
  149. }
  150. } else if order.GoodType == 1 { // 霸屏
  151. history := new(models.BullyScreenHistory)
  152. exist, err := history.GetByOutTradeNo(order.OutTradeNo)
  153. if err != nil || !exist || history.Status != -1 {
  154. return errors.New("订单状态异常")
  155. }
  156. _, err = history.UpdateStatus(history.Id, 0)
  157. if err != nil {
  158. return err
  159. }
  160. }
  161. return nil
  162. }
  163. // 处理商品订单超时的状态
  164. func HandleGoodOrder() error {
  165. orders, err := models.GetExpiredAtLiveCustomerOrder()
  166. if err != nil {
  167. return err
  168. }
  169. for _, v := range orders {
  170. if v.Type != 1 || v.Status != 0 {
  171. continue
  172. }
  173. res, err := OrderQuery(v.OutTradeNo)
  174. if err != nil {
  175. return err
  176. }
  177. if res["trade_state"] == define.CODE_TRADE_SUCCESS {
  178. v.Status = 1
  179. _, err = models.Update(v.Id, v, "status")
  180. if err != nil {
  181. return err
  182. }
  183. } else if res["trade_state"] == define.CODE_TRADE_NOTPAY {
  184. err = Close(v.OutTradeNo)
  185. if err != nil {
  186. return err
  187. }
  188. v.Status = 9
  189. v.CancelTime = time.Now()
  190. _, err = models.Update(v.Id, v, "status", "cancel_time")
  191. if err != nil {
  192. return err
  193. }
  194. }
  195. subs, err := models.GetSubOrdersByOrderId(v.Id)
  196. if err != nil {
  197. return err
  198. }
  199. for _, sub := range subs {
  200. _, err = new(models.CustomerGoods).IncrStockById(sub.GoodsId, sub.GoodsNum)
  201. if err != nil {
  202. return err
  203. }
  204. }
  205. }
  206. return nil
  207. }
  208. // 处理打赏
  209. func HandleReward() error {
  210. result, err := models.GetExpireRewardHistory()
  211. if err != nil {
  212. return err
  213. }
  214. for _, v := range result {
  215. if v.RehearsalId != 0 && v.Type == 0 { // 欧轩互动h5彩排
  216. continue
  217. }
  218. _, err := Refund("欧轩互动-打赏过期退款", v.OutTradeNo)
  219. if err != nil {
  220. return err
  221. }
  222. v.Status = 4
  223. _, err = models.Update(v.Id, v, "status")
  224. if err != nil {
  225. return err
  226. }
  227. }
  228. return nil
  229. }
  230. func HandleCancelOrder(outTradeNo string) error {
  231. return models.Commit(func(session *xorm.Session) error {
  232. order := new(models.CustomerOrder)
  233. exist, err := session.Where("is_delete=0 and status=0 and out_trade_no=?", outTradeNo).Get(order)
  234. if err != nil {
  235. return err
  236. }
  237. if !exist || order.Status != 0 {
  238. return errors.New("客户订单异常")
  239. }
  240. order.Status = 9
  241. order.CancelTime = time.Now()
  242. _, err = session.Where("is_delete=0 and out_trade_no=?", outTradeNo).
  243. Cols("status, cancel_time").Update(order)
  244. if err != nil {
  245. return err
  246. }
  247. subs := make([]*models.CustomerOrderSub, 0)
  248. err = session.Where("is_delete=0 and order_no=?", order.OrderNo).Find(&subs)
  249. if err != nil {
  250. return err
  251. }
  252. for _, sub := range subs {
  253. _, err = session.ID(sub.GoodsId).NoAutoCondition().Incr("stock", sub.GoodsNum).
  254. Update(&models.CustomerGoods{})
  255. if err != nil {
  256. return err
  257. }
  258. }
  259. return nil
  260. })
  261. }