tool.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/ugcpay/conf"
  6. "go-common/library/log"
  7. )
  8. func dayRange(offset int) (from, to time.Time) {
  9. tmp := time.Now().AddDate(0, 0, offset)
  10. from = time.Date(tmp.Year(), tmp.Month(), tmp.Day(), 0, 0, 0, 0, time.Local)
  11. to = from.Add(24*time.Hour - 1)
  12. return
  13. }
  14. func monthRange(offset int) (from, to time.Time) {
  15. tmp := time.Now().AddDate(0, offset, 0)
  16. from = time.Date(tmp.Year(), tmp.Month(), 1, 0, 0, 0, 0, time.Local)
  17. to = from.AddDate(0, 1, 0).Add(-1)
  18. return
  19. }
  20. func dailyBillVer(t time.Time) int64 {
  21. // 2006-01-02 15:04:05
  22. return int64(t.Year()*10000 + int(t.Month())*100 + t.Day())
  23. }
  24. func monthlyBillVer(t time.Time) int64 {
  25. return int64(t.Year()*100 + int(t.Month()))
  26. }
  27. func runCAS(ctx context.Context, fn func(ctx context.Context) (effected bool, err error)) (err error) {
  28. times := conf.Conf.Biz.RunCASTimes
  29. if times <= 0 {
  30. times = 2
  31. }
  32. effected := false
  33. for times > 0 {
  34. times--
  35. if effected, err = fn(ctx); err != nil {
  36. return
  37. }
  38. if effected {
  39. return
  40. }
  41. }
  42. if times <= 0 {
  43. log.Error("runCAS failed!!!")
  44. }
  45. return
  46. }
  47. func calcAssetIncome(fee int64) (userIncome int64, bizIncome int64) {
  48. if fee <= 0 {
  49. return 0, 0
  50. }
  51. userIncome = int64((1.0 - conf.Conf.Biz.Tax.AssetRate) * float64(fee))
  52. bizIncome = fee - userIncome
  53. return
  54. }