userTask.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package utility
  2. import (
  3. "analyze/internal/consts"
  4. "analyze/internal/model"
  5. "analyze/internal/model/entity"
  6. "app.yhyue.com/moapp/jybase/common"
  7. "app.yhyue.com/moapp/jybase/encrypt"
  8. "fmt"
  9. "github.com/gogf/gf/v2/database/gredis"
  10. "github.com/gogf/gf/v2/frame/g"
  11. "math"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. var (
  17. 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}
  18. )
  19. func GetPreviousMarket(sTime, eTime time.Time) int64 {
  20. var os_time int64
  21. s_time := sTime
  22. if SEMonth(sTime, eTime) {
  23. var min int
  24. //统计月份
  25. for sTime.Before(eTime) {
  26. sTime = sTime.AddDate(0, 1, 0)
  27. min++
  28. }
  29. os_time = s_time.AddDate(0, -min, 0).Unix()
  30. } else {
  31. os_time = s_time.AddDate(0, 0, -int(math.Ceil(eTime.Sub(sTime).Hours()/24))).Unix()
  32. }
  33. return os_time
  34. }
  35. // 判断是否月初到月末
  36. func SEMonth(sTime, eTime time.Time) bool {
  37. var day int
  38. month := int(eTime.Month())
  39. if month == 2 {
  40. if eTime.Year()%4 == 0 {
  41. day = 29
  42. } else {
  43. day = 28
  44. }
  45. } else {
  46. day = y_m_day[month]
  47. }
  48. if sTime.Day() == 1 && eTime.Day() == day {
  49. return true
  50. }
  51. return false
  52. }
  53. func GetMonthData(sTime, eTime time.Time) (_b bool, dateSpans []entity.DateSpan) {
  54. //整月多取一个月进行环比
  55. if SEMonth(sTime, eTime) {
  56. _b = true
  57. sTime = sTime.AddDate(0, -1, 0)
  58. }
  59. dateSpans = getBidAmountStatistics(sTime, eTime)
  60. return
  61. }
  62. func getBidAmountStatistics(sTime, eTime time.Time) (dateSpans []entity.DateSpan) {
  63. tmpTime, rTime, tEndTime := sTime, sTime, getMonthRange(eTime, false)
  64. for rTime.Before(tEndTime) {
  65. ts, te := getMonthRange(tmpTime, true), getMonthRange(tmpTime, false)
  66. if sTime == tmpTime {
  67. ts = sTime
  68. }
  69. if te == tEndTime {
  70. te = eTime
  71. }
  72. if ts.Before(te) {
  73. dateSpans = append(dateSpans, entity.DateSpan{
  74. Key: fmt.Sprintf("%d-%d", ts.Year(), ts.Month()),
  75. From: ts.Unix(),
  76. To: te.Unix(),
  77. })
  78. }
  79. rTime = rTime.AddDate(0, 1, 0)
  80. if int(rTime.Month())-int(tmpTime.Month()) > 1 {
  81. rTime = rTime.AddDate(0, -1, 0)
  82. }
  83. tmpTime = rTime
  84. }
  85. return
  86. }
  87. // getMonthRange获取月份范围
  88. // isStart true本月月初 false 本月月末(下月月初)
  89. func getMonthRange(t time.Time, isStart bool) time.Time {
  90. if isStart {
  91. return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
  92. }
  93. return time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, t.Location())
  94. }
  95. func GetYearData(sTime, eTime time.Time) (_b bool, dateSpans []entity.DateSpan) {
  96. //整年多取一年进行环比
  97. if sTime.Month() == 1 && sTime.Day() == 1 && eTime.Month() == 12 && eTime.Day() == 31 {
  98. _b = true
  99. sTime = sTime.AddDate(-1, 0, 0)
  100. }
  101. dateSpans = getCommonYearStatistics(sTime, eTime)
  102. return
  103. }
  104. // 年份统计
  105. func getCommonYearStatistics(sTime, eTime time.Time) (dateSpans []entity.DateSpan) {
  106. tmpTime, rTime, tEndTime := sTime, sTime, getYearRange(eTime, false)
  107. for rTime.Before(tEndTime) {
  108. ts, te := getYearRange(tmpTime, true), getYearRange(tmpTime, false)
  109. if sTime == tmpTime {
  110. ts = sTime
  111. }
  112. if te == tEndTime {
  113. te = eTime
  114. }
  115. if ts.Before(te) {
  116. dateSpans = append(dateSpans, entity.DateSpan{
  117. Key: fmt.Sprintf("%d", ts.Year()),
  118. From: ts.Unix(),
  119. To: te.Unix(),
  120. })
  121. }
  122. rTime = rTime.AddDate(1, 0, 0)
  123. tmpTime = rTime
  124. }
  125. return
  126. }
  127. // getYearRange获取月份范围
  128. // isStart true本月月初 false 本月月末(下月月初)
  129. func getYearRange(t time.Time, isStart bool) time.Time {
  130. if isStart {
  131. return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
  132. }
  133. return time.Date(t.Year()+1, 1, 1, 0, 0, 0, 0, t.Location())
  134. }
  135. // GetAllKeywordArr 获取所有匹配词
  136. func GetAllKeywordArr(res []entity.KeyWordGroup) (rData []entity.ViewKeyWord) {
  137. for _, kwg := range res {
  138. rData = append(rData, GetGroupKeywordArr(kwg.A_Key)...)
  139. }
  140. return
  141. }
  142. // GetGroupKeywordArr 模糊拆分为多个精准匹配
  143. func GetGroupKeywordArr(res []entity.ViewKeyWord) (rData []entity.ViewKeyWord) {
  144. for _, kw := range res {
  145. if kw.MatchWay == 1 {
  146. for _, kk := range kw.Keyword {
  147. rData = append(rData, entity.ViewKeyWord{
  148. Keyword: []string{kk},
  149. Exclude: kw.Exclude,
  150. })
  151. }
  152. for _, kk := range kw.Appended {
  153. rData = append(rData, entity.ViewKeyWord{
  154. Keyword: []string{kk},
  155. Exclude: kw.Exclude,
  156. })
  157. }
  158. } else {
  159. rData = append(rData, kw)
  160. }
  161. }
  162. return
  163. }
  164. func DateIsIn(from, to, n, m int64) bool {
  165. if m == 0 {
  166. return to >= n && from <= n
  167. }
  168. return to > n && from < n
  169. }
  170. func EncodeId(sid string) string {
  171. if sid == "" || sid == "-" { //不存在的id为-
  172. return ""
  173. }
  174. return encrypt.EncodeArticleId2ByCheck(sid)
  175. }
  176. // xxxx重点中标单位 更新分数值
  177. func UpdateWinnerInfoOfScore(key string, pt *entity.ProjectInfo, score float64) {
  178. if pt.Winners != "" {
  179. for k, w := range strings.Split(pt.Winners, ",") {
  180. id := common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string)
  181. UpdateIncrementRedis(key, fmt.Sprintf("%s##%s", w, id), score)
  182. }
  183. }
  184. }
  185. // xxxx重点采购单位 更新分数值
  186. func UpdateBuyerInfoOfScore(key string, pt *entity.ProjectInfo, score float64) {
  187. if pt.Buyer != "" {
  188. UpdateIncrementRedis(key, fmt.Sprintf("%s##%s", pt.Buyer, pt.Id), score)
  189. }
  190. }
  191. // 项目金额---TOP3地区的重点中标单位
  192. func SetWinnerInfoOfAmount(key string, pt *entity.ProjectInfo) {
  193. if pt.Winners != "" {
  194. for k, w := range strings.Split(pt.Winners, ",") {
  195. id := common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string)
  196. UpdateIncrementRedis(key, fmt.Sprintf("%s##%s", w, id), pt.Sortprice)
  197. }
  198. }
  199. }
  200. // 项目数量---TOP3地区的重点中标单位
  201. func SetWinnerInfoOfCount(key string, pt *entity.ProjectInfo) {
  202. if pt.Winners != "" {
  203. for k, w := range strings.Split(pt.Winners, ",") {
  204. id := common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string)
  205. UpdateIncrementRedis(key, fmt.Sprintf("%s##%s", w, id), 1)
  206. }
  207. }
  208. }
  209. // 细化市场重点中标单位-项目数量|金额
  210. func GetWinnerInfoTopList(pt *entity.ProjectInfo, value, total float64) []*entity.TopListS {
  211. var winnerTotal []*entity.TopListS
  212. if pt.Winners != "" {
  213. for k, w := range strings.Split(pt.Winners, ",") {
  214. winnerTotal = append(winnerTotal, &entity.TopListS{
  215. Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string),
  216. Name: w,
  217. Value: value,
  218. Prop: Formula(value, total),
  219. })
  220. }
  221. }
  222. return winnerTotal
  223. }
  224. // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位
  225. func GetWinnerInfoOfBuyerCount(pt *entity.ProjectInfo) []*entity.Winnertop3Number {
  226. var winnertop3Number []*entity.Winnertop3Number
  227. if pt.Winners != "" {
  228. for k, w := range strings.Split(pt.Winners, ",") {
  229. winnertop3Number = append(winnertop3Number, &entity.Winnertop3Number{
  230. Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string),
  231. Name: w,
  232. Number: 1,
  233. })
  234. }
  235. }
  236. return winnertop3Number
  237. }
  238. // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位
  239. func GetWinnerInfoOfBuyerAmount(pt *entity.ProjectInfo) []*entity.Winnertop3Amount {
  240. var winnertop3Amount []*entity.Winnertop3Amount
  241. if pt.Winners != "" {
  242. for k, w := range strings.Split(pt.Winners, ",") {
  243. winnertop3Amount = append(winnertop3Amount, &entity.Winnertop3Amount{
  244. Id: common.If(len(pt.Entidlist) > k, encrypt.EncodeArticleId2ByCheck(pt.Entidlist[k]), "").(string),
  245. Name: w,
  246. Amount: pt.Sortprice,
  247. })
  248. }
  249. }
  250. return winnertop3Amount
  251. }
  252. // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位
  253. func GetWinnerInfoOfWinnerCount(pt *entity.ProjectInfo) []*entity.Buyertop3Number {
  254. var buyertop3Number []*entity.Buyertop3Number
  255. if pt.Buyer != "" {
  256. buyertop3Number = append(buyertop3Number, &entity.Buyertop3Number{
  257. Name: pt.Buyer,
  258. Number: 1,
  259. })
  260. }
  261. return buyertop3Number
  262. }
  263. // 市场-采购单位&&中标企业---采购金额TOP30采购单位及其重点合作中标单位
  264. func GetWinnerInfoOfWinnerAmount(pt *entity.ProjectInfo) []*entity.Buyertop3Amount {
  265. var buyertop3Amount []*entity.Buyertop3Amount
  266. if pt.Buyer != "" {
  267. buyertop3Amount = append(buyertop3Amount, &entity.Buyertop3Amount{
  268. Name: pt.Buyer,
  269. Amount: pt.Sortprice,
  270. })
  271. }
  272. return buyertop3Amount
  273. }
  274. func GetJudgmentPrevKey(first int, next string) (key string) {
  275. return fmt.Sprintf("mk_%d_%s", first, next)
  276. }
  277. // redis update|| expire
  278. func UpdateIncrementRedis(key, member string, increment float64) {
  279. _, err := g.Redis().ZIncrBy(model.Ctx, key, increment, member)
  280. if err != nil {
  281. g.Log().Info(model.Ctx, "---update---err:", err.Error())
  282. } else {
  283. _, err = g.Redis().Expire(model.Ctx, key, consts.Top30) //缓存三十秒
  284. if err != nil {
  285. g.Log().Info(model.Ctx, "---expire---err:", err.Error())
  286. }
  287. }
  288. }
  289. // -- score
  290. func GetZrevRangeByScore(key string, max, min int64) (res []entity.MemberScore, err error) {
  291. gvar, _err := g.Redis().ZRevRange(model.Ctx, key, min, max, gredis.ZRevRangeOption{WithScores: true})
  292. if _err != nil {
  293. err = _err
  294. g.Log().Info(model.Ctx, "err:", err.Error())
  295. return
  296. }
  297. // 遍历返回结果
  298. for k, v := range gvar.Slice() {
  299. if k%2 == 0 {
  300. ms := entity.MemberScore{}
  301. ms.Member = strings.Split(fmt.Sprintf("%s", v), "##")[0]
  302. ms.Id = strings.Split(fmt.Sprintf("%s", v), "##")[1]
  303. res = append(res, ms)
  304. } else {
  305. res[k/2].Score, _ = strconv.ParseFloat(fmt.Sprintf("%s", v), 64)
  306. }
  307. }
  308. return
  309. }
  310. // --Del
  311. func DelRedisInfo(key string) {
  312. if _, err := g.Redis().Del(model.Ctx, key); err != nil {
  313. g.Log().Info(model.Ctx, "redis del err :", err.Error())
  314. }
  315. }
  316. // --key
  317. func GetSortedKeyOfRedis(prev int, key, id, suffix string) string {
  318. return fmt.Sprintf("sorted_%d_%s_%s_%s", prev, key, id, suffix)
  319. }