apiGroup.go 9.2 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7
package console_mysql

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 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
)

// 新建接口分组
func AddApiGroup(groupName string, projectID, parentGroupID int) (bool, interface{}, error) {
	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 {
		groupDepth += 1
		groupPath += "," + strconv.Itoa(int(groupID))
	}

	// 更新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
}

// 修改接口分组
func EditApiGroup(groupName string, groupID, projectID int) (bool, string, error) {
	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
}

// 删除接口分组
func DeleteApiGroup(projectID, groupID int) (bool, string, error) {
	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
	}
	flag, apiList, _ := GetApiListByGroupList(projectID, concatGroupID)
	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
}

// 获取接口分组列表
func GetApiGroupList(projectID int) (bool, []map[string]interface{}, error) {
	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
	} else {
		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,
			}
			groupList = append(groupList, groupInfo)
		}
		return true, groupList, nil
	}
}

// 更新接口分组脚本
func UpdateApiGroupScript() bool {
	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
	} else {
		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()
			if _, err = r.Columns(); err != nil {
				return false
			} else {
				for r.Next() {
					var childGroupID, childParentGroupID int
					var childGroupName, childGroupPath string
					err = r.Scan(&childGroupID, &childGroupName, &childParentGroupID)
					if err != nil {
						return false
					}
					childGroupPath = groupPath + "," + strconv.Itoa(childGroupID)
					sql = "SELECT groupID,groupName,parentGroupID FROM goku_gateway_api_group WHERE isChild = ? AND parentGroupID = ?;"
					rw, err := db.Query(sql, 2, childGroupID)
					if err != nil {
						return false
					}
					defer rw.Close()
					if _, err = rw.Columns(); err != nil {
						return false
					} else {
						for rw.Next() {
							var secChildGroupID, secChildParentGroupID int
							var secChildGroupName, secChildGroupPath string
							err = rw.Scan(&secChildGroupID, &secChildGroupName, &secChildParentGroupID)
							if err != nil {
								return false
							}
							secChildGroupPath = childGroupPath + "," + strconv.Itoa(secChildGroupID)
							groupList = append(groupList, map[string]interface{}{
								"groupID":       secChildGroupID,
								"groupName":     secChildGroupName,
								"groupDepth":    3,
								"parentGroupID": secChildParentGroupID,
								"groupPath":     secChildGroupPath,
							})
						}
					}
					groupList = append(groupList, map[string]interface{}{
						"groupID":       childGroupID,
						"groupName":     childGroupName,
						"groupDepth":    2,
						"parentGroupID": childParentGroupID,
						"groupPath":     childGroupPath,
					})
				}
			}
			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()

			}
		}
		Tx.Commit()
		return true
	}
}