12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385 |
- package internal
- import (
- "analyze/internal/consts"
- "analyze/internal/model"
- "analyze/internal/model/do"
- "analyze/internal/model/entity"
- "analyze/utility"
- "app.yhyue.com/moapp/jybase/common"
- "bp.jydev.jianyu360.cn/BaseService/pushpkg/dfa"
- "encoding/json"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/gctx"
- "sort"
- "strings"
- "sync"
- "sync/atomic"
- "time"
- )
- // user 信息
- type UserTask struct {
- MgoUserId string //用户id
- PositionId int64 //职位id
- Phone string //手机号
- StartTime int64 //开始时间
- EndTime int64 //结束时间
- IsEnd bool //是否结束
- IsCancel bool //是否已取消
- IsFalse bool //是否离线分析成功
- IsRatio bool //是否需要进行环比
- PInfo chan *entity.ProjectInfo //项目信息
- RId string //市场分析报告id
- AnalysisParam entity.AnalyzeParameters //市场分析报告参数
- FormatParam entity.AnalysisRequestFormat //格式化
- MatchingMode string //匹配方式 title:标题 content:项目名称/标的物
- Started int32 // 标志字段,表示任务是否已经启动
- Ticker *time.Ticker //
- Result *CalculateResult //结果集
- MonthDistributionKey []entity.DateSpan //月度分配key
- YearDistributionKey []entity.DateSpan //年度分配key
- JudgmentMap map[string]bool //地区分布|城市分布|客户 项目规模 地区top3|订阅分类|一个就行
- JudgmentLock sync.Mutex
- CalculateWG sync.WaitGroup
- }
- type CalculateResult struct {
- Scale *entity.MarketScaleMain // 市场概况+时间分布
- TopProject *entity.MarketTopProject // 项目规模TOP10 集合
- ProjectAllData *entity.MarketProjectAllData /* 项目规模 地区分布 客户分布 地区客户top3*/
- ScaleRefine *entity.MarketScaleRefine //细化市场
- BuyerAndWinner *entity.MarketBuyerAndWinner /* 市场-采购单位&&中标企业*/
- }
- func NewUserInfo() *UserTask {
- return &UserTask{
- PInfo: make(chan *entity.ProjectInfo, 2000),
- AnalysisParam: entity.AnalyzeParameters{},
- Ticker: time.NewTicker(time.Duration(10000) * time.Second),
- Result: &CalculateResult{
- Scale: &entity.MarketScaleMain{}, // 市场概况+时间分布
- TopProject: &entity.MarketTopProject{}, // 项目规模TOP10 集合
- ProjectAllData: &entity.MarketProjectAllData{}, /* 项目规模 地区分布 客户分布 地区客户top3*/
- ScaleRefine: &entity.MarketScaleRefine{}, //细化市场
- BuyerAndWinner: &entity.MarketBuyerAndWinner{}, /* 市场-采购单位&&中标企业*/
- },
- JudgmentMap: make(map[string]bool),
- CalculateWG: sync.WaitGroup{},
- Started: 0, //重置
- }
- }
- // 推送项目信息到用户任务的通道
- func (ut *UserTask) Push(info *entity.ProjectInfo) {
- ut.PInfo <- info
- }
- // 开始用户任务
- func (ut *UserTask) Start() {
- if atomic.CompareAndSwapInt32(&ut.Started, 0, 1) {
- g.Log().Info(gctx.New(), ut.MgoUserId, "----", ut.AnalysisParam)
- go ut.Run()
- }
- }
- func (ut *UserTask) Run() {
- for {
- if ut.IsEnd || ut.IsCancel {
- break
- }
- select {
- case info := <-ut.PInfo:
- //项目信息匹配
- if ut.MatchConditions(info) {
- //模块计算
- ut.Calculate(info)
- }
- case <-ut.Ticker.C:
- //是否已取消
- if !ut.IsCancel {
- ut.IsCancel = utility.IsAnalyzeCancel(ut.RId)
- }
- if !ut.IsCancel {
- //数据整理 及 二次计算
- ut.DataCollation()
- //结果保存
- ut.ResultSave()
- //消息通知
- ut.SendMsg()
- //结束
- ut.IsEnd = true
- }
- break
- }
- }
- ut.Reset()
- //结束后的处理
- }
- // MatchConditions 匹配项目名称、行业、地区等条件
- func (ut *UserTask) MatchConditions(p *entity.ProjectInfo) (b bool) {
- //area 和city 满足任何一个
- if !ut.AreaMatch(p.Area, p.City) {
- return
- }
- //行业
- if !ut.IndustryMatch(p.Industry) {
- return
- }
- //采购单位类型
- if !ut.BuyClassMatch(p.BuyerClass) {
- return
- }
- //关键词 附加词 满足一个
- //排除词 满足
- title := []string{p.ProjectName}
- if ut.MatchingMode == "" {
- title = append(title, p.Purchasing)
- }
- if !ut.KeysMatch(strings.Join(title, ",")) {
- return
- }
- return true
- }
- // BuyClassMatch 采购单位类型
- func (ut *UserTask) BuyClassMatch(buyerclass string) (b bool) {
- if len(ut.FormatParam.BuyerClass) == 0 {
- b = true
- return
- }
- if buyerclass == "" {
- return
- }
- if strings.Contains(strings.Join(ut.FormatParam.BuyerClass, ","), buyerclass) {
- b = true
- }
- return
- }
- // AreaMatch 省份和城市
- func (ut *UserTask) AreaMatch(area, city string) (b bool) {
- //没有地区分析条件
- if len(ut.FormatParam.Area) == 0 && len(ut.FormatParam.City) == 0 {
- b = true
- return
- }
- //项目信息没有地区字段
- if area == "" && city == "" {
- return
- }
- if strings.Contains(strings.Join(ut.FormatParam.Area, ","), area) || strings.Contains(strings.Join(ut.FormatParam.City, ","), city) {
- b = true
- return
- }
- return
- }
- // IndustryMatch 行业
- func (ut *UserTask) IndustryMatch(industry string) (b bool) {
- if len(ut.FormatParam.Industry) == 0 {
- b = true
- return
- }
- if industry == "" {
- return
- }
- for _, it := range strings.Split(industry, ",") {
- if strings.Contains(strings.Join(ut.FormatParam.Industry, ","), it) {
- b = true
- break
- }
- }
- return
- }
- // 关键词匹配--分类
- func (ut *UserTask) KeysMatchByKeysItems(matchObject string, keysItems entity.KeyWordGroup) (b bool) {
- var (
- dfaKey = dfa.DFA{}
- )
- //purchasing|projectname
- //订阅词分组
- for _, viewKeyWord := range utility.GetGroupKeywordArr(keysItems.A_Key) {
- var (
- keys = viewKeyWord.Keyword
- )
- //关键词 和 附加词
- if len(viewKeyWord.Appended) > 0 {
- keys = append(keys, viewKeyWord.Appended...)
- }
- dfaKey.AddWord(common.If(len(keys) > 0, strings.Join(keys, "+"), strings.Join(keys, "")).(string))
- //关键词 附加词
- if rk := dfaKey.Analy(matchObject); len(rk) != 0 {
- b = true
- break
- }
- }
- return
- }
- // KeysMatch 关键词
- func (ut *UserTask) KeysMatch(matchObject string) (b bool) {
- if len(ut.FormatParam.KeysItems) == 0 {
- b = true
- return
- }
- var (
- dfaKey = dfa.DFA{}
- dfaExclude = dfa.DFA{}
- )
- //purchasing|projectname
- //订阅词分组
- for _, viewKeyWord := range utility.GetAllKeywordArr(ut.FormatParam.KeysItems) {
- //排除词
- dfaExclude.AddWord(common.If(len(viewKeyWord.Exclude) > 0, strings.Join(viewKeyWord.Exclude, "+"), strings.Join(viewKeyWord.Exclude, "")).(string))
- if re := dfaExclude.Analy(matchObject); len(re) != 0 {
- b = false
- continue
- }
- var (
- keys = viewKeyWord.Keyword
- )
- //关键词 和 附加词
- if len(viewKeyWord.Appended) > 0 {
- keys = append(keys, viewKeyWord.Appended...)
- }
- dfaKey.AddWord(common.If(len(keys) > 0, strings.Join(keys, "+"), strings.Join(keys, "")).(string))
- //关键词 附加词
- if rk := dfaKey.Analy(matchObject); len(rk) != 0 {
- b = true
- break
- }
- }
- return
- }
- // GetMap
- func (ut *UserTask) GetJudgmentMap(key string) (b bool) {
- ut.JudgmentLock.Lock()
- defer ut.JudgmentLock.Unlock()
- b = ut.JudgmentMap[key]
- return
- }
- // SetMap
- func (ut *UserTask) SetJudgmentMap(keys ...string) (b bool) {
- ut.JudgmentLock.Lock()
- defer ut.JudgmentLock.Unlock()
- for _, key := range keys {
- ut.JudgmentMap[key] = true
- }
- return
- }
- // Calculate 模块计算逻辑
- func (ut *UserTask) Calculate(pt *entity.ProjectInfo) {
- //市场概况+时间分布
- scale := ut.Result.Scale
- var scaleFunc = func() {
- //项目信息结果时间在原始查询条件范围内
- if utility.DateIsIn(ut.StartTime, ut.EndTime, pt.JgTime, 0) {
- if pt.Buyer != "" {
- scale.MarketProfile.Buyercount++
- }
- if pt.Winners != "" {
- scale.MarketProfile.Winnercount += len(strings.Split(pt.Winners, ","))
- }
- scale.MarketProfile.ProjectCount++
- scale.MarketProfile.Projctamout += pt.Sortprice
- //月度分配
- if len(ut.MonthDistributionKey) > 0 {
- if monthKey := ut.GetMonthDistributionKey(pt.JgTime); monthKey != "" {
- monthKeyPrev := utility.GetJudgmentPrevKey(consts.JudgmentPrev11, monthKey)
- if !ut.GetJudgmentMap(monthKeyPrev) {
- ut.SetJudgmentMap(monthKeyPrev)
- //金额
- scale.MonthDistribution.ProjectAmount = append(scale.MonthDistribution.ProjectAmount, &entity.Distribution{
- Minth: monthKey,
- Value: pt.Sortprice,
- })
- //项目数量
- scale.MonthDistribution.ProjectCount = append(scale.MonthDistribution.ProjectCount, &entity.Distribution{
- Minth: monthKey,
- Value: 1,
- })
- } else {
- //金额
- for _, mdpa := range scale.MonthDistribution.ProjectAmount {
- if mdpa.Minth == monthKey {
- mdpa.Value += pt.Sortprice
- break
- }
- }
- //项目数量
- for _, mdpc := range scale.MonthDistribution.ProjectCount {
- if mdpc.Minth == monthKey {
- mdpc.Value++
- }
- }
- }
- }
- }
- //年度分配
- if len(ut.YearDistributionKey) > 0 {
- if yearKey := ut.GetYearDistributionKey(pt.JgTime); yearKey != "" {
- yearKeyPrev := utility.GetJudgmentPrevKey(consts.JudgmentPrev11, yearKey)
- if !ut.GetJudgmentMap(yearKeyPrev) {
- ut.SetJudgmentMap(yearKeyPrev)
- //金额
- scale.YearDistribution.ProjectAmount = append(scale.YearDistribution.ProjectAmount, &entity.Distribution{
- Minth: yearKey,
- Value: pt.Sortprice,
- })
- //项目数量
- scale.YearDistribution.ProjectCount = append(scale.YearDistribution.ProjectCount, &entity.Distribution{
- Minth: yearKey,
- Value: 1,
- })
- } else {
- //金额
- for _, mdpa := range scale.YearDistribution.ProjectAmount {
- if mdpa.Minth == yearKey {
- mdpa.Value += pt.Sortprice
- break
- }
- }
- //项目数量
- for _, mdpc := range scale.YearDistribution.ProjectCount {
- if mdpc.Minth == yearKey {
- mdpc.Value++
- }
- }
- }
- }
- }
- }
- //是否需要进行环比
- if ut.IsRatio {
- scale.ProjectInfoAll.ProjectCount++
- scale.ProjectInfoAll.Projctamout += pt.Sortprice
- if pt.Buyer != "" {
- scale.ProjectInfoAll.Buyercount++
- }
- if pt.Winners != "" {
- scale.ProjectInfoAll.Winnercount += len(strings.Split(pt.Winners, ","))
- }
- }
- }
- //项目规模TOP10 集合
- topProject := ut.Result.TopProject
- var topProjectFunc = func() {
- defer func() {
- ut.CalculateWG.Done()
- }()
- var (
- entIdList = pt.Entidlist
- )
- if len(pt.Entidlist) > 0 {
- //加密
- for _, entId := range pt.Entidlist {
- entIdList = append(entIdList, utility.EncodeId(entId))
- }
- }
- topProject.ProjectTop10 = append(topProject.ProjectTop10, &entity.ProjectTop{
- Id: common.If(pt.SourceInfoId != "", utility.EncodeId(pt.SourceInfoId), "").(string),
- Area: pt.Area,
- City: pt.City,
- Eidlist: entIdList,
- Jgtime: pt.JgTime,
- Projectname: pt.ProjectName,
- Sortprice: pt.Sortprice,
- WinnerS: strings.Split(pt.Winners, ","),
- })
- /*---------------B--------*/
- if len(topProject.ProjectTop10) > 1 {
- sort.Slice(topProject.ProjectTop10, func(i, j int) bool {
- return topProject.ProjectTop10[i].Sortprice > topProject.ProjectTop10[j].Sortprice // 根据 price 字段进行倒序排序
- })
- if len(topProject.ProjectTop10) > consts.Top10 {
- topProject.ProjectTop10 = topProject.ProjectTop10[:consts.Top10]
- }
- }
- }
- ///* 项目规模 地区分布 客户分布 地区客户top3*/
- pad := ut.Result.ProjectAllData
- var projectAllFunc = func() {
- defer func() {
- ut.CalculateWG.Done()
- }()
- //地区分布
- var areaInfoFunc = func() {
- areaInfoKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev0, pt.Area)
- cityInfoKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev1, fmt.Sprintf("%s_%s", pt.Area, pt.City))
- if !ut.GetJudgmentMap(areaInfoKey) {
- ut.SetJudgmentMap(areaInfoKey, cityInfoKey)
- pad.AreaInfos = append(pad.AreaInfos, &entity.AreaInfosS{
- Amount: pt.Sortprice,
- Area: pt.Area,
- AreaDetails: []*entity.AreaDetailsS{
- {
- Amount: pt.Sortprice,
- City: pt.City,
- Total: 1,
- },
- },
- Total: 1,
- })
- } else {
- var (
- wc = make(chan bool, common.If(len(pad.AreaInfos) > 10, consts.JudgmentPrev3, 1).(int))
- wg = &sync.WaitGroup{}
- stopLoop = false
- )
- for _, ai := range pad.AreaInfos {
- if stopLoop {
- break
- }
- wc <- true
- wg.Add(1)
- go func(ai *entity.AreaInfosS) {
- defer func() {
- wg.Done()
- <-wc
- }()
- if ai.Area == pt.Area {
- stopLoop = true
- if !ut.GetJudgmentMap(cityInfoKey) {
- ut.SetJudgmentMap(cityInfoKey)
- ai.AreaDetails = append(ai.AreaDetails, &entity.AreaDetailsS{
- Amount: pt.Sortprice,
- City: pt.City,
- Total: 1,
- })
- } else {
- for _, ci := range ai.AreaDetails {
- if ci.City == pt.City {
- ci.Amount += pt.Sortprice
- ci.Total++
- break
- }
- }
- }
- return
- }
- }(ai)
- }
- wg.Wait()
- }
- }
- areaInfoFunc()
- //客户分布
- var customerScaleFunc = func() {
- bc := pt.BuyerClass
- //客户分布
- if bc != "" {
- customerKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev2, bc)
- if bc == "其它" {
- //客户分布(其它)
- if !ut.GetJudgmentMap(customerKey) {
- ut.SetJudgmentMap(customerKey)
- //客户分布 其它
- pad.CustomerScaleOther = &entity.CustomerScaleS{
- Amount: pt.Sortprice,
- Buyclass: bc,
- Total: 1,
- }
- } else {
- pad.CustomerScaleOther.Amount += pt.Sortprice
- pad.CustomerScaleOther.Total++
- }
- } else {
- //客户分布
- if !ut.GetJudgmentMap(customerKey) {
- ut.SetJudgmentMap(customerKey)
- pad.CustomerScale = append(pad.CustomerScale, &entity.CustomerScaleS{
- Amount: pt.Sortprice,
- Buyclass: bc,
- Total: 1,
- })
- } else {
- var (
- wc = make(chan bool, common.If(len(pad.CustomerScale) > 10, consts.JudgmentPrev3, 1).(int))
- wg = &sync.WaitGroup{}
- stopLoop = false
- )
- for _, csm := range pad.CustomerScale {
- if stopLoop {
- break
- }
- wc <- true
- wg.Add(1)
- go func(csm *entity.CustomerScaleS) {
- defer func() {
- wg.Done()
- <-wc
- }()
- if csm.Buyclass == bc {
- stopLoop = true
- csm.Amount += pt.Sortprice
- csm.Total++
- return
- }
- }(csm)
- }
- wg.Wait()
- }
- }
- //TOP3
- //-------------redis----sorted---- 中标企业--score---项目数量TOP3客户类型的重点中标单位
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sbct", ut.MgoUserId, bc), pt, 1)
- //-------------redis----sorted---- 中标企业--score---项目金额TOP3客户类型的重点中标单位
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sbat", ut.MgoUserId, bc), pt, pt.Sortprice)
- top3Key := utility.GetJudgmentPrevKey(consts.JudgmentPrev3, fmt.Sprintf("%s_top3", bc))
- if !ut.GetJudgmentMap(top3Key) {
- ut.SetJudgmentMap(top3Key)
- //项目数量TOP3客户类型的重点中标单位
- pad.ScaleBuyclassCountTop = append(pad.ScaleBuyclassCountTop, &entity.ScaleBuyclassCountTopS{
- BuyclassCount: 1,
- Name: bc,
- })
- //项目金额TOP3客户类型的重点中标单位
- pad.ScaleBuyclassAmountTop = append(pad.ScaleBuyclassAmountTop, &entity.ScaleBuyclassAmountTopS{
- BuyclassAmount: pt.Sortprice,
- Name: bc,
- })
- } else {
- //项目数量TOP3客户类型的重点中标单位
- for _, sbct := range pad.ScaleBuyclassCountTop {
- if sbct.Name == bc {
- sbct.BuyclassCount++
- break
- }
- }
- //项目金额TOP3客户类型的重点中标单位
- for _, sbat := range pad.ScaleBuyclassAmountTop {
- if sbat.Name == bc {
- sbat.BuyclassAmount += pt.Sortprice
- break
- }
- }
- }
- }
- }
- if pt.Sortprice > 0 {
- customerScaleFunc()
- }
- //项目规模分布
- var ProjectScaleFunc = func() {
- var psk = ""
- for _, cp := range consts.ProjectScale {
- if cp.From < pt.Sortprice && common.If(cp.To > 0, cp.To > pt.Sortprice, true).(bool) {
- psk = cp.Key
- }
- }
- scaleKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev4, psk)
- if !ut.GetJudgmentMap(scaleKey) {
- ut.SetJudgmentMap(scaleKey)
- pad.ProjectScale = append(pad.ProjectScale, &entity.ProjectScaleS{
- Name: psk,
- Count: 1,
- Amount: pt.Sortprice,
- })
- } else {
- for _, ps := range pad.ProjectScale {
- if ps.Name == psk {
- ps.Count++
- ps.Amount += pt.Sortprice
- break
- }
- }
- }
- }
- ProjectScaleFunc()
- //项目金额TOP3地区的重点中标单位
- var scaleAreaAmountAndCountTopFunc = func() {
- //-------------redis----sorted---- 中标企业--score---项目金额---TOP3地区的重点中标单位
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev5, "saat", ut.MgoUserId, pt.Area), pt, pt.Sortprice)
- //-------------redis----sorted---- 中标企业--score---项目数量---TOP3地区的重点中标单位
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev5, "sact", ut.MgoUserId, pt.Area), pt, 1)
- saaactKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev5, pt.Area)
- if !ut.GetJudgmentMap(saaactKey) {
- ut.SetJudgmentMap(saaactKey)
- //项目金额---TOP3地区的重点中标单位
- pad.ScaleAreaAmountTop = append(pad.ScaleAreaAmountTop, &entity.ScaleAreaAmountTopS{
- AreaAmount: pt.Sortprice,
- Name: pt.Area,
- })
- //项目数量---TOP3地区的重点中标单位
- pad.ScaleAreaCountTop = append(pad.ScaleAreaCountTop, &entity.ScaleAreaCountTopS{
- AreaCount: 1,
- Name: pt.Area,
- })
- } else {
- //项目金额---TOP3地区的重点中标单位
- for _, saat := range pad.ScaleAreaAmountTop {
- if saat.Name == pt.Area {
- saat.AreaAmount += pt.Sortprice
- break
- }
- }
- //项目数量---TOP3地区的重点中标单位
- for _, sact := range pad.ScaleAreaCountTop {
- if sact.Name == pt.Area {
- sact.AreaCount++
- break
- }
- }
- }
- }
- scaleAreaAmountAndCountTopFunc()
- //
- }
- //细化市场
- sr := ut.Result.ScaleRefine
- var scaleRefineFunc = func() {
- defer func() {
- ut.CalculateWG.Done()
- }()
- //订阅分类|重点中标单位-项目数量|重点中标单位-项目金额
- var scaleRefineAllAndAmountAndTotalFunc = func() {
- for _, keysItems := range ut.FormatParam.KeysItems {
- if ut.KeysMatchByKeysItems(pt.ProjectName, keysItems) {
- itemName := keysItems.ItemName
- itemNameKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev6, itemName)
- //-------------redis----sorted---- 中标企业--score---重点中标单位-项目数量
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev6, "srtt", ut.MgoUserId, itemName), pt, 1)
- //-------------redis----sorted---- 中标企业--score---重点中标单位-金额
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev6, "srat", ut.MgoUserId, itemName), pt, pt.Sortprice)
- if !ut.GetJudgmentMap(itemNameKey) {
- ut.SetJudgmentMap(itemNameKey)
- //订阅分类
- sr.ScaleRefineAll = append(sr.ScaleRefineAll, &entity.ScaleRefineAllS{
- Amount: pt.Sortprice,
- Name: itemName,
- Total: 1,
- })
- //重点中标单位-项目数量
- sr.ScaleRefineTotalTop = append(sr.ScaleRefineTotalTop, &entity.ScaleRefineTotalTopS{
- Name: itemName,
- Value: 1,
- })
- //重点中标单位-金额
- sr.ScaleRefineAmountTop = append(sr.ScaleRefineAmountTop, &entity.ScaleRefineAmountTopS{
- Name: itemName,
- Value: pt.Sortprice,
- })
- } else {
- //订阅分类 300+
- for _, sra := range sr.ScaleRefineAll {
- if sra.Name == itemName {
- sra.Total++
- sra.Amount += pt.Sortprice
- break
- }
- }
- //重点中标单位-项目数量
- for _, srtt := range sr.ScaleRefineTotalTop {
- if srtt.Name == itemName {
- srtt.Value++
- break
- }
- }
- //重点中标单位-金额
- for _, srat := range sr.ScaleRefineAmountTop {
- if srat.Name == itemName {
- srat.Value += pt.Sortprice
- break
- }
- }
- }
- }
- }
- }
- scaleRefineAllAndAmountAndTotalFunc()
- }
- ///* 市场-采购单位&&中标企业*/
- baw := ut.Result.BuyerAndWinner
- var buyerAndWinnerFunc = func() {
- defer func() {
- ut.CalculateWG.Done()
- }()
- //采购单位
- var buyerAmountAndTotalTop30 = func() {
- //-------------redis----sorted---- 采购单位--score---项目数量TOP30采购单位-项目数量
- utility.UpdateBuyerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bct", ut.MgoUserId, "top30"), pt, 1)
- //-------------redis----sorted---- 采购单位--score---采购金额TOP30采购单位-金额
- utility.UpdateBuyerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bat", ut.MgoUserId, "top30"), pt, pt.Sortprice)
- //-------------redis----sorted---- 采购单位--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bct_winner", ut.MgoUserId, pt.Buyer), pt, 1)
- //-------------redis----sorted---- 采购单位--score---采购金额TOP30采购单位-下的前三个中标企业信息-金额
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bat_winner", ut.MgoUserId, pt.Buyer), pt, pt.Sortprice)
- //采购规模分布 --7
- var psk = ""
- for _, cp := range consts.ProjectScale {
- if cp.From < pt.Sortprice && common.If(cp.To > 0, cp.To > pt.Sortprice, true).(bool) {
- psk = cp.Key
- }
- }
- scaleKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev8, psk)
- if !ut.GetJudgmentMap(scaleKey) {
- ut.SetJudgmentMap(scaleKey)
- baw.BuyerTimeDistribution = append(baw.BuyerTimeDistribution, &entity.BuyerAndWinnerTimeDistributionS{
- Key: psk,
- Amount: pt.Sortprice,
- Number: 1,
- })
- } else {
- for _, ps := range baw.BuyerTimeDistribution {
- if ps.Key == psk {
- ps.Number++
- ps.Amount += pt.Sortprice
- break
- }
- }
- }
- }
- if pt.Buyer != "" {
- buyerAmountAndTotalTop30()
- }
- //中标单位
- var winnerAmountAndTotalTop30 = func() {
- for _, winner := range strings.Split(pt.Winners, ",") {
- //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-项目数量
- utility.UpdateBuyerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wct", ut.MgoUserId, "top30"), pt, 1)
- //-------------redis----sorted---- 中标企业--score---采购金额TOP30采购单位-金额
- utility.UpdateBuyerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wat", ut.MgoUserId, "top30"), pt, pt.Sortprice)
- //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wct_buyer", ut.MgoUserId, winner), pt, 1)
- //-------------redis----sorted---- 中标企业--score---采购金额TOP30采购单位-下的前三个中标企业信息-金额
- utility.UpdateWinnerInfoOfScore(utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wat_buyer", ut.MgoUserId, winner), pt, pt.Sortprice)
- }
- //中标规模分布
- var psk = ""
- for _, cp := range consts.ProjectScale {
- if cp.From < pt.Sortprice && common.If(cp.To > 0, cp.To > pt.Sortprice, true).(bool) {
- psk = cp.Key
- }
- }
- scaleKey := utility.GetJudgmentPrevKey(consts.JudgmentPrev10, psk)
- if !ut.GetJudgmentMap(scaleKey) {
- ut.SetJudgmentMap(scaleKey)
- baw.WinnerTimeDistribution = append(baw.WinnerTimeDistribution, &entity.BuyerAndWinnerTimeDistributionS{
- Key: psk,
- Amount: pt.Sortprice,
- Number: 1,
- })
- } else {
- for _, ps := range baw.WinnerTimeDistribution {
- if ps.Key == psk {
- ps.Number++
- ps.Amount += pt.Sortprice
- break
- }
- }
- }
- }
- if pt.Winners != "" {
- winnerAmountAndTotalTop30()
- }
- }
- //
- var nextFunc = func() {
- ut.CalculateWG.Add(4)
- go topProjectFunc()
- go projectAllFunc()
- go scaleRefineFunc()
- go buyerAndWinnerFunc()
- ut.CalculateWG.Wait()
- }
- scaleFunc()
- if utility.DateIsIn(ut.StartTime, ut.EndTime, pt.JgTime, 0) {
- nextFunc()
- }
- }
- // DataCollation 数据整理
- func (ut *UserTask) DataCollation() {
- //项目环比
- scale := ut.Result.Scale
- var ratioFunc = func() {
- //项目平均金额(万)
- scale.MarketProfile.Projectavgmoney = utility.Formula(scale.MarketProfile.Projctamout, float64(scale.MarketProfile.ProjectCount))
- //环比
- if ut.IsRatio {
- //项目平均金额(万)
- scale.ProjectInfoAll.Projectavgmoney = utility.Formula(scale.ProjectInfoAll.Projctamout, float64(scale.ProjectInfoAll.ProjectCount))
- //项目平均金额环比
- scale.MarketProfile.ProjectavgmoneyRatio = utility.Sequential(scale.MarketProfile.Projectavgmoney, scale.ProjectInfoAll.Projectavgmoney)
- //项目金额环比
- scale.MarketProfile.ProjctamountRatio = utility.Sequential(scale.MarketProfile.Projctamout, scale.ProjectInfoAll.Projctamout)
- //项目数量环比
- scale.MarketProfile.ProjectCountRatio = utility.Sequential(float64(scale.MarketProfile.ProjectCount), float64(scale.ProjectInfoAll.ProjectCount))
- //采购单位项目数量环比
- scale.MarketProfile.BuyercountRatio = utility.Sequential(float64(scale.MarketProfile.Buyercount), float64(scale.ProjectInfoAll.Buyercount))
- //中标企业项目数量环比
- scale.MarketProfile.WinnercountRatio = utility.Sequential(float64(scale.MarketProfile.Winnercount), float64(scale.ProjectInfoAll.Winnercount))
- //月度分配
- var monthRatioFunc = func() {
- //金额
- for k, v := range scale.MonthDistribution.ProjectAmount {
- if k == 0 {
- continue
- }
- v.Ratio = utility.Sequential(v.Value, scale.MonthDistribution.ProjectAmount[k-1].Value)
- }
- //项目数量
- for k, v := range scale.MonthDistribution.ProjectCount {
- if k == 0 {
- continue
- }
- v.Ratio = utility.Sequential(v.Value, scale.MonthDistribution.ProjectCount[k-1].Value)
- }
- }
- monthRatioFunc()
- //年度分配
- var yearRatioFunc = func() {
- //金额
- for k, v := range scale.YearDistribution.ProjectAmount {
- if k == 0 {
- continue
- }
- v.Ratio = utility.Sequential(v.Value, scale.YearDistribution.ProjectAmount[k-1].Value)
- }
- //项目数量
- for k, v := range scale.YearDistribution.ProjectCount {
- if k == 0 {
- continue
- }
- v.Ratio = utility.Sequential(v.Value, scale.YearDistribution.ProjectCount[k-1].Value)
- }
- }
- yearRatioFunc()
- }
- }
- ratioFunc()
- //项目规模----------0-0
- var projectAllDataFunc = func() {
- pad := ut.Result.ProjectAllData
- //项目数量TOP3客户类型的重点中标单位
- if len(pad.ScaleBuyclassCountTop) > 1 {
- /*-------------BuyerclassCount----------*/
- sort.Slice(pad.ScaleBuyclassCountTop, func(i, j int) bool {
- return pad.ScaleBuyclassCountTop[i].BuyclassCount > pad.ScaleBuyclassCountTop[j].BuyclassCount
- })
- if len(pad.ScaleBuyclassCountTop) > consts.Top3 {
- pad.ScaleBuyclassCountTop = pad.ScaleBuyclassCountTop[:consts.Top3]
- }
- //采购单位数量环比
- for _, v := range pad.ScaleBuyclassCountTop {
- v.BuyclassScale = utility.Formula(float64(v.BuyclassCount), float64(scale.MarketProfile.ProjectCount))
- redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sbct", ut.MgoUserId, v.Name)
- winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- v.Winner = append(v.Winner, &entity.WinnerTotalS{
- Id: winnerInfo.Id,
- Winner: winnerInfo.Member,
- WinnerTotal: int64(winnerInfo.Score),
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "项目数量TOP3客户类型的重点中标单位 err:", err, v.Name)
- }
- }
- }
- //项目金额TOP3客户类型的重点中标单位
- if len(pad.ScaleBuyclassAmountTop) > 1 {
- /*------------BuyerclassAmount-----------*/
- sort.Slice(pad.ScaleBuyclassAmountTop, func(i, j int) bool {
- return pad.ScaleBuyclassAmountTop[i].BuyclassAmount > pad.ScaleBuyclassAmountTop[j].BuyclassAmount
- })
- if len(pad.ScaleBuyclassAmountTop) > consts.Top3 {
- pad.ScaleBuyclassAmountTop = pad.ScaleBuyclassAmountTop[:consts.Top3]
- }
- //采购单位类型金额占比
- for _, v := range pad.ScaleBuyclassAmountTop {
- v.BuyclassScale = utility.Formula(v.BuyclassAmount, float64(scale.MarketProfile.ProjectCount))
- redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sbat", ut.MgoUserId, v.Name)
- winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- v.Winner = append(v.Winner, &entity.WinnerAmountS{
- Id: winnerInfo.Id,
- Winner: winnerInfo.Member,
- WinnerAmount: winnerInfo.Score,
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "项目金额TOP3客户类型的重点中标单位 err:", err, v.Name)
- }
- }
- }
- //项目金额TOP3地区的重点中标单位
- if len(pad.ScaleAreaAmountTop) > 1 {
- /*-----------------AreaAmount------*/
- sort.Slice(pad.ScaleAreaAmountTop, func(i, j int) bool {
- return pad.ScaleAreaAmountTop[i].AreaAmount > pad.ScaleAreaAmountTop[j].AreaAmount
- })
- if len(pad.ScaleAreaAmountTop) > consts.Top3 {
- pad.ScaleAreaAmountTop = pad.ScaleAreaAmountTop[:consts.Top3]
- }
- //地区环比
- for _, v := range pad.ScaleAreaAmountTop {
- v.AreaScale = utility.Formula(v.AreaAmount, scale.MarketProfile.Projctamout)
- redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev5, "saat", ut.MgoUserId, v.Name)
- winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- v.Winner = append(v.Winner, &entity.WinnerAmountS{
- Id: winnerInfo.Id,
- Winner: winnerInfo.Member,
- WinnerAmount: winnerInfo.Score,
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "项目金额TOP3地区的重点中标单位 err:", err, v.Name)
- }
- }
- }
- //项目数量TOP3地区的重点中标单位
- /*--------------AreaCount---------*/
- if len(pad.ScaleAreaCountTop) > 1 {
- sort.Slice(pad.ScaleAreaCountTop, func(i, j int) bool {
- return pad.ScaleAreaCountTop[i].AreaCount > pad.ScaleAreaCountTop[j].AreaCount
- })
- if len(pad.ScaleAreaCountTop) > consts.Top3 {
- pad.ScaleAreaCountTop = pad.ScaleAreaCountTop[:consts.Top3]
- }
- for _, v := range pad.ScaleAreaCountTop {
- v.AreaScale = utility.Formula(float64(v.AreaCount), scale.MarketProfile.Projctamout)
- redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev3, "sact", ut.MgoUserId, v.Name)
- winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- v.Winner = append(v.Winner, &entity.WinnerTotalS{
- Id: winnerInfo.Id,
- Winner: winnerInfo.Member,
- WinnerTotal: int64(winnerInfo.Score),
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "项目数量TOP3地区的重点中标单位 err:", err, v.Name)
- }
- }
- }
- //项目规模分布
- for _, v := range pad.ProjectScale {
- //项目总金额占比
- v.PersentC = utility.Formula(v.Amount, scale.MarketProfile.Projctamout)
- //项目总数占比
- v.PersentA = utility.Formula(float64(v.Count), float64(scale.MarketProfile.ProjectCount))
- }
- }
- /* 项目规模 地区分布 客户分布 地区客户top3*/
- projectAllDataFunc()
- //细化市场
- var scaleRefineFunc = func() {
- sr := ut.Result.ScaleRefine
- //细分市场的重点中标单位-项目数量占比
- for _, v := range sr.ScaleRefineTotalTop {
- v.Prop = utility.Formula(float64(v.Value), float64(scale.MarketProfile.ProjectCount))
- redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev6, "srtt", ut.MgoUserId, v.Name)
- winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- v.TopList = append(v.TopList, &entity.TopListS{
- Id: winnerInfo.Id,
- Name: winnerInfo.Member,
- Value: winnerInfo.Score,
- Prop: utility.Formula(winnerInfo.Score, float64(scale.MarketProfile.ProjectCount)),
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "细分市场的重点中标单位-项目数量占比 err:", err, v.Name)
- }
- }
- //细分市场的重点中标单位-分类金额百分比
- for _, v := range sr.ScaleRefineAmountTop {
- v.Prop = utility.Formula(v.Value, scale.MarketProfile.Projctamout)
- redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev6, "srat", ut.MgoUserId, v.Name)
- winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.JudgmentPrev2, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- v.TopList = append(v.TopList, &entity.TopListS{
- Id: winnerInfo.Id,
- Name: winnerInfo.Member,
- Value: winnerInfo.Score,
- Prop: utility.Formula(winnerInfo.Score, scale.MarketProfile.Projctamout),
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "细分市场的重点中标单位-分类金额百分比 err:", err, v.Name)
- }
- }
- }
- //细化市场
- scaleRefineFunc()
- ///* 市场-采购单位&&中标企业*/
- var buyerAndWinnerFunc = func() {
- baw := ut.Result.BuyerAndWinner
- //-------------redis----sorted---- 采购单位--score---项目数量TOP30采购单位-项目数量
- redisKey := utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bct", ut.MgoUserId, "top30")
- buyerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.Top30-1, 0)
- if err == nil && len(buyerInfos) > 0 {
- for _, buyerInfo := range buyerInfos {
- var winnertop3Number []*entity.Winnertop3Number
- //-------------redis----sorted---- 采购单位--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
- redisKey_winner := utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bct_winner", ut.MgoUserId, buyerInfo.Member)
- winnerInfos, err := utility.GetZrevRangeByScore(redisKey_winner, consts.JudgmentPrev2, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- winnertop3Number = append(winnertop3Number, &entity.Winnertop3Number{
- Id: winnerInfo.Id,
- Name: winnerInfo.Member,
- Number: int64(winnerInfo.Score),
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey_winner)
- } else {
- g.Log().Info(model.Ctx, "b--项目数量TOP30采购单位> winner err:", err, buyerInfo.Member)
- }
- baw.BuyerCountTop3 = append(baw.BuyerCountTop3, &entity.BuyerCountTop3S{
- Name: buyerInfo.Member,
- Number: int64(buyerInfo.Score),
- Winnertop3: winnertop3Number,
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "b--项目数量TOP30采购单位 err:", err)
- }
- //-------------redis----sorted---- 采购单位--score---采购金额TOP30采购单位-金额
- redisKey = utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bat", ut.MgoUserId, "top30")
- buyerInfos, err = utility.GetZrevRangeByScore(redisKey, consts.Top30-1, 0)
- if err == nil && len(buyerInfos) > 0 {
- for _, buyerInfo := range buyerInfos {
- var winnertop3Amount []*entity.Winnertop3Amount
- //-------------redis----sorted---- 中标企业--score---采购金额TOP30采购单位-下的前三个中标企业信息-金额
- redisKey_winner := utility.GetSortedKeyOfRedis(consts.JudgmentPrev7, "bat_winner", ut.MgoUserId, buyerInfo.Member)
- winnerInfos, err := utility.GetZrevRangeByScore(redisKey_winner, consts.JudgmentPrev2, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- winnertop3Amount = append(winnertop3Amount, &entity.Winnertop3Amount{
- Id: winnerInfo.Id,
- Name: winnerInfo.Member,
- Amount: winnerInfo.Score,
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey_winner)
- } else {
- g.Log().Info(model.Ctx, "b--采购金额TOP30采购单位> winner err:", err, buyerInfo.Member)
- }
- baw.BuyerAmountTop3 = append(baw.BuyerAmountTop3, &entity.BuyerAmountTop3S{
- Name: buyerInfo.Member,
- Amount: buyerInfo.Score,
- Winnertop3: winnertop3Amount,
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "b--采购金额TOP30采购单位 err:", err)
- }
- //采购规模分布
- for _, v := range baw.BuyerTimeDistribution {
- //总金额
- v.TotalAmount = utility.Formula(v.Amount, scale.MarketProfile.Projctamout)
- //总项目数量
- v.TotalNumber = utility.Formula(v.Number, float64(scale.MarketProfile.ProjectCount))
- }
- //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-项目数量
- redisKey = utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wct", ut.MgoUserId, "top30")
- winnerInfos, err := utility.GetZrevRangeByScore(redisKey, consts.Top30-1, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- var buyertop3 []*entity.Buyertop3Number
- //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
- redisKey_buyer := utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wct_buyer", ut.MgoUserId, winnerInfo.Member)
- buyerInfos, err := utility.GetZrevRangeByScore(redisKey_buyer, consts.JudgmentPrev2, 0)
- if err == nil && len(buyerInfos) > 0 {
- for _, buyerInfo := range buyerInfos {
- buyertop3 = append(buyertop3, &entity.Buyertop3Number{
- Name: buyerInfo.Member,
- Number: int64(buyerInfo.Score),
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey_buyer)
- } else {
- g.Log().Info(model.Ctx, "w--项目数量TOP30采购单位 --buyer err:", err)
- }
- baw.WinnerCountTop3 = append(baw.WinnerCountTop3, &entity.WinnerCountTop3S{
- Id: winnerInfo.Id,
- Name: winnerInfo.Member,
- Number: int64(winnerInfo.Score),
- Buyertop3: buyertop3,
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "w--项目数量TOP30采购单位 err:", err)
- }
- //-------------redis----sorted---- 中标企业--score---采购金额TOP30采购单位-金额
- redisKey = utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wat", ut.MgoUserId, "top30")
- winnerInfos, err = utility.GetZrevRangeByScore(redisKey, consts.Top30-1, 0)
- if err == nil && len(winnerInfos) > 0 {
- for _, winnerInfo := range winnerInfos {
- var buyertop3 []*entity.Buyertop3Amount
- //-------------redis----sorted---- 中标企业--score---项目数量TOP30采购单位-下的前三个中标企业信息-项目数量
- redisKey_buyer := utility.GetSortedKeyOfRedis(consts.JudgmentPrev9, "wat_buyer", ut.MgoUserId, winnerInfo.Member)
- buyerInfos, err := utility.GetZrevRangeByScore(redisKey_buyer, consts.JudgmentPrev2, 0)
- if err == nil && len(buyerInfos) > 0 {
- for _, buyerInfo := range buyerInfos {
- buyertop3 = append(buyertop3, &entity.Buyertop3Amount{
- Name: buyerInfo.Member,
- Amount: buyerInfo.Score,
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey_buyer)
- } else {
- g.Log().Info(model.Ctx, "w--采购金额TOP30采购单位 buyer err:", err)
- }
- baw.WinnerAmountTop3 = append(baw.WinnerAmountTop3, &entity.WinnerAmountTop3S{
- Id: winnerInfo.Id,
- Name: winnerInfo.Member,
- Amount: winnerInfo.Score,
- Buyertop3: buyertop3,
- })
- }
- //del cache
- go utility.DelRedisInfo(redisKey)
- } else {
- g.Log().Info(model.Ctx, "w--采购金额TOP30采购单位 err:", err)
- }
- //中标规模分布
- for _, v := range baw.WinnerTimeDistribution {
- //总金额
- v.TotalAmount = utility.Formula(v.Amount, scale.MarketProfile.Projctamout)
- //总项目数量
- v.TotalNumber = utility.Formula(v.Number, float64(scale.MarketProfile.ProjectCount))
- }
- }
- buyerAndWinnerFunc()
- /*---------------*/
- }
- // ResultSave 结果保存
- func (ut *UserTask) ResultSave() {
- //市场概况+时间分布
- scale := ut.Result.Scale
- if scale.MarketProfile.ProjectCount > 0 {
- var scaleMap = common.StructToMapMore(scale)
- scaleMap["s_m_id"] = ut.MgoUserId
- delete(scaleMap, "project_info_all")
- if id := do.Mgo.Save(consts.MarketScaleMain, scaleMap); id == "" {
- ut.IsFalse = true
- g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketScaleMain, ut.MgoUserId))
- }
- }
- //项目规模TOP10 集合
- topProject := ut.Result.TopProject
- if len(topProject.ProjectTop10) > 0 {
- var topProject10 = common.StructToMapMore(topProject.ProjectTop10)
- topProject10["s_m_id"] = ut.MgoUserId
- if id := do.Mgo.Save(consts.MarketTopProject, topProject10); id == "" {
- ut.IsFalse = true
- g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketTopProject, ut.MgoUserId))
- }
- }
- ///* 项目规模 地区分布 客户分布 地区客户top3*/
- pad := ut.Result.ProjectAllData
- if len(pad.ProjectScale) > 0 {
- var padMap = common.StructToMapMore(pad)
- padMap["s_m_id"] = ut.MgoUserId
- if id := do.Mgo.Save(consts.MarketProjectAllData, padMap); id == "" {
- ut.IsFalse = true
- g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketProjectAllData, ut.MgoUserId))
- }
- }
- //细化市场
- sr := ut.Result.ScaleRefine
- if len(sr.ScaleRefineAll) > 0 || len(sr.ScaleRefineTotalTop) > 0 {
- var srMap = common.StructToMapMore(sr)
- srMap["s_m_id"] = ut.MgoUserId
- if id := do.Mgo.Save(consts.MarketScaleRefine, srMap); id == "" {
- ut.IsFalse = true
- g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketScaleRefine, ut.MgoUserId))
- }
- }
- ///* 市场-采购单位&&中标企业*/
- baw := ut.Result.BuyerAndWinner
- if len(baw.WinnerCountTop3) > 0 || len(baw.BuyerCountTop3) > 0 {
- var bawMap = common.StructToMapMore(baw)
- bawMap["s_m_id"] = ut.MgoUserId
- if id := do.Mgo.Save(consts.MarketBuyerAndWinner, bawMap); id == "" {
- ut.IsFalse = true
- g.Log().Info(model.Ctx, fmt.Sprintf("marketanalysisreport -- 保存失败-:%s , %s", consts.MarketBuyerAndWinner, ut.MgoUserId))
- }
- }
- }
- // 消息通知
- func (ut *UserTask) SendMsg() {
- //分析报告离线分析异常
- if ut.IsFalse {
- 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)
- SendAlarmMail(model.Ctx, content)
- } else {
- SendReportMsg(model.Ctx, ut.MgoUserId, ut.PositionId, ut.RId)
- }
- }
- // Reset RESET
- func (ut *UserTask) Reset() {
- ut.JudgmentMap = map[string]bool{}
- ut.Result = &CalculateResult{
- Scale: &entity.MarketScaleMain{}, // 市场概况+时间分布
- TopProject: &entity.MarketTopProject{}, // 项目规模TOP10 集合
- ProjectAllData: &entity.MarketProjectAllData{}, /* 项目规模 地区分布 客户分布 地区客户top3*/
- ScaleRefine: &entity.MarketScaleRefine{}, //细化市场
- BuyerAndWinner: &entity.MarketBuyerAndWinner{}, /* 市场-采购单位&&中标企业*/
- }
- ut.AnalysisParam = entity.AnalyzeParameters{}
- close(ut.PInfo)
- ut.IsEnd = false
- ut.Started = 0
- ut.Ticker.Stop()
- }
- // ForMatData 获取格式化请求参数
- func (ut *UserTask) ForMatData() error {
- //格式化订阅词
- if err := json.Unmarshal([]byte(ut.AnalysisParam.KeysItemsStr), &ut.FormatParam.KeysItems); err != nil {
- return fmt.Errorf("关键词组格式异常")
- }
- if ut.FormatParam.KeysItems == nil || len(ut.FormatParam.KeysItems) == 0 {
- return fmt.Errorf("请选择关键词组")
- }
- //格式化时间段
- if timeArr := strings.Split(ut.AnalysisParam.RangeTime, "-"); len(timeArr) == 2 {
- ut.FormatParam.STime = common.Int64All(timeArr[0])
- ut.FormatParam.ETime = common.Int64All(timeArr[1])
- ut.StartTime = ut.FormatParam.STime //原始分析开始时间
- ut.EndTime = ut.FormatParam.ETime //原始分析结束时间
- if ut.FormatParam.STime == 0 || ut.FormatParam.ETime == 0 {
- return fmt.Errorf("开始时间和结束时间不能为空")
- }
- } else {
- return fmt.Errorf("时间戳格式异常")
- }
- //是否进行环比数据
- n_stime := ut.FormatParam.STime
- if time.Unix(ut.FormatParam.STime, 0).AddDate(1, 0, 0).Unix() >= time.Unix(ut.FormatParam.ETime, 0).Unix() {
- n_stime = utility.GetPreviousMarket(time.Unix(ut.FormatParam.STime, 0), time.Unix(ut.FormatParam.ETime, 0))
- }
- if time.Unix(ut.FormatParam.STime, 0).AddDate(0, 1, 0).Unix() < time.Unix(ut.FormatParam.ETime, 0).Unix() {
- var mon_time, year_time int64
- stime, etime := time.Unix(ut.FormatParam.STime, 0), time.Unix(ut.FormatParam.ETime, 0)
- mb, mds := utility.GetMonthData(stime, etime)
- ut.MonthDistributionKey = mds
- if mb {
- mon_time = stime.AddDate(0, -1, 0).Unix()
- } else {
- mon_time = ut.FormatParam.STime
- }
- //年度数据
- yb, yds := utility.GetYearData(stime, etime)
- ut.YearDistributionKey = yds
- if yb {
- year_time = stime.AddDate(-1, 0, 0).Unix()
- } else {
- year_time = ut.FormatParam.STime
- }
- if n_stime > mon_time {
- n_stime = mon_time
- }
- if n_stime > year_time {
- n_stime = year_time
- }
- }
- ut.FormatParam.STime = n_stime
- if ut.FormatParam.STime != ut.StartTime {
- ut.IsRatio = true
- }
- //格式化省份、城市
- if areaStr := strings.TrimSpace(ut.AnalysisParam.Area); areaStr != "" {
- imap := map[string][]string{}
- if err := json.Unmarshal([]byte(ut.AnalysisParam.Area), &imap); err != nil {
- return fmt.Errorf("非法地区信息")
- }
- var city, area []string
- for name, v := range imap {
- if len(v) == 0 {
- area = append(area, name)
- } else {
- for _, vv := range v {
- city = append(city, vv)
- }
- }
- }
- ut.FormatParam.Area = area
- ut.FormatParam.City = city
- }
- //格式化行业
- if industryStr := strings.TrimSpace(ut.AnalysisParam.Industry); industryStr != "" {
- imap := map[string][]string{}
- if err := json.Unmarshal([]byte(industryStr), &imap); err != nil {
- return fmt.Errorf("非法行业信息")
- }
- var farr []string
- for name, v := range imap {
- for _, vv := range v {
- farr = append(farr, fmt.Sprintf("%s_%s", name, vv))
- }
- }
- ut.FormatParam.Industry = farr
- }
- //格式化类型
- if buyerClassStr := strings.TrimSpace(ut.AnalysisParam.BuyerClass); buyerClassStr != "" {
- ut.FormatParam.BuyerClass = strings.Split(buyerClassStr, ",")
- }
- return nil
- }
- // GetMonthDistributionKey 获取月度key
- func (ut *UserTask) GetMonthDistributionKey(jgtime int64) (month string) {
- for _, mdv := range ut.MonthDistributionKey {
- if utility.DateIsIn(mdv.From, mdv.To, jgtime, 0) {
- month = mdv.Key
- break
- }
- }
- return
- }
- // GetYearDistributionKey 获取年度key
- func (ut *UserTask) GetYearDistributionKey(jgtime int64) (year string) {
- for _, ydv := range ut.YearDistributionKey {
- if utility.DateIsIn(ydv.From, ydv.To, jgtime, 0) {
- year = ydv.Key
- break
- }
- }
- return
- }
|