Roc 1 month ago
parent
commit
8ce9041c61
8 changed files with 99 additions and 86 deletions
  1. 0 1
      global/global.go
  2. 6 6
      global/initDb.go
  3. 4 0
      main.go
  4. 25 33
      models/business_conf.go
  5. 5 5
      models/data_manage/edb_source.go
  6. 15 15
      models/db.go
  7. 35 26
      models/edb_monitor/edb_monitor.go
  8. 9 0
      utils/constants.go

+ 0 - 1
global/global.go

@@ -1,7 +1,6 @@
 package global
 
 import (
-	_ "dm"
 	_ "github.com/go-sql-driver/mysql"
 	"gorm.io/gorm"
 )

+ 6 - 6
global/initDb.go

@@ -36,20 +36,20 @@ func InitDb() {
 	})
 
 	// 默认库
-	connectDb(utils.MYSQL_URL, "default", newLogger, dbMap, true)
+	connectDb(utils.MYSQL_URL, utils.DbNameMaster, newLogger, dbMap, true)
 
 	// 报告库
-	connectDb(utils.MYSQL_URL_RDDP, "rddp", newLogger, dbMap, false)
+	connectDb(utils.MYSQL_URL_RDDP, utils.DbNameReport, newLogger, dbMap, false)
 	// 手工数据库
-	connectDb(utils.MYSQL_URL_EDB, "edb", newLogger, dbMap, false)
+	connectDb(utils.MYSQL_URL_EDB, utils.DbNameManualIndex, newLogger, dbMap, false)
 	// 指标库
-	connectDb(utils.MYSQL_URL_DATA, "data", newLogger, dbMap, false)
+	connectDb(utils.MYSQL_URL_DATA, utils.DbNameIndex, newLogger, dbMap, false)
 	// 钢联库
-	connectDb(utils.MYSQL_URL_GL, "gl", newLogger, dbMap, false)
+	connectDb(utils.MYSQL_URL_GL, utils.DbNameGL, newLogger, dbMap, false)
 
 	if utils.MYSQL_AI_URL != "" {
 		// AI库
-		connectDb(utils.MYSQL_AI_URL, "ai", newLogger, dbMap, false)
+		connectDb(utils.MYSQL_AI_URL, utils.DbNameAI, newLogger, dbMap, false)
 	}
 
 	//全局赋值数据库链接

+ 4 - 0
main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"eta/eta_api/controllers"
+	"eta/eta_api/models"
 	"eta/eta_api/services/alarm_msg"
 	"fmt"
 	"runtime"
@@ -24,6 +25,9 @@ func main() {
 	}
 	go services.Task()
 
+	// 初始化表数据入库
+	models.AfterInitTable()
+
 	// 异常处理
 	web.ErrorController(&controllers.ErrorController{})
 

+ 25 - 33
models/business_conf.go

@@ -1,9 +1,9 @@
 package models
 
 import (
+	"eta/eta_api/global"
 	"eta/eta_api/utils"
 	"fmt"
-	"github.com/beego/beego/v2/client/orm"
 	"html"
 	"strings"
 	"time"
@@ -94,12 +94,11 @@ func (m *BusinessConf) PrimaryId() string {
 }
 
 func (m *BusinessConf) Create() (err error) {
-	o := orm.NewOrm()
-	id, err := o.Insert(m)
+	err = global.DEFAULT_DB.Create(m).Error
 	if err != nil {
 		return
 	}
-	m.Id = int(id)
+
 	return
 }
 
@@ -107,47 +106,46 @@ func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
 	if len(items) == 0 {
 		return
 	}
-	o := orm.NewOrm()
-	_, err = o.InsertMulti(len(items), items)
+	err = global.DEFAULT_DB.CreateInBatches(items, utils.MultiAddNum).Error
+
 	return
 }
 
 func (m *BusinessConf) Update(cols []string) (err error) {
-	o := orm.NewOrm()
-	_, err = o.Update(m, cols...)
+	err = global.DEFAULT_DB.Select(cols).Updates(m).Error
+
 	return
 }
 
 func (m *BusinessConf) Del() (err error) {
-	o := orm.NewOrm()
 	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
-	_, err = o.Raw(sql, m.Id).Exec()
+	err = global.DEFAULT_DB.Exec(sql, m.Id).Error
+
 	return
 }
 
 func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
-	o := orm.NewOrm()
 	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
-	err = o.Raw(sql, id).QueryRow(&item)
+	err = global.DEFAULT_DB.Raw(sql, id).First(&item).Error
+
 	return
 }
 
 func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
-	o := orm.NewOrm()
 	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
