Files
platform/web/services/node.go

68 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package services
import (
"encoding/json"
"fmt"
"platform/pkg/orm"
)
var Node = &nodeService{}
type nodeService struct{}
func (s *nodeService) Filter(userId int32, count int, config ...NodeFilterConfig) ([]*FilteredNode, error) {
_config := NodeFilterConfig{}
if len(config) > 0 {
_config = config[0]
}
// 筛选符合条件且未分配给用户过的节点
// 静态条件:省,市,运营商
// 排序方式1.分配给该用户的次数 2.分配给全部用户的次数 3.todo 节点的健康状态
var nodes []*FilteredNode
orm.DB.Raw(filterSqlRaw, userId, _config.Isp, _config.Prov, _config.City).
Limit(count).
Find(&nodes)
rs, _ := json.Marshal(nodes)
fmt.Printf(string(rs))
// 返回节点列表
return nodes, nil
}
type NodeFilterConfig struct {
Isp string
Prov string
City string
}
const filterSqlRaw = `
select
n.id as id,
n.name as name,
n.fwd_port as port,
count(c.*) as total,
count(c.*) filter ( where c.user_id = ? ) as assigned
from
node n
left join public.channel c
on n.id = c.node_id and c.expiration > now() and c.deleted_at is null
where
n.isp = ? and
n.prov = ? and
n.city = ?
group by
n.id
order by
assigned, total
`
type FilteredNode struct {
Id int32 `json:"id"`
Name string `json:"name"`
Port int32 `json:"port"`
Total int32 `json:"total"`
Assigned int32 `json:"assigned"`
}