123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- package front
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- "app.yhyue.com/moapp/jypkg/public"
- "encoding/json"
- "fmt"
- "jy/src/jfw/jyutil"
- "math/rand"
- "strings"
- //_ "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:"直辖市关键词"`
- NotShowCityCode map[string]bool `json:"notShowCityCode" doc:"不展示的城市,因数据量少"`
- }
- KeyWordSiteShow struct {
- Code string `json:"code" doc:"代码"`
- Name string `json:"name" doc:"名称"`
- //KeyWord string `json:"keyWord" doc:"对应关键词"`
- }
- argument struct {
- Name string
- Url string
- }
- keyBidding struct {
- code string
- area string
- city string
- district string
- }
- keyFw struct {
- code string
- keyword string
- }
- )
- var (
- siteCodeMap map[string]*KeyWordSiteNode
- specialArea = map[string]bool{"北京": true, "上海": true, "天津": true, "重庆": true}
- KeyBiddingAreaMap []keyBidding
- KeyFwArr []keyFw
- KeyJyMap map[string]string
- DistrictSArr []keyFw
- )
- func init() {
- siteCodeMap = make(map[string]*KeyWordSiteNode)
- KeyJyMap = make(map[string]string)
- 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() {
- notShowCityCodeMap := map[string]bool{}
- for _, cityCode := range strings.Split(gconv.String(m["notShowCityCode"]), ",") {
- notShowCityCodeMap[cityCode] = true
- }
- 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"]),
- NotShowCityCode: notShowCityCodeMap,
- }
- siteCodeMap[node.Code] = node
- }
- }
- KeyBiddingArea, _ := config.Sysconfig["keyBiddingArea"].(string)
- resArr, err := g.DB().Query(gctx.New(), fmt.Sprintf(`SELECT area ,city,district,pcode FROM seo_area_code where state = 1 and class = 4 and area in ('%s') %s`, strings.ReplaceAll(KeyBiddingArea, ",", `','`), `and (district LIKE '%县%' or district LIKE '%市%')`))
- if err == nil && !resArr.IsEmpty() {
- for _, m := range resArr.List() {
- KeyBiddingAreaMap = append(KeyBiddingAreaMap, keyBidding{
- area: gconv.String(m["area"]),
- city: gconv.String(m["city"]),
- district: gconv.String(m["district"]),
- code: gconv.String(m["pcode"]),
- })
- }
- }
- districtSData, err := g.DB().Query(gctx.New(), `SELECT site_code,keywords,alias FROM seo_siteKeywords_district where site_code = 1 `)
- if err == nil && !districtSData.IsEmpty() {
- for _, m := range districtSData.List() {
- DistrictSArr = append(DistrictSArr, keyFw{
- keyword: gconv.String(m["keywords"]),
- code: fmt.Sprintf("%s_S%d", m["alias"], m["site_code"]),
- })
- }
- }
- fwResArr, err := g.DB().Query(gctx.New(), `SELECT id, keyword FROM new_keyword_unified where unifiedSign = 'fw' and state = 1`)
- if err == nil && !fwResArr.IsEmpty() {
- for _, m := range fwResArr.List() {
- KeyFwArr = append(KeyFwArr, keyFw{
- keyword: gconv.String(m["keyword"]),
- code: gconv.String(m["id"]),
- })
- }
- }
- //区县教育局
- districtJyResArr, err := g.DB().Query(gctx.New(), `SELECT alias,keywords FROM seo_siteKeywords_district where site_code = 12`)
- if err == nil && !districtJyResArr.IsEmpty() {
- for _, m := range districtJyResArr.List() {
- KeyJyMap[gconv.String(m["alias"])] = gconv.String(m["keywords"])
- }
- }
- }
- type ClassCode struct {
- Class int
- Code string
- Url string
- }
- // 政府招标
- 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]ClassCode)
- res, err := g.DB().Query(gctx.New(), `SELECT area,city,pcode,class FROM seo_area_code where state = 1 and (class = 1 or class = 2 or class = 3)`)
- if !res.IsEmpty() && len(res.List()) > 0 && err == nil {
- for _, v := range res.List() {
- switch common.IntAll(v["class"]) {
- case 1, 3:
- name := common.ObjToString(v["area"])
- areaM[name] = ClassCode{
- Class: common.IntAll(v["class"]),
- Code: common.ObjToString(v["pcode"]),
- Url: "/list/area/%s_%s",
- }
- default:
- name := common.ObjToString(v["city"])
- areaM[name] = ClassCode{
- Class: common.IntAll(v["class"]),
- Code: common.ObjToString(v["pcode"]),
- Url: "/list/city/%s_%s",
- }
- }
- }
- }
- 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 node.NotShowCityCode[acronym.Code] {
- continue
- }
- if !siteCode[code] {
- if node.Alias != "" {
- tagName = node.Alias
- } else {
- switch acronym.Class {
- case 1:
- tagName = node.AreaKeyWord
- case 2:
- tagName = node.CityKeyWord
- case 3:
- tagName = node.CitySpecialKeyWord
- }
- }
- data = append(data, argument{
- fmt.Sprintf("%s%s", name, tagName),
- fmt.Sprintf(acronym.Url, acronym.Code, code),
- })
- siteCode[code] = true
- break
- }
- }
- } else {
- for code, node := range siteCodeMap {
- var tagName string
- if node.NotShowCityCode[acronym.Code] {
- continue
- }
- if node.Alias != "" {
- tagName = node.Alias
- } else {
- switch acronym.Class {
- case 1:
- tagName = node.AreaKeyWord
- case 2:
- tagName = node.CityKeyWord
- case 3:
- tagName = node.CitySpecialKeyWord
- }
- }
- data = append(data, argument{
- fmt.Sprintf("%s%s", name, tagName),
- fmt.Sprintf(acronym.Url, acronym.Code, code),
- })
- break
- }
- }
- }
- //随机获取10个区县教育局单位
- var count int
- for m, n := range KeyJyMap {
- count++
- if count > number {
- break
- }
- data = append(data, argument{
- n,
- fmt.Sprintf("/list/city/%s_S12", m),
- })
- }
- if len(data) > 0 {
- redis.Put(RedisNameNew, redisKey, data, RedisTimeout)
- }
- return data
- }
- // 重点招标区域
- func DistrictsTender(number int) []argument {
- redisKey := "districtsTender"
- 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
- }
- }
- var (
- data []argument
- )
- for _, i2 := range jyutil.GenerateRandomNumber(0, len(KeyBiddingAreaMap), number) {
- acronym := KeyBiddingAreaMap[i2]
- data = append(data, argument{
- common.If(strings.Contains(acronym.district, "区"), fmt.Sprintf("%s%s", acronym.city, acronym.district), acronym.district).(string),
- fmt.Sprintf("/list/city/%s.html", acronym.code),
- })
- }
- if data != nil && len(data) > 0 {
- redis.Put(RedisNameNew, redisKey, data, RedisTimeout)
- }
- return data
- }
- func DistrictsSList(number int) []argument {
- redisKey := "districtsSList"
- 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
- }
- }
- var (
- data []argument
- )
- for _, i2 := range jyutil.GenerateRandomNumber(0, len(DistrictSArr), number) {
- acronym := DistrictSArr[i2]
- data = append(data, argument{
- acronym.keyword,
- fmt.Sprintf("/list/city/%s/", acronym.code),
- })
- }
- if data != nil && len(data) > 0 {
- 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 {
- listData := []map[string]interface{}{}
- if len(*data) > 30 {
- r := rand.Intn(len(*data) - 30)
- listData = (*data)[r : r+30]
- } else {
- listData = *data
- }
- for _, m := range listData {
- 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
- }
- // ServiceProcurement 服务类采购
- func ServiceProcurement(number int) []argument {
- redisKey := "ServiceProcurement"
- 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
- }
- }
- var (
- data []argument
- )
- for _, i2 := range jyutil.GenerateRandomNumber(0, len(KeyFwArr), number) {
- acronym := KeyFwArr[i2]
- data = append(data, argument{
- acronym.keyword,
- fmt.Sprintf("/fuwu/fw%03s.html", acronym.code),
- })
- }
- redis.Put(RedisNameNew, redisKey, data, RedisTimeout)
- return data
- }
|