-	err = o.Raw(sql, pars).QueryRow(&item)
+	err = global.DEFAULT_DB.Raw(sql, pars).First(&item).Error
+
 	return
 }
 
 func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
-	o := orm.NewOrm()
 	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
-	err = o.Raw(sql, pars).QueryRow(&count)
+	err = global.DEFAULT_DB.Raw(sql, pars).Scan(&count).Error
+
 	return
 }
 
 func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
-	o := orm.NewOrm()
 	fields := strings.Join(fieldArr, ",")
 	if len(fieldArr) == 0 {
 		fields = `*`
@@ -157,12 +155,12 @@ func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{},
 		order = ` ORDER BY ` + orderRule
 	}
 	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
-	_, err = o.Raw(sql, pars).QueryRows(&items)
+	err = global.DEFAULT_DB.Raw(sql, pars).Find(&items).Error
+
 	return
 }
 
 func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
-	o := orm.NewOrm()
 	fields := strings.Join(fieldArr, ",")
 	if len(fieldArr) == 0 {
 		fields = `*`
@@ -172,7 +170,8 @@ func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interfac
 		order = ` ORDER BY ` + orderRule
 	}
 	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
-	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	err = global.DEFAULT_DB.Raw(sql, pars, startSize, pageSize).Find(&items).Error
+
 	return
 }
 
@@ -181,9 +180,8 @@ func GetBusinessConf() (list map[string]string, err error) {
 	list = make(map[string]string)
 
 	var items []*BusinessConf
-	o := orm.NewOrm()
 	sql := `SELECT * FROM business_conf`
-	_, err = o.Raw(sql).QueryRows(&items)
+	err = global.DEFAULT_DB.Raw(sql).Find(&items).Error
 	if err != nil {
 		return
 	}
@@ -206,27 +204,21 @@ type BusinessConfUpdate struct {
 
 // UpdateBusinessConfMulti 批量修改配置
 func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
-	o := orm.NewOrm()
-	p, err := o.Raw("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?").Prepare()
-	if err != nil {
-		return
-	}
-	defer func() {
-		_ = p.Close()
-	}()
+	sql := `UPDATE business_conf SET conf_val = ? WHERE conf_key = ?`
 	for _, v := range items {
-		_, err = p.Exec(v.ConfVal, v.ConfKey)
+		err = global.DEFAULT_DB.Exec(sql, v.ConfVal, v.ConfKey).Error
 		if err != nil {
 			return
 		}
 	}
+
 	return
 }
 
 func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
-	o := orm.NewOrm()
 	sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
-	err = o.Raw(sql, key).QueryRow(&item)
+	err = global.DEFAULT_DB.Raw(sql, key).First(&item).Error
+
 	return
 }
 

+ 5 - 5
models/data_manage/edb_source.go

@@ -1,9 +1,9 @@
 package data_manage
 
 import (
+	"eta/eta_api/global"
 	"eta/eta_api/utils"
 	"fmt"
-	"github.com/beego/beego/v2/client/orm"
 	"strings"
 )
 
