123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965 |
- package marketanalysis
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/encrypt"
- elastic "app.yhyue.com/moapp/jybase/es"
- "app.yhyue.com/moapp/jybase/mongodb"
- "encoding/json"
- "fmt"
- esV7 "github.com/olivere/elastic/v7"
- "log"
- "math"
- "strings"
- "sync"
- "time"
- )
- // 中标单位分析
- func WinningAnalysis(thisWinnerRow BuyerWinnerRow, rMap *sync.Map, entArrMap map[string]float64, winnerName map[string]string, sy *sync.WaitGroup, offline bool) {
- defer sy.Done()
- type s_Winner struct {
- Name string `json:"key"`
- TCount int `json:"t_count"` //项目数量
- TAmount float64 `json:"t_amount"` //项目金额
- TotalAmount interface{} `json:"total_amount"`
- TotalNumber interface{} `json:"total_number"`
- }
- //中标单位-中标规模分布
- winnerA := make(map[string]*distributionTrend)
- for _, v := range thisWinnerRow.WinnerAmountDistribution.EntidlistTerms.Buckets {
- if vs, ok := entArrMap[v.Key]; ok {
- v.Amount.Value += vs
- delete(entArrMap, v.Key)
- }
- amountDistribution(v.Amount.Value, winnerA)
- }
- for _, f := range entArrMap {
- amountDistribution(f, winnerA)
- }
- var (
- countAll int
- amountAll float64
- )
- for _, v := range winnerA {
- countAll += v.Count
- amountAll += v.Amount
- }
- buyerMap := []interface{}{}
- for _, v := range Analysis {
- var data s_Winner
- data.Name = v
- vlu, ok := winnerA[v]
- if ok {
- data.TCount = vlu.Count
- data.TAmount = vlu.Amount
- if countAll != 0 {
- data.TotalNumber = float64(vlu.Count) / float64(countAll)
- }
- if amountAll != 0 {
- data.TotalAmount = vlu.Amount / amountAll
- }
- }
- buyerMap = append(buyerMap, data)
- }
- type number_projects struct {
- Id interface{} `json:"id"`
- Name string `json:"name"`
- Number int64 `json:"number"`
- //Accounted interface{} `json:"accounted"`
- Data interface{} `json:"buyertop3"`
- }
- type number_project struct {
- Name string `json:"name"`
- Number int64 `json:"number"`
- //Accounted interface{} `json:"accounted"`
- }
- //中标单位-项目数量TOP3
- countMap := []interface{}{}
- for _, v := range thisWinnerRow.WinnerCountTop3.SWinnerCount {
- var _d number_projects
- _d.Number = v.BuyerCount.Value
- if winnerName[v.Key] == "" {
- continue
- }
- _d.Name = winnerName[v.Key]
- _d.Id = encodeId(v.Key)
- /*if thisWinnerRow.ProjectCount.Value != 0 {
- _d.Accounted = float64(v.BuyerCount.Value) / float64(thisWinnerRow.ProjectCount.Value)
- }*/
- var ss []interface{}
- for _, v1 := range v.BuyerTop.Buckets {
- var _dd number_project
- _dd.Name = v1.Key
- _dd.Number = v1.BuyerWinnerAmount.Value
- /*if _d.Number != 0 {
- _dd.Accounted = float64(v1.BuyerWinnerAmount.Value) / float64(_d.Number)
- }*/
- ss = append(ss, _dd)
- }
- _d.Data = ss
- if !offline {
- if len(ss) > Top3 {
- _d.Data = ss[:Top3]
- }
- if len(countMap) >= Top30 {
- break
- }
- }
- countMap = append(countMap, _d)
- }
- //中标单位-项目金额TOP3
- type amount_projects struct {
- Id interface{} `json:"id"`
- Name string `json:"name"`
- Amount float64 `json:"amount"`
- //Accounted interface{} `json:"accounted"`
- Data interface{} `json:"buyertop3"`
- }
- type buyertop3 struct {
- Name string `json:"name"`
- Amount float64 `json:"amount"`
- //Accounted interface{} `json:"accounted"`
- }
- amountMap := []interface{}{}
- for _, v := range thisWinnerRow.WinnerAmountTop3.SWinnerAmount {
- if v.SWinnerAmount.Value == 0 || winnerName[v.Key] == "" {
- continue
- }
- var _d amount_projects
- _d.Amount = v.SWinnerAmount.Value
- _d.Name = winnerName[v.Key]
- _d.Id = encodeId(v.Key)
- /*if thisWinnerRow.ProjectAmount.Value != 0 {
- _d.Accounted = v.SWinnerAmount.Value / thisWinnerRow.ProjectAmount.Value
- }*/
- var ss []interface{}
- for _, v1 := range v.BuyerTop.Buckets {
- if v1.BuyerWinnerAmount.Value == 0 {
- continue
- }
- var _dd buyertop3
- _dd.Name = v1.Key
- _dd.Amount = v1.BuyerWinnerAmount.Value
- /*if _d.Amount != 0 {
- _dd.Accounted = v1.BuyerWinnerAmount.Value / _d.Amount
- }*/
- ss = append(ss, _dd)
- }
- _d.Data = ss
- if !offline {
- if len(ss) > Top3 {
- _d.Data = ss[:Top3]
- }
- if len(amountMap) >= Top30 {
- break
- }
- }
- amountMap = append(amountMap, _d)
- }
- rMap.Store("winner_time_distribution", buyerMap)
- rMap.Store("winner_count_top3", countMap)
- rMap.Store("winner_amount_top3", amountMap)
- }
- func amountDistribution(v float64, data map[string]*distributionTrend) {
- if v <= 0 {
- return
- }
- if v < 100000 {
- if data["<10万"] == nil {
- data["<10万"] = new(distributionTrend)
- }
- data["<10万"].Amount += v
- data["<10万"].Count++
- } else if v < 500000 {
- if data["10万~50万"] == nil {
- data["10万~50万"] = new(distributionTrend)
- }
- data["10万~50万"].Amount += v
- data["10万~50万"].Count++
- } else if v < 1000000 {
- if data["50万~100万"] == nil {
- data["50万~100万"] = new(distributionTrend)
- }
- data["50万~100万"].Amount += v
- data["50万~100万"].Count++
- } else if v < 1000000*5 {
- if data["100万~500万"] == nil {
- data["100万~500万"] = new(distributionTrend)
- }
- data["100万~500万"].Amount += v
- data["100万~500万"].Count++
- } else if v < 1000000*10 {
- if data["500万~1000万"] == nil {
- data["500万~1000万"] = new(distributionTrend)
- }
- data["500万~1000万"].Amount += v
- data["500万~1000万"].Count++
- } else if v < 100000000 {
- if data["1000万~1亿"] == nil {
- data["1000万~1亿"] = new(distributionTrend)
- }
- data["1000万~1亿"].Amount += v
- data["1000万~1亿"].Count++
- } else if v >= 100000000 {
- if data["≥1亿"] == nil {
- data["≥1亿"] = new(distributionTrend)
- }
- data["≥1亿"].Amount += v
- data["≥1亿"].Count++
- }
- }
- func BuyerAnalysis(thisBuyerRow BuyerWinnerRow, rMap *sync.Map, winnerName map[string]string, sy *sync.WaitGroup, offline bool) {
- defer sy.Done()
- type buyer struct {
- Name string `json:"key"`
- TCount int `json:"t_count"` //项目数量
- TAmount float64 `json:"t_amount"` //金额数量
- TotalAmount interface{} `json:"total_amount"`
- TotalNumber interface{} `json:"total_number"`
- }
- //采购单位-采购规模分布
- buyerMap := []interface{}{}
- //计算采购单位各区间金额
- buyerA := make(map[string]*distributionTrend)
- for _, v := range thisBuyerRow.BuyerAmountDistribution.Buckets {
- amountDistribution(v.Amount.Value, buyerA)
- }
- var (
- countAll int
- amountAll float64
- )
- for _, v := range buyerA {
- countAll += v.Count
- amountAll += v.Amount
- }
- for _, v := range Analysis {
- var data buyer
- data.Name = v
- vlu, ok := buyerA[v]
- if ok {
- data.TAmount = vlu.Amount
- data.TCount = vlu.Count
- if countAll != 0 {
- data.TotalNumber = float64(vlu.Count) / float64(countAll)
- }
- if amountAll != 0 {
- data.TotalAmount = vlu.Amount / amountAll
- }
- }
- buyerMap = append(buyerMap, data)
- }
- type number_projects struct {
- Name string `json:"name"`
- Number int64 `json:"number"`
- //Accounted interface{} `json:"accounted"`
- Data interface{} `json:"winnertop3"`
- }
- type number_project struct {
- Id interface{} `json:"id"`
- Name string `json:"name"`
- Number int64 `json:"number"`
- //Accounted interface{} `json:"accounted"`
- }
- //采购单位-项目数量TOP3
- countMap := []interface{}{}
- for _, v := range thisBuyerRow.BuyerCountTop3.Buckets {
- var _d number_projects
- _d.Name = v.Key
- _d.Number = v.BuyerCount.Value
- /*if thisBuyerRow.ProjectCount.Value != 0 {
- _d.Accounted = float64(v.BuyerCount.Value) / float64(thisBuyerRow.ProjectCount.Value)
- }*/
- var ss []interface{}
- for _, v1 := range v.SWinnerTop.Buckets {
- var _dd number_project
- _dd.Number = v1.BuyerWinnerAmount.Value
- if winnerName[v1.Key] == "" {
- continue
- }
- _dd.Name = winnerName[v1.Key]
- _dd.Id = encodeId(v1.Key)
- /*if _d.Number != 0 {
- _dd.Accounted = float64(v1.BuyerWinnerAmount.Value) / float64(_d.Number)
- }*/
- ss = append(ss, _dd)
- }
- _d.Data = ss
- if !offline {
- if len(ss) > Top3 {
- _d.Data = ss[:Top3]
- }
- if len(countMap) >= Top30 {
- break
- }
- }
- countMap = append(countMap, _d)
- }
- //采购单位-项目金额TOP3
- type amount_projects struct {
- Name string `json:"name"`
- Amount float64 `json:"amount"`
- //Accounted interface{} `json:"accounted"`
- Data interface{} `json:"winnertop3"`
- }
- type amount_project struct {
- Id interface{} `json:"id"`
- Name string `json:"name"`
- Amount float64 `json:"amount"`
- //Accounted interface{} `json:"accounted"`
- }
- amountMap := []interface{}{}
- for _, v := range thisBuyerRow.BuyerAmountTop3.Buckets {
- if v.BuyerAmount.Value == 0 {
- continue
- }
- var _d amount_projects
- _d.Name = v.Key
- _d.Amount = v.BuyerAmount.Value
- /*if thisBuyerRow.ProjectAmount.Value != 0 {
- _d.Accounted = v.BuyerAmount.Value / thisBuyerRow.ProjectAmount.Value
- }*/
- var ss []interface{}
- for _, v1 := range v.SWinnerTop.Buckets {
- if v1.BuyerWinnerAmount.Value == 0 || winnerName[v1.Key] == "" {
- continue
- }
- var _dd amount_project
- _dd.Amount = v1.BuyerWinnerAmount.Value
- _dd.Name = winnerName[v1.Key]
- _dd.Id = encodeId(v1.Key)
- /* if _d.Amount != 0 {
- _dd.Accounted = v1.BuyerWinnerAmount.Value / _d.Amount
- }*/
- ss = append(ss, _dd)
- }
- _d.Data = ss
- if !offline {
- if len(ss) > Top3 {
- _d.Data = ss[:Top3]
- }
- if len(amountMap) >= Top30 {
- break
- }
- }
- amountMap = append(amountMap, _d)
- }
- rMap.Store("buyer_time_distribution", buyerMap)
- rMap.Store("buyer_count_top3", countMap)
- rMap.Store("buyer_amount_top3", amountMap)
- }
- // top3数量计算
- func CountCompute(thisRow AreaCTop, types string, eid map[string]string, Offline bool) (rData []map[string]interface{}) {
- if types == "area" {
- area_infos := thisRow.AreaCountTop3
- for _, v := range area_infos.Buckets {
- if v.Total == 0 {
- break
- }
- rM := map[string]interface{}{}
- rWinner := []map[string]interface{}{}
- rM["name"] = v.Area
- rM["area_count"] = v.Total
- for _, va := range v.WinnerTop.Buckets {
- if va.WinnerTotal == 0 {
- break
- }
- rW := map[string]interface{}{}
- id := encodeId(va.Winner)
- rW["id"] = id
- rW["winner"] = eid[va.Winner]
- if eid[va.Winner] == "" {
- continue
- }
- rW["winner_total"] = va.WinnerTotal
- rWinner = append(rWinner, rW)
- }
- if !Offline && len(rWinner) == 0 {
- continue
- }
- rM["winner"] = rWinner
- //实时分析 取前三个
- if len(rWinner) > Top3 && !Offline {
- rM["winner"] = rWinner[:Top3]
- }
- if thisRow.Total != 0 {
- vv := float64(v.Total) / float64(thisRow.Total)
- rM["area_scale"] = vv
- } else {
- rM["area_scale"] = 0
- }
- rData = append(rData, rM)
- }
- } else {
- area_infos := thisRow.BuyclassCountTop3
- for _, v := range area_infos.Buckets {
- if v.Total == 0 {
- break
- }
- rM := map[string]interface{}{}
- rWinner := []map[string]interface{}{}
- rM["name"] = v.Buyclass
- rM["buyclass_count"] = v.Total
- for _, va := range v.BidcountTop.Buckets {
- if va.WinnerTotal == 0 {
- break
- }
- rW := map[string]interface{}{}
- id := encodeId(va.Winner)
- rW["id"] = id
- rW["winner"] = eid[va.Winner]
- if eid[va.Winner] == "" {
- continue
- }
- rW["winner_total"] = va.WinnerTotal
- rWinner = append(rWinner, rW)
- }
- if !Offline && len(rWinner) == 0 {
- continue
- }
- rM["winner"] = rWinner
- //实时分析 取前三个
- if len(rWinner) > Top3 && !Offline {
- rM["winner"] = rWinner[:Top3]
- }
- if thisRow.Total != 0 {
- vv := float64(v.Total) / float64(thisRow.Total)
- rM["buyclass_scale"] = vv
- } else {
- rM["buyclass_scale"] = 0
- }
- rData = append(rData, rM)
- }
- }
- return
- }
- // top3金额计算
- func AmountCompute(thisRow AreaCTop, types string, eid map[string]string, Offline bool) (rData []map[string]interface{}) {
- if types == "area" {
- area_infos := thisRow.AreaAmountTop3
- for _, v := range area_infos.Buckets {
- if v.AreaAmount.Amount == 0 {
- break
- }
- rM := map[string]interface{}{}
- rWinner := []map[string]interface{}{}
- rM["name"] = v.Key
- rM["area_amount"] = v.AreaAmount.Amount
- for _, va := range v.WinnerTop.Buckets {
- if va.WinnerAmount.Amount == 0 {
- break
- }
- rW := map[string]interface{}{}
- id := encodeId(va.Winner)
- rW["id"] = id
- rW["winner"] = eid[va.Winner]
- if eid[va.Winner] == "" {
- continue
- }
- rW["winner_amount"] = va.WinnerAmount.Amount
- //rW["amount_scale"] = va.WinnerAmount.Amount / v.AreaAmount.Amount
- rWinner = append(rWinner, rW)
- }
- rM["winner"] = rWinner
- //实时分析 取前三个
- if len(rWinner) > Top3 && !Offline {
- rM["winner"] = rWinner[:Top3]
- }
- if thisRow.Amount.Value != 0 {
- vv := v.AreaAmount.Amount / thisRow.Amount.Value
- rM["area_scale"] = vv
- } else {
- rM["area_scale"] = 0
- }
- rData = append(rData, rM)
- }
- } else {
- area_infos := thisRow.BuyclassAmountTop3
- for _, v := range area_infos.Buckets {
- if v.AreaAmount.Amount == 0 {
- break
- }
- rM := map[string]interface{}{}
- rWinner := []map[string]interface{}{}
- rM["name"] = v.Key
- rM["buyclass_amount"] = v.AreaAmount.Amount
- for _, va := range v.WinnerTop.Buckets {
- if va.WinnerAmount.Amount == 0 {
- break
- }
- rW := map[string]interface{}{}
- id := encodeId(va.Winner)
- rW["id"] = id
- rW["winner"] = eid[va.Winner]
- if eid[va.Winner] == "" {
- continue
- }
- rW["winner_amount"] = va.WinnerAmount.Amount
- //rW["amount_scale"] = va.WinnerAmount.Amount / v.AreaAmount.Amount
- rWinner = append(rWinner, rW)
- }
- rM["winner"] = rWinner
- //实时分析 取前三个
- if len(rWinner) > Top3 && !Offline {
- rM["winner"] = rWinner[:Top3]
- }
- if thisRow.Amount.Value != 0 {
- vv := v.AreaAmount.Amount / thisRow.Amount.Value
- rM["buyclass_scale"] = vv
- } else {
- rM["buyclass_scale"] = 0
- }
- rData = append(rData, rM)
- }
- }
- return
- }
- // 企业id查企业名 传入数组
- func IDToName(ids []string) (iMap map[string]string) {
- iMap = map[string]string{}
- d := strings.Join(ids, `","`)
- finalSql := fmt.Sprintf(queryIdto, d, len(ids))
- log.Println("IDToName sql:", finalSql)
- hits := elastic.Get("qyxy", "qyxy", finalSql)
- if hits == nil || len(*hits) == 0 {
- return
- }
- for _, item := range *hits {
- id, _ := item["_id"].(string)
- name, _ := item["company_name"].(string)
- if id != "" && name != "" {
- iMap[id] = name
- }
- }
- return
- }
- // 客户分布
- func CustomerDistribute(thisRow AreaCTop) (data []map[string]interface{}, otherData map[string]interface{}) {
- var (
- total int64
- amount float64
- )
- for _, v := range thisRow.BuyerclassScale.Buckets {
- if v.AreaTotal == 0 && v.BuyclassAmount.Value == 0 {
- continue
- }
- rM := map[string]interface{}{}
- rM["buyclass"] = v.Area
- rM["total"] = v.AreaTotal
- rM["amount"] = v.BuyclassAmount.Value
- total += v.AreaTotal
- amount += v.BuyclassAmount.Value
- data = append(data, rM)
- }
- //for _, v := range thisRow.BuyerclassScaleOther.Buckets {
- // total += v.AreaTotal
- // amount += v.BuyclassAmount.Value
- //}
- otherData = make(map[string]interface{})
- otherData["buyclass"] = "其它"
- if pCount := common.Int64All(thisRow.ProjectCount.DocCount) - total; pCount > 0 {
- otherData["total"] = pCount
- }
- if pAmount := thisRow.Amount.Value - amount; pAmount > 0 {
- otherData["amount"] = pAmount
- }
- return
- }
- // 地区分布
- func AreaDistribute(thisRow AreaCTop) (data []map[string]interface{}) {
- for _, v := range thisRow.AreaDistribution.Buckets {
- rM := map[string]interface{}{}
- rM["area"] = v.Area
- rM["total"] = v.AreaTotal
- rM["amount"] = v.AreaAmount.Value
- var rmc []map[string]interface{}
- if len(v.CityGroup.Buckets) > 0 {
- for _, c := range v.CityGroup.Buckets {
- rmc = append(rmc, map[string]interface{}{
- "city": c.City,
- "total": c.CityTotal,
- "amount": c.CityAmount.Value,
- })
- }
- rM["areaDetails"] = rmc
- }
- data = append(data, rM)
- }
- return
- }
- // 项目规模分布
- func ProjectScale(thisRow AreaCTop) (data []interface{}) {
- ammount := thisRow.Amount.Value
- total := thisRow.CountNot0.Count
- buckets := thisRow.SortpriceRanges.Buckets
- type Scale struct {
- Name string `bson:"Name"`
- Persent_c float64 `bson:"Persent_c"`
- Persent_a float64 `bson:"Persent_a"`
- Ammount float64 `json:"ammount"`
- Total int64 `json:"total"`
- }
- for _, v := range buckets {
- data = append(data, Scale{
- Name: v.Name,
- Ammount: v.SumSortprice.Value,
- Total: v.Total,
- Persent_a: Formula(float64(v.Total), float64(total)),
- Persent_c: Formula(v.SumSortprice.Value, ammount),
- })
- }
- return
- }
- // 计算公式
- func Formula(current, total float64) (result float64) {
- if total == 0 {
- return 0
- }
- result = current / total
- return
- }
- func InterToSliceString(obj interface{}) []string {
- var sli = make([]string, 0)
- if obj == nil {
- return sli
- }
- if _, ok := obj.([]interface{}); ok {
- for _, i := range obj.([]interface{}) {
- sli = append(sli, i.(string))
- }
- }
- if _, ok := obj.([]string); ok {
- return obj.([]string)
- }
- return sli
- }
- func sequential(now, old float64) interface{} {
- if old == 0 {
- return nil
- }
- return (now - old) / old
- }
- // GetAggs 聚合查询
- func GetAggs(index, itype, query string) esV7.Aggregations {
- v1, _, _ := elastic.GetAggs(index, itype, query)
- return v1
- }
- func GetAggsWithCount(index, itype, query string) (esV7.Aggregations, int64) {
- if len(query) > 0 {
- query = `{"track_total_hits": true,` + query[1:]
- }
- v1, v2, _ := elastic.GetAggs(index, itype, query)
- return v1, v2
- }
- func getPreviousMarket(sTime, eTime time.Time) int64 {
- var os_time int64
- s_time := sTime
- if SEMonth(sTime, eTime) {
- var min int
- //统计月份
- for sTime.Before(eTime) {
- sTime = sTime.AddDate(0, 1, 0)
- min++
- }
- os_time = s_time.AddDate(0, -min, 0).Unix()
- } else {
- os_time = s_time.AddDate(0, 0, -int(math.Ceil(eTime.Sub(sTime).Hours()/24))).Unix()
- }
- return os_time
- }
- func Rest(res esV7.Aggregations, thisRow *marketTime) {
- for name, object := range res {
- bArr, err := object.MarshalJSON()
- if len(bArr) == 0 || err != nil {
- continue
- }
- if name == "thismarket" {
- if json.Unmarshal(bArr, &thisRow.Thismarket) != nil {
- continue
- }
- } else if name == "oldmarket" {
- if json.Unmarshal(bArr, &thisRow.Oldmarket) != nil {
- continue
- }
- } else if name == "monthtime" {
- if json.Unmarshal(bArr, &thisRow.Monthtime) != nil {
- continue
- }
- } else if name == "yeartime" {
- if json.Unmarshal(bArr, &thisRow.Yeartime) != nil {
- continue
- }
- }
- }
- }
- func GetMonthData(isOffline bool, sTime, eTime time.Time) (bool, string) {
- var _b bool
- //整月多取一个月进行环比
- if !isOffline && SEMonth(sTime, eTime) {
- _b = true
- sTime = sTime.AddDate(0, -1, 0)
- }
- return _b, getBidamountStatistics(sTime, eTime)
- }
- func getBidamountStatistics(sTime, eTime time.Time) string {
- timeRange := ``
- tmpTime, rTime, tEndTime := sTime, sTime, getMonthRange(eTime, false)
- for rTime.Before(tEndTime) {
- ts, te := getMonthRange(tmpTime, true), getMonthRange(tmpTime, false)
- if sTime == tmpTime {
- ts = sTime
- }
- if te == tEndTime {
- te = eTime
- }
- if ts.Before(te) {
- timeRange += fmt.Sprintf(`{"key":"%s","from":%d,"to":%d},`, fmt.Sprintf("%d-%d", ts.Year(), ts.Month()), ts.Unix(), te.Unix())
- }
- rTime = rTime.AddDate(0, 1, 0)
- if int(rTime.Month())-int(tmpTime.Month()) > 1 {
- rTime = rTime.AddDate(0, -1, 0)
- }
- tmpTime = rTime
- }
- if timeRange == `` {
- return ""
- }
- return timeRange[:len(timeRange)-1]
- }
- // getMonthRange获取月份范围
- // isStart true本月月初 false 本月月末(下月月初)
- func getMonthRange(t time.Time, isStart bool) time.Time {
- if isStart {
- return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
- }
- return time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, t.Location())
- }
- func GetYearData(isOffline bool, sTime, eTime time.Time) (bool, string) {
- var _b bool
- //整月多取一个月进行环比
- if !isOffline && sTime.Month() == 1 && sTime.Day() == 1 && eTime.Month() == 12 && eTime.Day() == 31 {
- _b = true
- sTime = sTime.AddDate(-1, 0, 0)
- }
- return _b, getCommonYearStatistics(sTime, eTime)
- }
- // getYearRange获取月份范围
- // isStart true本月月初 false 本月月末(下月月初)
- func getYearRange(t time.Time, isStart bool) time.Time {
- if isStart {
- return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
- }
- return time.Date(t.Year()+1, 1, 1, 0, 0, 0, 0, t.Location())
- }
- // 年份统计
- func getCommonYearStatistics(sTime, eTime time.Time) string {
- timeRange := ``
- tmpTime, rTime, tEndTime := sTime, sTime, getYearRange(eTime, false)
- for rTime.Before(tEndTime) {
- ts, te := getYearRange(tmpTime, true), getYearRange(tmpTime, false)
- if sTime == tmpTime {
- ts = sTime
- }
- if te == tEndTime {
- te = eTime
- }
- if ts.Before(te) {
- timeRange += fmt.Sprintf(`{"key":"%d","from":%d,"to":%d},`, ts.Year(), ts.Unix(), te.Unix())
- }
- rTime = rTime.AddDate(1, 0, 0)
- tmpTime = rTime
- }
- if timeRange == `` {
- return ""
- }
- return timeRange[:len(timeRange)-1]
- }
- func GetEntNameByIds(ids []string) (returnMap map[string]string) {
- returnMap = map[string]string{}
- if len(ids) == 0 {
- return
- }
- list := elastic.Get("qyxy", "qyxy", fmt.Sprintf(`{"query":{"bool":{"must":[{"terms":{"_id":["%s"]}}]}},"_source":["_id","company_name"],"size":%d}`, strings.Join(ids, `","`), len(ids)))
- if list == nil || len(*list) == 0 {
- return
- }
- for _, item := range *list {
- id, _ := item["_id"].(string)
- name, _ := item["company_name"].(string)
- if id != "" && name != "" {
- returnMap[id] = name
- }
- }
- return
- }
- // GetMsgOpen 获取用户服务通知开关是否开启
- func GetMsgOpen(mgo *mongodb.MongodbSim, mgoUserId string, positionType int, entId, entUserId int) bool {
- //-- 服务通知不区分身份,只存在user表中
- //pushSetMap := &map[string]interface{}{}
- //if positionType == 1 {
- // pushSetMap, _ = mgo.FindOne("ent_user", map[string]interface{}{"i_entid": entId, "i_userid": entUserId})
- //} else {
- // pushSetMap, _ = mgo.FindById("user", mgoUserId, `{"o_pushset":1,"s_m_openid":1}`)
- //}
- pushSetMap, _ := mgo.FindById("user", mgoUserId, `{"o_pushset":1,"s_m_openid":1}`)
- //log.Println(mgoUserId, pushSetMap)
- if pushSetMap != nil && len(*pushSetMap) > 0 {
- pushset := common.ObjToMap((*pushSetMap)["o_pushset"])
- if pushset == nil || len(*pushset) == 0 {
- return false
- }
- msgServicePushSet := common.ObjToMap((*pushset)["o_msg_service"])
- if msgServicePushSet != nil {
- if common.IntAll((*msgServicePushSet)["i_apppush"]) == 1 || common.IntAll((*msgServicePushSet)["i_wxpush"]) == 1 {
- return true
- }
- }
- }
- return false
- }
- func encodeId(sid string) string {
- if sid == "" || sid == "-" { //不存在的id为-
- return ""
- }
- return encrypt.EncodeArticleId2ByCheck(sid)
- }
- // getGroupKeywordArr 模糊拆分为多个精准匹配
- func getGroupKeywordArr(res []viewKeyWord) (rData []viewKeyWord) {
- for _, kw := range res {
- if kw.MatchWay == 1 {
- for _, kk := range kw.Keyword {
- rData = append(rData, viewKeyWord{
- Keyword: []string{kk},
- Exclude: kw.Exclude,
- })
- }
- for _, kk := range kw.Appended {
- rData = append(rData, viewKeyWord{
- Keyword: []string{kk},
- Exclude: kw.Exclude,
- })
- }
- } else {
- rData = append(rData, kw)
- }
- }
- return
- }
- // getAllKeywordArr 获取所有匹配词
- func getAllKeywordArr(res []keyWordGroup) (rData []viewKeyWord) {
- for _, kwg := range res {
- rData = append(rData, getGroupKeywordArr(kwg.A_Key)...)
- }
- return
- }
- func getKeyWordSql(v viewKeyWord, matchingMode string) string {
- var (
- shoulds, must_not []string
- //默认查询项目名称与标的物
- localMultiMatch = `{"multi_match": {"query": %s,"type": "phrase", "fields": [` + FieldsDetail + `]}}`
- )
- if matchingMode == "title" { //只匹配项目名称
- localMultiMatch = `{"multi_match": {"query": %s,"type": "phrase", "fields": [` + Fields + `]}}`
- }
- //附加词
- for _, vv := range v.Keyword {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- shoulds = append(shoulds, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
- }
- for _, vv := range v.Appended {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- shoulds = append(shoulds, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
- }
- //排除词
- for _, vv := range v.Exclude {
- vv = strings.TrimSpace(vv)
- if vv == "" {
- continue
- }
- must_not = append(must_not, fmt.Sprintf(localMultiMatch, "\""+vv+"\""))
- }
- //添加
- if len(shoulds) > 0 {
- notStr := ""
- if len(must_not) > 0 {
- notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
- }
- return fmt.Sprintf(queryBoolMustAnd, strings.Join(shoulds, ","), notStr)
- }
- return ""
- }
- // 判断是否月初到月末
- func SEMonth(sTime, eTime time.Time) bool {
- var day int
- month := int(eTime.Month())
- if month == 2 {
- if eTime.Year()%4 == 0 {
- day = 29
- } else {
- day = 28
- }
- } else {
- day = yMDay[month]
- }
- if sTime.Day() == 1 && eTime.Day() == day {
- return true
- }
- return false
- }
|