strategyGroup.go 4.3 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
package console_mysql

import (
	database2 "github.com/eolinker/goku/common/database"
	log "github.com/eolinker/goku/goku-log"
)

// 新建策略组分组
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
}

// 修改策略组分组
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
}

// 删除策略组分组
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()
	//获取记录列
	if _, err = rows.Columns(); err != nil {
		return false, "[ERROR]Illegal SQL statement!", err
	} else {
		for rows.Next() {
			var strategyID string
			err = rows.Scan(&strategyID)
			if err != nil {
				return false, "[ERROR]Fail to excute SQL statement!", err
			}
			strategyIDList = append(strategyIDList, strategyID)
		}
	}
	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
}

// 获取策略组分组列表
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()
	//获取记录列
	if _, err = rows.Columns(); err != nil {
		log.Info(err.Error())
		return false, nil, err
	} else {
		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
			}

			groupInfo := map[string]interface{}{
				"groupID":   groupID,
				"groupName": groupName,
			}
			groupList = append(groupList, groupInfo)
		}
		return true, groupList, nil
	}
}

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
	} else {
		return false
	}
}