[ADD]数据源

This commit is contained in:
viletyy 2019-08-12 01:39:17 +08:00
parent e7468ab608
commit ef49bdf01b
11 changed files with 854 additions and 3 deletions

View File

@ -1,6 +1,8 @@
package basic
import (
"github.com/Unknwon/com"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"
"github.com/viletyy/potato/models/basic"
_ "github.com/viletyy/potato/models/basic"
@ -37,3 +39,201 @@ func GetMetaDatabases(c *gin.Context) {
"basic": data,
})
}
// @Summary 新增数据源
// @Tags meta_databases
// @Description
// @Accept mpfd
// @Produce json
// @Param name formData string true "数据源 名称"
// @Param host formData string true "数据源 地址"
// @Param port formData int true "数据源 端口号"
// @Param db_name formData string true "数据源 数据库名称"
// @Param username formData string true "数据源 用户名"
// @Param password formData string true "数据源 密码"
// @Param comment formData string false "数据源 备注"
// @Param vendor_id formData int true "系统厂商 id"
// @Param business_id formData int true "业务系统 id"
// @Success 200 {string} json "{"code": 200, data: {}, "msg" : "ok"}"
// @Router /v1/meta_databases [post]
func AddMetaDatabase(c *gin.Context) {
name := c.PostForm("name")
host := c.PostForm("host")
port := com.StrTo(c.PostForm("port")).MustInt()
dbName := c.PostForm("db_name")
username := c.PostForm("username")
password := c.PostForm("password")
comment := c.PostForm("comment")
vendorId := com.StrTo(c.PostForm("vendor_id")).MustInt()
businessId := com.StrTo(c.PostForm("business_id")).MustInt()
valid := validation.Validation{}
valid.Required(name, "name").Message("名称不能为空")
valid.Required(host, "host").Message("地址不能为空")
valid.Required(port, "port").Message("端口号不能为空")
valid.Required(dbName, "db_name").Message("数据库名称不能为空")
valid.Required(username, "username").Message("用户名不能为空")
valid.Required(password, "password").Message("密码不能为空")
valid.Min(vendorId, 1,"vendor_id").Message("必须是有效的系统厂商ID")
valid.Min(businessId, 1,"business_id").Message("必须是有效的业务系统ID")
data := make(map[string]interface{})
code := e.INVALID_PARAMS
if ! basic.ExistVendorById(vendorId) {
code = e.ERROR_NOT_EXIST_VENDOR
}
if ! basic.ExistBusinessById(businessId) {
code = e.ERROR_NOT_EXIST_BUSINESS
}
if ! valid.HasErrors() {
if ! basic.ExistMetaDatabaseByName(name) {
data["Name"] = name
data["Host"] = host
data["Port"] = port
data["DbName"] = dbName
data["Username"] = username
data["Password"] = password
data["Comment"] = comment
data["VendorId"] = vendorId
data["BusinessId"] = businessId
code = e.SUCCESS
} else {
code = e.ERROR_EXIST_META_DATABASE
}
}
if code == 200 {
basic.AddMetaDatabase(data)
}
c.JSON(http.StatusOK, gin.H{
"code" : code,
"msg" : e.GetMsg(code),
"data" : data,
})
}
// @Summary 修改数据源
// @Tags meta_databases
// @Description
// @Accept mpfd
// @Produce json
// @Param id path int true "数据源 ID"
// @Param name formData string false "数据源 名称"
// @Param host formData string false "数据源 地址"
// @Param port formData int false "数据源 端口号"
// @Param db_name formData string false "数据源 数据库名称"
// @Param username formData string false "数据源 用户名"
// @Param password formData string false "数据源 密码"
// @Param comment formData string false "数据源 备注"
// @Param vendor_id formData int false "系统厂商 id"
// @Param business_id formData int false "业务系统 id"
// @Success 200 {string} json "{"code" : 200, "msg" : "ok"}"
// @Router /v1/meta_databases/{id} [patch]
func EditMetaDatabase(c *gin.Context) {
id := com.StrTo(c.Param("id")).MustInt()
name := c.PostForm("name")
host := c.PostForm("host")
port := com.StrTo(c.PostForm("port")).MustInt()
dbName := c.PostForm("db_name")
username := c.PostForm("username")
password := c.PostForm("password")
comment := c.PostForm("comment")
vendorId := com.StrTo(c.PostForm("vendor_id")).MustInt()
businessId := com.StrTo(c.PostForm("business_id")).MustInt()
valid := validation.Validation{}
valid.Min(id, 1, "id").Message("必须是有效的数据源id")
data := make(map[string]interface{})
code := e.INVALID_PARAMS
if ! valid.HasErrors() {
if basic.ExistMetaDatabaseById(id) {
code = e.SUCCESS
if name != "" {
data["name"] = name
}
if host != "" {
data["host"] = host
}
if port != 0 {
data["port"] = port
}
if dbName != "" {
data["db_name"] = dbName
}
if username != "" {
data["username"] = username
}
if password != "" {
data["password"] = password
}
if comment != "" {
data["comment"] = comment
}
if vendorId != 0 {
if basic.ExistVendorById(vendorId) {
data["vendor_id"] = vendorId
} else {
code = e.ERROR_NOT_EXIST_VENDOR
}
}
if businessId != 0 {
if basic.ExistBusinessById(businessId) {
data["business_id"] = businessId
} else {
code = e.ERROR_NOT_EXIST_BUSINESS
}
}
} else {
code = e.ERROR_NOT_EXIST_META_DATABASE
}
}
if code == 200 {
basic.EditMetaDatabase(id, data)
}
c.JSON(http.StatusOK, gin.H{
"code" : code,
"msg" : e.GetMsg(code),
"data" : data,
})
}
// @Summary 删除数据源
// @Tags meta_databases
// @Description
// @Accept json
// @Produce json
// @Param id path int true "数据源 ID"
// @Success 200 {string} json "{"code" : 200, "msg" : "ok"}"
// @Router /v1/meta_databases/{id} [delete]
func DeleteMetaDatabase(c *gin.Context) {
id := com.StrTo(c.Param("id")).MustInt()
valid := validation.Validation{}
valid.Min(id, 1, "id").Message("必须是有效的数据源ID")
code := e.INVALID_PARAMS
if ! valid.HasErrors() {
if basic.ExistMetaDatabaseById(id) {
code = e.SUCCESS
} else {
code = e.ERROR_NOT_EXIST_META_DATABASE
}
}
if code == 200 {
basic.DeleteMetaDatabase(id)
}
c.JSON(http.StatusOK, gin.H{
"code" : code,
"msg" : e.GetMsg(code),
})
}

