areaStruct.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. "jybxseo/internal/consts"
  9. )
  10. var (
  11. JyBxSeoAreaRoot *AreaRoot = &AreaRoot{}
  12. )
  13. type (
  14. AreaRoot struct {
  15. areaTree map[string]*AreaNode //层级关系
  16. areaCodeMap map[string]*AreaNode //所有节点平铺 key:code
  17. areaNameMap map[string]*AreaNode //所有节点平铺 key:name
  18. rootList []*AreaNode //根节点
  19. Location []*AreaLocation //区位
  20. }
  21. AreaNode struct {
  22. Name string `json:"name" doc:"名称"`
  23. Code string `json:"code" doc:"代码"`
  24. Href string `json:"href" doc:"地址"`
  25. PNode *AreaNode `json:"pNode" doc:"父节点代码"`
  26. Type int `json:"type" doc:"1省份 2城市 3直辖市"`
  27. Child []*AreaNode `json:"child" doc:"子栏目"`
  28. }
  29. AreaLocation struct {
  30. Name string `json:"name" doc:"华北、华中、东北、华东、华南、西南、西北"`
  31. AreaList []*AreaNode `json:"areaList" doc:"地区列表"`
  32. }
  33. )
  34. func init() {
  35. JyBxSeoAreaRoot.LoadAreasFrom(gctx.New())
  36. }
  37. // LoadAreasFrom 加载地区表示
  38. func (aRoot *AreaRoot) LoadAreasFrom(ctx context.Context) {
  39. areaTree, areaCodeMap, areaNameMap := map[string]*AreaNode{}, map[string]*AreaNode{}, map[string]*AreaNode{}
  40. var rootArr []*AreaNode
  41. for _, m := range g.Cfg("global").MustGet(context.Background(), "allAreaTree").Maps() {
  42. root := &AreaNode{
  43. Name: gconv.String(m["name"]),
  44. Code: gconv.String(m["code"]),
  45. }
  46. root.Href = fmt.Sprintf(consts.WebDomainFormat, root.Code)
  47. if gconv.Bool(m["special"]) {
  48. root.Type = consts.CitySpecialType
  49. } else {
  50. root.Type = consts.AreaType
  51. }
  52. for _, n := range gconv.Maps(m["child"]) {
  53. node := &AreaNode{
  54. Name: gconv.String(n["name"]),
  55. Code: gconv.String(n["code"]),
  56. Type: consts.CityType,
  57. PNode: root,
  58. }
  59. node.Href = fmt.Sprintf("%s/%s/", root.Href, node.Code)
  60. root.Child = append(root.Child, node)
  61. areaCodeMap[node.Code] = node
  62. areaNameMap[node.Name] = node
  63. }
  64. areaTree[root.Code] = root
  65. areaCodeMap[root.Code] = root
  66. areaCodeMap[root.Name] = root
  67. rootArr = append(rootArr, root)
  68. }
  69. //加载区位
  70. for _, m := range g.Cfg("global").MustGet(context.Background(), "areaLocation").Maps() {
  71. tLocation := &AreaLocation{
  72. Name: gconv.String(m["name"]),
  73. AreaList: nil,
  74. }
  75. for _, areaCode := range gconv.Strings(m["values"]) {
  76. if node := aRoot.areaCodeMap[areaCode]; node != nil {
  77. tLocation.AreaList = append(tLocation.AreaList, node)
  78. }
  79. }
  80. aRoot.Location = append(aRoot.Location, tLocation)
  81. }
  82. aRoot.areaTree, aRoot.areaCodeMap = areaTree, areaCodeMap
  83. aRoot.areaNameMap = areaNameMap
  84. aRoot.rootList = rootArr
  85. }
  86. // GetData 获取地区数据
  87. func (aRoot *AreaRoot) GetData(ctx context.Context, maxTotal int, query *SeoBiddingQuery) []map[string]interface{} {
  88. var sql string
  89. var values []interface{}
  90. if query.district != "" {
  91. sql += " AND b.district=? "
  92. values = append(values, query.district)
  93. } else if query.city != "" {
  94. sql += " AND b.city=? "
  95. values = append(values, query.city)
  96. } else if query.area != "" {
  97. sql += " AND b.area=? "
  98. values = append(values, query.area)
  99. }
  100. if query.topType != "" {
  101. sql += " AND b.industry=? "
  102. values = append(values, query.topType)
  103. }
  104. if query.status > 0 {
  105. sql += " AND b.status=? "
  106. values = append(values, query.status)
  107. }
  108. values = append(values, maxTotal+50)
  109. queryRes, err := g.DB().Query(ctx, fmt.Sprintf(`SELECT b.bid_id
  110. FROM jyseo.column_bidList b
  111. WHERE 1=1 %s
  112. ORDER BY b.publish_time DESC
  113. LIMIT 0,?`, sql), values...)
  114. if err != nil || queryRes.IsEmpty() {
  115. return nil
  116. }
  117. return FillingBiddingBaseFields(ctx, queryRes.List())
  118. }
  119. func (an *AreaNode) GetChildNode(code string) *AreaNode {
  120. if len(an.Child) == 0 {
  121. return nil
  122. }
  123. for _, node := range an.Child {
  124. if node.Code == code {
  125. return node
  126. }
  127. }
  128. return nil
  129. }
  130. // GetNodeByCode 根据code
  131. func (aRoot *AreaRoot) GetNodeByCode(code string) *AreaNode {
  132. return aRoot.areaCodeMap[code]
  133. }
  134. // GetNodeByName 根据省份名字获取node
  135. func (aRoot *AreaRoot) GetNodeByName(name string) *AreaNode {
  136. return aRoot.areaNameMap[name]
  137. }
  138. // GetAllRootNodes 获取全部根地区节点
  139. func (aRoot *AreaRoot) GetAllRootNodes() []*AreaNode {
  140. return aRoot.rootList
  141. }
  142. // GetAllLocation 获取所有区位
  143. func (aRoot *AreaRoot) GetAllLocation() []*AreaLocation {
  144. return aRoot.Location
  145. }
  146. // GetSortLocation 获取关联区位信息
  147. // 规则:按现有的华东华中华南等分类,显示当前省份所在分类的其他省份,再显示下个地区分类的全部省份;以此类推循环;
  148. func (aRoot *AreaRoot) GetSortLocation(code string, num int) (rData []*AreaNode) {
  149. var isFind bool
  150. var findTimes int = 0
  151. findStart:
  152. for _, location := range aRoot.Location {
  153. if !isFind {
  154. for _, node := range location.AreaList {
  155. if node.Code == code {
  156. isFind = true
  157. break
  158. }
  159. }
  160. }
  161. if isFind {
  162. for _, node := range location.AreaList {
  163. rData = append(rData, node)
  164. if len(rData) == num {
  165. return
  166. }
  167. }
  168. }
  169. }
  170. if findTimes < 2 {
  171. findTimes++
  172. goto findStart
  173. }
  174. return
  175. }