package data_manage import ( "eta/eta_api/global" "eta/eta_api/utils" "github.com/rdlucklib/rdluck_tools/paging" "gorm.io/gorm" ) type SciClassify struct { TypeName string `orm:"column(type_name)" description:"分类名称"` TypeCode string `orm:"column(type_code)" description:"分类名称编码"` } func GetSciClassify() (items []*SciClassify, err error) { sql := `SELECT CONCAT(type_2,type_3) AS type_name,CONCAT(type_2,'#',type_3) type_code FROM base_from_sci_index GROUP BY CONCAT(type_2,type_3) ORDER BY CONCAT(type_2,type_3) ASC ` o := global.DbMap[utils.DbNameIndex] err = o.Raw(sql).Find(&items).Error return } type SciFrequency struct { Frequency string `description:"频度"` } func GetSciFrequencyByClassifyId(classifyId int) (items []*GlFrequency, err error) { o := global.DbMap[utils.DbNameIndex] sql := ` SELECT frequency FROM base_from_sci_index WHERE classify_id = ? ` sql += ` GROUP BY frequency ORDER BY frequency ASC ` err = o.Raw(sql, classifyId).Find(&items).Error return } type SciIndex struct { BaseFromSciIndexId int `orm:"column(base_from_sci_index_id);pk" gorm:"primaryKey"` Interface string Name string IndexCode string IndexName string Type1 string `gorm:"column:type_1"` Type2 string `gorm:"column:type_2"` Type3 string `gorm:"column:type_3"` Frequency string Unit string ApiStartTime string ApiUpdateTime string StartTime string FinishTime string CreateTime string ModifyTime string } func (sciIndex *SciIndex) AfterFind(tx *gorm.DB) (err error) { if utils.NeedDateOrTimeFormat(utils.DbDriverName) { if sciIndex.CreateTime != "" { sciIndex.CreateTime = utils.GormDateStrToDateTimeStr(sciIndex.CreateTime) } if sciIndex.ModifyTime != "" { sciIndex.ModifyTime = utils.GormDateStrToDateTimeStr(sciIndex.ModifyTime) } if sciIndex.ApiStartTime != "" { sciIndex.ApiStartTime = utils.GormDateStrToDateStr(sciIndex.ApiStartTime) } if sciIndex.ApiUpdateTime != "" { sciIndex.ApiUpdateTime = utils.GormDateStrToDateTimeStr(sciIndex.ApiUpdateTime) } if sciIndex.StartTime != "" { sciIndex.StartTime = utils.GormDateStrToDateTimeStr(sciIndex.StartTime) } if sciIndex.FinishTime != "" { sciIndex.FinishTime = utils.GormDateStrToDateTimeStr(sciIndex.FinishTime) } } return } func GetSciIndex(condition string, pars []interface{}) (items []*SciIndex, err error) { o := global.DbMap[utils.DbNameIndex] sql := ` SELECT * FROM base_from_sci_index WHERE 1=1 ` if condition != "" { sql += condition } sql += `ORDER BY sort ASC, base_from_sci_index_id asc` err = o.Raw(sql, pars...).Find(&items).Error return } type SciExportIndex struct { TypeName string IndexCode string IndexName string Type1 string `gorm:"column:type_1"` Type2 string `gorm:"column:type_2"` Type3 string `gorm:"column:type_3"` Frequency string Unit string ModifyTime string } func GetExportSciIndex(typeCodes []string) (items []*SciExportIndex, err error) { if len(typeCodes) == 0 { return } o := global.DbMap[utils.DbNameIndex] sql := ` SELECT *,CONCAT(type_2, "#", type_3) AS type_name FROM base_from_sci_index WHERE CONCAT(type_2, "#", type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `) ORDER BY frequency ASC,index_code ASC` err = o.Raw(sql, typeCodes).Find(&items).Error return } func GetSciFrequency(classifyId int) (items []*string, err error) { sql := `SELECT DISTINCT frequency FROM base_from_sci_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') ` o := global.DbMap[utils.DbNameIndex] err = o.Raw(sql, classifyId).Find(&items).Error return } func GetSciFrequencyByCode(code string) (items []*string, err error) { sql := `SELECT DISTINCT frequency FROM base_from_sci_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') ` o := global.DbMap[utils.DbNameIndex] err = o.Raw(sql, code).Find(&items).Error return } type SciIndexList struct { BaseFromSciIndexId int `orm:"column(base_from_sci_index_id);pk" gorm:"primaryKey"` Interface string Name string IndexCode string IndexName string Type1 string `gorm:"column:type_1"` Type2 string `gorm:"column:type_2"` Type3 string `gorm:"column:type_3"` Frequency string Unit string ApiStartTime string ApiUpdateTime string StartTime string FinishTime string ModifyTime string DataList []*SciIndexData `gorm:"-"` Paging *paging.PagingItem `description:"分页数据" gorm:"-"` } type SciIndexData struct { Value string `orm:"column(value)" description:"日期"` DataTime string `orm:"column(data_time)" description:"值"` } func (sciIndexData *SciIndexData) AfterFind(tx *gorm.DB) (err error) { if utils.NeedDateOrTimeFormat(utils.DbDriverName) { if sciIndexData.DataTime != "" { sciIndexData.DataTime = utils.GormDateStrToDateStr(sciIndexData.DataTime) } } return } func GetSciIndexData(indexCode string, startSize, pageSize int) (items []*SciIndexData, err error) { o := global.DbMap[utils.DbNameIndex] sql := ` SELECT * FROM base_from_sci_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? ` err = o.Raw(sql, indexCode, startSize, pageSize).Find(&items).Error return } func GetSciIndexDataCount(indexCode string) (count int, err error) { o := global.DbMap[utils.DbNameIndex] sql := ` SELECT COUNT(1) AS count FROM base_from_sci_data WHERE index_code=? ` err = o.Raw(sql, indexCode).Scan(&count).Error return } // GetSciItemList 模糊查询Sci数据库指标列表 func GetSciItemList(keyword string) (items []*SciIndex, err error) { o := global.DbMap[utils.DbNameIndex] sql := "SELECT * FROM base_from_sci_index WHERE CONCAT(index_name,index_code) LIKE ? " err = o.Raw(sql, utils.GetLikeKeyword(keyword)).Find(&items).Error return } func GetSciIndexDataByCode(indexCode string) (items []*SciIndexData, err error) { o := global.DbMap[utils.DbNameIndex] sql := ` SELECT * FROM base_from_sci_data WHERE index_code=? ORDER BY data_time DESC ` err = o.Raw(sql, indexCode).Find(&items).Error return } func GetSciDataMaxCount(classifyId int) (count int, err error) { o := global.DbMap[utils.DbNameIndex] sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM ( SELECT COUNT(1) AS num FROM base_from_sci_index AS a INNER JOIN base_from_sci_data AS b ON a.index_code=b.index_code WHERE a.classify_id=? GROUP BY a.base_from_sci_index_id )AS t ` err = o.Raw(sql, classifyId).Scan(&count).Error return } type ExportSciDataMaxCount struct { TypeName string Count int } func GetExportSciDataMaxCount(typeCodes []string) (items []*ExportSciDataMaxCount, err error) { if len(typeCodes) == 0 { return } o := global.DbMap[utils.DbNameIndex] sql := ` SELECT COALESCE(MAX(t.num), 0) AS count, t.type_name FROM ( SELECT COUNT(1) AS num, CONCAT(a.type_2, "#", a.type_3) AS type_name FROM base_from_sci_index AS a INNER JOIN base_from_sci_data AS b ON a.index_code = b.index_code WHERE CONCAT(a.type_2, "#", a.type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `) GROUP BY a.base_from_sci_index_id ) AS t GROUP BY type_name ` err = o.Raw(sql, typeCodes).Find(&items).Error return } type ExportSciIndexData struct { Value string `orm:"column(value)" description:"日期"` DataTime string `orm:"column(data_time)" description:"值"` IndexCode string `orm:"column(index_code)" description:"指标编码"` } func GetExportSciIndexDataByCodes(indexCodes []string) (items []*ExportSciIndexData, err error) { if len(indexCodes) == 0 { return } o := global.DbMap[utils.DbNameIndex] sql := ` SELECT index_code,data_time,value FROM base_from_sci_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) ORDER BY data_time DESC ` err = o.Raw(sql, indexCodes).Find(&items).Error return }