strategyGroup.go 4.1 KB
Newer Older
Y
Your Name 已提交
1
package consolemysql
E
eoLinker API Management 已提交
2 3

import (
黄孟柱 已提交
4
	database2 "github.com/eolinker/goku-api-gateway/common/database"
E
eoLinker API Management 已提交
5 6
)

Y
Your Name 已提交
7
//AddStrategyGroup 新建策略组分组
E
eoLinker API Management 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
func AddStrategyGroup(groupName string) (bool, interface{}, error) {
	db := database2.GetConnection()
	sql := "INSERT INTO goku_gateway_strategy_group (groupName) VALUES (?);"
	stmt, err := db.Prepare(sql)
	if err != nil {
		return false, "[ERROR]Illegal SQL statement!", err
	}
	defer stmt.Close()
	r, err := stmt.Exec(groupName)
	if err != nil {
		return false, "[ERROR]Fail to insert data!", err
	}
	groupID, err := r.LastInsertId()
	if err != nil {
		return false, "[ERROR]Fail to insert data!", err
	}
	return true, groupID, nil
}

Y
Your Name 已提交
27
//EditStrategyGroup 修改策略组分组
E
eoLinker API Management 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
func EditStrategyGroup(groupName string, groupID int) (bool, string, error) {
	db := database2.GetConnection()
	sql := "UPDATE goku_gateway_strategy_group SET groupName = ? WHERE groupID = ?;"
	stmt, err := db.Prepare(sql)
	if err != nil {
		return false, "[ERROR]Illegal SQL statement!", err
	}
	defer stmt.Close()
	_, err = stmt.Exec(groupName, groupID)
	if err != nil {
		return false, "[ERROR]Fail to update data!", err
	}
	return true, "", nil
}

Y
Your Name 已提交
43
//DeleteStrategyGroup 删除策略组分组
E
eoLinker API Management 已提交
44 45 46 47 48 49 50 51 52 53 54 55
func DeleteStrategyGroup(groupID int) (bool, string, error) {
	db := database2.GetConnection()
	// 查询该分组下所有策略组ID
	sql := "SELECT strategyID FROM goku_gateway_strategy WHERE groupID = ?;"
	rows, err := db.Query(sql, groupID)
	if err != nil {
		return false, "[ERROR]Illegal SQL statement!", err
	}
	strategyIDList := make([]string, 0)
	//延时关闭Rows
	defer rows.Close()
	//获取记录列
Y
Your Name 已提交
56 57 58 59 60 61

	for rows.Next() {
		var strategyID string
		err = rows.Scan(&strategyID)
		if err != nil {
			return false, "[ERROR]Fail to excute SQL statement!", err
E
eoLinker API Management 已提交
62
		}
Y
Your Name 已提交
63
		strategyIDList = append(strategyIDList, strategyID)
E
eoLinker API Management 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	}
	Tx, _ := db.Begin()
	_, err = Tx.Exec("DELETE FROM goku_gateway_strategy_group WHERE groupID = ?;", groupID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to excute SQL statement!", err
	}
	if len(strategyIDList) > 0 {
		code := ""
		s := make([]interface{}, 0)
		for i := 0; i < len(strategyIDList); i++ {
			code += "?"
			if i < len(strategyIDList)-1 {
				code += ","
			}
			s = append(s, strategyIDList[i])
		}
		// 删除绑定的接口
		sql = "DELETE FROM goku_conn_strategy_api WHERE strategyID IN (" + code + ");"
		_, err = Tx.Exec(sql, s...)
		if err != nil {
			Tx.Rollback()
			return false, "[ERROR]Fail to excute SQL statement!", err
		}

		_, err = Tx.Exec("DELETE FROM goku_gateway_strategy WHERE strategyID IN ("+code+");", s...)
		if err != nil {
			Tx.Rollback()
			return false, "[ERROR]Fail to delete data!", err
		}
		_, err = Tx.Exec("DELETE FROM goku_conn_plugin_strategy WHERE strategyID IN ("+code+")", s...)
		if err != nil {
			Tx.Rollback()
			return false, "[ERROR]Fail to delete data!", err
		}

		_, err = Tx.Exec("DELETE FROM goku_conn_plugin_api WHERE strategyID IN ("+code+")", s...)
		if err != nil {
			Tx.Rollback()
			return false, "[ERROR]Fail to delete data!", err
		}
	}
	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
110
//GetStrategyGroupList 获取策略组分组列表
E
eoLinker API Management 已提交
111 112 113 114 115 116 117 118 119 120
func GetStrategyGroupList() (bool, []map[string]interface{}, error) {
	db := database2.GetConnection()
	sql := "SELECT groupID,groupName,groupType FROM goku_gateway_strategy_group WHERE groupType = 0;"
	rows, err := db.Query(sql)
	if err != nil {
		return false, nil, err
	}
	defer rows.Close()
	//获取记录列

Y
Your Name 已提交
121 122 123 124 125 126 127
	groupList := make([]map[string]interface{}, 0)
	for rows.Next() {
		var groupID, groupType int
		var groupName string
		err = rows.Scan(&groupID, &groupName, &groupType)
		if err != nil {
			return false, nil, err
E
eoLinker API Management 已提交
128
		}
Y
Your Name 已提交
129 130 131 132 133 134

		groupInfo := map[string]interface{}{
			"groupID":   groupID,
			"groupName": groupName,
		}
		groupList = append(groupList, groupInfo)
E
eoLinker API Management 已提交
135
	}
Y
Your Name 已提交
136
	return true, groupList, nil
E
eoLinker API Management 已提交
137 138
}

Y
Your Name 已提交
139
//CheckIsOpenGroup 检查是否是开放分组
E
eoLinker API Management 已提交
140 141 142 143 144 145 146 147 148 149 150
func CheckIsOpenGroup(groupID int) bool {
	db := database2.GetConnection()
	var groupType int
	sql := "SELECT groupType FROM goku_gateway_strategy_group WHERE groupID = ?;"
	err := db.QueryRow(sql, groupID).Scan(&groupType)
	if err != nil {
		return false
	}
	if groupType == 1 {
		return true
	}
Y
Your Name 已提交
151
	return false
E
eoLinker API Management 已提交
152
}