userTask.go 48 KB

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