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

import (
	"fmt"
	"strconv"
	"strings"
	"time"

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

Y
Your Name 已提交
14
//AddProject 新建项目
E
eoLinker API Management 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
func AddProject(projectName string) (bool, interface{}, error) {
	now := time.Now().Format("2006-01-02 15:04:05")
	db := database2.GetConnection()
	sql := "INSERT INTO goku_gateway_project (projectName,createTime,updateTime) VALUES (?,?,?);"
	stmt, err := db.Prepare(sql)
	if err != nil {
		return false, err.Error(), err
	}
	defer stmt.Close()
	r, err := stmt.Exec(projectName, now, now)
	if err != nil {
		return false, "[ERROR]Fail to insert data!", err
	}
	projectID, _ := r.LastInsertId()
	return true, projectID, nil
}

Y
Your Name 已提交
32
//EditProject 修改项目信息
E
eoLinker API Management 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
func EditProject(projectName string, projectID int) (bool, string, error) {
	now := time.Now().Format("2006-01-02 15:04:05")
	db := database2.GetConnection()
	sql := "UPDATE goku_gateway_project SET projectName = ?,updateTime = ? WHERE projectID = ?;"
	stmt, err := db.Prepare(sql)
	if err != nil {
		return false, err.Error(), err
	}
	defer stmt.Close()
	_, err = stmt.Exec(projectName, now, projectID)
	if err != nil {
		return false, "[ERROR]Fail to update data!", err
	}
	return true, "", nil
}

Y
Your Name 已提交
49
//DeleteProject 修改项目信息
E
eoLinker API Management 已提交
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
func DeleteProject(projectID int) (bool, string, error) {
	db := database2.GetConnection()
	Tx, _ := db.Begin()
	// 获取项目分组列表
	sql := "SELECT groupID FROM goku_gateway_api_group WHERE projectID = ?;"
	rows, err := Tx.Query(sql, projectID)
	if err != nil {
		return false, "", err
	}
	//延时关闭Rows
	defer rows.Close()
	//获取记录列
	groupIDList := ""
	if _, err = rows.Columns(); err != nil {
		Tx.Rollback()
		return false, "", err
	}
	for rows.Next() {
		var groupID int
		err = rows.Scan(&groupID)
		if err != nil {
			Tx.Rollback()
			log.Info(err.Error())
		}
		groupIDList += strconv.Itoa(groupID) + ","
	}
	groupLen := len(groupIDList)
	if groupLen > 0 {
		if string(groupIDList[groupLen-1]) == "," {
			groupIDList = groupIDList[:groupLen-1]
		}
		sql = "DELETE FROM goku_gateway_api_group WHERE groupID IN (" + groupIDList + ");"
		_, err := Tx.Exec(sql)
		if err != nil {
			Tx.Rollback()
			return false, "[ERROR]Fail to excute SQL statement!", err
		}
		// 获取接口ID列表
		sql = "SELECT apiID FROM goku_gateway_api WHERE projectID = ?;"
		r, err := Tx.Query(sql, projectID)
		if err != nil {
			Tx.Rollback()
			return false, "", err
		}
		if _, err = r.Columns(); err != nil {
			Tx.Rollback()
			return false, "", err
		}
		apiIDList := ""
		for r.Next() {
			var apiID int
			err = r.Scan(&apiID)
			if err != nil {
				Tx.Rollback()
				log.Info(err.Error())
			}
			apiIDList += strconv.Itoa(apiID) + ","
		}
		apiLen := len(apiIDList)
		if apiLen > 0 {
			if string(apiIDList[apiLen-1]) == "," {
				apiIDList = apiIDList[:apiLen-1]
			}
			sql = "DELETE FROM goku_gateway_api WHERE apiID IN (" + apiIDList + ");"
			_, err = Tx.Exec(sql)
			if err != nil {
				Tx.Rollback()
				return false, "[ERROR]Fail to excute SQL statement!", 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 excute SQL statement!", 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_gateway_project WHERE projectID = ?;"
	_, err = Tx.Exec(sql, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to excute SQL statement!", err
	}

	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
155
//BatchDeleteProject 批量删除项目
E
eoLinker API Management 已提交
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
func BatchDeleteProject(projectIDList string) (bool, string, error) {
	db := database2.GetConnection()
	Tx, _ := db.Begin()
	// 获取项目分组列表
	sql := "SELECT groupID FROM goku_gateway_api_group WHERE projectID IN (" + projectIDList + ");"
	rows, err := Tx.Query(sql)
	if err != nil {
		Tx.Rollback()
		return false, "", err
	}
	//延时关闭Rows
	defer rows.Close()
	//获取记录列
	groupIDList := ""
	if _, err = rows.Columns(); err != nil {
		Tx.Rollback()
		return false, "", err
	}
	for rows.Next() {
		var groupID int
		err = rows.Scan(&groupID)
		if err != nil {
			Tx.Rollback()
		}
		groupIDList += strconv.Itoa(groupID) + ","
	}
	groupLen := len(groupIDList)
	if groupLen > 0 && string(groupIDList[groupLen-1]) == "," {
		groupIDList = groupIDList[:groupLen-1]
		sql = "DELETE FROM goku_gateway_api_group WHERE groupID IN (" + groupIDList + ");"
		_, err := Tx.Exec(sql)
		if err != nil {
			Tx.Rollback()
			return false, "[ERROR]Fail to excute SQL statement!", err
		}
	}
	// 获取接口ID列表
	sql = "SELECT apiID FROM goku_gateway_api WHERE projectID IN (" + projectIDList + ");"
	r, err := Tx.Query(sql)
	if err != nil {
		Tx.Rollback()
		return false, "", err
	}
	if _, err = r.Columns(); err != nil {
		Tx.Rollback()
		return false, "", err
	}
	apiIDList := ""
	for r.Next() {
		var apiID int
		err = r.Scan(&apiID)
		if err != nil {
			log.Info(err.Error())
		}
		apiIDList += strconv.Itoa(apiID) + ","
	}
	apiLen := len(apiIDList)
	if apiLen != 0 && string(apiIDList[apiLen-1]) == "," {
		apiIDList = apiIDList[:apiLen-1]
		sql = "DELETE FROM goku_gateway_api WHERE apiID IN (" + apiIDList + ");"
		_, err = Tx.Exec(sql)
		if err != nil {
			Tx.Rollback()
			return false, "[ERROR]Fail to excute SQL statement!", 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 excute SQL statement!", 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_gateway_project WHERE projectID IN (" + projectIDList + ");"
	_, err = Tx.Exec(sql)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to excute SQL statement!", err
	}

	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
248
//GetProjectInfo 获取项目信息
E
eoLinker API Management 已提交
249 250 251 252 253 254 255 256 257 258 259
func GetProjectInfo(projectID int) (bool, entity.Project, error) {
	db := database2.GetConnection()
	var project entity.Project
	sql := "SELECT projectID,projectName,createTime,updateTime FROM goku_gateway_project WHERE projectID = ?;"
	err := db.QueryRow(sql, projectID).Scan(&project.ProjectID, &project.ProjectName, &project.CreateTime, &project.UpdateTime)
	if err != nil {
		return false, entity.Project{}, err
	}
	return true, project, nil
}

Y
Your Name 已提交
260
//GetProjectList 获取项目列表
E
eoLinker API Management 已提交
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
func GetProjectList(keyword string) (bool, []*entity.Project, error) {

	sql := "SELECT `projectID`,`projectName`,`updateTime` FROM `goku_gateway_project` %s ORDER BY `updateTime` DESC;"
	keywordValue := strings.Trim(keyword, "%")
	arg := []interface{}{}
	where := ""
	if keywordValue != "" {

		kvp := fmt.Sprint("%", keywordValue, "%")
		where = fmt.Sprint("WHERE `projectName` LIKE ?")
		arg = []interface{}{
			kvp,
		}
	}
	sql = fmt.Sprintf(sql, where)
	db := database2.GetConnection()
	rows, err := db.Query(sql, arg...)
	if err != nil {
		panic(err)
		return false, nil, err
	}
	//延时关闭Rows
	defer rows.Close()
	//获取记录列
	projectList := make([]*entity.Project, 0, 25)
	for rows.Next() {
		var project entity.Project
		err = rows.Scan(&project.ProjectID, &project.ProjectName, &project.UpdateTime)
		if err != nil {
			panic(err)
			return false, nil, err
		}
		projectList = append(projectList, &project)
	}
	return true, projectList, nil

}

Y
Your Name 已提交
299
//CheckProjectIsExist 检查项目是否存在
E
eoLinker API Management 已提交
300 301 302 303 304 305 306 307
func CheckProjectIsExist(projectID int) (bool, error) {
	db := database2.GetConnection()
	sql := "SELECT projectID FROM goku_gateway_project WHERE projectID = ?;"
	var id int
	err := db.QueryRow(sql, projectID).Scan(&id)
	if err != nil {
		return false, err
	}
Y
Your Name 已提交
308
	return true, err
E
eoLinker API Management 已提交
309 310
}

Y
Your Name 已提交
311 312
//GetAPIListFromProjectNotInStrategy 获取项目列表中没有被策略组绑定的接口
func GetAPIListFromProjectNotInStrategy() (bool, []map[string]interface{}, error) {
E
eoLinker API Management 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	db := database2.GetConnection()
	sql := "SELECT projectID,projectName FROM goku_gateway_project;"
	projectRows, err := db.Query(sql)
	if err != nil {
		return false, nil, err
	}
	//延时关闭Rows
	defer projectRows.Close()
	//获取记录列
	projectList := make([]map[string]interface{}, 0, 20)
	if _, err = projectRows.Columns(); err != nil {
		return false, nil, err
	}
	for projectRows.Next() {
		var projectID int
		var projectName string
		err = projectRows.Scan(&projectID, &projectName)
		if err != nil {
			return false, nil, err
		}
		sql = "SELECT groupID,groupName,parentGroupID,groupDepth FROM goku_gateway_api_group WHERE projectID = ?;"
		rows, err := db.Query(sql, projectID)
		if err != nil {
			return false, nil, err
		}
		defer rows.Close()
		//获取记录列
Y
Your Name 已提交
340 341 342 343 344 345 346
		groupList := make([]map[string]interface{}, 0, 20)
		for rows.Next() {
			var groupID, parentGroupID, groupDepth int
			var groupName string
			err = rows.Scan(&groupID, &groupName, &parentGroupID, &groupDepth)
			if err != nil {
				return false, nil, err
E
eoLinker API Management 已提交
347
			}
Y
Your Name 已提交
348 349 350 351 352
			groupInfo := map[string]interface{}{
				"groupID":       groupID,
				"groupName":     groupName,
				"groupDepth":    groupDepth,
				"parentGroupID": parentGroupID,
E
eoLinker API Management 已提交
353
			}
Y
Your Name 已提交
354 355 356 357 358 359
			groupList = append(groupList, groupInfo)
		}
		projectInfo := map[string]interface{}{
			"projectID":   projectID,
			"projectName": projectName,
			"groupList":   groupList,
E
eoLinker API Management 已提交
360
		}
Y
Your Name 已提交
361
		projectList = append(projectList, projectInfo)
E
eoLinker API Management 已提交
362 363 364 365
	}
	return true, projectList, nil

}