userTask.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package utility
  2. import (
  3. "analyze/internal/model/entity"
  4. "app.yhyue.com/moapp/jybase/common"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. "fmt"
  7. "math"
  8. "strings"
  9. "time"
  10. )
  11. var (
  12. 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}
  13. )
  14. func GetPreviousMarket(sTime, eTime time.Time) int64 {
  15. var os_time int64
  16. s_time := sTime
  17. if SEMonth(sTime, eTime) {
  18. var min int
  19. //统计月份
  20. for sTime.Before(eTime) {
  21. sTime = sTime.AddDate(0, 1, 0)
  22. min++
  23. }
  24. os_time = s_time.AddDate(0, -min, 0).Unix()
  25. } else {
  26. os_time = s_time.AddDate(0, 0, -int(math.Ceil(eTime.Sub(sTime).Hours()/24))).Unix()
  27. }
  28. return os_time
  29. }
  30. // 判断是否月初到月末
  31. func SEMonth(sTime, eTime time.Time) bool {
  32. var day int
  33. month := int(eTime.Month())
  34. if month == 2 {
  35. if eTime.Year()%4 == 0 {
  36. day = 29
  37. } else {
  38. day = 28
  39. }
  40. } else {
  41. day = y_m_day[month]
  42. }
  43. if sTime.Day() == 1 && eTime.Day() == day {
  44. return true
  45. }
  46. return false
  47. }
  48. func GetMonthData(sTime, eTime time.Time) (_b bool, dateSpans []entity.DateSpan) {
  49. //整月多取一个月进行环比
  50. if SEMonth(sTime, eTime) {
  51. _b = true
  52. sTime = sTime.AddDate(0, -1, 0)
  53. }
  54. dateSpans = getBidAmountStatistics(sTime, eTime)
  55. return
  56. }
  57. func getBidAmountStatistics(sTime, eTime time.Time) (dateSpans []entity.DateSpan) {
  58. tmpTime, rTime, tEndTime := sTime, sTime, getMonthRange(eTime, false)
  59. for rTime.Before(tEndTime) {
  60. ts, te := getMonthRange(tmpTime, true), getMonthRange(tmpTime, false)
  61. if sTime == tmpTime {
  62. ts = sTime
  63. }
  64. if te == tEndTime {
  65. te = eTime
  66. }
  67. if ts.Before(te) {
  68. dateSpans = append(dateSpans, entity.DateSpan{
  69. Key: fmt.Sprintf("%d-%d", ts.Year(), ts.Month()),
  70. From: ts.Unix(),
  71. To: te.Unix(),
  72. })
  73. }
  74. rTime = rTime.AddDate(0, 1, 0)
  75. if int(rTime.Month())-int(tmpTime.Month()) > 1 {
  76. rTime = rTime.AddDate(0, -1, 0)
  77. }
  78. tmpTime = rTime
  79. }
  80. return
  81. }
  82. // getMonthRange获取月份范围
  83. // isStart true本月月初 false 本月月末(下月月初)
  84. func getMonthRange(t time.Time, isStart bool) time.Time {
  85. if isStart {
  86. return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
  87. }
  88. return time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, t.Location())
  89. }
  90. func GetYearData(sTime, eTime time.Time) (_b bool, dateSpans []entity.DateSpan) {
  91. //整年多取一年进行环比
  92. if sTime.Month() == 1 && sTime.Day() == 1 && eTime.Month() == 12 && eTime.Day() == 31 {
  93. _b = true
  94. sTime = sTime.AddDate(-1, 0, 0)
  95. }
  96. dateSpans = getCommonYearStatistics(sTime, eTime)
  97. return
  98. }
  99. // 年份统计
  100. func getCommonYearStatistics(sTime, eTime time.Time) (dateSpans []entity.DateSpan) {
  101. tmpTime, rTime, tEndTime := sTime, sTime, getYearRange(eTime, false)
  102. for rTime.Before(tEndTime) {
  103. ts, te := getYearRange(tmpTime, true), getYearRange(tmpTime, false)
  104. if sTime == tmpTime {
  105. ts = sTime
  106. }
  107. if te == tEndTime {
  108. te = eTime
  109. }
  110. if ts.Before(te) {
  111. dateSpans = append(dateSpans, entity.DateSpan{
  112. Key: fmt.Sprintf("%d", ts.Year()),
  113. From: ts.Unix(),
  114. To: te.Unix(),
  115. })
  116. }
  117. rTime = rTime.AddDate(1, 0, 0)
  118. tmpTime = rTime
  119. }
  120. return
  121. }
  122. // getYearRange获取月份范围
  123. // isStart true本月月初 false 本月月末(下月月初)
  124. func getYearRange(t time.Time, isStart bool) time.Time {
  125. if isStart {
  126. return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
  127. }
  128. return time.Date(t.Year()+1, 1, 1, 0, 0, 0, 0, t.Location())
  129. }
  130. // GetAllKeywordArr 获取所有匹配词
  131. func GetAllKeywordArr(res []entity.KeyWordGroup) (rData []entity.ViewKeyWord) {
  132. for _, kwg := range res {
  133. rData = append(rData, GetGroupKeywordArr(kwg.A_Key)...)
  134. }
  135. return
  136. }
  137. // GetGroupKeywordArr 模糊拆分为多个精准匹配
  138. func GetGroupKeywordArr(res []entity.ViewKeyWord) (rData []entity.ViewKeyWord) {
  139. for _, kw := range res {
  140. if kw.MatchWay == 1 {
  141. for _, kk := range kw.Keyword {
  142. rData = append(rData, entity.ViewKeyWord{
  143. Keyword: []string{kk},
  144. Exclude: kw.Exclude,
  145. })
  146. }
  147. for _, kk := range kw.Appended {
  148. rData = append(rData, entity.ViewKeyWord{
  149. Keyword: []string{kk},
  150. Exclude: kw.Exclude,
  151. })
  152. }
  153. } else {
  154. rData = append(rData, kw)
  155. }
  156. }
  157. return
  158. }
  159. func DateIsIn(from, to, n, m int64) bool {
  160. if m == 0 {
  161. return to >= n && from <= n
  162. }
  163. return to > n && from < n
  164. }
  165. func EncodeId(sid string) string {
  166. if sid == "" || sid == "-" { //不存在的id为-
  167. return ""
  168. }
  169. return encrypt.EncodeArticleId2ByCheck(sid)
  170. }
  171. // 项目金额---TOP3地区的重点中标单位
  172. func GetWinnerInfoOfAmount(pt *entity.ProjectInfo) []*entity.WinnerAmountS {
  173. var winnerAmount []*entity.WinnerAmountS
  174. if pt.Winners != "" {
  175. for k, w := range strings.Split(pt.Winners, ",") {
  176. winnerAmount = append(winnerAmount, &entity.WinnerAmountS{
  177. Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string),
  178. Winner: w,
  179. WinnerAmount: pt.Sortprice,
  180. })
  181. }
  182. }
  183. return winnerAmount
  184. }
  185. // 项目数量---TOP3地区的重点中标单位
  186. func GetWinnerInfoOfCount(pt *entity.ProjectInfo) []*entity.WinnerTotalS {
  187. var winnerCount []*entity.WinnerTotalS
  188. if pt.Winners != "" {
  189. for k, w := range strings.Split(pt.Winners, ",") {
  190. winnerCount = append(winnerCount, &entity.WinnerTotalS{
  191. Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string),
  192. Winner: w,
  193. WinnerTotal: 1,
  194. })
  195. }
  196. }
  197. return winnerCount
  198. }
  199. // 细化市场重点中标单位-项目数量|金额
  200. func GetWinnerInfoTopList(pt *entity.ProjectInfo, value, total float64) []*entity.TopListS {
  201. var winnerTotal []*entity.TopListS
  202. if pt.Winners != "" {
  203. for k, w := range strings.Split(pt.Winners, ",") {
  204. winnerTotal = append(winnerTotal, &entity.TopListS{
  205. Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string),
  206. Name: w,
  207. Value: value,
  208. Prop: Formula(value, total),
  209. })
  210. }
  211. }
  212. return winnerTotal
  213. }
  214. // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位
  215. func GetWinnerInfoOfBuyerCount(pt *entity.ProjectInfo) []*entity.Winnertop3Number {
  216. var winnertop3Number []*entity.Winnertop3Number
  217. if pt.Winners != "" {
  218. for k, w := range strings.Split(pt.Winners, ",") {
  219. winnertop3Number = append(winnertop3Number, &entity.Winnertop3Number{
  220. Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string),
  221. Name: w,
  222. Number: 1,
  223. })
  224. }
  225. }
  226. return winnertop3Number
  227. }
  228. // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位
  229. func GetWinnerInfoOfBuyerAmount(pt *entity.ProjectInfo) []*entity.Winnertop3Amount {
  230. var winnertop3Amount []*entity.Winnertop3Amount
  231. if pt.Winners != "" {
  232. for k, w := range strings.Split(pt.Winners, ",") {
  233. winnertop3Amount = append(winnertop3Amount, &entity.Winnertop3Amount{
  234. Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string),
  235. Name: w,
  236. Amount: pt.Sortprice,
  237. })
  238. }
  239. }
  240. return winnertop3Amount
  241. }
  242. // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位
  243. func GetWinnerInfoOfWinnerCount(pt *entity.ProjectInfo) []*entity.Buyertop3Number {
  244. var buyertop3Number []*entity.Buyertop3Number
  245. if pt.Buyer != "" {
  246. buyertop3Number = append(buyertop3Number, &entity.Buyertop3Number{
  247. Name: pt.Buyer,
  248. Number: 1,
  249. })
  250. }
  251. return buyertop3Number
  252. }
  253. // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位
  254. func GetWinnerInfoOfWinnerAmount(pt *entity.ProjectInfo) []*entity.Buyertop3Amount {
  255. var buyertop3Amount []*entity.Buyertop3Amount
  256. if pt.Buyer != "" {
  257. buyertop3Amount = append(buyertop3Amount, &entity.Buyertop3Amount{
  258. Name: pt.Buyer,
  259. Amount: pt.Sortprice,
  260. })
  261. }
  262. return buyertop3Amount
  263. }
  264. func GetJudgmentPrevKey(first int, next string) (key string) {
  265. return fmt.Sprintf("%d-%s", first, next)
  266. }