123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- package front
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- "app.yhyue.com/moapp/jypkg/public"
- "encoding/json"
- "fmt"
- //_ "github.com/gogf/gf/contrib/drivers/clickhouse/v2"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/gogf/gf/v2/util/gconv"
- "jy/src/jfw/config"
- )
- // HotIndustryAndRegion 热门行业/地区年度招标趋势
- func HomeBiddingTrends() []config.IndustryTrend {
- return config.IndustryTrendConfig
- }
- type (
- KeyWordSiteRoot struct {
- SiteCodeMap map[string]*KeyWordSiteNode //省份直辖市简称对照
- SiteArr []*KeyWordSiteNode
- }
- KeyWordSiteNode struct {
- Code string `json:"code" doc:"代码"`
- Alias string `json:"alias" doc:"名称"`
- AreaKeyWord string `json:"areaKeyword" doc:"省份关键词"`
- CityKeyWord string `json:"cityKeyword" doc:"城市关键词"`
- CitySpecialKeyWord string `json:"citySpecialKeyword" doc:"直辖市关键词"`
- }
- KeyWordSiteShow struct {
- Code string `json:"code" doc:"代码"`
- Name string `json:"name" doc:"名称"`
- //KeyWord string `json:"keyWord" doc:"对应关键词"`
- }
- argument struct {
- Name string
- Url string
- }
- )
- var (
- siteCodeMap map[string]*KeyWordSiteNode
- specialArea = map[string]bool{"北京": true, "上海": true, "天津": true, "重庆": true}
- )
- func init() {
- siteCodeMap = make(map[string]*KeyWordSiteNode)
- res, err := g.DB().Query(gctx.New(), `SELECT * FROM seo_siteKeywords_splicing order by site_code asc`)
- if err == nil && len(res.List()) > 0 {
- for _, m := range res.List() {
- node := &KeyWordSiteNode{
- Code: fmt.Sprintf("S%s", gconv.String(m["site_code"])),
- Alias: gconv.String(m["alias"]),
- AreaKeyWord: gconv.String(m["area"]),
- CityKeyWord: gconv.String(m["city"]),
- CitySpecialKeyWord: gconv.String(m["city_special"]),
- }
- siteCodeMap[node.Code] = node
- }
- }
- }
- // 政府招标
- func GovernmentTender(number int) []argument {
- redisKey := "governmentTender"
- redisData := redis.Get(RedisNameNew, redisKey)
- if redisData != nil {
- if d, err := json.Marshal(redisData); err == nil {
- var signature []argument
- json.Unmarshal(d, &signature)
- return signature
- }
- }
- areaM := make(map[string]string)
- //省份
- areaMap, _ := config.Seoconfig["area"].(map[string]interface{})
- if areaMap != nil && len(areaMap) > 0 {
- for k, v := range areaMap {
- area := v.(map[string]interface{})
- name := common.ObjToString(area["NAME"])
- if name != "全国" && name != "香港" && name != "澳门" && name != "台湾" {
- areaM[name] = k
- }
- }
- }
- var (
- data []argument
- upperLimit int
- )
- siteCode := make(map[string]bool)
- for name, acronym := range areaM {
- upperLimit++
- if upperLimit > number {
- break
- }
- if len(siteCode) < len(siteCodeMap) {
- for code, node := range siteCodeMap {
- var tagName string
- if !siteCode[code] {
- if node.Alias != "" {
- tagName = node.Alias
- } else {
- tagName = common.If(specialArea[name], node.CitySpecialKeyWord, node.AreaKeyWord).(string)
- }
- data = append(data, argument{
- fmt.Sprintf("%s%s", name, tagName),
- fmt.Sprintf("/list/area/%s_%s", acronym, code),
- })
- siteCode[code] = true
- break
- }
- }
- } else {
- for code, node := range siteCodeMap {
- var tagName string
- if node.Alias != "" {
- tagName = node.Alias
- } else {
- tagName = common.If(specialArea[name], node.CitySpecialKeyWord, node.AreaKeyWord).(string)
- }
- data = append(data, argument{
- fmt.Sprintf("%s%s", name, tagName),
- fmt.Sprintf("/list/area/%s_%s", acronym, code),
- })
- break
- }
- }
- }
- redis.Put(RedisNameNew, redisKey, data, RedisTimeout)
- return data
- }
- /*
- const (
- PSearch_DecMust = `"bidstatus": ["中标","成交","合同","单一"]`
- query_bool_must = `{"terms": {%s}}`
- )
- var (
- industryMap map[string][]string
- )
- type ServiceRes struct {
- industry []industryRes
- }
- type industryRes struct {
- name string
- areas []areaRes
- }
- type areaRes struct {
- name string
- data []map[string]interface{}
- }
- type subzone struct {
- industry, province string
- }
- type marketBuckets struct {
- ProjectCount struct {
- DocCount int `json:"doc_count"`
- } `json:"project_count"`
- }
- func getQuarter(st int64) int {
- currentTime := time.Unix(st, 0)
- // 获取当前月份
- currentMonth := int(currentTime.Month())
- // 获取当前月份所属的季度
- return (currentMonth-1)/3 + 1
- }
- func init() {
- IndustryInit()
- BiddingTrendInit()
- }
- func BiddingTrend(st int64, et int64) {
- currentQuarter := getQuarter(st)
- for _, industry := range config.ServiceArea.Industry {
- ds := subzone{
- industry: industry,
- }
- for _, area := range config.ServiceArea.Area {
- ds.province = area
- res, _, _ := elastic.GetAggs("projectset", "projectset", GetCommonQuerySql(st, et, area, industry))
- if res != nil {
- thisRow := marketBuckets{}
- for name, object := range res {
- bArr, err := object.MarshalJSON()
- if len(bArr) == 0 || err != nil {
- break
- }
- if name == "project_count" {
- _ = json.Unmarshal(bArr, &thisRow.ProjectCount)
- quarterData := map[string]interface{}{
- "name": fmt.Sprintf("%d-第%d季度", time.Now().Year(), currentQuarter),
- "count": thisRow.ProjectCount.DocCount,
- }
- var redisDataArr []map[string]interface{}
- redisData, _ := redis.Get(RedisNameNew, fmt.Sprintf("biddingTrend_%s_%s", industry, area)).([]interface{})
- if redisData != nil && len(redisData) > 0 {
- redisDataArr = common.ObjArrToMapArr(redisData)
- if len(redisDataArr) == 4 {
- redisDataArr = append(redisDataArr[1:], quarterData)
- redis.Put(RedisNameNew, fmt.Sprintf("biddingTrend_%s_%s", industry, area), redisDataArr, 24*3600*120)
- continue
- }
- }
- redisDataArr = append(redisDataArr, quarterData)
- redis.Put(RedisNameNew, fmt.Sprintf("biddingTrend_%s_%s", industry, area), redisDataArr, 24*3600*120)
- }
- }
- }
- }
- }
- }
- func IndustryInit() {
- industryMap = make(map[string][]string)
- for _, industry := range config.ServiceArea.Industry {
- industryData := public.BaseMysql.SelectBySql(fmt.Sprintf(`SELECT CONCAT(b.name, '_', a.name) as name
- FROM global_common_data.code_bidscope a
- LEFT JOIN (
- SELECT code, name
- FROM global_common_data.code_bidscope
- WHERE name = '%s'
- ) b ON a.pcode = b.code
- WHERE b.code IS NOT NULL;`, industry))
- var names []string
- for _, v := range *industryData {
- names = append(names, common.InterfaceToStr(v["name"]))
- }
- industryMap[industry] = names
- }
- }
- func BiddingTrendInit() {
- for _, industry := range config.ServiceArea.Industry {
- ds := subzone{
- industry: industry,
- }
- for _, area := range config.ServiceArea.Area {
- ds.province = area
- ok, err := redis.Exists(RedisNameNew, fmt.Sprintf("biddingTrend_%s_%s", industry, area))
- if ok && err == nil {
- continue
- }
- eQuarter := Quarter()
- sQuarter := eQuarter.AddDate(-1, 0, 0)
- var redisDataArr []map[string]interface{}
- for sQuarter.Before(eQuarter) {
- currentQuarter := getQuarter(sQuarter.Unix())
- res, _, _ := elastic.GetAggs("projectset", "projectset", GetCommonQuerySql(sQuarter.Unix(), sQuarter.AddDate(0, 3, 0).Unix(), area, industry))
- if res != nil {
- thisRow := marketBuckets{}
- for name, object := range res {
- bArr, err := object.MarshalJSON()
- if len(bArr) == 0 || err != nil {
- break
- }
- if name == "project_count" {
- _ = json.Unmarshal(bArr, &thisRow.ProjectCount)
- quarterData := map[string]interface{}{
- "name": fmt.Sprintf("%d-第%d季度", sQuarter.Year(), currentQuarter),
- "count": thisRow.ProjectCount.DocCount,
- }
- redisDataArr = append(redisDataArr, quarterData)
- }
- }
- }
- sQuarter = sQuarter.AddDate(0, 3, 0)
- }
- redis.Put(RedisNameNew, fmt.Sprintf("biddingTrend_%s_%s", industry, area), redisDataArr, 24*3600*120)
- }
- }
- }
- // GetCommonQuerySql 公共筛选
- func GetCommonQuerySql(st, et int64, area, industry string) string {
- var musts, bools []string
- //时间
- musts = append(musts, fmt.Sprintf(`{"range":{"jgtime":{"gte":%d,"lte":%d}}}`, st, et))
- //地区
- if area != "" {
- musts = append(musts, fmt.Sprintf(`{"terms":{"area":["%s"]}}`, area))
- }
- //行业
- if len(industryMap[industry]) > 0 {
- musts = append(musts, fmt.Sprintf(`{"terms":{"subscopeclass":["%s"]}}`, strings.Join(industryMap[industry], `","`)))
- }
- //分析报告中标状态限制
- musts = append(musts, fmt.Sprintf(query_bool_must, PSearch_DecMust))
- aa := fmt.Sprintf(`{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": %d}},"aggs":{"project_count": {"filter": {"match_all":{}}}},"size":0}`, strings.Join(musts, ","), strings.Join(bools, ","), common.If(len(bools) > 0, 1, 0).(int))
- fmt.Println(aa)
- return fmt.Sprintf(`{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": %d}},"aggs":{"project_count": {"filter": {"match_all":{}}}},"size":0}`, strings.Join(musts, ","), strings.Join(bools, ","), common.If(len(bools) > 0, 1, 0).(int))
- }
- func Quarter() time.Time {
- currentTime := time.Now()
- // 获取当前季度
- currentQuarter := (currentTime.Month()-1)/3 + 1
- // 计算当前季度的开始时间
- startMonth := (currentQuarter-1)*3 + 1
- quarterStart := time.Date(currentTime.Year(), startMonth, 1, 0, 0, 0, 0, currentTime.Location())
- return quarterStart
- }
- */
- // 热门采购数据
- func PurchasingData() []map[string]interface{} {
- redisData, _ := redis.Get(RedisNameNew, "hotPurchasingData").([]interface{})
- if len(redisData) > 0 {
- return common.ObjArrToMapArr(redisData)
- }
- var keyWords []map[string]interface{}
- data := public.BaseMysql.SelectBySql("SELECT keyword FROM data_supermarket GROUP BY keyword")
- if data != nil && len(*data) > 0 {
- for _, m := range *data {
- keyWords = append(keyWords, map[string]interface{}{
- "keyword": common.InterfaceToStr(m["keyword"]),
- "url": fmt.Sprintf("/datasmt/index_1?searchValue=%s", common.InterfaceToStr(m["keyword"])),
- })
- }
- redis.Put(RedisNameNew, "hotPurchasingData", keyWords, RedisTimeout)
- }
- return keyWords
- }
|