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

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