userTask.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package utility
  2. import (
  3. "analyze/internal/model/entity"
  4. "fmt"
  5. "math"
  6. "time"
  7. )
  8. var (
  9. y_m_day = map[int]int{1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
  10. )
  11. func GetPreviousMarket(sTime, eTime time.Time) int64 {
  12. var os_time int64
  13. s_time := sTime
  14. if SEMonth(sTime, eTime) {
  15. var min int
  16. //统计月份
  17. for sTime.Before(eTime) {
  18. sTime = sTime.AddDate(0, 1, 0)
  19. min++
  20. }
  21. os_time = s_time.AddDate(0, -min, 0).Unix()
  22. } else {
  23. os_time = s_time.AddDate(0, 0, -int(math.Ceil(eTime.Sub(sTime).Hours()/24))).Unix()
  24. }
  25. return os_time
  26. }
  27. // 判断是否月初到月末
  28. func SEMonth(sTime, eTime time.Time) bool {
  29. var day int
  30. month := int(eTime.Month())
  31. if month == 2 {
  32. if eTime.Year()%4 == 0 {
  33. day = 29
  34. } else {
  35. day = 28
  36. }
  37. } else {
  38. day = y_m_day[month]
  39. }
  40. if sTime.Day() == 1 && eTime.Day() == day {
  41. return true
  42. }
  43. return false
  44. }
  45. func GetMonthData(sTime, eTime time.Time) (_b bool, dateSpans []entity.DateSpan) {
  46. //整月多取一个月进行环比
  47. if SEMonth(sTime, eTime) {
  48. _b = true
  49. sTime = sTime.AddDate(0, -1, 0)
  50. }
  51. dateSpans = getBidAmountStatistics(sTime, eTime)
  52. return
  53. }
  54. func getBidAmountStatistics(sTime, eTime time.Time) (dateSpans []entity.DateSpan) {
  55. tmpTime, rTime, tEndTime := sTime, sTime, getMonthRange(eTime, false)
  56. for rTime.Before(tEndTime) {
  57. ts, te := getMonthRange(tmpTime, true), getMonthRange(tmpTime, false)
  58. if sTime == tmpTime {
  59. ts = sTime
  60. }
  61. if te == tEndTime {
  62. te = eTime
  63. }
  64. if ts.Before(te) {
  65. dateSpans = append(dateSpans, entity.DateSpan{
  66. Key: fmt.Sprintf("%d-%d", ts.Year(), ts.Month()),
  67. From: ts.Unix(),
  68. To: te.Unix(),
  69. })
  70. }
  71. rTime = rTime.AddDate(0, 1, 0)
  72. if int(rTime.Month())-int(tmpTime.Month()) > 1 {
  73. rTime = rTime.AddDate(0, -1, 0)
  74. }
  75. tmpTime = rTime
  76. }
  77. return
  78. }
  79. // getMonthRange获取月份范围
  80. // isStart true本月月初 false 本月月末(下月月初)
  81. func getMonthRange(t time.Time, isStart bool) time.Time {
  82. if isStart {
  83. return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
  84. }
  85. return time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, t.Location())
  86. }
  87. func GetYearData(sTime, eTime time.Time) (_b bool, dateSpans []entity.DateSpan) {
  88. //整年多取一年进行环比
  89. if sTime.Month() == 1 && sTime.Day() == 1 && eTime.Month() == 12 && eTime.Day() == 31 {
  90. _b = true
  91. sTime = sTime.AddDate(-1, 0, 0)
  92. }
  93. dateSpans = getCommonYearStatistics(sTime, eTime)
  94. return
  95. }
  96. // 年份统计
  97. func getCommonYearStatistics(sTime, eTime time.Time) (dateSpans []entity.DateSpan) {
  98. tmpTime, rTime, tEndTime := sTime, sTime, getYearRange(eTime, false)
  99. for rTime.Before(tEndTime) {
  100. ts, te := getYearRange(tmpTime, true), getYearRange(tmpTime, false)
  101. if sTime == tmpTime {
  102. ts = sTime
  103. }
  104. if te == tEndTime {
  105. te = eTime
  106. }
  107. if ts.Before(te) {
  108. dateSpans = append(dateSpans, entity.DateSpan{
  109. Key: fmt.Sprintf("%d", ts.Year()),
  110. From: ts.Unix(),
  111. To: te.Unix(),
  112. })
  113. }
  114. rTime = rTime.AddDate(1, 0, 0)
  115. tmpTime = rTime
  116. }
  117. return
  118. }
  119. // getYearRange获取月份范围
  120. // isStart true本月月初 false 本月月末(下月月初)
  121. func getYearRange(t time.Time, isStart bool) time.Time {
  122. if isStart {
  123. return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
  124. }
  125. return time.Date(t.Year()+1, 1, 1, 0, 0, 0, 0, t.Location())
  126. }
  127. // GetAllKeywordArr 获取所有匹配词
  128. func GetAllKeywordArr(res []entity.KeyWordGroup) (rData []entity.ViewKeyWord) {
  129. for _, kwg := range res {
  130. rData = append(rData, getGroupKeywordArr(kwg.A_Key)...)
  131. }
  132. return
  133. }
  134. // getGroupKeywordArr 模糊拆分为多个精准匹配
  135. func getGroupKeywordArr(res []entity.ViewKeyWord) (rData []entity.ViewKeyWord) {
  136. for _, kw := range res {
  137. if kw.MatchWay == 1 {
  138. for _, kk := range kw.Keyword {
  139. rData = append(rData, entity.ViewKeyWord{
  140. Keyword: []string{kk},
  141. Exclude: kw.Exclude,
  142. })
  143. }
  144. for _, kk := range kw.Appended {
  145. rData = append(rData, entity.ViewKeyWord{
  146. Keyword: []string{kk},
  147. Exclude: kw.Exclude,
  148. })
  149. }
  150. } else {
  151. rData = append(rData, kw)
  152. }
  153. }
  154. return
  155. }
  156. func DateIsIn(from, to, n, m int64) bool {
  157. if m == 0 {
  158. return to >= n && from <= n
  159. }
  160. return to > n && from < n
  161. }