get.go 2.6 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11
package dao_balance

import (
	"fmt"
	"github.com/eolinker/goku/common/database"
	entity "github.com/eolinker/goku/server/entity/balance-entity-service"
	"strings"
)

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 已提交
12 13 14 15 16
	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 已提交
17 18
	}

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

Y
Your Name 已提交
22
func GetAll() ([]*entity.Balance, error) {
E
eoLinker API Management 已提交
23
	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 已提交
24 25 26 27
	db := database.GetConnection()
	rows, err := db.Query(sql)
	if err != nil {
		return nil, err
E
eoLinker API Management 已提交
28 29 30
	}
	defer rows.Close()

Y
Your Name 已提交
31 32 33 34 35 36
	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 已提交
37
		}
Y
Your Name 已提交
38
		r = append(r, v.Type())
E
eoLinker API Management 已提交
39
	}
Y
Your Name 已提交
40
	return r, nil
E
eoLinker API Management 已提交
41 42
}

Y
Your Name 已提交
43
func Search(keyword string) ([]*entity.Balance, error) {
E
eoLinker API Management 已提交
44 45
	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 已提交
46 47 48 49 50 51 52
	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 已提交
53
	}
Y
Your Name 已提交
54 55 56 57 58
	sql := fmt.Sprintf(sqlTpl, where)
	db := database.GetConnection()
	rows, err := db.Query(sql, args...)
	if err != nil {
		return nil, err
E
eoLinker API Management 已提交
59 60 61
	}
	defer rows.Close()

Y
Your Name 已提交
62 63 64 65 66 67
	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 已提交
68
		}
Y
Your Name 已提交
69
		r = append(r, v.Type())
E
eoLinker API Management 已提交
70
	}
Y
Your Name 已提交
71 72
	return r, nil
}