apiGroup.go 9.0 KB
Newer Older
Y
Your Name 已提交
1
package consolemysql
E
eoLinker API Management 已提交
2 3 4 5 6 7

import (
	SQL "database/sql"
	"strconv"
	"time"

黄孟柱 已提交
8 9
	database2 "github.com/eolinker/goku-api-gateway/common/database"
	log "github.com/eolinker/goku-api-gateway/goku-log"
E
eoLinker API Management 已提交
10 11
)

Y
Your Name 已提交
12 13
// AddAPIGroup 新建接口分组
func AddAPIGroup(groupName string, projectID, parentGroupID int) (bool, interface{}, error) {
E
eoLinker API Management 已提交
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
	db := database2.GetConnection()
	now := time.Now().Format("2006-01-02 15:04:05")
	Tx, _ := db.Begin()
	groupPath := ""
	groupDepth := 1
	sql := ""

	// 查询父分组信息
	if parentGroupID > 0 {
		const sql = "SELECT groupDepth,groupPath FROM goku_gateway_api_group WHERE groupID = ?;"
		err := Tx.QueryRow(sql, parentGroupID).Scan(&groupDepth, &groupPath)
		if err != nil {
			if err != SQL.ErrNoRows {
				Tx.Rollback()
				return false, "[ERROR]Illegal SQL statement!", err
			}
		}
		if groupDepth > 4 {
			Tx.Rollback()
			return false, "[ERROR]Exceeding the grouping level!", err
		}
	}

	const sql2 = "INSERT INTO goku_gateway_api_group (projectID,groupName,parentGroupID) VALUES (?,?,?);"
	result, err := Tx.Exec(sql2, projectID, groupName, parentGroupID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Illegal SQL statement!", err
	}
	groupID, err := result.LastInsertId()
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to insert data!", err
	}
	if groupPath == "" {
		groupPath = strconv.Itoa(int(groupID))
	} else {
Y
Your Name 已提交
51 52
		groupDepth = groupDepth + 1
		groupPath = groupPath + "," + strconv.Itoa(int(groupID))
E
eoLinker API Management 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	}

	// 更新groupDepth和groupPath
	sql = "UPDATE goku_gateway_api_group SET groupPath = ?,groupDepth = ? WHERE groupID = ?;"
	_, err = Tx.Exec(sql, groupPath, groupDepth, groupID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	// 更新项目更新时间
	_, err = Tx.Exec("UPDATE goku_gateway_project SET updateTime = ? WHERE projectID = ?;", now, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	Tx.Commit()
	return true, groupID, nil
}

Y
Your Name 已提交
72 73
// EditAPIGroup 修改接口分组
func EditAPIGroup(groupName string, groupID, projectID int) (bool, string, error) {
E
eoLinker API Management 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	db := database2.GetConnection()
	now := time.Now().Format("2006-01-02 15:04:05")
	Tx, _ := db.Begin()
	sql := "UPDATE goku_gateway_api_group SET groupName = ? WHERE groupID = ? AND projectID = ?;"
	_, err := Tx.Exec(sql, groupName, groupID, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	// 更新项目更新时间
	_, err = Tx.Exec("UPDATE goku_gateway_project SET updateTime = ? WHERE projectID = ?;", now, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
93 94
// DeleteAPIGroup 删除接口分组
func DeleteAPIGroup(projectID, groupID int) (bool, string, error) {
E
eoLinker API Management 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	db := database2.GetConnection()
	Tx, _ := db.Begin()
	var groupPath string
	// 获取分组信息
	sql := "SELECT groupPath FROM goku_gateway_api_group WHERE groupID = ?"
	err := Tx.QueryRow(sql, groupID).Scan(&groupPath)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to delete data!", err
	}
	var concatGroupID string
	sql = "SELECT GROUP_CONCAT(DISTINCT groupID) AS groupID FROM goku_gateway_api_group WHERE projectID = ? AND groupPath LIKE ?;"
	err = Tx.QueryRow(sql, projectID, groupPath+"%").Scan(&concatGroupID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to delete data!", err
	}
	sql = "DELETE FROM goku_gateway_api_group WHERE groupID IN (" + concatGroupID + ");"
	_, err = Tx.Exec(sql)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to delete data!", err
	}
Y
Your Name 已提交
118
	flag, apiList, _ := GetAPIListByGroupList(projectID, concatGroupID)
E
eoLinker API Management 已提交
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	if flag {
		listLen := len(apiList)
		if listLen > 0 {
			apiIDList := ""
			for i, api := range apiList {
				apiIDList += strconv.Itoa(api["apiID"].(int))
				if i < listLen-1 {
					apiIDList += ","
				}
			}
			sql = "DELETE FROM goku_gateway_api WHERE apiID IN (" + apiIDList + ");"
			_, err = Tx.Exec(sql)
			if err != nil {
				Tx.Rollback()
				return false, "[ERROR]Fail to delete data!", err
			}
			//sql = "DELETE FROM goku_gateway_api_cache WHERE apiID IN (" + apiIDList + ");"
			//_, err = Tx.Exec(sql)
			//if err != nil {
			//	Tx.Rollback()
			//	return false, "[ERROR]Fail to delete data!", err
			//}

			sql = "DELETE FROM goku_conn_strategy_api WHERE apiID IN (" + apiIDList + ");"
			_, err = Tx.Exec(sql)
			if err != nil {
				Tx.Rollback()
				return false, "[ERROR]Fail to delete data!", err
			}

			sql = "DELETE FROM goku_conn_plugin_api WHERE apiID IN (" + apiIDList + ");"
			_, err = Tx.Exec(sql)
			if err != nil {
				Tx.Rollback()
				return false, "[ERROR]Fail to delete data!", err
			}
			//sql = "DELETE FROM goku_conn_plugin_api_cache WHERE apiID IN (" + apiIDList + ");"
			//_, err = Tx.Exec(sql)
			//if err != nil {
			//	Tx.Rollback()
			//	return false, "[ERROR]Fail to delete data!", err
			//}
		}
	}
	now := time.Now().Format("2006-01-02 15:04:05")
	// 更新项目更新时间
	_, err = Tx.Exec("UPDATE goku_gateway_project SET updateTime = ? WHERE projectID = ?;", now, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	// 获取接口分组下面的接口ID
	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
175 176
// GetAPIGroupList 获取接口分组列表
func GetAPIGroupList(projectID int) (bool, []map[string]interface{}, error) {
E
eoLinker API Management 已提交
177 178 179 180 181 182 183 184 185 186 187 188
	db := database2.GetConnection()
	sql := "SELECT groupID,groupName,parentGroupID,groupDepth FROM goku_gateway_api_group WHERE projectID = ?;"
	rows, err := db.Query(sql, projectID)
	if err != nil {
		return false, make([]map[string]interface{}, 0), err
	}
	defer rows.Close()
	//获取记录列
	if _, err = rows.Columns(); err != nil {
		info := err.Error()
		log.Info(info)
		return false, make([]map[string]interface{}, 0), err
Y
Your Name 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202
	}
	groupList := make([]map[string]interface{}, 0)
	for rows.Next() {
		var groupID, parentGroupID, groupDepth int
		var groupName string
		err = rows.Scan(&groupID, &groupName, &parentGroupID, &groupDepth)
		if err != nil {
			return false, make([]map[string]interface{}, 0), err
		}
		groupInfo := map[string]interface{}{
			"groupID":       groupID,
			"groupName":     groupName,
			"groupDepth":    groupDepth,
			"parentGroupID": parentGroupID,
E
eoLinker API Management 已提交
203
		}
Y
Your Name 已提交
204
		groupList = append(groupList, groupInfo)
E
eoLinker API Management 已提交
205
	}
Y
Your Name 已提交
206
	return true, groupList, nil
E
eoLinker API Management 已提交
207 208
}

Y
Your Name 已提交
209 210
// UpdateAPIGroupScript 更新接口分组脚本
func UpdateAPIGroupScript() bool {
E
eoLinker API Management 已提交
211 212 213 214 215 216 217 218 219 220 221
	db := database2.GetConnection()
	// 获取一级分组
	sql := "SELECT groupID,groupName,parentGroupID FROM goku_gateway_api_group WHERE isChild = ?;"
	rows, err := db.Query(sql, 0)
	if err != nil {
		return false
	}
	defer rows.Close()
	//获取记录列
	if _, err = rows.Columns(); err != nil {
		return false
Y
Your Name 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	}
	groupList := make([]map[string]interface{}, 0)
	for rows.Next() {
		var groupID, parentGroupID int
		var groupName, groupPath string
		err = rows.Scan(&groupID, &groupName, &parentGroupID)
		if err != nil {
			return false
		}
		sql = "SELECT groupID,groupName,parentGroupID FROM goku_gateway_api_group WHERE isChild = ? AND parentGroupID = ?;"
		r, err := db.Query(sql, 1, groupID)
		if err != nil {
			return false
		}
		groupPath = strconv.Itoa(groupID)
		defer r.Close()

		for r.Next() {
			var childGroupID, childParentGroupID int
			var childGroupName, childGroupPath string
			err = r.Scan(&childGroupID, &childGroupName, &childParentGroupID)
E
eoLinker API Management 已提交
243 244 245
			if err != nil {
				return false
			}
Y
Your Name 已提交
246
			childGroupPath = groupPath + "," + strconv.Itoa(childGroupID)
E
eoLinker API Management 已提交
247
			sql = "SELECT groupID,groupName,parentGroupID FROM goku_gateway_api_group WHERE isChild = ? AND parentGroupID = ?;"
Y
Your Name 已提交
248
			rw, err := db.Query(sql, 2, childGroupID)
E
eoLinker API Management 已提交
249 250 251
			if err != nil {
				return false
			}
Y
Your Name 已提交
252 253 254 255 256 257 258
			defer rw.Close()
			for rw.Next() {
				var secChildGroupID, secChildParentGroupID int
				var secChildGroupName, secChildGroupPath string
				err = rw.Scan(&secChildGroupID, &secChildGroupName, &secChildParentGroupID)
				if err != nil {
					return false
E
eoLinker API Management 已提交
259
				}
Y
Your Name 已提交
260 261 262 263 264 265 266 267
				secChildGroupPath = childGroupPath + "," + strconv.Itoa(secChildGroupID)
				groupList = append(groupList, map[string]interface{}{
					"groupID":       secChildGroupID,
					"groupName":     secChildGroupName,
					"groupDepth":    3,
					"parentGroupID": secChildParentGroupID,
					"groupPath":     secChildGroupPath,
				})
E
eoLinker API Management 已提交
268
			}
Y
Your Name 已提交
269 270 271 272 273 274 275
			groupList = append(groupList, map[string]interface{}{
				"groupID":       childGroupID,
				"groupName":     childGroupName,
				"groupDepth":    2,
				"parentGroupID": childParentGroupID,
				"groupPath":     childGroupPath,
			})
E
eoLinker API Management 已提交
276
		}
Y
Your Name 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290
		groupInfo := map[string]interface{}{
			"groupID":       groupID,
			"groupName":     groupName,
			"groupDepth":    1,
			"parentGroupID": parentGroupID,
			"groupPath":     groupPath,
		}
		groupList = append(groupList, groupInfo)
	}
	Tx, _ := db.Begin()
	for _, groupInfo := range groupList {
		_, err = Tx.Exec("UPDATE goku_gateway_api_group SET groupPath = ?,groupDepth =? WHERE groupID = ?;", groupInfo["groupPath"].(string), groupInfo["groupDepth"].(int), groupInfo["groupID"].(int))
		if err != nil {
			Tx.Rollback()
E
eoLinker API Management 已提交
291 292 293

		}
	}
Y
Your Name 已提交
294 295
	Tx.Commit()
	return true
E
eoLinker API Management 已提交
296
}