sTypeStruct.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "jyseo/internal/consts"
  9. )
  10. var (
  11. JySeoSTypeRoot *STypeRoot = &STypeRoot{}
  12. )
  13. type (
  14. STypeRoot struct {
  15. sTypeTree map[string]*STypeNode //字母code层级关系
  16. sTypeAllNode map[string]*STypeNode //字母code所有节点平铺
  17. sTypeIdMap map[string]*STypeNode //数字id对应的信息类型
  18. nameMap map[string]*STypeNode
  19. parentNodes []*STypeNode //父级节点
  20. }
  21. STypeNode struct {
  22. Name string `json:"name" doc:"名称"`
  23. Code string `json:"code" doc:"代码"`
  24. PNode *STypeNode `json:"pNode" doc:"父节点"`
  25. Id string `json:"id" doc:"id"`
  26. Alias string `json:"alias" doc:"别称"`
  27. SIGN int `json:"sign" doc:"标签"`
  28. Child []*STypeNode `json:"child" doc:"子栏目"`
  29. }
  30. )
  31. func init() {
  32. JySeoSTypeRoot.LoadSTypeFrom(gctx.New())
  33. }
  34. func (sRoot *STypeRoot) LoadSTypeFrom(ctx context.Context) {
  35. sTypeTree, nameMap, sTypeAllNode, sTypeIdMap := map[string]*STypeNode{}, map[string]*STypeNode{}, map[string]*STypeNode{}, map[string]*STypeNode{}
  36. var parentNodes []*STypeNode
  37. for _, m := range g.Cfg("global").MustGet(context.Background(), "allSTypeTree").Maps() {
  38. root := &STypeNode{
  39. Name: gconv.String(m["name"]),
  40. Code: gconv.String(m["code"]),
  41. Id: gconv.String(m["id"]),
  42. Alias: gconv.String(m["name"]),
  43. SIGN: gconv.Int(m["signId"]),
  44. }
  45. if alias := gconv.String(m["alias"]); alias != "" {
  46. root.Alias = alias
  47. }
  48. for _, n := range gconv.Maps(m["child"]) {
  49. node := &STypeNode{
  50. Name: gconv.String(n["name"]),
  51. Code: gconv.String(n["code"]),
  52. Alias: gconv.String(n["name"]),
  53. PNode: root,
  54. }
  55. root.Child = append(root.Child, node)
  56. sTypeAllNode[node.Code] = node
  57. nameMap[node.Name] = node
  58. }
  59. nameMap[root.Name] = root
  60. sTypeTree[root.Code] = root
  61. sTypeAllNode[root.Code] = root
  62. sTypeIdMap[root.Id] = root
  63. parentNodes = append(parentNodes, root)
  64. }
  65. sRoot.sTypeTree, sRoot.sTypeAllNode, sRoot.sTypeIdMap = sTypeTree, sTypeAllNode, sTypeIdMap
  66. sRoot.parentNodes = parentNodes
  67. sRoot.nameMap = nameMap
  68. }
  69. func (sRoot *STypeRoot) GetData(ctx context.Context, maxTotal int, query *SeoBiddingQuery) []map[string]interface{} {
  70. var sql string
  71. var values []interface{}
  72. if query.topType != "" {
  73. if val, _ := consts.TopTypeMap[query.topType]; val != "" {
  74. sql += " AND b.toptype=? "
  75. values = append(values, val)
  76. } else {
  77. sql += " AND b.toptype=? "
  78. values = append(values, query.topType)
  79. }
  80. } else if query.subType != "" && query.keys == "" {
  81. sql += " AND b.subtype=? "
  82. values = append(values, query.subType)
  83. } else if query.topType == "" && query.subType == "" {
  84. sql += " AND (b.toptype !='拟建' AND b.toptype !='采购意向') "
  85. }
  86. values = append(values, maxTotal)
  87. queryRes, err := g.DB().Query(ctx, fmt.Sprintf(`SELECT b.bid_id
  88. FROM new_areaClass b
  89. WHERE 1=1 %s
  90. ORDER BY b.publish_time DESC
  91. LIMIT 0,?`, sql), values...)
  92. if err != nil || queryRes.IsEmpty() {
  93. return nil
  94. }
  95. return FillingBiddingBaseFields(ctx, queryRes.List())
  96. }
  97. // GetNodeByCode 根据字母code获取信息类型节点
  98. func (sRoot *STypeRoot) GetNodeByCode(code string) *STypeNode {
  99. return sRoot.sTypeAllNode[code]
  100. }
  101. // GetNodeById 根据id获取信息类型节点
  102. func (sRoot *STypeRoot) GetNodeById(code string) *STypeNode {
  103. return sRoot.sTypeIdMap[code]
  104. }
  105. // GetParentNodes 获取节点树
  106. func (sRoot *STypeRoot) GetParentNodes() []*STypeNode {
  107. return sRoot.parentNodes
  108. }
  109. func (sRoot *STypeRoot) GetNodeByName(name string) *STypeNode {
  110. return sRoot.nameMap[name]
  111. }