123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- package data_manage
- import (
- "eta/eta_api/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "strings"
- "time"
- )
- // FactorEdbSeriesMapping 因子指标系列-指标关联表
- type FactorEdbSeriesMapping struct {
- FactorEdbSeriesMappingId int `orm:"column(factor_edb_series_mapping_id);pk"`
- FactorEdbSeriesId int `description:"因子指标系列ID"`
- EdbInfoId int `description:"指标ID"`
- EdbCode string `description:"指标编码"`
- CalculateData string `description:"计算数据-JSON(如相关性矩阵等等)"`
- CreateTime time.Time `description:"创建时间"`
- ModifyTime time.Time `description:"修改时间"`
- }
- func (m *FactorEdbSeriesMapping) TableName() string {
- return "factor_edb_series_mapping"
- }
- type FactorEdbSeriesMappingCols struct {
- PrimaryId string
- FactorEdbSeriesId string
- EdbInfoId string
- EdbCode string
- CalculateData string
- CreateTime string
- ModifyTime string
- }
- func (m *FactorEdbSeriesMapping) Cols() FactorEdbSeriesMappingCols {
- return FactorEdbSeriesMappingCols{
- PrimaryId: "factor_edb_series_mapping_id",
- FactorEdbSeriesId: "factor_edb_series_id",
- EdbInfoId: "edb_info_id",
- EdbCode: "edb_code",
- CalculateData: "calculate_data",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- }
- }
- func (m *FactorEdbSeriesMapping) Create() (err error) {
- o := orm.NewOrmUsingDB("data")
- id, err := o.Insert(m)
- if err != nil {
- return
- }
- m.FactorEdbSeriesMappingId = int(id)
- return
- }
- func (m *FactorEdbSeriesMapping) CreateMulti(items []*FactorEdbSeriesMapping) (err error) {
- if len(items) == 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- _, err = o.InsertMulti(len(items), items)
- return
- }
- func (m *FactorEdbSeriesMapping) Update(cols []string) (err error) {
- o := orm.NewOrmUsingDB("data")
- _, err = o.Update(m, cols...)
- return
- }
- func (m *FactorEdbSeriesMapping) Remove() (err error) {
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- _, err = o.Raw(sql, m.FactorEdbSeriesMappingId).Exec()
- return
- }
- func (m *FactorEdbSeriesMapping) MultiRemove(ids []int) (err error) {
- if len(ids) == 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
- _, err = o.Raw(sql, ids).Exec()
- return
- }
- func (m *FactorEdbSeriesMapping) RemoveByCondition(condition string, pars []interface{}) (err error) {
- if condition == "" {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
- _, err = o.Raw(sql, pars).Exec()
- return
- }
- func (m *FactorEdbSeriesMapping) GetItemById(id int) (item *FactorEdbSeriesMapping, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
- err = o.Raw(sql, id).QueryRow(&item)
- return
- }
- func (m *FactorEdbSeriesMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *FactorEdbSeriesMapping, err error) {
- o := orm.NewOrmUsingDB("data")
- order := ``
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
- err = o.Raw(sql, pars).QueryRow(&item)
- return
- }
- func (m *FactorEdbSeriesMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- func (m *FactorEdbSeriesMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*FactorEdbSeriesMapping, err error) {
- o := orm.NewOrmUsingDB("data")
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
- if orderRule != "" {
- 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)
- return
- }
- func (m *FactorEdbSeriesMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*FactorEdbSeriesMapping, err error) {
- o := orm.NewOrmUsingDB("data")
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
- if orderRule != "" {
- 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)
- return
- }
- // FactorEdbSeriesMappingItem 因子指标系列-指标关联信息
- type FactorEdbSeriesMappingItem struct {
- SeriesId int `description:"因子指标系列ID"`
- EdbInfoId int `description:"指标ID"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- EdbNameEn string `description:"指标名称-英文"`
- }
- func (m *FactorEdbSeriesMapping) Format2Item() (item *FactorEdbSeriesMappingItem) {
- item = new(FactorEdbSeriesMappingItem)
- item.SeriesId = m.FactorEdbSeriesId
- item.EdbInfoId = m.EdbInfoId
- item.EdbCode = m.EdbCode
- return
- }
- // FactorEdbSeriesMappingUpdateCalculate 更新计算数据
- //type FactorEdbSeriesMappingUpdateCalculate struct {
- // SeriesId int `description:"因子指标系列ID"`
- // EdbInfoId int `description:"指标ID"`
- // CalculateData string `description:"计算数据-JSON"`
- //}
- //
- //func (m *FactorEdbSeriesMapping) UpdateCalculateData(updates []*FactorEdbSeriesMappingUpdateCalculate) (err error) {
- // if len(updates) == 0 {
- // return
- // }
- // o := orm.NewOrm()
- // sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ? AND %s = ?`, m.TableName(), m.Cols().CalculateData, m.Cols().FactorEdbSeriesId, m.Cols().EdbInfoId)
- // p, e := o.Raw(sql).Prepare()
- // if e != nil {
- // err = fmt.Errorf("sql prepare err: %v", e)
- // return
- // }
- // defer func() {
- // _ = p.Close()
- // }()
- // for _, v := range updates {
- // _, e = p.Exec(v.CalculateData, v.SeriesId, v.EdbInfoId)
- // if e != nil {
- // err = fmt.Errorf("update exec err: %v", e)
- // return
- // }
- // }
- // return
- //}
- // GetChartUsedFactorSeriesEdb 获取图表引用的系列指标
- func GetChartUsedFactorSeriesEdb(chartId int) (items []*FactorEdbSeriesMapping, err error) {
- o := orm.NewOrmUsingDB("data")
- chartOb := new(FactorEdbSeriesChartMapping)
- edbOb := new(FactorEdbSeriesMapping)
- sql := fmt.Sprintf(`SELECT b.* FROM %s AS a
- JOIN %s AS b ON a.%s = b.%s AND a.%s = b.%s
- WHERE a.%s = ? ORDER BY %s ASC`, chartOb.TableName(), edbOb.TableName(), chartOb.Cols().FactorEdbSeriesId, edbOb.Cols().FactorEdbSeriesId, chartOb.Cols().EdbInfoId, edbOb.Cols().EdbInfoId, chartOb.Cols().ChartInfoId, edbOb.Cols().FactorEdbSeriesId)
- _, err = o.Raw(sql, chartId).QueryRows(&items)
- return
- }
|