package models import ( "eta_gn/eta_index_lib/global" "eta_gn/eta_index_lib/utils" "fmt" "gorm.io/gorm" "strings" "time" ) // 指标检索数据 type EdbDataItem struct { EdbCode string `description:"指标编码"` StartDate string `description:"起始日期"` EndDate string `description:"终止日期"` EdbName string `description:"指标名称"` Unit string `description:"单位"` Frequency string `description:"频率"` DataList []*EdbData } type EdbData struct { EdbDataId int `gorm:"primaryKey;autoIncrement;column:edb_data_id"` EdbInfoId int `gorm:"column:edb_info_id"` EdbCode string `gorm:"column:edb_code"` DataTime string `gorm:"column:data_time"` Value string `gorm:"column:value"` Status int `gorm:"column:status"` CreateTime time.Time `gorm:"column:create_time"` ModifyTime time.Time `gorm:"column:modify_time"` DataTimestamp int64 `gorm:"column:data_timestamp"` } // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据 func (m *EdbData) AfterFind(db *gorm.DB) (err error) { if m.DataTime == "" { return } minDateTmp, err := time.ParseInLocation(utils.FormatDateWallWithLoc, m.DataTime, time.Local) if err != nil { return } m.DataTime = minDateTmp.Format(utils.FormatDate) return } func GetAddSql(edbInfoId, edbCode, dataTime, timestampStr string, value string) (addSql string) { nowStr := time.Now().Format(utils.FormatDateTime) addSql += "(" addSql += edbInfoId + "," + "'" + edbCode + "'" + "," + "'" + dataTime + "'" + "," + value + "," + "'" + nowStr + "'" + "," + "'" + nowStr + "'" addSql += "," + "'" + timestampStr + "'" addSql += ")," return } type AddEdbInfoReq struct { EdbCode string `description:"指标编码"` Source int `description:"指标来源ID"` StockCode string `description:"证券代码"` Frequency string `description:"频度"` ExtraPars string `description:"额外参数(如同花顺日期序列)"` } // GetEdbInfoCountByCondition 获取指标数量 func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) { sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 ` if condition != "" { sql += condition } err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error return } // GetEdbDataAllByEdbCode 根据指标编码获取指标数据列表 func GetEdbDataAllByEdbCode(edbCode string, source, subSource, limit int) (items []*EdbInfoSearchData, err error) { var pars []interface{} pars = append(pars, edbCode) tableName := GetEdbDataTableName(source, subSource) sql := ` SELECT * FROM %s WHERE edb_code=? ORDER BY data_time DESC` if limit > 0 { sql += ` LIMIT ? ` pars = append(pars, limit) } sql = fmt.Sprintf(sql, tableName) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&items).Error return } func GetEdbDataByCondition(source, subSource int, condition string, pars []interface{}) (items []*EdbInfoSearchData, err error) { tableName := GetEdbDataTableName(source, subSource) sql := ` SELECT * FROM %s WHERE 1=1 ` if condition != "" { sql += condition } sql += ` ORDER BY data_time DESC ` sql = fmt.Sprintf(sql, tableName) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&items).Error if err != nil { return } for _, v := range items { v.ConvertTimeStr() } return } // GetEdbDataByDate 根据数据日期获取指标数据 func GetEdbDataByDate(source, subSource int, edbCode, dataTime string) (item *EdbInfoSearchData, err error) { tableName := GetEdbDataTableName(source, subSource) sql := ` SELECT * FROM %s WHERE 1=1 AND edb_code = ? AND data_time =? ORDER BY data_time DESC ` sql = fmt.Sprintf(sql, tableName) err = global.DEFAULT_DmSQL.Raw(sql, edbCode, dataTime).First(&item).Error return } func ModifyEdbDataById(source, subSource, edbDataId int, value string) (err error) { tableName := GetEdbDataTableName(source, subSource) sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_data_id=? ` sql = fmt.Sprintf(sql, tableName) err = global.DEFAULT_DmSQL.Exec(sql, value, edbDataId).Error return } func DeleteEdbDataById(source, subSource, edbDataId int) (err error) { sql := ` DELETE FROM %s WHERE edb_data_id=? ` tableName := GetEdbDataTableName(source, subSource) sql = fmt.Sprintf(sql, tableName) err = global.DEFAULT_DmSQL.Exec(sql, edbDataId).Error return } type RefreshEdbInfoReq struct { EdbInfoId int `description:"指标ID"` EdbCode string `description:"指标编码"` StartDate string `description:"开始日期"` Source int `description:"指标来源ID"` } // GetAllEdbDataListByTo 获取所有的指标数据列表 func GetAllEdbDataListByTo(to *gorm.DB, edbInfoId, source, subSource int) (existDataList []*EdbData, err error) { dataTableName := GetEdbDataTableName(source, subSource) sql := `SELECT * FROM %s WHERE edb_info_id=? order by data_time asc` sql = fmt.Sprintf(sql, dataTableName) err = to.Raw(sql, edbInfoId).Find(&existDataList).Error return } // GetFinalLastByTo 获取所有的指标数据列表 func GetFinalLastByTo(to *gorm.DB, edbInfoId, source, subSource int, latestDate string) (finalLast EdbInfoSearchData, err error) { dataTableName := GetEdbDataTableName(source, subSource) sql := fmt.Sprintf(` SELECT data_time , value FROM %s WHERE edb_info_id=? and data_time<=? ORDER BY data_time DESC `, dataTableName) err = to.Raw(sql, edbInfoId, latestDate).First(&finalLast).Error if err != nil && err.Error() != utils.ErrNoRow() { return } return } // 新版本 type EdbDataV1 struct { EdbDataId int `gorm:"primaryKey;autoIncrement;column:edb_data_id"` EdbInfoId int `gorm:"column:edb_info_id"` EdbCode string `gorm:"column:edb_code"` DataTime string `gorm:"column:data_time"` Value string `gorm:"column:value"` Status int `gorm:"column:status"` CreateTime time.Time `gorm:"column:create_time"` ModifyTime time.Time `gorm:"column:modify_time"` DataTimestamp int64 `gorm:"column:data_timestamp"` } type EdbDataList struct { EdbDataId int `description:" 指标数据ID"` EdbInfoId int `description:"指标ID"` DataTime string //`json:"-" description:"数据日期"` DataTimestamp int64 `description:"数据日期"` Value float64 `description:"数据值"` } func DelEdbDataByMysql(to *gorm.DB, edbInfoId int, tableName string, removeDateList []string) (err error) { if len(removeDateList) <= 0 { return } // 移除不存在的日期数据 removeDateStr := strings.Join(removeDateList, `','`) removeDateStr = `'` + removeDateStr + `'` sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr) err = to.Exec(sql, edbInfoId).Error return } // GetEdbDataList // @Description: // @author: Roc // @datetime 2024-05-08 16:10:49 // @param source int // @param subSource int // @param endInfoId int // @param startDate string // @param endDate string // @return list []*EdbDataList // @return err error func GetEdbDataList(source, subSource, endInfoId int, startDate, endDate string) (list []*EdbDataList, err error) { list = make([]*EdbDataList, 0) tmpDataList, err := GetEdbDataListAll(source, subSource, FindEdbDataListAllCond{ EdbInfoId: endInfoId, StartDataTime: startDate, StartDataTimeCond: ">=", EndDataTime: endDate, EndDataTimeCond: "<=", }, 1) if err != nil { return } // 数据拼接 for _, v := range tmpDataList { list = append(list, &EdbDataList{ EdbDataId: v.EdbDataId, EdbInfoId: v.EdbInfoId, DataTime: v.DataTime, DataTimestamp: v.DataTimestamp, Value: v.Value, }) } return } type AddEdbBaseInfoReq struct { EdbCode string `description:"指标编码"` EdbName string `description:"指标名称"` Unit string `description:"单位"` ClassifyId int `description:"所属分类"` SysUserId int `description:"用户id"` SysUserRealName string `description:"用户真实名称"` } // EditEdbBaseInfoReq 编辑基础指标请求参数 type EditEdbBaseInfoReq struct { SysUserId int `description:"用户id"` SysUserRealName string `description:"用户真实名称"` EdbInfoId int `description:"指标id"` EdbName string `description:"指标名称"` EdbNameEn string `description:"英文指标名称"` Frequency string `description:"频率"` Unit string `description:"单位"` UnitEn string `description:"英文单位"` ClassifyId int `description:"分类id"` } // ConvertDate // @Description: 格式化EdbData的日期 // @receiver e func (m *EdbData) ConvertDate() { m.DataTime = utils.GormDateStrToDateStr(m.DataTime) return }