base_from_edb_mapping.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. // BaseFromEdbMapping 同花顺高频数据
  10. type BaseFromEdbMapping struct {
  11. Id int `orm:"column(id);pk" gorm:"primaryKey" `
  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. err = global.DmSQL["data"].Create(m).Error
  53. if err != nil {
  54. return
  55. }
  56. return
  57. }
  58. func (m *BaseFromEdbMapping) CreateMulti(items []*BaseFromEdbMapping) (err error) {
  59. if len(items) == 0 {
  60. return
  61. }
  62. err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
  63. return
  64. }
  65. func (m *BaseFromEdbMapping) Update(cols []string) (err error) {
  66. err = global.DmSQL["data"].Select(cols).Updates(m).Error
  67. return
  68. }
  69. func (m *BaseFromEdbMapping) Remove() (err error) {
  70. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  71. err = global.DmSQL["data"].Exec(sql, m.Id).Error
  72. return
  73. }
  74. func (m *BaseFromEdbMapping) MultiRemove(ids []int) (err error) {
  75. if len(ids) == 0 {
  76. return
  77. }
  78. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  79. err = global.DmSQL["data"].Exec(sql, ids).Error
  80. return
  81. }
  82. func (m *BaseFromEdbMapping) RemoveByCondition(condition string, pars []interface{}) (err error) {
  83. if condition == "" {
  84. return
  85. }
  86. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  87. err = global.DmSQL["data"].Exec(sql, pars...).Error
  88. return
  89. }
  90. func (m *BaseFromEdbMapping) GetItemById(id int) (item *BaseFromEdbMapping, err error) {
  91. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  92. err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
  93. return
  94. }
  95. func (m *BaseFromEdbMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromEdbMapping, err error) {
  96. order := ``
  97. if orderRule != "" {
  98. order = ` ORDER BY ` + orderRule
  99. }
  100. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  101. err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
  102. return
  103. }
  104. func (m *BaseFromEdbMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  105. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  106. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  107. return
  108. }
  109. func (m *BaseFromEdbMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromEdbMapping, err error) {
  110. fields := strings.Join(fieldArr, ",")
  111. if len(fieldArr) == 0 {
  112. fields = `*`
  113. }
  114. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  115. if orderRule != "" {
  116. order = ` ORDER BY ` + orderRule
  117. }
  118. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  119. err = global.DmSQL["data"].Raw(sql, pars...).First(&items).Error
  120. return
  121. }
  122. func (m *BaseFromEdbMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromEdbMapping, err error) {
  123. fields := strings.Join(fieldArr, ",")
  124. if len(fieldArr) == 0 {
  125. fields = `*`
  126. }
  127. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  128. if orderRule != "" {
  129. order = ` ORDER BY ` + orderRule
  130. }
  131. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  132. pars = append(pars, startSize)
  133. pars = append(pars, pageSize)
  134. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error
  135. return
  136. }