get.go 2.7 KB
Newer Older
Y
Your Name 已提交
1
package daobalance
E
eoLinker API Management 已提交
2 3 4

import (
	"fmt"
黄孟柱 已提交
5 6
	"github.com/eolinker/goku-api-gateway/common/database"
	entity "github.com/eolinker/goku-api-gateway/server/entity/balance-entity-service"
E
eoLinker API Management 已提交
7 8 9
	"strings"
)

Y
Your Name 已提交
10
//Get 获取负载信息
E
eoLinker API Management 已提交
11 12
func Get(name string) (*entity.Balance, error) {
	const sql = "SELECT A.`balanceName`,A.`serviceName`,IFNULL(B.`driver`,''),A.`appName`,IFNULL(A.`static`,''),IFNULL(A.`staticCluster`,''),A.`balanceDesc`,A.`updateTime`,A.`createTime` FROM `goku_balance` A LEFT JOIN `goku_service_config` B ON A.`serviceName` = B.`NAME` WHERE A.`balanceName`= ?;"
Y
Your Name 已提交
13 14 15 16 17
	db := database.GetConnection()
	v := new(entity.Balance)
	err := db.QueryRow(sql, name).Scan(&v.Name, &v.ServiceName, &v.ServiceDriver, &v.AppName, &v.Static, &v.StaticCluster, &v.Desc, &v.UpdateTime, &v.CreateTime)
	if err != nil {
		return nil, err
E
eoLinker API Management 已提交
18 19
	}

Y
Your Name 已提交
20
	return v.Type(), nil
E
eoLinker API Management 已提交
21 22
}

Y
Your Name 已提交
23
//GetAll 获取负载列表
Y
Your Name 已提交
24
func GetAll() ([]*entity.Balance, error) {
E
eoLinker API Management 已提交
25
	const sql = "SELECT A.`balanceName`,A.`serviceName`,IFNULL(B.`driver`,''),A.`appName`,IFNULL(A.`static`,''),IFNULL(A.`staticCluster`,''),A.`balanceDesc`,A.`updateTime`,A.`createTime` FROM `goku_balance` A LEFT JOIN `goku_service_config` B ON A.`serviceName` = B.`name` ORDER BY `updateTime` DESC;"
Y
Your Name 已提交
26 27 28 29
	db := database.GetConnection()
	rows, err := db.Query(sql)
	if err != nil {
		return nil, err
E
eoLinker API Management 已提交
30 31 32
	}
	defer rows.Close()

Y
Your Name 已提交
33 34 35 36 37 38
	r := make([]*entity.Balance, 0, 20)
	for rows.Next() {
		v := new(entity.Balance)
		err := rows.Scan(&v.Name, &v.ServiceName, &v.ServiceDriver, &v.AppName, &v.Static, &v.StaticCluster, &v.Desc, &v.UpdateTime, &v.CreateTime)
		if err != nil {
			return nil, err
E
eoLinker API Management 已提交
39
		}
Y
Your Name 已提交
40
		r = append(r, v.Type())
E
eoLinker API Management 已提交
41
	}
Y
Your Name 已提交
42
	return r, nil
E
eoLinker API Management 已提交
43 44
}

Y
Your Name 已提交
45
//Search 查询
Y
Your Name 已提交
46
func Search(keyword string) ([]*entity.Balance, error) {
E
eoLinker API Management 已提交
47 48
	const sqlTpl = "SELECT A.`balanceName`,A.`serviceName`,IFNULL(B.`driver`,''),A.`appName`,IFNULL(A.`static`,''),IFNULL(A.`staticCluster`,''),A.`balanceDesc`,A.`updateTime`,A.`createTime` FROM `goku_balance` A LEFT JOIN `goku_service_config` B ON A.`serviceName` = B.`name` %s ORDER BY `updateTime` DESC;"

Y
Your Name 已提交
49 50 51 52 53 54 55
	where := ""
	args := make([]interface{}, 0, 3)
	keywordvalue := strings.Trim(keyword, "%")
	if keywordvalue != "" {
		where = "WHERE A.`balanceName` LIKE ? OR A.`serviceName` LIKE ? OR B.`driver` LIKE ?"
		kp := fmt.Sprint("%", keywordvalue, "%")
		args = append(args, kp, kp, kp)
E
eoLinker API Management 已提交
56
	}
Y
Your Name 已提交
57 58 59 60 61
	sql := fmt.Sprintf(sqlTpl, where)
	db := database.GetConnection()
	rows, err := db.Query(sql, args...)
	if err != nil {
		return nil, err
E
eoLinker API Management 已提交
62 63 64
	}
	defer rows.Close()

Y
Your Name 已提交
65 66 67 68 69 70
	r := make([]*entity.Balance, 0, 20)
	for rows.Next() {
		v := new(entity.Balance)
		err := rows.Scan(&v.Name, &v.ServiceName, &v.ServiceDriver, &v.AppName, &v.Static, &v.StaticCluster, &v.Desc, &v.UpdateTime, &v.CreateTime)
		if err != nil {
			return nil, err
E
eoLinker API Management 已提交
71
		}
Y
Your Name 已提交
72
		r = append(r, v.Type())
E
eoLinker API Management 已提交
73
	}
Y
Your Name 已提交
74 75
	return r, nil
}