order.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/vip/model"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _autoRenewFailTwoMsg = "连续包月大会员今天凌晨续费又失败了,服务已暂停。如果想要再次享受连续包月大会员服务,请先取消连续包月,再去开通哦~"
  13. _autoRenewFailOneMsg = "连续包月大会员今天凌晨续费失败了,%s 0点将会再次重试。"
  14. _deadlineAutoRenewMsg = "您的连续包月服务将在%s 0点续费。"
  15. _autoRenewFailTitle = "连续包月服务续费失败"
  16. _deadlineAutoRenewTitle = "连续包月服务即将续费"
  17. _sleep = 20 * time.Millisecond
  18. _maxtime = 20
  19. )
  20. func (s *Service) handlerinsertorderproc() {
  21. var (
  22. err error
  23. )
  24. defer func() {
  25. if x := recover(); x != nil {
  26. log.Error("service.handlerinsertorderproc panic(%v)", x)
  27. go s.handlerinsertorderproc()
  28. log.Info("service.handlerinsertorderproc recover")
  29. }
  30. }()
  31. for {
  32. order := <-s.handlerInsertOrder
  33. for i := 0; i < s.c.Property.Retry; i++ {
  34. if err = s.insertOrder(order); err == nil {
  35. break
  36. }
  37. log.Error("error(%+v)", err)
  38. }
  39. }
  40. }
  41. func (s *Service) insertOrder(r *model.VipPayOrder) (err error) {
  42. var aff int64
  43. if aff, err = s.dao.AddPayOrder(context.TODO(), r); err != nil {
  44. err = errors.WithStack(err)
  45. return
  46. }
  47. if aff != 1 {
  48. return
  49. }
  50. log.Info("vip_pay_order sysn data(%+v)", r)
  51. rlog := new(model.VipPayOrderLog)
  52. rlog.Mid = r.Mid
  53. rlog.OrderNo = r.OrderNo
  54. rlog.Status = r.Status
  55. if _, err = s.dao.AddPayOrderLog(context.TODO(), rlog); err != nil {
  56. log.Error("add pay order log(%+v) error(%+v)", rlog, err)
  57. err = nil
  58. }
  59. return
  60. }
  61. func (s *Service) handlerupdateorderproc() {
  62. var (
  63. err error
  64. flag bool
  65. )
  66. defer func() {
  67. if x := recover(); x != nil {
  68. log.Error("service.handlerupdateorderproc panic(%v)", x)
  69. go s.handlerupdateorderproc()
  70. log.Info("service.handlerupdateorderproc recover")
  71. }
  72. }()
  73. for {
  74. order := <-s.handlerUpdateOrder
  75. flag = true
  76. for i := 0; i < s.c.Property.Retry; i++ {
  77. if err = s.updatePayOrder(context.TODO(), order); err == nil {
  78. flag = false
  79. break
  80. }
  81. log.Error("error(%+v)", err)
  82. }
  83. if flag {
  84. s.handlerFailPayOrder <- order
  85. }
  86. }
  87. }
  88. func (s *Service) handlerfailpayorderproc() {
  89. var (
  90. err error
  91. )
  92. defer func() {
  93. if x := recover(); x != nil {
  94. log.Error("service.handlerfailpayorderproc panic(%v)", x)
  95. go s.handlerfailpayorderproc()
  96. log.Info("service.handlerfailpayorderproc recover")
  97. }
  98. }()
  99. for {
  100. order := <-s.handlerFailPayOrder
  101. _time := 0
  102. for {
  103. if err = s.updatePayOrder(context.TODO(), order); err == nil {
  104. break
  105. }
  106. log.Error("pay order error(%+v)", err)
  107. _time++
  108. if _time > _maxtime {
  109. break
  110. }
  111. }
  112. }
  113. }
  114. func (s *Service) handlerfailrechargeorderproc() {
  115. var (
  116. eff int64
  117. err error
  118. )
  119. defer func() {
  120. if x := recover(); x != nil {
  121. log.Error("service.handlerfailrechargeorderproc panic(%v)", x)
  122. go s.handlerfailrechargeorderproc()
  123. log.Info("service.handlerfailrechargeorderproc recover")
  124. }
  125. }()
  126. for {
  127. order := <-s.handlerFailRechargeOrder
  128. _time := 0
  129. for {
  130. if eff, err = s.dao.UpdateRechargeOrder(context.TODO(), order); err != nil {
  131. log.Error("error(%+v)", err)
  132. break
  133. }
  134. if eff > 0 {
  135. break
  136. }
  137. _time++
  138. if _time > _maxtime {
  139. break
  140. }
  141. time.Sleep(_sleep)
  142. }
  143. }
  144. }
  145. func (s *Service) handlerupdaterechargeorderproc() {
  146. var (
  147. eff int64
  148. err error
  149. flag bool
  150. )
  151. defer func() {
  152. if x := recover(); x != nil {
  153. log.Error("service.handlerupdaterechargeorderproc panic(%v)", x)
  154. go s.handlerupdaterechargeorderproc()
  155. log.Info("service.handlerupdaterechargeorderproc recover")
  156. }
  157. }()
  158. for {
  159. order := <-s.handlerRechargeOrder
  160. flag = true
  161. for i := 0; i < s.c.Property.Retry; i++ {
  162. if eff, err = s.dao.UpdateRechargeOrder(context.TODO(), order); err != nil {
  163. log.Error("error(%+v)", err)
  164. continue
  165. }
  166. if eff > 0 {
  167. log.Info("update recharge order(%+v)", order)
  168. flag = false
  169. break
  170. }
  171. time.Sleep(_sleep)
  172. }
  173. if flag {
  174. s.handlerFailRechargeOrder <- order
  175. }
  176. }
  177. }
  178. func (s *Service) updatePayOrder(c context.Context, r *model.VipPayOrder) (err error) {
  179. var eff int64
  180. if eff, err = s.dao.UpdatePayOrderStatus(c, r); err != nil {
  181. err = errors.WithStack(err)
  182. return
  183. }
  184. if eff <= 0 {
  185. err = fmt.Errorf("order更新未执行(%+v)", r)
  186. time.Sleep(_sleep)
  187. return
  188. }
  189. log.Info("cur pay order update order(%+v)", r)
  190. rlogKey := fmt.Sprintf("%v:%v", r.OrderNo, r.Status)
  191. if succeed := s.dao.AddTransferLock(c, rlogKey); succeed {
  192. rlog := new(model.VipPayOrderLog)
  193. rlog.Mid = r.Mid
  194. rlog.OrderNo = r.OrderNo
  195. rlog.Status = r.Status
  196. if _, err = s.dao.AddPayOrderLog(context.TODO(), rlog); err != nil {
  197. log.Error("add pay order log(%+v) error(%+v)", rlog, err)
  198. err = nil
  199. }
  200. return
  201. }
  202. return
  203. }
  204. func (s *Service) convertPayOrder(r *model.VipPayOrderOldMsg) (res *model.VipPayOrder) {
  205. res = new(model.VipPayOrder)
  206. res.Mid = r.Mid
  207. res.AppID = r.AppID
  208. res.AppSubID = r.AppSubID
  209. res.BuyMonths = r.BuyMonths
  210. res.Money = r.Money
  211. res.RechargeBp = r.RechargeBp
  212. res.OrderNo = r.OrderNo
  213. res.OrderType = r.OrderType
  214. res.PayType = r.PayType
  215. res.Platform = r.Platform
  216. res.Status = r.Status
  217. res.ToMid = r.Bmid
  218. res.Ver = r.Ver
  219. res.CouponMoney = r.CouponMoney
  220. if paymentTime, err := time.ParseInLocation("2006-01-02 15:04:05", r.PaymentTime, time.Local); err == nil {
  221. res.PaymentTime = xtime.Time(paymentTime.Unix())
  222. }
  223. res.Ctime = xtime.Time(parseTime(r.Ctime).Unix())
  224. res.Mtime = xtime.Time(parseTime(r.Mtime).Unix())
  225. return
  226. }
  227. func (s *Service) convertPayOrderByMsg(r *model.VipRechargeOrderMsg) (res *model.VipPayOrder) {
  228. res = new(model.VipPayOrder)
  229. res.Mid = r.PayMid
  230. res.OrderNo = r.PayOrderNo
  231. res.ThirdTradeNo = r.ThirdTradeNo
  232. res.RechargeBp = r.RechargeBp
  233. return
  234. }
  235. func convertPayOrderOldToNew(r *model.VipPayOrderOld) (res *model.VipPayOrder) {
  236. res = new(model.VipPayOrder)
  237. res.Mid = r.Mid
  238. res.AppID = r.AppID
  239. res.AppSubID = r.AppSubID
  240. res.BuyMonths = r.BuyMonths
  241. res.Money = r.Money
  242. res.OrderNo = r.OrderNo
  243. res.OrderType = r.OrderType
  244. res.PayType = r.PayType
  245. res.Platform = r.Platform
  246. res.Status = r.Status
  247. res.ToMid = r.Bmid
  248. res.Ver = r.Ver
  249. res.PaymentTime = r.PaymentTime
  250. res.CouponMoney = r.CouponMoney
  251. return
  252. }
  253. //HandlerPayOrder handler pay order
  254. func (s *Service) HandlerPayOrder() (err error) {
  255. var (
  256. size = 1000
  257. oldMaxID int
  258. )
  259. if oldMaxID, err = s.dao.SelOldOrderMaxID(context.TODO()); err != nil {
  260. log.Error("selOldOrderMaxID error(%+v)", err)
  261. return
  262. }
  263. page := oldMaxID / size
  264. if oldMaxID%size != 0 {
  265. page++
  266. }
  267. for i := 0; i < page; i++ {
  268. startID := i * size
  269. endID := (i + 1) * size
  270. if endID > oldMaxID {
  271. endID = oldMaxID
  272. }
  273. var (
  274. res []*model.VipPayOrderOld
  275. batchOrder []*model.VipPayOrder
  276. orderNos []string
  277. oldRechargeOrder []*model.VipRechargeOrder
  278. )
  279. rechargeMap := make(map[string]*model.VipRechargeOrder)
  280. if res, err = s.dao.SelOldPayOrder(context.TODO(), startID, endID); err != nil {
  281. log.Error("selOldPayOrder(startID:%v endID:%v) error(%+v)", startID, endID, err)
  282. return
  283. }
  284. for _, v := range res {
  285. batchOrder = append(batchOrder, convertPayOrderOldToNew(v))
  286. }
  287. for _, v := range batchOrder {
  288. orderNos = append(orderNos, v.OrderNo)
  289. }
  290. if oldRechargeOrder, err = s.dao.SelOldRechargeOrder(context.TODO(), orderNos); err != nil {
  291. return
  292. }
  293. for _, v := range oldRechargeOrder {
  294. rechargeMap[v.PayOrderNo] = v
  295. }
  296. for _, v := range batchOrder {
  297. rechargeOrder := rechargeMap[v.OrderNo]
  298. if rechargeOrder != nil {
  299. v.ThirdTradeNo = rechargeOrder.ThirdTradeNo
  300. v.RechargeBp = rechargeOrder.RechargeBp
  301. }
  302. }
  303. if err = s.dao.BatchAddPayOrder(context.TODO(), batchOrder); err != nil {
  304. return
  305. }
  306. }
  307. return
  308. }
  309. func (s *Service) willDedutionMsg() (err error) {
  310. var (
  311. size = 5000
  312. endID int
  313. now time.Time
  314. vips []*model.VipUserInfo
  315. )
  316. if now, err = time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Local); err != nil {
  317. log.Error("time.ParseInLocation(%v) error(%+v)", time.Now(), err)
  318. return
  319. }
  320. start := now.AddDate(0, 0, 1)
  321. end := start.AddDate(0, 0, 3)
  322. if endID, err = s.dao.SelMaxID(context.TODO()); err != nil {
  323. return
  324. }
  325. page := endID / size
  326. if endID%size != 0 {
  327. page++
  328. }
  329. for i := 0; i < page; i++ {
  330. startID := i * size
  331. eID := (i + 1) * size
  332. if vips, err = s.dao.SelVipUsers(context.TODO(), startID, eID, xtime.Time(start.Unix()), xtime.Time(end.Unix())); err != nil {
  333. continue
  334. }
  335. for _, v := range vips {
  336. if v.OverdueTime.Time().Equal(start) {
  337. s.dao.SendMultipMsg(context.TODO(), fmt.Sprintf("%v", v.Mid),
  338. _autoRenewFailTwoMsg,
  339. _autoRenewFailTitle,
  340. vipWillExpiredMsgCode,
  341. systemNotify)
  342. } else if start.AddDate(0, 0, 1).Equal(v.OverdueTime.Time()) {
  343. s.dao.SendMultipMsg(context.TODO(), fmt.Sprintf("%v", v.Mid),
  344. fmt.Sprint(_autoRenewFailOneMsg, v.OverdueTime.Time().AddDate(0, 0, -1).Format("2006-01-02")),
  345. _autoRenewFailTitle,
  346. vipWillExpiredMsgCode,
  347. systemNotify)
  348. } else if start.AddDate(0, 0, 2).Equal(v.OverdueTime.Time()) {
  349. s.dao.SendMultipMsg(context.TODO(), fmt.Sprintf("%v", v.Mid),
  350. fmt.Sprint(_deadlineAutoRenewMsg, start.Format("2006-01-02")),
  351. _deadlineAutoRenewTitle,
  352. vipWillExpiredMsgCode,
  353. systemNotify)
  354. }
  355. }
  356. }
  357. return
  358. }
  359. func (s *Service) autoRenews() (err error) {
  360. //var (
  361. // size = 5000
  362. // endID int
  363. // now time.Time
  364. // vips []*model.VipUserInfo
  365. // price float64
  366. //)
  367. //if now, err = time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Local); err != nil {
  368. // log.Error("time.ParseInLocation(%v) error(%+v)", time.Now(), err)
  369. // return
  370. //}
  371. //
  372. //start := now.AddDate(0, 0, 1)
  373. //
  374. //end := start.AddDate(0, 0, 3)
  375. //if price, err = s.vipRPC.Price(context.TODO(), 1, xmodel.DevicePC, xmodel.AutoRenew, 1); err != nil {
  376. // err = errors.WithStack(err)
  377. // return
  378. //}
  379. //if endID, err = s.dao.SelMaxID(context.TODO()); err != nil {
  380. // return
  381. //}
  382. //
  383. //page := endID / size
  384. //if endID%size != 0 {
  385. // page++
  386. //}
  387. //for i := 0; i < page; i++ {
  388. // startID := i * size
  389. // eID := (i + 1) * size
  390. // if vips, err = s.dao.SelVipUsers(context.TODO(), startID, eID, xtime.Time(start.Unix()), xtime.Time(end.Unix())); err != nil {
  391. // err = errors.WithStack(err)
  392. // continue
  393. // }
  394. // for _, v := range vips {
  395. // var params = make(map[string]interface{}, 0)
  396. // if params, err = s.vipRPC.CreateOrderPlatfrom(context.TODO(), int64(v.Mid), 0, 0, 1, price, xmodel.DevicePC, 5, xmodel.AutoRenew, ""); err != nil {
  397. // log.Error("CreateOrderPlatform error(%+v)", err)
  398. // continue
  399. // }
  400. // params["payChannelId"] = v.PayChannelId
  401. // params["payChannel"] = s.c.Property.PayMapping[strconv.Itoa(int(v.PayChannelId))]
  402. // if err = s.dao.PayOrder(context.TODO(), params); err != nil {
  403. // log.Error("handler fail orderId->%v mid:%v", params["orderId"], v.Mid)
  404. // continue
  405. // }
  406. // log.Info("handler success orderId:%v mid:%v", params["orderId"], v.Mid)
  407. // }
  408. //}
  409. return
  410. }
  411. // AutoRenewJob auto renew job.
  412. //func (s *Service) autoRenewJob() {
  413. // defer func() {
  414. // if x := recover(); x != nil {
  415. // log.Error("service.autoRenewJob panic(%v)", x)
  416. // go s.autoRenewJob()
  417. // log.Info("service.autoRenewJob recover")
  418. // }
  419. // }()
  420. // log.Info("auto renew job start.................................")
  421. // var err error
  422. // if err = s.autoRenews(); err != nil {
  423. // log.Error("autoRenews error(%+v)", err)
  424. // }
  425. // log.Info("auto renew job end...................................")
  426. //}
  427. // SendMessageJob send message job.
  428. func (s *Service) sendMessageJob() {
  429. defer func() {
  430. if x := recover(); x != nil {
  431. log.Error("service.sendMessageJob panic(%v)", x)
  432. go s.sendMessageJob()
  433. log.Info("service.sendMessageJob recover")
  434. }
  435. }()
  436. log.Info("sendMessage job start .........................")
  437. s.willDedutionMsg()
  438. log.Info("sendMessage job end .........................")
  439. }