base_from_edb_mapping.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // BaseFromEdbMapping 同花顺高频数据
  10. type BaseFromEdbMapping struct {
  11. Id int `orm:"column(id);pk"`
  12. BaseFromIndexId int `description:"源指标ID"`
  13. BaseIndexCode string `description:"源指标编码"`
  14. EdbInfoId int `description:"指标ID"`
  15. EdbCode string `description:"指标编码"`
  16. Source int `description:"指标来源:1-同花顺..."`
  17. SubSource int `description:"子数据来源:0-经济数据库;1-日期序列;2-高频数据"`
  18. ConvertRule string `description:"转换规则"`
  19. CreateTime time.Time `description:"创建时间"`
  20. ModifyTime time.Time `description:"修改时间"`
  21. }
  22. func (m *BaseFromEdbMapping) TableName() string {
  23. return "base_from_edb_mapping"
  24. }
  25. type BaseFromEdbMappingCols struct {
  26. PrimaryId string
  27. BaseFromIndexId string
  28. BaseIndexCode string
  29. EdbInfoId string
  30. EdbCode string
  31. Source string
  32. SubSource string
  33. ConvertRule string
  34. CreateTime string
  35. ModifyTime string
  36. }
  37. func (m *BaseFromEdbMapping) Cols() BaseFromEdbMappingCols {
  38. return BaseFromEdbMappingCols{
  39. PrimaryId: "id",
  40. BaseFromIndexId: "base_from_index_id",
  41. BaseIndexCode: "base_index_code",
  42. EdbInfoId: "edb_info_id",
  43. EdbCode: "edb_code",
  44. Source: "source",
  45. SubSource: "sub_source",
  46. ConvertRule: "convert_rule",
  47. CreateTime: "create_time",
  48. ModifyTime: "modify_time",
  49. }
  50. }
  51. func (m *BaseFromEdbMapping) Create() (err error) {
  52. o := orm.NewOrm()
  53. id, err := o.Insert(m)
  54. if err != nil {
  55. return
  56. }
  57. m.Id = int(id)
  58. return
  59. }
  60. func (m *BaseFromEdbMapping) CreateMulti(items []*BaseFromEdbMapping) (err error) {
  61. if len(items) == 0 {
  62. return
  63. }
  64. o := orm.NewOrm()
  65. _, err = o.InsertMulti(len(items), items)
  66. return
  67. }
  68. func (m *BaseFromEdbMapping) Update(cols []string) (err error) {
  69. o := orm.NewOrm()
  70. _, err = o.Update(m, cols...)
  71. return
  72. }
  73. func (m *BaseFromEdbMapping) Remove() (err error) {
  74. o := orm.NewOrm()
  75. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  76. _, err = o.Raw(sql, m.Id).Exec()
  77. return
  78. }
  79. func (m *BaseFromEdbMapping) MultiRemove(ids []int) (err error) {
  80. if len(ids) == 0 {
  81. return
  82. }
  83. o := orm.NewOrm()
  84. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  85. _, err = o.Raw(sql, ids).Exec()
  86. return
  87. }
  88. func (m *BaseFromEdbMapping) RemoveByCondition(condition string, pars []interface{}) (err error) {
  89. if condition == "" {
  90. return
  91. }
  92. o := orm.NewOrm()
  93. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  94. _, err = o.Raw(sql, pars).Exec()
  95. return
  96. }
  97. func (m *BaseFromEdbMapping) GetItemById(id int) (item *BaseFromEdbMapping, err error) {
  98. o := orm.NewOrm()
  99. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  100. err = o.Raw(sql, id).QueryRow(&item)
  101. return
  102. }
  103. func (m *BaseFromEdbMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromEdbMapping, err error) {
  104. o := orm.NewOrm()
  105. order := ``
  106. if orderRule != "" {
  107. order = ` ORDER BY ` + orderRule
  108. }
  109. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  110. err = o.Raw(sql, pars).QueryRow(&item)
  111. return
  112. }
  113. func (m *BaseFromEdbMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  114. o := orm.NewOrm()
  115. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  116. err = o.Raw(sql, pars).QueryRow(&count)
  117. return
  118. }
  119. func (m *BaseFromEdbMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromEdbMapping, err error) {
  120. o := orm.NewOrm()
  121. fields := strings.Join(fieldArr, ",")
  122. if len(fieldArr) == 0 {
  123. fields = `*`
  124. }
  125. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  126. if orderRule != "" {
  127. order = ` ORDER BY ` + orderRule
  128. }
  129. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  130. _, err = o.Raw(sql, pars).QueryRows(&items)
  131. return
  132. }
  133. func (m *BaseFromEdbMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromEdbMapping, err error) {
  134. o := orm.NewOrm()
  135. fields := strings.Join(fieldArr, ",")
  136. if len(fieldArr) == 0 {
  137. fields = `*`
  138. }
  139. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  140. if orderRule != "" {
  141. order = ` ORDER BY ` + orderRule
  142. }
  143. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  144. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  145. return
  146. }