View File

@ -139,6 +139,5 @@ func DeleteVendor(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code" : code,
"msg" : e.GetMsg(code),
"data" : make(map[string]string),
})
}

View File

@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2019-08-11 15:32:17.412899 +0800 CST m=+0.054203802
// 2019-08-12 01:35:45.643682 +0800 CST m=+0.068783392
package docs
@ -187,6 +187,204 @@ var doc = `{
}
}
}
},
"post": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"meta_databases"
],
"summary": "新增数据源",
"parameters": [
{
"type": "string",
"description": "数据源 名称",
"name": "name",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 地址",
"name": "host",
"in": "formData",
"required": true
},
{
"type": "integer",
"description": "数据源 端口号",
"name": "port",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 数据库名称",
"name": "db_name",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 用户名",
"name": "username",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 密码",
"name": "password",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 备注",
"name": "comment",
"in": "formData"
},
{
"type": "integer",
"description": "系统厂商 id",
"name": "vendor_id",
"in": "formData",
"required": true
},
{
"type": "integer",
"description": "业务系统 id",
"name": "business_id",
"in": "formData",
"required": true
}
],
"responses": {
"200": {
"description": "{\"code\": 200, data: {}, \"msg\" : \"ok\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/v1/meta_databases/{id}": {
"delete": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"meta_databases"
],
"summary": "删除数据源",
"parameters": [
{
"type": "integer",
"description": "数据源 ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "{\"code\" : 200, \"msg\" : \"ok\"}",
"schema": {
"type": "string"
}
}
}
},
"patch": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"meta_databases"
],
"summary": "修改数据源",
"parameters": [
{
"type": "integer",
"description": "数据源 ID",
"name": "id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "数据源 名称",
"name": "name",
"in": "formData"
},
{
"type": "string",
"description": "数据源 地址",
"name": "host",
"in": "formData"
},
{
"type": "integer",
"description": "数据源 端口号",
"name": "port",
"in": "formData"
},
{
"type": "string",
"description": "数据源 数据库名称",
"name": "db_name",
"in": "formData"
},
{
"type": "string",
"description": "数据源 用户名",
"name": "username",
"in": "formData"
},
{
"type": "string",
"description": "数据源 密码",
"name": "password",
"in": "formData"
},
{
"type": "string",
"description": "数据源 备注",
"name": "comment",
"in": "formData"
},
{
"type": "integer",
"description": "系统厂商 id",
"name": "vendor_id",
"in": "formData"
},
{
"type": "integer",
"description": "业务系统 id",
"name": "business_id",
"in": "formData"
}
],
"responses": {
"200": {
"description": "{\"code\" : 200, \"msg\" : \"ok\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/v1/meta_databases/{id}/meta_tables": {

View File

@ -170,6 +170,204 @@
}
}
}
},
"post": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"meta_databases"
],
"summary": "新增数据源",
"parameters": [
{
"type": "string",
"description": "数据源 名称",
"name": "name",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 地址",
"name": "host",
"in": "formData",
"required": true
},
{
"type": "integer",
"description": "数据源 端口号",
"name": "port",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 数据库名称",
"name": "db_name",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 用户名",
"name": "username",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 密码",
"name": "password",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "数据源 备注",
"name": "comment",
"in": "formData"
},
{
"type": "integer",
"description": "系统厂商 id",
"name": "vendor_id",
"in": "formData",
"required": true
},
{
"type": "integer",
"description": "业务系统 id",
"name": "business_id",
"in": "formData",
"required": true
}
],
"responses": {
"200": {
"description": "{\"code\": 200, data: {}, \"msg\" : \"ok\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/v1/meta_databases/{id}": {
"delete": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"meta_databases"
],
"summary": "删除数据源",
"parameters": [
{
"type": "integer",
"description": "数据源 ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "{\"code\" : 200, \"msg\" : \"ok\"}",
"schema": {
"type": "string"
}
}
}
},
"patch": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"meta_databases"
],
"summary": "修改数据源",
"parameters": [
{
"type": "integer",
"description": "数据源 ID",
"name": "id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "数据源 名称",
"name": "name",
"in": "formData"
},
{
"type": "string",
"description": "数据源 地址",
"name": "host",
"in": "formData"
},
{
"type": "integer",
"description": "数据源 端口号",
"name": "port",
"in": "formData"
},
{
"type": "string",
"description": "数据源 数据库名称",
"name": "db_name",
"in": "formData"
},
{
"type": "string",
"description": "数据源 用户名",
"name": "username",
"in": "formData"
},
{
"type": "string",
"description": "数据源 密码",
"name": "password",
"in": "formData"
},
{
"type": "string",
"description": "数据源 备注",
"name": "comment",
"in": "formData"
},
{
"type": "integer",
"description": "系统厂商 id",
"name": "vendor_id",
"in": "formData"
},
{
"type": "integer",
"description": "业务系统 id",
"name": "business_id",
"in": "formData"
}
],
"responses": {
"200": {
"description": "{\"code\" : 200, \"msg\" : \"ok\"}",
"schema": {
"type": "string"
}
}
}
}
},
"/v1/meta_databases/{id}/meta_tables": {

View File

@ -112,6 +112,139 @@ paths:
summary: 数据源列表
tags:
- meta_databases
post:
consumes:
- multipart/form-data
parameters:
- description: 数据源 名称
in: formData
name: name
required: true
type: string
- description: 数据源 地址
in: formData
name: host
required: true
type: string
- description: 数据源 端口号
in: formData
name: port
required: true
type: integer
- description: 数据源 数据库名称
in: formData
name: db_name
required: true
type: string
- description: 数据源 用户名
in: formData
name: username
required: true
type: string
- description: 数据源 密码
in: formData
name: password
required: true
type: string
- description: 数据源 备注
in: formData
name: comment
type: string
- description: 系统厂商 id
in: formData
name: vendor_id
required: true
type: integer
- description: 业务系统 id
in: formData
name: business_id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: '{"code": 200, data: {}, "msg" : "ok"}'
schema:
type: string
summary: 新增数据源
tags:
- meta_databases
/v1/meta_databases/{id}:
delete:
consumes:
- application/json
parameters:
- description: 数据源 ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: '{"code" : 200, "msg" : "ok"}'
schema:
type: string
summary: 删除数据源
tags:
- meta_databases
patch:
consumes:
- multipart/form-data
parameters:
- description: 数据源 ID
in: path
name: id
required: true
type: integer
- description: 数据源 名称
in: formData
name: name
type: string
- description: 数据源 地址
in: formData
name: host
type: string
- description: 数据源 端口号
in: formData
name: port
type: integer
- description: 数据源 数据库名称
in: formData
name: db_name
type: string
- description: 数据源 用户名
in: formData
name: username
type: string
- description: 数据源 密码
in: formData
name: password
type: string
- description: 数据源 备注
in: formData
name: comment
type: string
- description: 系统厂商 id
in: formData
name: vendor_id
type: integer
- description: 业务系统 id
in: formData
name: business_id
type: integer
produces:
- application/json
responses:
"200":
description: '{"code" : 200, "msg" : "ok"}'
schema:
type: string
summary: 修改数据源
tags:
- meta_databases
/v1/meta_databases/{id}/meta_tables:
get:
consumes:

View File

@ -2,6 +2,7 @@ package basic
import (
"github.com/viletyy/potato/pkg/util"
"log"
"time"
)
@ -32,3 +33,44 @@ func GetMetaDatabaseTotal(maps interface{}) (count int) {
return
}
func ExistMetaDatabaseByName(name string) bool {
var metaDatabase MetaDatabase
util.DB.Select("id").Where("name = ?", name).First(&metaDatabase)
if metaDatabase.ID > 0 {
return true
}
return false
}
func ExistMetaDatabaseById(id int) bool {
var metaDatabase MetaDatabase
util.DB.Select("id").Where("id = ?", id).First(&metaDatabase)
if metaDatabase.ID > 0 {
return true
}
return false
}
func AddMetaDatabase(data map[string]interface{}) bool {
metaDatabase := &MetaDatabase{}
error := util.FillStruct(data, metaDatabase)
if error != nil {
log.Printf("Fill Struct is Fail")
}
util.DB.Create(metaDatabase)
return true
}
func EditMetaDatabase(id int, data interface{}) bool {
util.DB.Model(&MetaDatabase{}).Where("id = ?", id).Update(data)
return true
}
func DeleteMetaDatabase(id int) bool {
util.DB.Where("id = ?", id).Delete(&MetaDatabase{})
return true
}

View File

@ -1,6 +1,8 @@
package basic
import "github.com/viletyy/potato/pkg/util"
import (
"github.com/viletyy/potato/pkg/util"
)
type Vendor struct {
util.Model

View File

@ -9,6 +9,10 @@ const (
ERROR_NOT_EXIST_VENDOR = 10012
ERROR_EXIST_BUSINESS = 10021
ERROR_NOT_EXIST_BUSINESS = 10022
ERROR_EXIST_META_DATABASE = 10031
ERROR_NOT_EXIST_META_DATABASE = 10032
ERROR_EXIST_META_TABLE = 10041
ERROR_NOT_EXIST_META_TABLE = 10042
ERROR_AUTH_CHECK_TOKEN_FAIL = 20001
ERROR_AUTH_CHECK_TOKEN_TIMEOUT = 20002

View File

@ -8,6 +8,10 @@ var MsgFlags = map[int]string {
ERROR_NOT_EXIST_VENDOR : "该系统厂商不存在",
ERROR_EXIST_BUSINESS : "已存在该业务系统",
ERROR_NOT_EXIST_BUSINESS : "该业务系统不存在",
ERROR_EXIST_META_DATABASE : "已存在该数据源",
ERROR_NOT_EXIST_META_DATABASE : "该数据源不存在",
ERROR_EXIST_META_TABLE : "已存在该元数据",
ERROR_NOT_EXIST_META_TABLE : "该元数据不存在",
ERROR_AUTH_CHECK_TOKEN_FAIL : "Token鉴权失败",
ERROR_AUTH_CHECK_TOKEN_TIMEOUT : "Token已超时",

View File

@ -1,12 +1,15 @@
package util
import (
"errors"
"fmt"
"github.com/go-redis/redis"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/viletyy/potato/pkg/logging"
"github.com/viletyy/potato/pkg/setting"
"reflect"
"strconv"
"time"
)
@ -90,3 +93,68 @@ func InitRedis() {
logging.Fatal("redis连接失败!", err)
}
}
func FillStruct(data map[string]interface{}, obj interface{}) error {
for k, v := range data {
err := SetField(obj, k, v)
if err != nil {
return err
}
}
return nil
}
func SetField(obj interface{}, name string, value interface{}) error {
structValue := reflect.ValueOf(obj).Elem()
structFieldValue := structValue.FieldByName(name)
if !structFieldValue.IsValid() {
return fmt.Errorf("No such field: %s in obj", name)
}
if !structFieldValue.CanSet() {
return fmt.Errorf("Cannot set %s field value", name)
}
structFieldType := structFieldValue.Type() //结构体的类型
val := reflect.ValueOf(value) //map值的反射值
var err error
if structFieldType != val.Type() {
val, err = TypeConversion(fmt.Sprintf("%v", value), structFieldValue.Type().Name()) //类型转换
if err != nil {
return err
}
}
structFieldValue.Set(val)
return nil
}
func TypeConversion(value string, ntype string) (reflect.Value, error) {
if ntype == "string" {
return reflect.ValueOf(value), nil
} else if ntype == "time.Time" {
t, err := time.ParseInLocation("2006-01-02 15:04:05", value, time.Local)
return reflect.ValueOf(t), err
} else if ntype == "Time" {
t, err := time.ParseInLocation("2006-01-02 15:04:05", value, time.Local)
return reflect.ValueOf(t), err
} else if ntype == "int" {
i, err := strconv.Atoi(value)
return reflect.ValueOf(i), err
} else if ntype == "int8" {
i, err := strconv.ParseInt(value, 10, 64)
return reflect.ValueOf(int8(i)), err
} else if ntype == "int32" {
i, err := strconv.ParseInt(value, 10, 64)
return reflect.ValueOf(int64(i)), err
} else if ntype == "int64" {
i, err := strconv.ParseInt(value, 10, 64)
return reflect.ValueOf(i), err
} else if ntype == "float32" {
i, err := strconv.ParseFloat(value, 64)
return reflect.ValueOf(float32(i)), err
} else if ntype == "float64" {
i, err := strconv.ParseFloat(value, 64)
return reflect.ValueOf(i), err
}
//else if .......增加其他一些类型的转换
return reflect.ValueOf(value), errors.New("未知的类型:" + ntype)
}

View File

@ -8,6 +8,9 @@ func V1InitBasicRouter() {
metaDatabases := V1RouterGroup.Group("/meta_databases")
{
metaDatabases.GET("", basic.GetMetaDatabases)
metaDatabases.POST("", basic.AddMetaDatabase)
metaDatabases.PATCH("/:id", basic.EditMetaDatabase)
metaDatabases.DELETE("/:id", basic.DeleteMetaDatabase)
metaDatabases.GET("/:id/meta_tables", basic.GetMetaTables)
}
vendors := V1RouterGroup.Group("/vendors")