@@ -29,7 +29,6 @@ type EdbSource struct {
 
 // GetEdbSourceItemsByCondition 获取指标来源列表
 func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbSource, err error) {
-	o := orm.NewOrmUsingDB("data")
 	fields := strings.Join(fieldArr, ",")
 	if len(fieldArr) == 0 {
 		fields = `*`
@@ -39,7 +38,8 @@ func GetEdbSourceItemsByCondition(condition string, pars []interface{}, fieldArr
 		order = ` ORDER BY ` + orderRule
 	}
 	sql := fmt.Sprintf(`SELECT %s FROM edb_source WHERE 1=1 %s %s`, fields, condition, order)
-	_, err = o.Raw(sql, pars).QueryRows(&items)
+	err = global.DbMap[utils.DbNameIndex].Raw(sql, pars).Find(&items).Error
+
 	return
 }
 
@@ -54,9 +54,9 @@ type EdbSourceChild struct {
 
 // GetEdbSourceItemByCondition 获取指标来源
 func GetEdbSourceItemByCondition(condition string, pars []interface{}) (item *EdbSource, err error) {
-	o := orm.NewOrmUsingDB("data")
 	sql := fmt.Sprintf(`SELECT * FROM edb_source WHERE 1=1 %s`, condition)
-	err = o.Raw(sql, pars).QueryRow(&item)
+	err = global.DbMap[utils.DbNameIndex].Raw(sql, pars).First(&item).Error
+
 	return
 }
 

+ 15 - 15
models/db.go

@@ -39,7 +39,7 @@ import (
 	"github.com/beego/beego/v2/client/orm"
 )
 
-func init() {
+func init2() {
 
 	_ = orm.RegisterDataBase("default", "mysql", utils.MYSQL_URL)
 	orm.SetMaxIdleConns("default", 50)
@@ -227,7 +227,7 @@ func init() {
 	}
 
 	// 初始化部分数据表变量(直接init会有顺序问题=_=!)
-	afterInitTable()
+	AfterInitTable()
 
 	// 智能看板
 	initBiDashBoard()
@@ -537,18 +537,18 @@ func initChartFramework() {
 // initExcel 初始化EXCEL
 func initExcel() {
 	orm.RegisterModel(
-		new(excel.ExcelClassify),         //ETA excel表格分类
-		new(excel.ExcelInfo),             //ETA excel表格
-		new(excel.ExcelDraft),            //ETA excel表格草稿
-		new(excel.ExcelSheet),            //ETA excel sheet
-		new(excel.ExcelSheetData),        //ETA excel sheet data
-		new(excel.ExcelEdbMapping),       //ETA excel 与 指标 的关系表
-		new(excel.ExcelWorker),           // 平衡表协作人表格
-		new(excel.ExcelChartEdb),         // 平衡表做图指标
-		new(excel.ExcelChartData),        // 平衡表作图数据
-		new(excel.ExcelInfoRuleMapping),  //表格的管理规则
+		new(excel.ExcelClassify),                         //ETA excel表格分类
+		new(excel.ExcelInfo),                             //ETA excel表格
+		new(excel.ExcelDraft),                            //ETA excel表格草稿
+		new(excel.ExcelSheet),                            //ETA excel sheet
+		new(excel.ExcelSheetData),                        //ETA excel sheet data
+		new(excel.ExcelEdbMapping),                       //ETA excel 与 指标 的关系表
+		new(excel.ExcelWorker),                           // 平衡表协作人表格
+		new(excel.ExcelChartEdb),                         // 平衡表做图指标
+		new(excel.ExcelChartData),                        // 平衡表作图数据
+		new(excel.ExcelInfoRuleMapping),                  //表格的管理规则
 		new(tradeAnalysisModel.TradeAnalysisTableColumn), // 持仓分析表格-自定义列
-		new(excel.ReferencedExcelConfig), //表格的样式引用
+		new(excel.ReferencedExcelConfig),                 //表格的样式引用
 	)
 }
 
@@ -723,11 +723,11 @@ func initAiPredictModel() {
 	)
 }
 
-// afterInitTable
+// AfterInitTable
 // @Description: 初始化表结构的的后置操作
 // @author: Roc
 // @datetime 2024-07-01 13:31:09
-func afterInitTable() {
+func AfterInitTable() {
 	// 初始化指标来源配置
 	data_manage.InitEdbSourceVar()
 

+ 35 - 26
models/edb_monitor/edb_monitor.go

@@ -1,10 +1,9 @@
 package edbmonitor
 
 import (
+	"eta/eta_api/global"
 	"eta/eta_api/utils"
 	"time"
-
-	"github.com/beego/beego/v2/client/orm"
 )
 
 type EdbMonitorInfo struct {
@@ -32,37 +31,41 @@ type EdbMonitorInfo struct {
 }
 
 func (m *EdbMonitorInfo) Insert() (int64, error) {
-	o := orm.NewOrmUsingDB("data")
-	return o.Insert(m)
+	err := global.DbMap[utils.DbNameIndex].Create(m).Error
+	if err != nil {
+		return 0, err
+	}
+
+	return int64(m.EdbMonitorId), err
 }
 
 func (m *EdbMonitorInfo) Update(cols []string) (err error) {
 	if len(cols) == 0 {
 		return
 	}
-	o := orm.NewOrmUsingDB("data")
-	_, err = o.Update(m, cols...)
+	err = global.DbMap[utils.DbNameIndex].Select(cols).Updates(m).Error
 	return
 }
 
 func GetEdbMonitorLevelList() (list []string, err error) {
-	o := orm.NewOrmUsingDB("data")
 	sql := `SELECT DISTINCT monitor_level FROM edb_monitor_info`
-	_, err = o.Raw(sql).QueryRows(&list)
+	err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&list).Error
+
 	return
 }
 
 func GetEdbMonitorEdbInfoList() (items []*EdbMonitorInfo, err error) {
-	o := orm.NewOrmUsingDB("data")
 	sql := `SELECT * FROM edb_monitor_info WHERE state <> 0`
-	_, err = o.Raw(sql).QueryRows(&items)
+	err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&items).Error
+
 	return
 }
 
 func DeleteEdbMonitorInfoById(id int) (err error) {
-	o := orm.NewOrmUsingDB("data")
+	o := global.DbMap[utils.DbNameIndex]
 	sql := `DELETE FROM edb_monitor_info WHERE edb_monitor_id =?`
-	_, err = o.Raw(sql, id).Exec()
+	err = o.Exec(sql, id).Error
+
 	return
 }
 
@@ -70,23 +73,26 @@ func DeleteEdbMonitorInfoByIdList(ids []int) (err error) {
 	if len(ids) == 0 {
 		return
 	}
-	o := orm.NewOrmUsingDB("data")
+	o := global.DbMap[utils.DbNameIndex]
 	sql := `DELETE FROM edb_monitor_info WHERE edb_monitor_id IN(` + utils.GetOrmInReplace(len(ids)) + `)`
-	_, err = o.Raw(sql, ids).Exec()
+	err = o.Exec(sql, ids).Error
+
 	return
 }
 
 func GetEdbMonitorInfoById(id int) (item *EdbMonitorInfo, err error) {
-	o := orm.NewOrmUsingDB("data")
+	o := global.DbMap[utils.DbNameIndex]
 	sql := `SELECT * FROM edb_monitor_info WHERE edb_monitor_id =?`
-	err = o.Raw(sql, id).QueryRow(&item)
+	err = o.Raw(sql, id).First(&item).Error
+
 	return
 }
 
 func GetEdbMonitorInfoByEdbInfoId(edbInfoId int) (items []*EdbMonitorInfo, err error) {
-	o := orm.NewOrmUsingDB("data")
+	o := global.DbMap[utils.DbNameIndex]
 	sql := `SELECT * FROM edb_monitor_info WHERE edb_info_id =?`
-	_, err = o.Raw(sql, edbInfoId).QueryRows(&items)
+	err = o.Raw(sql, edbInfoId).Find(&items).Error
+
 	return
 }
 
@@ -94,36 +100,39 @@ func GetEdbMonitorInfoCountByClassifyId(classifyId []int) (count int, err error)
 	if len(classifyId) == 0 {
 		return
 	}
-	o := orm.NewOrmUsingDB("data")
+	o := global.DbMap[utils.DbNameIndex]
 	sql := `SELECT COUNT(*) AS count FROM edb_monitor_info WHERE edb_monitor_classify_id IN(` + utils.GetOrmInReplace(len(classifyId)) + `)`
-	err = o.Raw(sql, classifyId).QueryRow(&count)
+	err = o.Raw(sql, classifyId).Scan(&count).Error
+
 	return
 }
 
 func GetEdbMonitorInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
-	o := orm.NewOrmUsingDB("data")
+	o := global.DbMap[utils.DbNameIndex]
 	sql := `SELECT COUNT(*) AS count FROM edb_monitor_info WHERE 1=1 `
 	if condition != "" {
 		sql += condition
 	}
-	err = o.Raw(sql, pars).QueryRow(&count)
+	err = o.Raw(sql, pars).Scan(&count).Error
 	return
 }
 
 func GetEdbMonitorInfoPageByCondition(condition string, pars []interface{}, startSize int, pageSize int) (items []*EdbMonitorInfo, err error) {
-	o := orm.NewOrmUsingDB("data")
+	o := global.DbMap[utils.DbNameIndex]
 	sql := `SELECT * FROM edb_monitor_info WHERE 1=1 `
 	if condition != "" {
 		sql += condition
 	}
 	sql += ` ORDER BY create_time DESC LIMIT?,?`
-	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	err = o.Raw(sql, pars, startSize, pageSize).Find(&items).Error
+
 	return
 }
 
 func GetEdbMonitorCreateUserId() (userIds []int, err error) {
-	o := orm.NewOrmUsingDB("data")
+	o := global.DbMap[utils.DbNameIndex]
 	sql := `SELECT DISTINCT create_user_id FROM edb_monitor_info`
-	_, err = o.Raw(sql).QueryRows(&userIds)
+	err = o.Raw(sql).Scan(&userIds).Error
+
 	return
 }

+ 9 - 0
utils/constants.go

@@ -549,3 +549,12 @@ const (
 	DbDriverByMysql = "mysql"
 	DbDriverByDm    = "dm"
 )
+
+const (
+	DbNameMaster      = "eta_master"
+	DbNameReport      = "eta_report"
+	DbNameManualIndex = "eta_manual_index"
+	DbNameIndex       = "eta_index"
+	DbNameGL          = "eta_gl"
+	DbNameAI          = "eta_ai"
+)