userTask.go 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. package internal
  2. import (
  3. "analyze/internal/consts"
  4. "analyze/internal/model"
  5. "analyze/internal/model/do"
  6. "analyze/internal/model/entity"
  7. "analyze/utility"
  8. "app.yhyue.com/moapp/jybase/common"
  9. "bp.jydev.jianyu360.cn/BaseService/pushpkg/dfa"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/gogf/gf/v2/frame/g"
  13. "github.com/gogf/gf/v2/os/gctx"
  14. "sort"
  15. "strings"
  16. "sync"
  17. "sync/atomic"
  18. "time"
  19. )
  20. // user 信息
  21. type UserTask struct {
  22. MgoUserId string //用户id
  23. PositionId int64 //职位id
  24. Phone string //手机号
  25. StartTime int64 //开始时间
  26. EndTime int64 //结束时间
  27. IsEnd bool //是否结束
  28. IsCancel bool //是否已取消-----------------待处理
  29. IsFalse bool //是否离线分析成功
  30. IsRatio bool //是否需要进行环比
  31. PInfo chan *entity.ProjectInfo //项目信息
  32. RId string //市场分析报告id
  33. AnalysisParam entity.AnalyzeParameters //市场分析报告参数
  34. FormatParam entity.AnalysisRequestFormat //格式化
  35. MatchingMode string //匹配方式 title:标题 content:项目名称/标的物
  36. Started int32 // 标志字段,表示任务是否已经启动
  37. Ticker *time.Ticker //
  38. Result *CalculateResult //结果集
  39. MonthDistributionKey []entity.DateSpan //月度分配key
  40. YearDistributionKey []entity.DateSpan //年度分配key
  41. JudgmentMap map[string]bool //地区分布|城市分布|客户 项目规模 地区top3|订阅分类|一个就行
  42. CalculateWG sync.WaitGroup
  43. }
  44. type CalculateResult struct {
  45. Scale entity.MarketScaleMain // 市场概况+时间分布
  46. TopProject entity.MarketTopProject // 项目规模TOP10 集合
  47. ProjectAllData entity.MarketProjectAllData /* 项目规模 地区分布 客户分布 地区客户top3*/
  48. ScaleRefine entity.MarketScaleRefine //细化市场
  49. BuyerAndWinner entity.MarketBuyerAndWinner /* 市场-采购单位&&中标企业*/
  50. }
  51. func NewUserInfo() *UserTask {
  52. return &UserTask{
  53. PInfo: make(chan *entity.ProjectInfo, 2000),
  54. AnalysisParam: entity.AnalyzeParameters{},
  55. Ticker: time.NewTicker(time.Duration(10000) * time.Second),
  56. Result: &CalculateResult{},
  57. JudgmentMap: make(map[string]bool),
  58. CalculateWG: sync.WaitGroup{},
  59. Started: 0, //重置
  60. }
  61. }
  62. // 推送项目信息到用户任务的通道
  63. func (ut *UserTask) Push(info *entity.ProjectInfo) {
  64. ut.PInfo <- info
  65. }
  66. // 开始用户任务
  67. func (ut *UserTask) Start() {
  68. if atomic.CompareAndSwapInt32(&ut.Started, 0, 1) {
  69. g.Log().Info(gctx.New(), ut.MgoUserId, "----", ut.AnalysisParam)
  70. go ut.Run()
  71. }
  72. }
  73. func (ut *UserTask) Run() {
  74. for {
  75. if ut.IsEnd {
  76. break
  77. }
  78. select {
  79. case info := <-ut.PInfo:
  80. //项目信息匹配
  81. if ut.MatchConditions(info) {
  82. //模块计算
  83. ut.Calculate(info)
  84. //数据整理
  85. ut.DataCollation()
  86. }
  87. case <-ut.Ticker.C:
  88. ut.ResultSave()
  89. ut.SendMsg()
  90. ut.IsEnd = true
  91. break
  92. }
  93. }
  94. ut.Reset()
  95. //结束后的处理
  96. }
  97. // MatchConditions 匹配项目名称、行业、地区等条件
  98. func (ut *UserTask) MatchConditions(p *entity.ProjectInfo) (b bool) {
  99. //area 和city 满足任何一个
  100. if !ut.AreaMatch(p.Area, p.City) {
  101. return
  102. }
  103. //行业
  104. if !ut.IndustryMatch(p.Industry) {
  105. return
  106. }
  107. //采购单位类型
  108. if !ut.BuyClassMatch(p.BuyerClass) {
  109. return
  110. }
  111. //关键词 附加词 满足一个
  112. //排除词 满足
  113. title := []string{p.ProjectName}
  114. if ut.MatchingMode == "" {
  115. title = append(title, p.Purchasing)
  116. }
  117. if !ut.KeysMatch(strings.Join(title, ",")) {
  118. return
  119. }
  120. return true
  121. }
  122. // BuyClassMatch 采购单位类型
  123. func (ut *UserTask) BuyClassMatch(buyerclass string) (b bool) {
  124. if len(ut.FormatParam.BuyerClass) == 0 {
  125. b = true
  126. return
  127. }
  128. if buyerclass == "" {
  129. return
  130. }
  131. if strings.Contains(strings.Join(ut.FormatParam.BuyerClass, ","), buyerclass) {
  132. b = true
  133. }
  134. return
  135. }
  136. // AreaMatch 省份和城市
  137. func (ut *UserTask) AreaMatch(area, city string) (b bool) {
  138. //没有地区分析条件
  139. if len(ut.FormatParam.Area) == 0 && len(ut.FormatParam.City) == 0 {
  140. b = true
  141. return
  142. }
  143. //项目信息没有地区字段
  144. if area == "" && city == "" {
  145. return
  146. }
  147. if strings.Contains(strings.Join(ut.FormatParam.Area, ","), area) || strings.Contains(strings.Join(ut.FormatParam.City, ","), city) {
  148. b = true
  149. return
  150. }
  151. return
  152. }
  153. // IndustryMatch 行业
  154. func (ut *UserTask) IndustryMatch(industry string) (b bool) {
  155. if len(ut.FormatParam.Industry) == 0 {
  156. b = true
  157. return
  158. }
  159. if industry == "" {
  160. return
  161. }
  162. for _, it := range strings.Split(industry, ",") {
  163. if strings.Contains(strings.Join(ut.FormatParam.Industry, ","), it) {
  164. b = true
  165. break
  166. }
  167. }
  168. return
  169. }
  170. // 关键词匹配--分类
  171. func (ut *UserTask) KeysMatchByKeysItems(matchObject string, keysItems entity.KeyWordGroup) (b bool) {
  172. var (
  173. dfaKey = dfa.DFA{}
  174. )
  175. //purchasing|projectname
  176. //订阅词分组
  177. for _, viewKeyWord := range utility.GetGroupKeywordArr(keysItems.A_Key) {
  178. var (
  179. keys = viewKeyWord.Keyword
  180. )
  181. //关键词 和 附加词
  182. if len(viewKeyWord.Appended) > 0 {
  183. keys = append(keys, viewKeyWord.Appended...)
  184. }
  185. dfaKey.AddWord(common.If(len(keys) > 0, strings.Join(keys, "+"), strings.Join(keys, "")).(string))
  186. //关键词 附加词
  187. if rk := dfaKey.Analy(matchObject); len(rk) != 0 {
  188. b = true
  189. break
  190. }
  191. }
  192. return
  193. }
  194. // KeysMatch 关键词
  195. func (ut *UserTask) KeysMatch(matchObject string) (b bool) {
  196. if len(ut.FormatParam.KeysItems) == 0 {
  197. b = true
  198. return
  199. }
  200. var (
  201. dfaKey = dfa.DFA{}
  202. dfaExclude = dfa.DFA{}
  203. )
  204. //purchasing|projectname
  205. //订阅词分组
  206. for _, viewKeyWord := range utility.GetAllKeywordArr(ut.FormatParam.KeysItems) {
  207. //排除词
  208. dfaExclude.AddWord(common.If(len(viewKeyWord.Exclude) > 0, strings.Join(viewKeyWord.Exclude, "+"), strings.Join(viewKeyWord.Exclude, "")).(string))
  209. if re := dfaExclude.Analy(matchObject); len(re) != 0 {
  210. b = false
  211. continue
  212. }
  213. var (
  214. keys = viewKeyWord.Keyword
  215. )
  216. //关键词 和 附加词
  217. if len(viewKeyWord.Appended) > 0 {
  218. keys = append(keys, viewKeyWord.Appended...)
  219. }
  220. dfaKey.AddWord(common.If(len(keys) > 0, strings.Join(keys, "+"), strings.Join(keys, "")).(string))
  221. //关键词 附加词
  222. if rk := dfaKey.Analy(matchObject); len(rk) != 0 {
  223. b = true
  224. break
  225. }
  226. }
  227. return
  228. }
  229. // Calculate 模块计算逻辑
  230. func (ut *UserTask) Calculate(pt *entity.ProjectInfo) {
  231. //市场概况+时间分布
  232. scale := ut.Result.Scale
  233. var scaleFunc = func() {
  234. //项目信息结果时间在原始查询条件范围内
  235. if utility.DateIsIn(ut.StartTime, ut.EndTime, pt.JgTime, 0) {
  236. if pt.Buyer != "" {
  237. scale.MarketProfile.Buyercount++
  238. }
  239. if pt.Winners != "" {
  240. scale.MarketProfile.Winnercount += len(strings.Split(pt.Winners, ","))
  241. }
  242. scale.MarketProfile.ProjectCount++
  243. scale.MarketProfile.Projctamout += pt.Sortprice
  244. //月度分配
  245. if len(ut.MonthDistributionKey) > 0 {
  246. if monthKey := ut.GetMonthDistributionKey(pt.JgTime); monthKey != "" {
  247. monthKeyPrev := utility.GetJudgmentPrevKey(consts.JudgmentPrev11, monthKey)
  248. if !ut.JudgmentMap[monthKeyPrev] {
  249. ut.JudgmentMap[monthKeyPrev] = true
  250. //金额
  251. scale.MonthDistribution.ProjectAmount = append(scale.MonthDistribution.ProjectAmount, &entity.Distribution{
  252. Minth: monthKey,
  253. Value: pt.Sortprice,
  254. })
  255. //项目数量
  256. scale.MonthDistribution.ProjectCount = append(scale.MonthDistribution.ProjectCount, &entity.Distribution{
  257. Minth: monthKey,
  258. Value: 1,
  259. })
  260. } else {
  261. //金额
  262. for _, mdpa := range scale.MonthDistribution.ProjectAmount {
  263. if mdpa.Minth == monthKey {
  264. mdpa.Value += pt.Sortprice
  265. break
  266. }
  267. }
  268. //项目数量
  269. for _, mdpc := range scale.MonthDistribution.ProjectCount {
  270. if mdpc.Minth == monthKey {
  271. mdpc.Value++
  272. }
  273. }
  274. }
  275. }
  276. }
  277. //年度分配
  278. if len(ut.YearDistributionKey) > 0 {
  279. if yearKey := ut.GetYearDistributionKey(pt.JgTime); yearKey != "" {
  280. yearKeyPrev := utility.GetJudgmentPrevKey(consts.JudgmentPrev11, yearKey)
  281. if !ut.JudgmentMap[yearKeyPrev] {
  282. ut.JudgmentMap[yearKeyPrev] = true
  283. //金额
  284. scale.YearDistribution.ProjectAmount = append(scale.YearDistribution.ProjectAmount, &entity.Distribution{
  285. Minth: yearKey,
  286. Value: pt.Sortprice,
  287. })
  288. //项目数量
  289. scale.YearDistribution.ProjectCount = append(scale.YearDistribution.ProjectCount, &entity.Distribution{
  290. Minth: yearKey,
  291. Value: 1,
  292. })
  293. } else {
  294. //金额
  295. for _, mdpa := range scale.YearDistribution.ProjectAmount {
  296. if mdpa.Minth == yearKey {
  297. mdpa.Value += pt.Sortprice
  298. break
  299. }
  300. }
  301. //项目数量
  302. for _, mdpc := range scale.YearDistribution.ProjectCount {
  303. if mdpc.Minth == yearKey {
  304. mdpc.Value++
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. //是否需要进行环比
  312. if ut.IsRatio {
  313. scale.ProjectInfoAll.ProjectCount++
  314. scale.ProjectInfoAll.Projctamout += pt.Sortprice
  315. if pt.Buyer != "" {
  316. scale.ProjectInfoAll.Buyercount++
  317. }
  318. if pt.Winners != "" {
  319. scale.ProjectInfoAll.Winnercount += len(strings.Split(pt.Winners, ","))
  320. }
  321. }
  322. }
  323. //项目规模TOP10 集合
  324. topProject := ut.Result.TopProject
  325. var topProjectFunc = func() {
  326. defer func() {
  327. ut.CalculateWG.Done()
  328. }()
  329. var (
  330. entIdList = pt.Entidlist
  331. )
  332. if len(pt.Entidlist) > 0 {
  333. //加密
  334. for _, entId := range pt.Entidlist {
  335. entIdList = append(entIdList, utility.EncodeId(entId))
  336. }
  337. }
  338. topProject.ProjectTop10 = append(topProject.ProjectTop10, &entity.ProjectTop{
  339. Id: common.If(pt.SourceInfoId != "", utility.EncodeId(pt.SourceInfoId), "").(string),
  340. Area: pt.Area,
  341. City: pt.City,
  342. Eidlist: entIdList,
  343. Jgtime: pt.JgTime,
  344. Projectname: pt.ProjectName,
  345. Sortprice: pt.Sortprice,
  346. WinnerS: strings.Split(pt.Winners, ","),
  347. })
  348. /*---------------B--------*/
  349. if len(topProject.ProjectTop10) > 1 {
  350. sort.Slice(topProject.ProjectTop10, func(i, j int) bool {
  351. return topProject.ProjectTop10[i].Sortprice > topProject.ProjectTop10[j].Sortprice // 根据 price 字段进行倒序排序
  352. })
  353. if len(topProject.ProjectTop10) > consts.Top10 {
  354. topProject.ProjectTop10 = topProject.ProjectTop10[:consts.Top10]
  355. }
  356. }
  357. }
  358. ///* 项目规模 地区分布 客户分布 地区客户top3*/
  359. pad := ut.Result.ProjectAllData
  360. var projectAllFunc = func() {
  361. defer func() {
  362. ut.CalculateWG.Done()
  363. }()
  364. //地区分布
  365. var areaInfoFunc = func() {
  366. areaInfoKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev0, pt.Area)
  367. cityInfoKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev1, fmt.Sprintf("%s_%s", pt.Area, pt.City))
  368. if !ut.JudgmentMap[areaInfoKey] {
  369. ut.JudgmentMap[areaInfoKey] = true
  370. ut.JudgmentMap[cityInfoKey] = true
  371. pad.AreaInfos = append(pad.AreaInfos, &entity.AreaInfosS{
  372. Amount: pt.Sortprice,
  373. Area: pt.Area,
  374. AreaDetails: []*entity.AreaDetailsS{
  375. {
  376. Amount: pt.Sortprice,
  377. City: pt.City,
  378. Total: 1,
  379. },
  380. },
  381. Total: 1,
  382. })
  383. } else {
  384. var (
  385. wc = make(chan bool, common.If(len(pad.AreaInfos) > 10, consts.JudgmentPrev3, 1).(int))
  386. wg = &sync.WaitGroup{}
  387. stopLoop = false
  388. )
  389. for _, ai := range pad.AreaInfos {
  390. if stopLoop {
  391. break
  392. }
  393. wc <- true
  394. wg.Add(1)
  395. go func(ai *entity.AreaInfosS) {
  396. defer func() {
  397. wg.Done()
  398. <-wc
  399. }()
  400. if ai.Area == pt.Area {
  401. stopLoop = true
  402. if !ut.JudgmentMap[cityInfoKey] {
  403. ut.JudgmentMap[cityInfoKey] = true
  404. ai.AreaDetails = append(ai.AreaDetails, &entity.AreaDetailsS{
  405. Amount: pt.Sortprice,
  406. City: pt.City,
  407. Total: 1,
  408. })
  409. } else {
  410. for _, ci := range ai.AreaDetails {
  411. if ci.City == pt.City {
  412. ci.Amount += pt.Sortprice
  413. ci.Total++
  414. break
  415. }
  416. }
  417. }
  418. return
  419. }
  420. }(ai)
  421. }
  422. wg.Wait()
  423. }
  424. }
  425. areaInfoFunc()
  426. //客户分布
  427. var customerScaleFunc = func() {
  428. bc := pt.BuyerClass
  429. //客户分布
  430. if bc != "" {
  431. customerKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev2, bc)
  432. if bc == "其它" {
  433. //客户分布(其它)
  434. if !ut.JudgmentMap[customerKey] {
  435. ut.JudgmentMap[customerKey] = true
  436. //客户分布 其它
  437. pad.CustomerScaleOther = &entity.CustomerScaleS{
  438. Amount: pt.Sortprice,
  439. Buyclass: bc,
  440. Total: 1,
  441. }
  442. } else {
  443. pad.CustomerScaleOther.Amount += pt.Sortprice
  444. pad.CustomerScaleOther.Total++
  445. }
  446. } else {
  447. //客户分布
  448. if !ut.JudgmentMap[customerKey] {
  449. ut.JudgmentMap[customerKey] = true
  450. pad.CustomerScale = append(pad.CustomerScale, &entity.CustomerScaleS{
  451. Amount: pt.Sortprice,
  452. Buyclass: bc,
  453. Total: 1,
  454. })
  455. } else {
  456. var (
  457. wc = make(chan bool, common.If(len(pad.CustomerScale) > 10, consts.JudgmentPrev3, 1).(int))
  458. wg = &sync.WaitGroup{}
  459. stopLoop = false
  460. )
  461. for _, csm := range pad.CustomerScale {
  462. if stopLoop {
  463. break
  464. }
  465. wc <- true
  466. wg.Add(1)
  467. go func(csm *entity.CustomerScaleS) {
  468. defer func() {
  469. wg.Done()
  470. <-wc
  471. }()
  472. if csm.Buyclass == bc {
  473. stopLoop = true
  474. csm.Amount += pt.Sortprice
  475. csm.Total++
  476. return
  477. }
  478. }(csm)
  479. }
  480. wg.Wait()
  481. }
  482. }
  483. //TOP3
  484. //-------------redis----sorted---- 中标企业--score---项目数量TOP3客户类型的重点中标单位
  485. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sbct", ut.MgoUserId, bc), pt, 1)
  486. //-------------redis----sorted---- 中标企业--score---项目金额TOP3客户类型的重点中标单位
  487. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sbat", ut.MgoUserId, bc), pt, pt.Sortprice)
  488. top3Key := utility.GetJudgmentPrevKey(consts.JudgmentPrev3, fmt.Sprintf("%s_top3", bc))
  489. if !ut.JudgmentMap[top3Key] {
  490. ut.JudgmentMap[top3Key] = true
  491. //项目数量TOP3客户类型的重点中标单位
  492. pad.ScaleBuyclassCountTop = append(pad.ScaleBuyclassCountTop, &entity.ScaleBuyclassCountTopS{
  493. BuyclassCount: 1,
  494. Name: bc,
  495. })
  496. //项目金额TOP3客户类型的重点中标单位
  497. pad.ScaleBuyclassAmountTop = append(pad.ScaleBuyclassAmountTop, &entity.ScaleBuyclassAmountTopS{
  498. BuyclassAmount: pt.Sortprice,
  499. Name: bc,
  500. })
  501. } else {
  502. //项目数量TOP3客户类型的重点中标单位
  503. for _, sbct := range pad.ScaleBuyclassCountTop {
  504. if sbct.Name == bc {
  505. sbct.BuyclassCount++
  506. break
  507. }
  508. }
  509. //项目金额TOP3客户类型的重点中标单位
  510. for _, sbat := range pad.ScaleBuyclassAmountTop {
  511. if sbat.Name == bc {
  512. sbat.BuyclassAmount += pt.Sortprice
  513. break
  514. }
  515. }
  516. }
  517. }
  518. }
  519. if pt.Sortprice > 0 {
  520. customerScaleFunc()
  521. }
  522. //项目规模分布
  523. var ProjectScaleFunc = func() {
  524. var psk = ""
  525. for _, cp := range consts.ProjectScale {
  526. if cp.From < pt.Sortprice && common.If(cp.To > 0, cp.To > pt.Sortprice, true).(bool) {
  527. psk = cp.Key
  528. }
  529. }
  530. scaleKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev4, psk)
  531. if !ut.JudgmentMap[scaleKey] {
  532. ut.JudgmentMap[scaleKey] = true
  533. pad.ProjectScale = append(pad.ProjectScale, &entity.ProjectScaleS{
  534. Name: psk,
  535. Count: 1,
  536. Amount: pt.Sortprice,
  537. })
  538. } else {
  539. for _, ps := range pad.ProjectScale {
  540. if ps.Name == psk {
  541. ps.Count++
  542. ps.Amount += pt.Sortprice
  543. break
  544. }
  545. }
  546. }
  547. }
  548. ProjectScaleFunc()
  549. //项目金额TOP3地区的重点中标单位
  550. var scaleAreaAmountAndCountTopFunc = func() {
  551. //-------------redis----sorted---- 中标企业--score---项目金额---TOP3地区的重点中标单位
  552. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev5, "saat", ut.MgoUserId, pt.Area), pt, pt.Sortprice)
  553. //-------------redis----sorted---- 中标企业--score---项目数量---TOP3地区的重点中标单位
  554. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev5, "sact", ut.MgoUserId, pt.Area), pt, 1)
  555. saaactKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev5, pt.Area)
  556. if !ut.JudgmentMap[saaactKey] {
  557. ut.JudgmentMap[saaactKey] = true
  558. //项目金额---TOP3地区的重点中标单位
  559. pad.ScaleAreaAmountTop = append(pad.ScaleAreaAmountTop, &entity.ScaleAreaAmountTopS{
  560. AreaAmount: pt.Sortprice,
  561. Name: pt.Area,
  562. })
  563. //项目数量---TOP3地区的重点中标单位
  564. pad.ScaleAreaCountTop = append(pad.ScaleAreaCountTop, &entity.ScaleAreaCountTopS{
  565. AreaCount: 1,
  566. Name: pt.Area,
  567. })
  568. } else {
  569. //项目金额---TOP3地区的重点中标单位
  570. for _, saat := range pad.ScaleAreaAmountTop {
  571. if saat.Name == pt.Area {
  572. saat.AreaAmount += pt.Sortprice
  573. break
  574. }
  575. }
  576. //项目数量---TOP3地区的重点中标单位
  577. for _, sact := range pad.ScaleAreaCountTop {
  578. if sact.Name == pt.Area {
  579. sact.AreaCount++
  580. break
  581. }
  582. }
  583. }
  584. }
  585. scaleAreaAmountAndCountTopFunc()
  586. //
  587. }
  588. //细化市场
  589. sr := ut.Result.ScaleRefine
  590. var scaleRefineFunc = func() {
  591. defer func() {
  592. ut.CalculateWG.Done()
  593. }()
  594. //订阅分类|重点中标单位-项目数量|重点中标单位-项目金额
  595. var scaleRefineAllAndAmountAndTotalFunc = func() {
  596. for _, keysItems := range ut.FormatParam.KeysItems {
  597. if ut.KeysMatchByKeysItems(pt.ProjectName, keysItems) {
  598. itemName := keysItems.ItemName
  599. itemNameKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev6, itemName)
  600. //-------------redis----sorted---- 中标企业--score---重点中标单位-项目数量
  601. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev6, "srtt", ut.MgoUserId, itemName), pt, 1)
  602. //-------------redis----sorted---- 中标企业--score---重点中标单位-金额
  603. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev6, "srat", ut.MgoUserId, itemName), pt, pt.Sortprice)
  604. if !ut.JudgmentMap[itemNameKey] {
  605. ut.JudgmentMap[itemNameKey] = true
  606. //订阅分类
  607. sr.ScaleRefineAll = append(sr.ScaleRefineAll, &entity.ScaleRefineAllS{
  608. Amount: pt.Sortprice,
  609. Name: itemName,
  610. Total: 1,
  611. })
  612. //重点中标单位-项目数量
  613. sr.ScaleRefineTotalTop = append(sr.ScaleRefineTotalTop, &entity.ScaleRefineTotalTopS{
  614. Name: itemName,
  615. Value: 1,
  616. })
  617. //重点中标单位-金额
  618. sr.ScaleRefineAmountTop = append(sr.ScaleRefineAmountTop, &entity.ScaleRefineAmountTopS{
  619. Name: itemName,
  620. Value: pt.Sortprice,
  621. })
  622. } else {
  623. //订阅分类 300+
  624. for _, sra := range sr.ScaleRefineAll {
  625. if sra.Name == itemName {
  626. sra.Total++
  627. sra.Amount += pt.Sortprice
  628. break
  629. }
  630. }
  631. //重点中标单位-项目数量
  632. for _, srtt := range sr.ScaleRefineTotalTop {
  633. if srtt.Name == itemName {
  634. srtt.Value++
  635. break
  636. }
  637. }
  638. //重点中标单位-金额
  639. for _, srat := range sr.ScaleRefineAmountTop {
  640. if srat.Name == itemName {
  641. srat.Value += pt.Sortprice
  642. break
  643. }
  644. }
  645. }
  646. }
  647. }
  648. }
  649. scaleRefineAllAndAmountAndTotalFunc()
  650. }
  651. ///* 市场-采购单位&&中标企业*/
  652. baw := ut.Result.BuyerAndWinner
  653. var buyerAndWinnerFunc = func() {
  654. defer func() {
  655. ut.CalculateWG.Done()
  656. }()
  657. //采购单位
  658. var buyerAmountAndTotalTop30 = func() {
  659. //-------------redis----sorted---- 采购单位--score---项目数量TOP30采购单位-项目数量
  660. utility.UpdateBuyerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bct", ut.MgoUserId, "top30"), pt, 1)
  661. //-------------redis----sorted---- 采购单位--score---采购金额TOP30采购单位-金额
  662. utility.UpdateBuyerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bat", ut.MgoUserId, "top30"), pt, pt.Sortprice)
  663. //-------------redis----sorted---- 采购单位--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
  664. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bct_winner", ut.MgoUserId, pt.Buyer), pt, 1)
  665. //-------------redis----sorted---- 采购单位--score---采购金额TOP30采购单位-下的前三个中标企业信息-金额
  666. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bat_winner", ut.MgoUserId, pt.Buyer), pt, pt.Sortprice)
  667. //采购规模分布 --7
  668. var psk = ""
  669. for _, cp := range consts.ProjectScale {
  670. if cp.From < pt.Sortprice && common.If(cp.To > 0, cp.To > pt.Sortprice, true).(bool) {
  671. psk = cp.Key
  672. }
  673. }
  674. scaleKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev8, psk)
  675. if !ut.JudgmentMap[scaleKey] {
  676. ut.JudgmentMap[scaleKey] = true
  677. baw.BuyerTimeDistribution = append(baw.BuyerTimeDistribution, &entity.BuyerAndWinnerTimeDistributionS{
  678. Key: psk,
  679. Amount: pt.Sortprice,
  680. Number: 1,
  681. })
  682. } else {
  683. for _, ps := range baw.BuyerTimeDistribution {
  684. if ps.Key == psk {
  685. ps.Number++
  686. ps.Amount += pt.Sortprice
  687. break
  688. }
  689. }
  690. }
  691. }
  692. if pt.Buyer != "" {
  693. buyerAmountAndTotalTop30()
  694. }
  695. //中标单位
  696. var winnerAmountAndTotalTop30 = func() {
  697. for _, winner := range strings.Split(pt.Winners, ",") {
  698. //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-项目数量
  699. utility.UpdateBuyerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wct", ut.MgoUserId, "top30"), pt, 1)
  700. //-------------redis----sorted---- 中标企业--score---采购金额TOP30采购单位-金额
  701. utility.UpdateBuyerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wat", ut.MgoUserId, "top30"), pt, pt.Sortprice)
  702. //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
  703. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wct_buyer", ut.MgoUserId, winner), pt, 1)
  704. //-------------redis----sorted---- 中标企业--score---采购金额TOP30采购单位-下的前三个中标企业信息-金额
  705. utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wat_buyer", ut.MgoUserId, winner), pt, pt.Sortprice)
  706. }
  707. //中标规模分布
  708. var psk = ""
  709. for _, cp := range consts.ProjectScale {
  710. if cp.From < pt.Sortprice && common.If(cp.To > 0, cp.To > pt.Sortprice, true).(bool) {
  711. psk = cp.Key
  712. }
  713. }
  714. scaleKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev10, psk)
  715. if !ut.JudgmentMap[scaleKey] {
  716. ut.JudgmentMap[scaleKey] = true
  717. baw.WinnerTimeDistribution = append(baw.WinnerTimeDistribution, &entity.BuyerAndWinnerTimeDistributionS{
  718. Key: psk,
  719. Amount: pt.Sortprice,
  720. Number: 1,
  721. })
  722. } else {
  723. for _, ps := range baw.WinnerTimeDistribution {
  724. if ps.Key == psk {
  725. ps.Number++
  726. ps.Amount += pt.Sortprice
  727. break
  728. }
  729. }
  730. }
  731. }
  732. if pt.Winners != "" {
  733. winnerAmountAndTotalTop30()
  734. }
  735. }
  736. //
  737. var nextFunc = func() {
  738. ut.CalculateWG.Add(4)
  739. go topProjectFunc()
  740. go projectAllFunc()
  741. go scaleRefineFunc()
  742. go buyerAndWinnerFunc()
  743. ut.CalculateWG.Wait()
  744. }
  745. scaleFunc()
  746. if utility.DateIsIn(ut.StartTime, ut.EndTime, pt.JgTime, 0) {
  747. nextFunc()
  748. }
  749. }
  750. // DataCollation 数据整理
  751. func (ut *UserTask) DataCollation() {
  752. //项目环比
  753. scale := ut.Result.Scale
  754. var ratioFunc = func() {
  755. //项目平均金额(万)
  756. scale.MarketProfile.Projectavgmoney = utility.Formula(scale.MarketProfile.Projctamout, float64(scale.MarketProfile.ProjectCount))
  757. //环比
  758. if ut.IsRatio {
  759. //项目平均金额(万)
  760. scale.ProjectInfoAll.Projectavgmoney = utility.Formula(scale.ProjectInfoAll.Projctamout, float64(scale.ProjectInfoAll.ProjectCount))
  761. //项目平均金额环比
  762. scale.MarketProfile.ProjectavgmoneyRatio = utility.Sequential(scale.MarketProfile.Projectavgmoney, scale.ProjectInfoAll.Projectavgmoney)
  763. //项目金额环比
  764. scale.MarketProfile.ProjctamountRatio = utility.Sequential(scale.MarketProfile.Projctamout, scale.ProjectInfoAll.Projctamout)
  765. //项目数量环比
  766. scale.MarketProfile.ProjectCountRatio = utility.Sequential(float64(scale.MarketProfile.ProjectCount), float64(scale.ProjectInfoAll.ProjectCount))
  767. //采购单位项目数量环比
  768. scale.MarketProfile.BuyercountRatio = utility.Sequential(float64(scale.MarketProfile.Buyercount), float64(scale.ProjectInfoAll.Buyercount))
  769. //中标企业项目数量环比
  770. scale.MarketProfile.WinnercountRatio = utility.Sequential(float64(scale.MarketProfile.Winnercount), float64(scale.ProjectInfoAll.Winnercount))
  771. //月度分配
  772. var monthRatioFunc = func() {
  773. //金额
  774. for k, v := range scale.MonthDistribution.ProjectAmount {
  775. if k == 0 {
  776. continue
  777. }
  778. v.Ratio = utility.Sequential(v.Value, scale.MonthDistribution.ProjectAmount[k-1].Value)
  779. }
  780. //项目数量
  781. for k, v := range scale.MonthDistribution.ProjectCount {
  782. if k == 0 {
  783. continue
  784. }
  785. v.Ratio = utility.Sequential(v.Value, scale.MonthDistribution.ProjectCount[k-1].Value)
  786. }
  787. }
  788. monthRatioFunc()
  789. //年度分配
  790. var yearRatioFunc = func() {
  791. //金额
  792. for k, v := range scale.YearDistribution.ProjectAmount {
  793. if k == 0 {
  794. continue
  795. }
  796. v.Ratio = utility.Sequential(v.Value, scale.YearDistribution.ProjectAmount[k-1].Value)
  797. }
  798. //项目数量
  799. for k, v := range scale.YearDistribution.ProjectCount {
  800. if k == 0 {
  801. continue
  802. }
  803. v.Ratio = utility.Sequential(v.Value, scale.YearDistribution.ProjectCount[k-1].Value)
  804. }
  805. }
  806. yearRatioFunc()
  807. }
  808. }
  809. ratioFunc()
  810. //项目规模----------0-0
  811. var projectAllDataFunc = func() {
  812. pad := ut.Result.ProjectAllData
  813. //项目数量TOP3客户类型的重点中标单位
  814. if len(pad.ScaleBuyclassCountTop) > 1 {
  815. /*-------------BuyerclassCount----------*/
  816. sort.Slice(pad.ScaleBuyclassCountTop, func(i, j int) bool {
  817. return pad.ScaleBuyclassCountTop[i].BuyclassCount > pad.ScaleBuyclassCountTop[j].BuyclassCount
  818. })
  819. if len(pad.ScaleBuyclassCountTop) > consts.Top3 {
  820. pad.ScaleBuyclassCountTop = pad.ScaleBuyclassCountTop[:consts.Top3]
  821. }
  822. //采购单位数量环比
  823. for _, v := range pad.ScaleBuyclassCountTop {
  824. v.BuyclassScale = utility.Formula(float64(v.BuyclassCount), float64(scale.MarketProfile.ProjectCount))
  825. redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sbct", ut.MgoUserId, v.Name)
  826. winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
  827. if err == nil && len(winnerInfos) > 0 {
  828. for _, winnerInfo := range winnerInfos {
  829. v.Winner = append(v.Winner, &entity.WinnerTotalS{
  830. Id: winnerInfo.Id,
  831. Winner: winnerInfo.Member,
  832. WinnerTotal: int64(winnerInfo.Score),
  833. })
  834. }
  835. //del cache
  836. go utility.DelRedisInfo(redisKey)
  837. } else {
  838. g.Log().Info(model.Ctx, "项目数量TOP3客户类型的重点中标单位 err:", err, v.Name)
  839. }
  840. }
  841. }
  842. //项目金额TOP3客户类型的重点中标单位
  843. if len(pad.ScaleBuyclassAmountTop) > 1 {
  844. /*------------BuyerclassAmount-----------*/
  845. sort.Slice(pad.ScaleBuyclassAmountTop, func(i, j int) bool {
  846. return pad.ScaleBuyclassAmountTop[i].BuyclassAmount > pad.ScaleBuyclassAmountTop[j].BuyclassAmount
  847. })
  848. if len(pad.ScaleBuyclassAmountTop) > consts.Top3 {
  849. pad.ScaleBuyclassAmountTop = pad.ScaleBuyclassAmountTop[:consts.Top3]
  850. }
  851. //采购单位类型金额占比
  852. for _, v := range pad.ScaleBuyclassAmountTop {
  853. v.BuyclassScale = utility.Formula(v.BuyclassAmount, float64(scale.MarketProfile.ProjectCount))
  854. redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sbat", ut.MgoUserId, v.Name)
  855. winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
  856. if err == nil && len(winnerInfos) > 0 {
  857. for _, winnerInfo := range winnerInfos {
  858. v.Winner = append(v.Winner, &entity.WinnerAmountS{
  859. Id: winnerInfo.Id,
  860. Winner: winnerInfo.Member,
  861. WinnerAmount: winnerInfo.Score,
  862. })
  863. }
  864. //del cache
  865. go utility.DelRedisInfo(redisKey)
  866. } else {
  867. g.Log().Info(model.Ctx, "项目金额TOP3客户类型的重点中标单位 err:", err, v.Name)
  868. }
  869. }
  870. }
  871. //项目金额TOP3地区的重点中标单位
  872. if len(pad.ScaleAreaAmountTop) > 1 {
  873. /*-----------------AreaAmount------*/
  874. sort.Slice(pad.ScaleAreaAmountTop, func(i, j int) bool {
  875. return pad.ScaleAreaAmountTop[i].AreaAmount > pad.ScaleAreaAmountTop[j].AreaAmount
  876. })
  877. if len(pad.ScaleAreaAmountTop) > consts.Top3 {
  878. pad.ScaleAreaAmountTop = pad.ScaleAreaAmountTop[:consts.Top3]
  879. }
  880. //地区环比
  881. for _, v := range pad.ScaleAreaAmountTop {
  882. v.AreaScale = utility.Formula(v.AreaAmount, scale.MarketProfile.Projctamout)
  883. redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev5, "saat", ut.MgoUserId, v.Name)
  884. winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
  885. if err == nil && len(winnerInfos) > 0 {
  886. for _, winnerInfo := range winnerInfos {
  887. v.Winner = append(v.Winner, &entity.WinnerAmountS{
  888. Id: winnerInfo.Id,
  889. Winner: winnerInfo.Member,
  890. WinnerAmount: winnerInfo.Score,
  891. })
  892. }
  893. //del cache
  894. go utility.DelRedisInfo(redisKey)
  895. } else {
  896. g.Log().Info(model.Ctx, "项目金额TOP3地区的重点中标单位 err:", err, v.Name)
  897. }
  898. }
  899. }
  900. //项目数量TOP3地区的重点中标单位
  901. /*--------------AreaCount---------*/
  902. if len(pad.ScaleAreaCountTop) > 1 {
  903. sort.Slice(pad.ScaleAreaCountTop, func(i, j int) bool {
  904. return pad.ScaleAreaCountTop[i].AreaCount > pad.ScaleAreaCountTop[j].AreaCount
  905. })
  906. if len(pad.ScaleAreaCountTop) > consts.Top3 {
  907. pad.ScaleAreaCountTop = pad.ScaleAreaCountTop[:consts.Top3]
  908. }
  909. for _, v := range pad.ScaleAreaCountTop {
  910. v.AreaScale = utility.Formula(float64(v.AreaCount), scale.MarketProfile.Projctamout)
  911. redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sact", ut.MgoUserId, v.Name)
  912. winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
  913. if err == nil && len(winnerInfos) > 0 {
  914. for _, winnerInfo := range winnerInfos {
  915. v.Winner = append(v.Winner, &entity.WinnerTotalS{
  916. Id: winnerInfo.Id,
  917. Winner: winnerInfo.Member,
  918. WinnerTotal: int64(winnerInfo.Score),
  919. })
  920. }
  921. //del cache
  922. go utility.DelRedisInfo(redisKey)
  923. } else {
  924. g.Log().Info(model.Ctx, "项目数量TOP3地区的重点中标单位 err:", err, v.Name)
  925. }
  926. }
  927. }
  928. //项目规模分布
  929. for _, v := range pad.ProjectScale {
  930. //项目总金额占比
  931. v.PersentC = utility.Formula(v.Amount, scale.MarketProfile.Projctamout)
  932. //项目总数占比
  933. v.PersentA = utility.Formula(float64(v.Count), float64(scale.MarketProfile.ProjectCount))
  934. }
  935. }
  936. /* 项目规模 地区分布 客户分布 地区客户top3*/
  937. projectAllDataFunc()
  938. //细化市场
  939. var scaleRefineFunc = func() {
  940. sr := ut.Result.ScaleRefine
  941. //细分市场的重点中标单位-项目数量占比
  942. for _, v := range sr.ScaleRefineTotalTop {
  943. v.Prop = utility.Formula(float64(v.Value), float64(scale.MarketProfile.ProjectCount))
  944. redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev6, "srtt", ut.MgoUserId, v.Name)
  945. winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
  946. if err == nil && len(winnerInfos) > 0 {
  947. for _, winnerInfo := range winnerInfos {
  948. v.TopList = append(v.TopList, &entity.TopListS{
  949. Id: winnerInfo.Id,
  950. Name: winnerInfo.Member,
  951. Value: winnerInfo.Score,
  952. Prop: utility.Formula(winnerInfo.Score, float64(scale.MarketProfile.ProjectCount)),
  953. })
  954. }
  955. //del cache
  956. go utility.DelRedisInfo(redisKey)
  957. } else {
  958. g.Log().Info(model.Ctx, "细分市场的重点中标单位-项目数量占比 err:", err, v.Name)
  959. }
  960. }
  961. //细分市场的重点中标单位-分类金额百分比
  962. for _, v := range sr.ScaleRefineAmountTop {
  963. v.Prop = utility.Formula(v.Value, scale.MarketProfile.Projctamout)
  964. redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev6, "srat", ut.MgoUserId, v.Name)
  965. winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
  966. if err == nil && len(winnerInfos) > 0 {
  967. for _, winnerInfo := range winnerInfos {
  968. v.TopList = append(v.TopList, &entity.TopListS{
  969. Id: winnerInfo.Id,
  970. Name: winnerInfo.Member,
  971. Value: winnerInfo.Score,
  972. Prop: utility.Formula(winnerInfo.Score, scale.MarketProfile.Projctamout),
  973. })
  974. }
  975. //del cache
  976. go utility.DelRedisInfo(redisKey)
  977. } else {
  978. g.Log().Info(model.Ctx, "细分市场的重点中标单位-分类金额百分比 err:", err, v.Name)
  979. }
  980. }
  981. }
  982. //细化市场
  983. scaleRefineFunc()
  984. ///* 市场-采购单位&&中标企业*/
  985. var buyerAndWinnerFunc = func() {
  986. baw := ut.Result.BuyerAndWinner
  987. //-------------redis----sorted---- 采购单位--score---项目数量TOP30采购单位-项目数量
  988. redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bct", ut.MgoUserId, "top30")
  989. buyerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.Top30-1, 0)
  990. if err == nil && len(buyerInfos) > 0 {
  991. for _, buyerInfo := range buyerInfos {
  992. var winnertop3Number []*entity.Winnertop3Number
  993. //-------------redis----sorted---- 采购单位--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
  994. redisKey_winner := utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bct_winner", ut.MgoUserId, buyerInfo.Member)
  995. winnerInfos, err := utility.GetZrevRangeByScore(redisKey_winner, consts.JudgmentPrev2, 0)
  996. if err == nil && len(winnerInfos) > 0 {
  997. for _, winnerInfo := range winnerInfos {
  998. winnertop3Number = append(winnertop3Number, &entity.Winnertop3Number{
  999. Id: winnerInfo.Id,
  1000. Name: winnerInfo.Member,
  1001. Number: int64(winnerInfo.Score),
  1002. })
  1003. }
  1004. //del cache
  1005. go utility.DelRedisInfo(redisKey_winner)
  1006. } else {
  1007. g.Log().Info(model.Ctx, "b--项目数量TOP30采购单位> winner err:", err, buyerInfo.Member)
  1008. }
  1009. baw.BuyerCountTop3 = append(baw.BuyerCountTop3, &entity.BuyerCountTop3S{
  1010. Name: buyerInfo.Member,
  1011. Number: int64(buyerInfo.Score),
  1012. Winnertop3: winnertop3Number,
  1013. })
  1014. }
  1015. //del cache
  1016. go utility.DelRedisInfo(redisKey)
  1017. } else {
  1018. g.Log().Info(model.Ctx, "b--项目数量TOP30采购单位 err:", err)
  1019. }
  1020. //-------------redis----sorted---- 采购单位--score---采购金额TOP30采购单位-金额
  1021. redisKey = utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bat", ut.MgoUserId, "top30")
  1022. buyerInfos, err = utility.GetZrevRangeByScore(redisKey, consts.Top30-1, 0)
  1023. if err == nil && len(buyerInfos) > 0 {
  1024. for _, buyerInfo := range buyerInfos {
  1025. var winnertop3Amount []*entity.Winnertop3Amount
  1026. //-------------redis----sorted---- 中标企业--score---采购金额TOP30采购单位-下的前三个中标企业信息-金额
  1027. redisKey_winner := utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bat_winner", ut.MgoUserId, buyerInfo.Member)
  1028. winnerInfos, err := utility.GetZrevRangeByScore(redisKey_winner, consts.JudgmentPrev2, 0)
  1029. if err == nil && len(winnerInfos) > 0 {
  1030. for _, winnerInfo := range winnerInfos {
  1031. winnertop3Amount = append(winnertop3Amount, &entity.Winnertop3Amount{
  1032. Id: winnerInfo.Id,
  1033. Name: winnerInfo.Member,
  1034. Amount: winnerInfo.Score,
  1035. })
  1036. }
  1037. //del cache
  1038. go utility.DelRedisInfo(redisKey_winner)
  1039. } else {
  1040. g.Log().Info(model.Ctx, "b--采购金额TOP30采购单位> winner err:", err, buyerInfo.Member)
  1041. }
  1042. baw.BuyerAmountTop3 = append(baw.BuyerAmountTop3, &entity.BuyerAmountTop3S{
  1043. Name: buyerInfo.Member,
  1044. Amount: buyerInfo.Score,
  1045. Winnertop3: winnertop3Amount,
  1046. })
  1047. }
  1048. //del cache
  1049. go utility.DelRedisInfo(redisKey)
  1050. } else {
  1051. g.Log().Info(model.Ctx, "b--采购金额TOP30采购单位 err:", err)
  1052. }
  1053. //采购规模分布
  1054. for _, v := range baw.BuyerTimeDistribution {
  1055. //总金额
  1056. v.TotalAmount = utility.Formula(v.Amount, scale.MarketProfile.Projctamout)
  1057. //总项目数量
  1058. v.TotalNumber = utility.Formula(v.Number, float64(scale.MarketProfile.ProjectCount))
  1059. }
  1060. //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-项目数量
  1061. redisKey = utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wct", ut.MgoUserId, "top30")
  1062. winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.Top30-1, 0)
  1063. if err == nil && len(winnerInfos) > 0 {
  1064. for _, winnerInfo := range winnerInfos {
  1065. var buyertop3 []*entity.Buyertop3Number
  1066. //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
  1067. redisKey_buyer := utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wct_buyer", ut.MgoUserId, winnerInfo.Member)
  1068. buyerInfos, err := utility.GetZrevRangeByScore(redisKey_buyer, consts.JudgmentPrev2, 0)
  1069. if err == nil && len(buyerInfos) > 0 {
  1070. for _, buyerInfo := range buyerInfos {
  1071. buyertop3 = append(buyertop3, &entity.Buyertop3Number{
  1072. Name: buyerInfo.Member,
  1073. Number: int64(buyerInfo.Score),
  1074. })
  1075. }
  1076. //del cache
  1077. go utility.DelRedisInfo(redisKey_buyer)
  1078. } else {
  1079. g.Log().Info(model.Ctx, "w--项目数量TOP30采购单位 --buyer err:", err)
  1080. }
  1081. baw.WinnerCountTop3 = append(baw.WinnerCountTop3, &entity.WinnerCountTop3S{
  1082. Id: winnerInfo.Id,
  1083. Name: winnerInfo.Member,
  1084. Number: int64(winnerInfo.Score),
  1085. Buyertop3: buyertop3,
  1086. })
  1087. }
  1088. //del cache
  1089. go utility.DelRedisInfo(redisKey)
  1090. } else {
  1091. g.Log().Info(model.Ctx, "w--项目数量TOP30采购单位 err:", err)
  1092. }
  1093. //-------------redis----sorted---- 中标企业--score---采购金额TOP30采购单位-金额
  1094. redisKey = utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wat", ut.MgoUserId, "top30")
  1095. winnerInfos, err = utility.GetZrevRangeByScore(redisKey, consts.Top30-1, 0)
  1096. if err == nil && len(winnerInfos) > 0 {
  1097. for _, winnerInfo := range winnerInfos {
  1098. var buyertop3 []*entity.Buyertop3Amount
  1099. //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
  1100. redisKey_buyer := utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wat_buyer", ut.MgoUserId, winnerInfo.Member)
  1101. buyerInfos, err := utility.GetZrevRangeByScore(redisKey_buyer, consts.JudgmentPrev2, 0)
  1102. if err == nil && len(buyerInfos) > 0 {
  1103. for _, buyerInfo := range buyerInfos {
  1104. buyertop3 = append(buyertop3, &entity.Buyertop3Amount{
  1105. Name: buyerInfo.Member,
  1106. Amount: buyerInfo.Score,
  1107. })
  1108. }
  1109. //del cache
  1110. go utility.DelRedisInfo(redisKey_buyer)
  1111. } else {
  1112. g.Log().Info(model.Ctx, "w--采购金额TOP30采购单位 buyer err:", err)
  1113. }
  1114. baw.WinnerAmountTop3 = append(baw.WinnerAmountTop3, &entity.WinnerAmountTop3S{
  1115. Id: winnerInfo.Id,
  1116. Name: winnerInfo.Member,
  1117. Amount: winnerInfo.Score,
  1118. Buyertop3: buyertop3,
  1119. })
  1120. }
  1121. //del cache
  1122. go utility.DelRedisInfo(redisKey)
  1123. } else {
  1124. g.Log().Info(model.Ctx, "w--采购金额TOP30采购单位 err:", err)
  1125. }
  1126. //中标规模分布
  1127. for _, v := range baw.WinnerTimeDistribution {
  1128. //总金额
  1129. v.TotalAmount = utility.Formula(v.Amount, scale.MarketProfile.Projctamout)
  1130. //总项目数量
  1131. v.TotalNumber = utility.Formula(v.Number, float64(scale.MarketProfile.ProjectCount))
  1132. }
  1133. }
  1134. buyerAndWinnerFunc()
  1135. /*---------------*/
  1136. }
  1137. // ResultSave 结果保存
  1138. func (ut *UserTask) ResultSave() {
  1139. //市场概况+时间分布
  1140. scale := ut.Result.Scale
  1141. if scale.MarketProfile.ProjectCount > 0 {
  1142. var scaleMap = common.StructToMapMore(scale)
  1143. scaleMap["s_m_id"] = ut.MgoUserId
  1144. delete(scaleMap, "project_info_all")
  1145. if id := do.Mgo.Save(consts.MarketScaleMain, scaleMap); id == "" {
  1146. ut.IsFalse = true
  1147. g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketScaleMain, ut.MgoUserId))
  1148. }
  1149. }
  1150. //项目规模TOP10 集合
  1151. topProject := ut.Result.TopProject
  1152. if len(topProject.ProjectTop10) > 0 {
  1153. var topProject10 = common.StructToMapMore(topProject.ProjectTop10)
  1154. topProject10["s_m_id"] = ut.MgoUserId
  1155. if id := do.Mgo.Save(consts.MarketTopProject, topProject10); id == "" {
  1156. ut.IsFalse = true
  1157. g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketTopProject, ut.MgoUserId))
  1158. }
  1159. }
  1160. ///* 项目规模 地区分布 客户分布 地区客户top3*/
  1161. pad := ut.Result.ProjectAllData
  1162. if len(pad.ProjectScale) > 0 {
  1163. var padMap = common.StructToMapMore(pad)
  1164. padMap["s_m_id"] = ut.MgoUserId
  1165. if id := do.Mgo.Save(consts.MarketProjectAllData, padMap); id == "" {
  1166. ut.IsFalse = true
  1167. g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketProjectAllData, ut.MgoUserId))
  1168. }
  1169. }
  1170. //细化市场
  1171. sr := ut.Result.ScaleRefine
  1172. if len(sr.ScaleRefineAll) > 0 || len(sr.ScaleRefineTotalTop) > 0 {
  1173. var srMap = common.StructToMapMore(sr)
  1174. srMap["s_m_id"] = ut.MgoUserId
  1175. if id := do.Mgo.Save(consts.MarketScaleRefine, srMap); id == "" {
  1176. ut.IsFalse = true
  1177. g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketScaleRefine, ut.MgoUserId))
  1178. }
  1179. }
  1180. ///* 市场-采购单位&&中标企业*/
  1181. baw := ut.Result.BuyerAndWinner
  1182. if len(baw.WinnerCountTop3) > 0 || len(baw.BuyerCountTop3) > 0 {
  1183. var bawMap = common.StructToMapMore(baw)
  1184. bawMap["s_m_id"] = ut.MgoUserId
  1185. if id := do.Mgo.Save(consts.MarketBuyerAndWinner, bawMap); id == "" {
  1186. ut.IsFalse = true
  1187. g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketBuyerAndWinner, ut.MgoUserId))
  1188. }
  1189. }
  1190. }
  1191. // 消息通知
  1192. func (ut *UserTask) SendMsg() {
  1193. //分析报告离线分析异常
  1194. if ut.IsFalse {
  1195. content := fmt.Sprintf(g.Cfg("task.yaml").MustGet(model.Ctx, "falseMsg").String(), ut.RId, ut.MgoUserId, ut.PositionId, ut.Phone, ut.AnalysisParam.KeysItemsStr, ut.AnalysisParam.Area, ut.AnalysisParam.Industry, ut.AnalysisParam.BuyerClass, ut.MatchingMode)
  1196. SendAlarmMail(model.Ctx, content)
  1197. } else {
  1198. SendReportMsg(model.Ctx, ut.MgoUserId, ut.PositionId, ut.RId)
  1199. }
  1200. }
  1201. // Reset RESET
  1202. func (ut *UserTask) Reset() {
  1203. ut.JudgmentMap = map[string]bool{}
  1204. ut.Result = &CalculateResult{}
  1205. ut.AnalysisParam = entity.AnalyzeParameters{}
  1206. close(ut.PInfo)
  1207. ut.IsEnd = false
  1208. ut.Started = 0
  1209. ut.Ticker.Stop()
  1210. }
  1211. // ForMatData 获取格式化请求参数
  1212. func (ut *UserTask) ForMatData() error {
  1213. //格式化订阅词
  1214. if err := json.Unmarshal([]byte(ut.AnalysisParam.KeysItemsStr), &ut.FormatParam.KeysItems); err != nil {
  1215. return fmt.Errorf("关键词组格式异常")
  1216. }
  1217. if ut.FormatParam.KeysItems == nil || len(ut.FormatParam.KeysItems) == 0 {
  1218. return fmt.Errorf("请选择关键词组")
  1219. }
  1220. //格式化时间段
  1221. if timeArr := strings.Split(ut.AnalysisParam.RangeTime, "-"); len(timeArr) == 2 {
  1222. ut.FormatParam.STime = common.Int64All(timeArr[0])
  1223. ut.FormatParam.ETime = common.Int64All(timeArr[1])
  1224. ut.StartTime = ut.FormatParam.STime //原始分析开始时间
  1225. ut.EndTime = ut.FormatParam.ETime //原始分析结束时间
  1226. if ut.FormatParam.STime == 0 || ut.FormatParam.ETime == 0 {
  1227. return fmt.Errorf("开始时间和结束时间不能为空")
  1228. }
  1229. } else {
  1230. return fmt.Errorf("时间戳格式异常")
  1231. }
  1232. //是否进行环比数据
  1233. n_stime := ut.FormatParam.STime
  1234. if time.Unix(ut.FormatParam.STime, 0).AddDate(1, 0, 0).Unix() >= time.Unix(ut.FormatParam.ETime, 0).Unix() {
  1235. n_stime = utility.GetPreviousMarket(time.Unix(ut.FormatParam.STime, 0), time.Unix(ut.FormatParam.ETime, 0))
  1236. }
  1237. if time.Unix(ut.FormatParam.STime, 0).AddDate(0, 1, 0).Unix() < time.Unix(ut.FormatParam.ETime, 0).Unix() {
  1238. var mon_time, year_time int64
  1239. stime, etime := time.Unix(ut.FormatParam.STime, 0), time.Unix(ut.FormatParam.ETime, 0)
  1240. mb, mds := utility.GetMonthData(stime, etime)
  1241. ut.MonthDistributionKey = mds
  1242. if mb {
  1243. mon_time = stime.AddDate(0, -1, 0).Unix()
  1244. } else {
  1245. mon_time = ut.FormatParam.STime
  1246. }
  1247. //年度数据
  1248. yb, yds := utility.GetYearData(stime, etime)
  1249. ut.YearDistributionKey = yds
  1250. if yb {
  1251. year_time = stime.AddDate(-1, 0, 0).Unix()
  1252. } else {
  1253. year_time = ut.FormatParam.STime
  1254. }
  1255. if n_stime > mon_time {
  1256. n_stime = mon_time
  1257. }
  1258. if n_stime > year_time {
  1259. n_stime = year_time
  1260. }
  1261. }
  1262. ut.FormatParam.STime = n_stime
  1263. if ut.FormatParam.STime != ut.StartTime {
  1264. ut.IsRatio = true
  1265. }
  1266. //格式化省份、城市
  1267. if areaStr := strings.TrimSpace(ut.AnalysisParam.Area); areaStr != "" {
  1268. imap := map[string][]string{}
  1269. if err := json.Unmarshal([]byte(ut.AnalysisParam.Area), &imap); err != nil {
  1270. return fmt.Errorf("非法地区信息")
  1271. }
  1272. var city, area []string
  1273. for name, v := range imap {
  1274. if len(v) == 0 {
  1275. area = append(area, name)
  1276. } else {
  1277. for _, vv := range v {
  1278. city = append(city, vv)
  1279. }
  1280. }
  1281. }
  1282. ut.FormatParam.Area = area
  1283. ut.FormatParam.City = city
  1284. }
  1285. //格式化行业
  1286. if industryStr := strings.TrimSpace(ut.AnalysisParam.Industry); industryStr != "" {
  1287. imap := map[string][]string{}
  1288. if err := json.Unmarshal([]byte(industryStr), &imap); err != nil {
  1289. return fmt.Errorf("非法行业信息")
  1290. }
  1291. var farr []string
  1292. for name, v := range imap {
  1293. for _, vv := range v {
  1294. farr = append(farr, fmt.Sprintf("%s_%s", name, vv))
  1295. }
  1296. }
  1297. ut.FormatParam.Industry = farr
  1298. }
  1299. //格式化类型
  1300. if buyerClassStr := strings.TrimSpace(ut.AnalysisParam.BuyerClass); buyerClassStr != "" {
  1301. ut.FormatParam.BuyerClass = strings.Split(buyerClassStr, ",")
  1302. }
  1303. return nil
  1304. }
  1305. // GetMonthDistributionKey 获取月度key
  1306. func (ut *UserTask) GetMonthDistributionKey(jgtime int64) (month string) {
  1307. for _, mdv := range ut.MonthDistributionKey {
  1308. if utility.DateIsIn(mdv.From, mdv.To, jgtime, 0) {
  1309. month = mdv.Key
  1310. break
  1311. }
  1312. }
  1313. return
  1314. }
  1315. // GetYearDistributionKey 获取年度key
  1316. func (ut *UserTask) GetYearDistributionKey(jgtime int64) (year string) {
  1317. for _, ydv := range ut.YearDistributionKey {
  1318. if utility.DateIsIn(ydv.From, ydv.To, jgtime, 0) {
  1319. year = ydv.Key
  1320. break
  1321. }
  1322. }
  1323. return
  1324. }