123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package service
- import (
- "context"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/gogf/gf/v2/util/gconv"
- "jybxseo/internal/consts"
- )
- var (
- JyBxSeoAreaRoot *AreaRoot = &AreaRoot{}
- )
- type (
- AreaRoot struct {
- areaTree map[string]*AreaNode //层级关系
- areaCodeMap map[string]*AreaNode //所有节点平铺 key:code
- areaNameMap map[string]*AreaNode //所有节点平铺 key:name
- rootList []*AreaNode //根节点
- Location []*AreaLocation //区位
- }
- AreaNode struct {
- Name string `json:"name" doc:"名称"`
- Code string `json:"code" doc:"代码"`
- Href string `json:"href" doc:"地址"`
- PNode *AreaNode `json:"pNode" doc:"父节点代码"`
- Type int `json:"type" doc:"1省份 2城市 3直辖市"`
- Child []*AreaNode `json:"child" doc:"子栏目"`
- }
- AreaLocation struct {
- Name string `json:"name" doc:"华北、华中、东北、华东、华南、西南、西北"`
- AreaList []*AreaNode `json:"areaList" doc:"地区列表"`
- }
- )
- func init() {
- JyBxSeoAreaRoot.LoadAreasFrom(gctx.New())
- }
- // LoadAreasFrom 加载地区表示
- func (aRoot *AreaRoot) LoadAreasFrom(ctx context.Context) {
- areaTree, areaCodeMap, areaNameMap := map[string]*AreaNode{}, map[string]*AreaNode{}, map[string]*AreaNode{}
- var rootArr []*AreaNode
- for _, m := range g.Cfg("global").MustGet(context.Background(), "allAreaTree").Maps() {
- root := &AreaNode{
- Name: gconv.String(m["name"]),
- Code: gconv.String(m["code"]),
- }
- root.Href = fmt.Sprintf(consts.WebDomainFormat, root.Code)
- if gconv.Bool(m["special"]) {
- root.Type = consts.CitySpecialType
- } else {
- root.Type = consts.AreaType
- }
- for _, n := range gconv.Maps(m["child"]) {
- node := &AreaNode{
- Name: gconv.String(n["name"]),
- Code: gconv.String(n["code"]),
- Type: consts.CityType,
- PNode: root,
- }
- node.Href = fmt.Sprintf("%s/%s/", root.Href, node.Code)
- root.Child = append(root.Child, node)
- areaCodeMap[node.Code] = node
- areaNameMap[node.Name] = node
- }
- areaTree[root.Code] = root
- areaCodeMap[root.Code] = root
- areaCodeMap[root.Name] = root
- rootArr = append(rootArr, root)
- }
- //加载区位
- for _, m := range g.Cfg("global").MustGet(context.Background(), "areaLocation").Maps() {
- tLocation := &AreaLocation{
- Name: gconv.String(m["name"]),
- AreaList: nil,
- }
- for _, areaCode := range gconv.Strings(m["values"]) {
- if node := aRoot.areaCodeMap[areaCode]; node != nil {
- tLocation.AreaList = append(tLocation.AreaList, node)
- }
- }
- aRoot.Location = append(aRoot.Location, tLocation)
- }
- aRoot.areaTree, aRoot.areaCodeMap = areaTree, areaCodeMap
- aRoot.areaNameMap = areaNameMap
- aRoot.rootList = rootArr
- }
- // GetData 获取地区数据
- func (aRoot *AreaRoot) GetData(ctx context.Context, maxTotal int, query *SeoBiddingQuery) []map[string]interface{} {
- var sql string
- var values []interface{}
- if query.district != "" {
- sql += " AND b.district=? "
- values = append(values, query.district)
- } else if query.city != "" {
- sql += " AND b.city=? "
- values = append(values, query.city)
- } else if query.area != "" {
- sql += " AND b.area=? "
- values = append(values, query.area)
- }
- if query.topType != "" {
- sql += " AND b.industry=? "
- values = append(values, query.topType)
- }
- if query.status > 0 {
- sql += " AND b.status=? "
- values = append(values, query.status)
- }
- values = append(values, maxTotal+50)
- queryRes, err := g.DB().Query(ctx, fmt.Sprintf(`SELECT b.bid_id
- FROM jyseo.column_bidList b
- WHERE 1=1 %s
- ORDER BY b.publish_time DESC
- LIMIT 0,?`, sql), values...)
- if err != nil || queryRes.IsEmpty() {
- return nil
- }
- return FillingBiddingBaseFields(ctx, queryRes.List())
- }
- func (an *AreaNode) GetChildNode(code string) *AreaNode {
- if len(an.Child) == 0 {
- return nil
- }
- for _, node := range an.Child {
- if node.Code == code {
- return node
- }
- }
- return nil
- }
- // GetNodeByCode 根据code
- func (aRoot *AreaRoot) GetNodeByCode(code string) *AreaNode {
- return aRoot.areaCodeMap[code]
- }
- // GetNodeByName 根据省份名字获取node
- func (aRoot *AreaRoot) GetNodeByName(name string) *AreaNode {
- return aRoot.areaNameMap[name]
- }
- // GetAllRootNodes 获取全部根地区节点
- func (aRoot *AreaRoot) GetAllRootNodes() []*AreaNode {
- return aRoot.rootList
- }
- // GetAllLocation 获取所有区位
- func (aRoot *AreaRoot) GetAllLocation() []*AreaLocation {
- return aRoot.Location
- }
- // GetSortLocation 获取关联区位信息
- // 规则:按现有的华东华中华南等分类,显示当前省份所在分类的其他省份,再显示下个地区分类的全部省份;以此类推循环;
- func (aRoot *AreaRoot) GetSortLocation(code string, num int) (rData []*AreaNode) {
- var isFind bool
- var findTimes int = 0
- findStart:
- for _, location := range aRoot.Location {
- if !isFind {
- for _, node := range location.AreaList {
- if node.Code == code {
- isFind = true
- break
- }
- }
- }
- if isFind {
- for _, node := range location.AreaList {
- rData = append(rData, node)
- if len(rData) == num {
- return
- }
- }
- }
- }
- if findTimes < 2 {
- findTimes++
- goto findStart
- }
- return
- }
|