package data_manage import ( "errors" "eta_gn/eta_task/global" "eta_gn/eta_task/utils" "fmt" "strconv" "time" ) type EdbInfo struct { EdbInfoId int `gorm:"column:edb_info_id;primaryKey"` //`orm:"column(edb_info_id);pk"` EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"` SourceName string `description:"来源名称"` Source int `description:"来源id"` EdbCode string `description:"指标编码"` EdbName string `description:"指标名称"` EdbNameEn string `description:"英文指标名称"` EdbNameSource string `description:"指标名称来源"` Frequency string `description:"频率"` Unit string `description:"单位"` UnitEn string `description:"英文单位"` StartDate time.Time `description:"起始日期"` EndDate time.Time `description:"终止日期"` ClassifyId int `description:"分类id"` SysUserId int SysUserRealName string UniqueCode string `description:"指标唯一编码"` CreateTime time.Time ModifyTime time.Time BaseModifyTime time.Time MinValue float64 `description:"指标最小值"` MaxValue float64 `description:"指标最大值"` CalculateFormula string `description:"计算公式"` EdbType int `description:"指标类型:1:基础指标,2:计算指标"` Sort int `description:"排序字段"` LatestDate string `description:"数据最新日期(实际日期)"` LatestValue float64 `description:"数据最新值(实际值)"` EndValue float64 `description:"数据的最新值(预测日期的最新值)"` MoveType int `description:"移动方式:1:领先(默认),2:滞后"` MoveFrequency string `description:"移动频度"` NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"` ServerUrl string `description:"服务器地址"` ChartImage string `description:"图表图片"` Calendar string `description:"公历/农历" orm:"default(公历);"` DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"` ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"` EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"` MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"` TerminalCode string `description:"终端编码,用于配置在机器上"` DataUpdateTime string `description:"最近一次数据发生变化的时间"` ErDataUpdateDate string `description:"本次更新,数据发生变化的最早日期"` SourceIndexName string `description:"数据源中的指标名称"` SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"` SubSourceName string `description:"子数据来源名称"` IndicatorCode string `description:"指标代码"` StockCode string `description:"证券代码"` Extra string `description:"指标额外配置"` IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"` OriginalEdbCode string `description:"指标原始编码"` } type EdbInfoList struct { EdbInfoId int `gorm:"column:edb_info_id;primaryKey"` // `orm:"column(edb_info_id);pk"` SourceName string `description:"来源名称"` Source int `description:"来源id"` SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"` SubSourceName string `description:"子数据来源名称"` EdbCode string `description:"指标编码"` EdbName string `description:"指标名称"` Frequency string `description:"频率"` Unit string `description:"单位"` StartDate time.Time `description:"起始日期"` EndDate time.Time `description:"终止日期"` ClassifyId int `description:"分类id"` UniqueCode string `description:"指标唯一编码"` CalculateFormula string `description:"计算公式"` ModifyTime string `description:"更新时间"` NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"` } type EdbInfoSearchData struct { EdbDataId int `description:"指标数据Id"` DataTime string `description:"数据日期"` Value float64 `description:"数据"` } type EdbInfoSearchDataV1 struct { DataTime string `description:"数据日期"` Value string `description:"数据"` } func GetEdbInfoByCondition(condition string, pars []interface{}, order int) (item []*EdbInfoList, err error) { //o := orm.NewOrm() sql := ` SELECT * FROM edb_info WHERE 1=1 ` if condition != "" { sql += condition } if order == 1 { sql += ` ORDER BY end_date ASC ` } else { sql += ` ORDER BY edb_info_id ASC ` } //_, err = o.Raw(sql, pars).QueryRows(&item) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error return } func ModifyEdbDataInfoDate(edbInfoId int, maxDate string) (err error) { //o := orm.NewOrm() sql := ` UPDATE edb_info SET end_date=?,modify_time=NOW() WHERE edb_info_id=? ` //_, err = o.Raw(sql, maxDate, edbInfoId).Exec() err = global.DEFAULT_DmSQL.Exec(sql, maxDate, edbInfoId).Error return } type EdbInfoMaxAndMinInfo struct { MinDate string `description:"最小日期"` MaxDate string `description:"最大日期"` MinValue float64 `description:"最小值"` MaxValue float64 `description:"最大值"` LatestValue float64 `description:"最新值"` } func GetEdbInfoMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) { //o := orm.NewOrm() sql := `` tableName := GetEdbDataTableName(source, subSource) if tableName == "" { err = errors.New("无效的表名称:source:" + strconv.Itoa(source)) return nil, err } sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM %s WHERE edb_code=? ` sql = fmt.Sprintf(sql, tableName) //err = o.Raw(sql, edbCode).QueryRow(&item) err = global.DEFAULT_DmSQL.Raw(sql, edbCode).First(&item).Error var latest_value float64 sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 ` sql = fmt.Sprintf(sql, tableName) //err = o.Raw(sql, edbCode).QueryRow(&latest_value) err = global.DEFAULT_DmSQL.Raw(sql, edbCode).First(&latest_value).Error item.LatestValue = latest_value return } func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) { //o := orm.NewOrm() sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id=? ` //_, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec() err = global.DEFAULT_DmSQL.Exec(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Error return } //order:1升序,其余值为降序 func GetEdbDataListAll(condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchData, err error) { //o := orm.NewOrm() sql := `` tableName := GetEdbDataTableName(source, subSource) sql = ` SELECT * FROM %s WHERE 1=1 ` sql = fmt.Sprintf(sql, tableName) if condition != "" { sql += condition } if order == 1 { sql += ` ORDER BY data_time ASC ` } else { sql += ` ORDER BY data_time DESC ` } //_, err = o.Raw(sql, pars).QueryRows(&item) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error return } func GetEdbDataListAllV1(condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchDataV1, err error) { //o := orm.NewOrm() sql := `` tableName := GetEdbDataTableName(source, subSource) sql = ` SELECT * FROM %s WHERE 1=1 ` sql = fmt.Sprintf(sql, tableName) if condition != "" { sql += condition } if order == 1 { sql += ` ORDER BY data_time ASC ` } else { sql += ` ORDER BY data_time DESC ` } //_, err = o.Raw(sql, pars).QueryRows(&item) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error return } func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) { //o := orm.NewOrm() sql := ` SELECT * FROM edb_info WHERE edb_info_id=? ` //err = o.Raw(sql, edbInfoId).QueryRow(&item) err = global.DEFAULT_DmSQL.Raw(sql, edbInfoId).First(&item).Error return } func GetQuarterEdbInfo() (item []*EdbInfo, err error) { //o := orm.NewOrm() //sql := ` SELECT c.* FROM chart_info AS a // INNER JOIN chart_edb_mapping AS b ON a.chart_info_id=b.chart_info_id // INNER JOIN edb_info AS c ON b.edb_info_id=c.edb_info_id // WHERE a.chart_type=2 // GROUP BY b.edb_info_id // ORDER BY b.edb_info_id ASC ` sql := `SELECT c.edb_info_id, c.edb_info_type, c.source_name, c.source, c.edb_code, c.edb_name, c.edb_name_source, c.frequency, c.unit, c.start_date, c.end_date, c.classify_id, c.sys_user_id, c.sys_user_real_name, c.create_time, c.modify_time, c.unique_code, c.min_value, c.max_value, c.calculate_formula, c.edb_type, c.is_update, c.sort, c.latest_date, c.latest_value, c.move_type, c.move_frequency, c.no_update, c.server_url, c.edb_name_en, c.unit_en, c.chart_image, c.calendar, c.empty_type, c.max_empty_type, c.data_date_type, c.manual_save, c.terminal_code, c.data_update_time, c.er_data_update_date, c.source_index_name, c.end_value, c.sub_source, c.sub_source_name, c.indicator_code, c.stock_code, c.extra, c.is_join_permission, c.base_modify_time, c.is_static_data, c.set_update_time FROM chart_info AS a INNER JOIN chart_edb_mapping AS b ON a.chart_info_id = b.chart_info_id INNER JOIN edb_info AS c ON b.edb_info_id = c.edb_info_id WHERE a.chart_type = 2 GROUP BY b.edb_info_id, c.edb_info_id, c.edb_info_type, c.source_name, c.source, c.edb_code, c.edb_name, c.edb_name_source, c.frequency, c.unit, c.start_date, c.end_date, c.classify_id, c.sys_user_id, c.sys_user_real_name, c.create_time, c.modify_time, c.unique_code, c.min_value, c.max_value, c.calculate_formula, c.edb_type, c.is_update, c.sort, c.latest_date, c.latest_value, c.move_type, c.move_frequency, c.no_update, c.server_url, c.edb_name_en, c.unit_en, c.chart_image, c.calendar, c.empty_type, c.max_empty_type, c.data_date_type, c.manual_save, c.terminal_code, c.data_update_time, c.er_data_update_date, c.source_index_name, c.end_value, c.sub_source, c.sub_source_name, c.indicator_code, c.stock_code, c.extra, c.is_join_permission, c.base_modify_time, c.is_static_data, c.set_update_time ORDER BY b.edb_info_id ASC ` //_, err = o.Raw(sql).QueryRows(&item) err = global.DEFAULT_DmSQL.Raw(sql).Find(&item).Error return } func ResetEdbInfoIsUpdate() (err error) { //o := orm.NewOrm() sql := ` UPDATE edb_info SET is_update=1 ` //_, err = o.Raw(sql).Exec() err = global.DEFAULT_DmSQL.Exec(sql).Error return } // GetEdbInfoCalculateListByCondition 获取指标关系列表 func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) { //o := orm.NewOrm() //calculateTableName := GetEdbInfoCalculateTableName(source) //if calculateTableName == "" { // err = errors.New("无效的表名") // return //} sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 ` //sql = fmt.Sprintf(sql, calculateTableName) if condition != "" { sql += condition } //_, err = o.Raw(sql, pars).QueryRows(&items) err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error return } func DeleteEdbDataByIdAndSource(edbDataId, source, subSource int) (err error) { sql := ` DELETE FROM %s WHERE edb_data_id=? ` tableName := GetEdbDataTableName(source, subSource) sql = fmt.Sprintf(sql, tableName) //o := orm.NewOrm() //_, err = o.Raw(sql, edbDataId).Exec() err = global.DEFAULT_DmSQL.Exec(sql, edbDataId).Error return } type EdbInfoClassify struct { EdbInfoId int `gorm:"column:edb_info_id;primaryKey"` // `orm:"column(edb_info_id);pk"` SourceName string `description:"来源名称"` Source int `description:"来源id"` EdbCode string `description:"指标编码"` ClassifyId int `description:"分类id"` SysUserId int SysUserRealName string UniqueCode string `description:"指标唯一编码"` CreateTime time.Time ModifyTime time.Time } // GetAllEdbInfoClassifyListByCondition // @Description: 获取指标与分类的关系列表 // @author: Roc // @datetime 2024-02-29 10:55:38 // @param condition string // @param pars []interface{} // @return item []*EdbInfoUpdateLog // @return err error func GetAllEdbInfoClassifyListByCondition(condition string, pars []interface{}) (item []*EdbInfoClassify, err error) { //o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM edb_info WHERE 1=1 ` if condition != "" { sql += condition } sql += `ORDER BY edb_info_id ASC ` //_, err = o.Raw(sql, pars).QueryRows(&item) err = global.DmSQL["data"].Raw(sql, pars...).Find(&item).Error return } // GetEdbInfoItemByCodeAndSource // @Description: 根据指标编码和来源id获取指标信息 // @author: Roc // @datetime 2024-03-11 16:26:23 // @param source int // @param edbCode string // @return item *EdbInfo // @return err error func GetEdbInfoItemByCodeAndSource(source int, edbCode string) (item *EdbInfoItem, err error) { //o := orm.NewOrm() sql := ` SELECT * FROM edb_info WHERE edb_code=? AND source = ?` //err = o.Raw(sql, source, edbCode).QueryRow(&item) err = global.DEFAULT_DmSQL.Raw(sql, source, edbCode).First(&item).Error return } // GetEdbInfoMaxModifyTime // @Description: 根据指标来源和编码获取该指标数据最晚修改时间 // @author: Roc // @datetime 2024-03-11 17:01:01 // @param source int // @param edbCode string // @return modifyTime string // @return err error func GetEdbInfoMaxModifyTime(source, subSource int, edbCode string) (modifyTime string, err error) { //o := orm.NewOrmUsingDB("data") tableName := GetEdbDataTableName(source, subSource) if tableName == "" { err = errors.New("无效的表名称:source:" + strconv.Itoa(source)) return } sql := ` SELECT MAX(modify_time) AS modify_time FROM %s WHERE edb_code=? ` sql = fmt.Sprintf(sql, tableName) //err = o.Raw(sql, edbCode).QueryRow(&modifyTime) err = global.DmSQL["data"].Raw(sql, edbCode).Scan(&modifyTime).Error return } func GetEdbInfoPageByCondition(condition string, pars []interface{}, startPage, pageSize int) (item []*EdbInfo, err error) { //o := orm.NewOrm() sql := ` SELECT * FROM edb_info WHERE 1=1 ` if condition != "" { sql += condition } sql += ` LIMIT ?,? ` //_, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&item) pars = append(pars, startPage, pageSize) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error return } func GetEdbInfoCountByCondition(condition string, pars []interface{}) (total int64, err error) { //o := orm.NewOrm() sql := ` SELECT count(*) FROM edb_info WHERE 1=1 ` if condition != "" { sql += condition } //err = o.Raw(sql, pars).QueryRow(&total) err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&total).Error return } func ModifyEdbUpdateStatus(edbIdList []int, indexCodeList []string, calculateEdbInfoIds []int) (err error) { idNum := len(edbIdList) if idNum <= 0 { return } //o, err := orm.NewOrmUsingDB("data").Begin() o := global.DmSQL["data"].Begin() if err != nil { return } defer func() { if err != nil { _ = o.Rollback() return } _ = o.Commit() }() // 更改指标的更新状态 sql := ` UPDATE edb_info SET no_update = 1 WHERE source in (?, ?) AND edb_info_id IN (` + utils.GetOrmInReplace(idNum) + `) AND no_update = 0` //_, err = o.Raw(sql, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND, edbIdList).Exec() err = o.Exec(sql, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND, edbIdList).Error if err != nil { return } // 更改钢联化工指标更新状态 if len(indexCodeList) > 0 { // 更改数据源的更新状态 sql = ` UPDATE base_from_mysteel_chemical_index SET is_stop = 1 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=0` //_, err = o.Raw(sql, indexCodeList).Exec() err = o.Exec(sql, indexCodeList).Error if err != nil { return } } // 更新相关的计算指标状态 if len(calculateEdbInfoIds) > 0 { // 批量更新相关联的指标ID sql = ` UPDATE edb_info SET no_update = 1 WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(calculateEdbInfoIds)) + `) AND no_update = 0` //_, err = o.Raw(sql, calculateEdbInfoIds).Exec() err = o.Exec(sql, calculateEdbInfoIds).Error if err != nil { return } } return } // GetEdbInfoByIdList 根据指标id集合 获取 指标列表 func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) { num := len(edbInfoIdList) if num <= 0 { return } //o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) ` //_, err = o.Raw(sql, edbInfoIdList).QueryRows(&items) err = global.DmSQL["data"].Raw(sql, edbInfoIdList).Find(&items).Error return } // GetAllBaseEdbInfo // @Description: 获取所有基础指标(只有国能数据节点的数据) // @return item // @return err func GetAllBaseEdbInfo() (item []*EdbInfo, err error) { sql := ` SELECT * FROM edb_info WHERE 1=1 AND "edb_info_type" = ? AND "edb_type" = ?` pars := []interface{}{utils.EDB_INFO_TYPE, utils.EdbTypeBase} err = global.DmSQL["data"].Raw(sql, pars...).Find(&item).Error return } // AddEdbInfo // @Description: 新增指标 // @param item // @return err func AddEdbInfo(item *EdbInfo) (err error) { err = global.DmSQL["data"].Create(item).Error if err != nil { return } return } // Update // @Description: 更新指定参数 // @receiver m // @param cols // @return err func (m *EdbInfo) Update(cols []string) (err error) { err = global.DmSQL["data"].Select(cols).Updates(m).Error return }