|
@@ -0,0 +1,128 @@
|
|
|
+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 (
|
|
|
+ JyBxSeoClassRoot *ClassRoot = &ClassRoot{}
|
|
|
+)
|
|
|
+
|
|
|
+type (
|
|
|
+ ClassRoot struct {
|
|
|
+ classTree map[string]*ClassNode //层级关系
|
|
|
+ classCodeMap map[string]*ClassNode //所有节点平铺 key:code
|
|
|
+ classNameMap map[string]*ClassNode //所有节点平铺 key:name
|
|
|
+ rootList []*ClassNode //根节点
|
|
|
+ }
|
|
|
+ ClassNode struct {
|
|
|
+ Name string `json:"name" doc:"名称"`
|
|
|
+ Code string `json:"code" doc:"代码"`
|
|
|
+ PNode *ClassNode `json:"pNode" doc:"父节点代码"`
|
|
|
+ Type int `json:"type" doc:"1一级分类 2二级分类"`
|
|
|
+ Child []*ClassNode `json:"child" doc:"子栏目"`
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
+func init() {
|
|
|
+ JyBxSeoClassRoot.LoadClassFrom(gctx.New())
|
|
|
+}
|
|
|
+
|
|
|
+func (cRoot *ClassRoot) LoadClassFrom(ctx context.Context) {
|
|
|
+ classTree, classCodeMap, classNameMap := map[string]*ClassNode{}, map[string]*ClassNode{}, map[string]*ClassNode{}
|
|
|
+ var rootArr []*ClassNode
|
|
|
+ for _, m := range g.Cfg("global").MustGet(context.Background(), "allClassTree").Maps() {
|
|
|
+ root := &ClassNode{
|
|
|
+ Name: gconv.String(m["name"]),
|
|
|
+ Code: gconv.String(m["code"]),
|
|
|
+ Type: 1,
|
|
|
+ }
|
|
|
+ for _, n := range gconv.Maps(m["child"]) {
|
|
|
+ node := &ClassNode{
|
|
|
+ Name: gconv.String(n["name"]),
|
|
|
+ Code: gconv.String(n["code"]),
|
|
|
+ PNode: root,
|
|
|
+ Type: 2,
|
|
|
+ }
|
|
|
+ root.Child = append(root.Child, node)
|
|
|
+
|
|
|
+ //if classCodeMap[node.Code] != nil {
|
|
|
+ // fmt.Println("node.Code重复", node.Code)
|
|
|
+ //}
|
|
|
+ //if classNameMap[node.Name] != nil {
|
|
|
+ // fmt.Println("node.Name重复", node.Name)
|
|
|
+ //}
|
|
|
+
|
|
|
+ classCodeMap[node.Code] = node
|
|
|
+ classNameMap[node.Name] = node
|
|
|
+ }
|
|
|
+ classTree[root.Code] = root
|
|
|
+ classCodeMap[root.Code] = root
|
|
|
+ classCodeMap[root.Name] = root
|
|
|
+ rootArr = append(rootArr, root)
|
|
|
+ }
|
|
|
+ cRoot.classTree, cRoot.classCodeMap = classTree, classCodeMap
|
|
|
+ cRoot.classNameMap = classNameMap
|
|
|
+ cRoot.rootList = rootArr
|
|
|
+}
|
|
|
+
|
|
|
+// GetData 获取地区数据
|
|
|
+func (cRoot *ClassRoot) 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 != "" {
|
|
|
+ if val, _ := consts.TopTypeMap[query.topType]; val != "" {
|
|
|
+ sql += " AND b.toptype=? "
|
|
|
+ values = append(values, val)
|
|
|
+ } else {
|
|
|
+ sql += " AND b.toptype=? "
|
|
|
+ values = append(values, query.topType)
|
|
|
+ }
|
|
|
+ } else if query.subType != "" && query.keys == "" {
|
|
|
+ sql += " AND b.subtype=? "
|
|
|
+ values = append(values, query.subType)
|
|
|
+ } else if query.topType == "" && query.subType == "" {
|
|
|
+ sql += " AND (b.toptype !='拟建' AND b.toptype !='采购意向') "
|
|
|
+ }
|
|
|
+ values = append(values, maxTotal+50)
|
|
|
+ queryRes, err := g.DB().Query(ctx, fmt.Sprintf(`SELECT b.bid_id
|
|
|
+ FROM jyseo.new_areaClass 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())
|
|
|
+}
|
|
|
+
|
|
|
+// GetNodeByCode 根据code
|
|
|
+func (cRoot *ClassRoot) GetNodeByCode(code string) *ClassNode {
|
|
|
+ return cRoot.classCodeMap[code]
|
|
|
+}
|
|
|
+
|
|
|
+// GetNodeByName 根据省份名字获取node
|
|
|
+func (cRoot *ClassRoot) GetNodeByName(name string) *ClassNode {
|
|
|
+ return cRoot.classNameMap[name]
|
|
|
+}
|
|
|
+
|
|
|
+// GetAllRootNodes 获取全部根地区节点
|
|
|
+func (cRoot *ClassRoot) GetAllRootNodes() []*ClassNode {
|
|
|
+ return cRoot.rootList
|
|
|
+}
|