|
@@ -1,39 +1,36 @@
|
|
|
package models
|
|
|
|
|
|
import (
|
|
|
+ "eta/eta_index_lib/global"
|
|
|
"eta/eta_index_lib/utils"
|
|
|
"fmt"
|
|
|
- "github.com/beego/beego/v2/client/orm"
|
|
|
+
|
|
|
+ "gorm.io/gorm"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
// EdbDataCalculateZjpj 直接拼接数据结构体
|
|
|
type EdbDataCalculateZjpj struct {
|
|
|
- EdbDataId int `orm:"column(edb_data_id);pk"`
|
|
|
- EdbInfoId int
|
|
|
- EdbCode string
|
|
|
- DataTime string
|
|
|
- Value float64
|
|
|
- Status int
|
|
|
- CreateTime time.Time
|
|
|
- ModifyTime time.Time
|
|
|
- DataTimestamp int64
|
|
|
+ EdbDataId int `gorm:"primaryKey;autoIncrement;column:edb_data_id"`
|
|
|
+ EdbInfoId int `gorm:"column:edb_info_id" description:"指标ID"`
|
|
|
+ EdbCode string `gorm:"column:edb_code" description:"指标编码"`
|
|
|
+ DataTime string `gorm:"column:data_time" description:"数据日期"`
|
|
|
+ Value float64 `gorm:"column:value" description:"数据值"`
|
|
|
+ Status int `gorm:"column:status" description:"状态"`
|
|
|
+ CreateTime time.Time `gorm:"column:status" description:"创建时间"`
|
|
|
+ ModifyTime time.Time `gorm:"column:create_time" description:"修改时间"`
|
|
|
+ DataTimestamp int64 `gorm:"column:modify_time" description:"数据日期时间戳"`
|
|
|
}
|
|
|
|
|
|
// AddCalculateZjpj 新增直接拼接数据
|
|
|
func AddCalculateZjpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfo *EdbInfo, err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
- to, err := o.Begin()
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
+ to := global.DEFAULT_DB.Begin()
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
|
- fmt.Println("AddCalculateZjpj,Err:" + err.Error())
|
|
|
- _ = to.Rollback()
|
|
|
+ to.Rollback()
|
|
|
} else {
|
|
|
- _ = to.Commit()
|
|
|
+ to.Commit()
|
|
|
}
|
|
|
}()
|
|
|
if req.EdbInfoId <= 0 {
|
|
@@ -59,12 +56,12 @@ func AddCalculateZjpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdb
|
|
|
EdbType: 2,
|
|
|
Sort: GetAddEdbMaxSortByClassifyId(req.ClassifyId, utils.EDB_INFO_TYPE),
|
|
|
}
|
|
|
- newEdbInfoId, tmpErr := to.Insert(edbInfo)
|
|
|
+ tmpErr := to.Create(edbInfo).Error
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
return
|
|
|
}
|
|
|
- edbInfo.EdbInfoId = int(newEdbInfoId)
|
|
|
+
|
|
|
} else {
|
|
|
edbInfo, err = GetEdbInfoById(req.EdbInfoId)
|
|
|
if err != nil {
|
|
@@ -89,14 +86,14 @@ func AddCalculateZjpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdb
|
|
|
fmt.Println("dataTableName:" + dataTableName)
|
|
|
deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
|
|
|
deleteSql = fmt.Sprintf(deleteSql, dataTableName)
|
|
|
- _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
|
|
|
+ err = to.Exec(deleteSql, req.EdbInfoId).Error
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//删除指标关系
|
|
|
sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
|
|
|
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
+ err = to.Exec(sql, edbInfo.EdbInfoId).Error
|
|
|
}
|
|
|
|
|
|
//关联关系
|
|
@@ -121,12 +118,11 @@ func AddCalculateZjpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdb
|
|
|
ModifyTime: time.Now(),
|
|
|
FromSubSource: firstEdbInfo.SubSource,
|
|
|
}
|
|
|
- insertId, tmpErr := to.Insert(existItemA)
|
|
|
+ tmpErr := to.Create(existItemA).Error
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
return
|
|
|
}
|
|
|
- existItemA.EdbInfoCalculateMappingId = int(insertId)
|
|
|
}
|
|
|
|
|
|
//第二个指标
|
|
@@ -148,12 +144,11 @@ func AddCalculateZjpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdb
|
|
|
ModifyTime: time.Now(),
|
|
|
FromSubSource: secondEdbInfo.SubSource,
|
|
|
}
|
|
|
- insertId, tmpErr := to.Insert(existItemB)
|
|
|
+ tmpErr := to.Create(existItemB).Error
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
return
|
|
|
}
|
|
|
- existItemB.EdbInfoCalculateMappingId = int(insertId)
|
|
|
}
|
|
|
|
|
|
//拼接数据
|
|
@@ -164,17 +159,12 @@ func AddCalculateZjpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdb
|
|
|
|
|
|
// EditCalculateZjpj 编辑直接拼接数据
|
|
|
func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
- to, err := o.Begin()
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
+ to := global.DEFAULT_DB.Begin()
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
|
- fmt.Println("EditCalculateZjpj,Err:" + err.Error())
|
|
|
- _ = to.Rollback()
|
|
|
+ to.Rollback()
|
|
|
} else {
|
|
|
- _ = to.Commit()
|
|
|
+ to.Commit()
|
|
|
}
|
|
|
}()
|
|
|
|
|
@@ -190,7 +180,7 @@ func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo,
|
|
|
edbInfo.EdbNameEn = req.EdbNameEn
|
|
|
edbInfo.UnitEn = req.UnitEn
|
|
|
edbInfo.ModifyTime = time.Now()
|
|
|
- _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EdbNameEn", "UnitEn")
|
|
|
+ err = to.Model(edbInfo).Select([]string{"EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EdbNameEn", "UnitEn"}).Updates(edbInfo).Error
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -229,7 +219,7 @@ func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo,
|
|
|
if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
|
|
|
//删除之前的A指标关联关系
|
|
|
sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
|
|
|
- _, err = to.Raw(sql, edbInfo.EdbInfoId, existItemA.FromEdbInfoId).Exec()
|
|
|
+ err = to.Exec(sql, edbInfo.EdbInfoId, existItemA.FromEdbInfoId).Error
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
|
|
|
return
|
|
@@ -254,13 +244,11 @@ func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo,
|
|
|
ModifyTime: time.Now(),
|
|
|
FromSubSource: firstEdbInfo.SubSource,
|
|
|
}
|
|
|
- insertId, tmpErr := to.Insert(existItemA)
|
|
|
+ tmpErr := to.Create(existItemA).Error
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
return
|
|
|
}
|
|
|
- existItemA.EdbInfoCalculateMappingId = int(insertId)
|
|
|
-
|
|
|
isNeedCalculateData = true
|
|
|
}
|
|
|
}
|
|
@@ -272,7 +260,7 @@ func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo,
|
|
|
if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
|
|
|
//删除之前的B指标关联关系
|
|
|
sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
|
|
|
- _, err = to.Raw(sql, edbInfo.EdbInfoId, existItemB.FromEdbInfoId).Exec()
|
|
|
+ err = to.Exec(sql, edbInfo.EdbInfoId, existItemB.FromEdbInfoId).Error
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
|
|
|
return
|
|
@@ -296,12 +284,11 @@ func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo,
|
|
|
ModifyTime: time.Now(),
|
|
|
FromSubSource: secondEdbInfo.SubSource,
|
|
|
}
|
|
|
- insertId, tmpErr := to.Insert(existItemB)
|
|
|
+ tmpErr := to.Create(existItemB).Error
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
return
|
|
|
}
|
|
|
- existItemB.EdbInfoCalculateMappingId = int(insertId)
|
|
|
|
|
|
isNeedCalculateData = true
|
|
|
}
|
|
@@ -313,7 +300,7 @@ func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo,
|
|
|
|
|
|
tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
|
|
|
sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? `, tableName)
|
|
|
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
+ err = to.Exec(sql, edbInfo.EdbInfoId).Error
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("删除历史数据失败,Err:" + err.Error())
|
|
|
return
|
|
@@ -325,24 +312,14 @@ func EditCalculateZjpj(req *EdbInfoCalculateBatchEditReq, edbInfo, firstEdbInfo,
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// GetAllEdbDataCalculateZjpjByEdbInfoId 根据指标id获取全部的数据
|
|
|
-func GetAllEdbDataCalculateZjpjByEdbInfoId(edbInfoId int) (items []*EdbDataCalculateZjpj, err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
- sql := ` SELECT * FROM edb_data_calculate_zjpj WHERE edb_info_id=? ORDER BY data_time DESC `
|
|
|
- _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
// RefreshAllCalculateZjpj 刷新所有 直接拼接 数据
|
|
|
func RefreshAllCalculateZjpj(edbInfo *EdbInfo) (err error) {
|
|
|
- o := orm.NewOrm()
|
|
|
- to, err := o.Begin()
|
|
|
+ to := global.DEFAULT_DB.Begin()
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
|
- fmt.Println("RefreshAllCalculateZjpj,Err:" + err.Error())
|
|
|
- _ = to.Rollback()
|
|
|
+ to.Rollback()
|
|
|
} else {
|
|
|
- _ = to.Commit()
|
|
|
+ to.Commit()
|
|
|
}
|
|
|
}()
|
|
|
//查询关联指标信息
|
|
@@ -371,7 +348,7 @@ func RefreshAllCalculateZjpj(edbInfo *EdbInfo) (err error) {
|
|
|
}
|
|
|
|
|
|
// refreshAllCalculateZjpj 刷新所有 直接拼接 数据
|
|
|
-func refreshAllCalculateZjpj(to orm.TxOrmer, edbInfo *EdbInfo, existItemA, existItemB *EdbInfoCalculateMapping) (err error) {
|
|
|
+func refreshAllCalculateZjpj(to *gorm.DB, edbInfo *EdbInfo, existItemA, existItemB *EdbInfoCalculateMapping) (err error) {
|
|
|
//查询当前指标现有的数据
|
|
|
var condition string
|
|
|
var pars []interface{}
|
|
@@ -380,7 +357,7 @@ func refreshAllCalculateZjpj(to orm.TxOrmer, edbInfo *EdbInfo, existItemA, exist
|
|
|
|
|
|
var dataList []*EdbDataCalculateZjpj
|
|
|
sql := ` SELECT * FROM edb_data_calculate_zjpj WHERE edb_info_id=? ORDER BY data_time DESC `
|
|
|
- _, err = to.Raw(sql, edbInfo.EdbInfoId).QueryRows(&dataList)
|
|
|
+ err = to.Raw(sql, edbInfo.EdbInfoId).Scan(&dataList).Error
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -416,7 +393,7 @@ func refreshAllCalculateZjpj(to orm.TxOrmer, edbInfo *EdbInfo, existItemA, exist
|
|
|
if edbData.Value != v.Value {
|
|
|
//更新指标数据
|
|
|
edbData.Value = v.Value
|
|
|
- _, _ = to.Update(edbData, "Value")
|
|
|
+ _ = to.Model(edbData).Select([]string{"Value"}).Updates(edbData).Error
|
|
|
}
|
|
|
} else {
|
|
|
//时间戳
|
|
@@ -461,7 +438,7 @@ func refreshAllCalculateZjpj(to orm.TxOrmer, edbInfo *EdbInfo, existItemA, exist
|
|
|
//更新指标数据
|
|
|
edbData.Value = v.Value
|
|
|
edbData.ModifyTime = time.Now()
|
|
|
- _, tmpErr := to.Update(edbData, "Value", "ModifyTime")
|
|
|
+ tmpErr := to.Model(edbData).Select([]string{"Value", "ModifyTime"}).Updates(edbData).Error
|
|
|
if tmpErr != nil {
|
|
|
fmt.Println("tmpErr:", tmpErr)
|
|
|
}
|
|
@@ -493,13 +470,13 @@ func refreshAllCalculateZjpj(to orm.TxOrmer, edbInfo *EdbInfo, existItemA, exist
|
|
|
removeDateList = append(removeDateList, dateTime)
|
|
|
}
|
|
|
if len(removeDateList) > 0 {
|
|
|
- removeDateStr := strings.Join(removeDateList, `","`)
|
|
|
- removeDateStr = `"` + removeDateStr + `"`
|
|
|
+ removeDateStr := strings.Join(removeDateList, `','`)
|
|
|
+ removeDateStr = `'` + removeDateStr + `'`
|
|
|
//如果拼接指标变更了,那么需要删除所有的指标数据
|
|
|
tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
|
|
|
sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
|
|
|
|
|
|
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
+ err = to.Exec(sql, edbInfo.EdbInfoId).Error
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("删除不存在的直接拼接指标数据失败,Err:" + err.Error())
|
|
|
return
|
|
@@ -515,7 +492,7 @@ func refreshAllCalculateZjpj(to orm.TxOrmer, edbInfo *EdbInfo, existItemA, exist
|
|
|
tmpAddDataList = append(tmpAddDataList, v)
|
|
|
i++
|
|
|
if i >= 500 {
|
|
|
- _, err = to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
|
|
|
+ err = to.CreateInBatches(tmpAddDataList, 500).Error
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -525,7 +502,7 @@ func refreshAllCalculateZjpj(to orm.TxOrmer, edbInfo *EdbInfo, existItemA, exist
|
|
|
}
|
|
|
|
|
|
if len(tmpAddDataList) > 0 {
|
|
|
- _, err = to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
|
|
|
+ err = to.CreateInBatches(tmpAddDataList, 500).Error
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|