You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package DatasourceDAO
import (
"dsSupport/MyModel/MySwagger"
"dsSupport/Utils/CommonUtil"
"dsSupport/Utils/DbUtil"
"dsSupport/Utils/ErrorConst"
"dsSupport/Utils/LogUtil"
"dsSupport/models"
"encoding/json"
"fmt"
)
//数据库
var db = DbUtil.Engine
func GetDatasourceRow(query string) (bool, string, map[string]interface{}, error) {
sql := "SELECT * FROM t_dataex_datasource WHERE 1 = 1 " + query
var datasource models.TDataexDatasource
has, _ := DbUtil.Engine.SQL(sql).Get(&datasource)
if has == true {
m := make(map[string]interface{})
j, _ := json.Marshal(datasource)
json.Unmarshal(j, &m)
return true, "数据获取成功", m, nil
} else {
return false, "数据获取失败,数据源不存在", nil, nil
}
}
func GetDatasourceResults(query MySwagger.DatasourceQuery) (bool, string, int, []map[string]interface{}, error) {
sql := "SELECT * FROM t_dataex_datasource WHERE 1 = 1" + query.Conditions + " ORDER BY create_time DESC, change_time DESC"
//接收传入参数
var limit = 100
var offset = (query.Page - 1) * limit
//条件查询语句
conditionSql := fmt.Sprintf("%s", " limit ? offset ? ")
//分页的语句
pageSql := fmt.Sprintf("%s %s", sql, conditionSql)
//数据条数
count, _ := DbUtil.Engine.SQL(sql).Query().Count()
//分页数据
list, err := DbUtil.Engine.SQL(pageSql, limit, offset).Query().List()
if list != nil {
listJson, _ :=json.Marshal(list)
sql1 := "SELECT * FROM t_dataex_orgtree WHERE id IN (SELECT provide_orgid FROM t_dataex_datasource WHERE 1 = 1" + query.Conditions + ")"
//var offset1 = (query.Page - 1) * 10
//conditionSql1 := fmt.Sprintf("%s", " limit ? offset ? ")
//pageSql1 := fmt.Sprintf("%s %s", sql1, conditionSql1)
joinList1, _ := DbUtil.Engine.SQL(sql1).Query().List()
joinListJson1, _ := json.Marshal(joinList1)
mergedList := CommonUtil.ListMerge(string(listJson), string(joinListJson1), "provide_orgid", "id", "provide_orgname", "org_name")
var datas []map[string]interface{}
json.Unmarshal([]byte(mergedList), &datas)
return true, "数据获取成功", count, datas, err
} else {
return false, "数据获取失败,数据源不存在", count, nil, nil
}
}
func GetAllDatasourceResults(query string) (bool, string, int, []map[string]interface{}, error) {
sql := "SELECT * FROM t_dataex_datasource WHERE 1 = 1 " + query + " ORDER BY create_time DESC, change_time DESC"
// 数量
count, _ := DbUtil.Engine.SQL(sql).Query().Count()
// 数据条数
list, err := DbUtil.Engine.SQL(sql).Query().List()
if list != nil {
return true, "数据获取成功", count, list, err
} else {
return false, "数据获取失败,数据源不存在", count, nil, nil
}
}
func CreateDatasource(model *models.TDataexDatasource) (bool, string, error) {
_, err := db.Insert(model)
if err != nil {
fmt.Println("err=", err)
LogUtil.Error(ErrorConst.SqlUpdateError, "接入系统authToken Update数据库操作发生严重错误"+err.Error())
return false, "数据库操作失败", err
}
return true, "创建成功!", nil
}
func UpdateDatasource(id string, model *models.TDataexDatasource) (bool, string, error) {
_, err := db.Where(" id = ?", id).Update(model)
if err != nil {
LogUtil.Error(ErrorConst.SqlUpdateError, "接入系统authToken Update数据库操作发生严重错误"+err.Error())
return false, "数据库操作失败", err
}
return true, "修改成功!", nil
}
func RemoveDatasource(id string, model *models.TDataexDatasource) (bool, string, error) {
_, err := db.Where(" id = ?", id).Update(model)
if err != nil {
LogUtil.Error(ErrorConst.SqlUpdateError, "接入系统authToken Update数据库操作发生严重错误"+err.Error())
return false, "数据库操作失败", err
}
return true, "删除成功!", nil
}
func GetDatasourceCountByCode(code string) int64 {
business := new(models.TDataexDatasource)
total, err := db.Where("datasource_code =?", code).Count(business)
if err != nil {
LogUtil.Error(ErrorConst.SqlQueryError, "数据库操作发生严重错误:"+err.Error())
}
return total
}
func IsDatasourceExistsByCode(code string) bool {
if GetDatasourceCountByCode(code) > 0 {
return true
}
return false
}
func GetDatasourceIdByCode(code string) (bool, string, interface{}, error) {
sql := "SELECT id FROM t_dataex_datasource WHERE datasource_code=?"
var id int
has, _ := DbUtil.Engine.SQL(sql, code).Get(&id)
if has == true {
return true, "数据获取成功", id, nil
} else {
return false, "数据获取失败,数据源不存在", nil, nil
}
}