base_from_radish_research_data.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. // BaseFromRadishResearchData 萝卜投研-指标数据
  10. type BaseFromRadishResearchData struct {
  11. BaseFromRadishResearchDataId int `orm:"column(base_from_radish_research_data_id);pk" gorm:"primaryKey"`
  12. BaseFromRadishResearchIndexId int `description:"指标ID"`
  13. IndexCode string `description:"指标编码"`
  14. DataTime time.Time `description:"数据日期"`
  15. Value float64 `description:"数据值"`
  16. CreateTime time.Time `description:"创建时间"`
  17. ModifyTime time.Time `description:"修改时间"`
  18. DataTimestamp int64 `description:"数据日期时间戳"`
  19. }
  20. func (m *BaseFromRadishResearchData) TableName() string {
  21. return "base_from_radish_research_data"
  22. }
  23. type BaseFromRadishResearchDataCols struct {
  24. PrimaryId string
  25. BaseFromRadishResearchIndexId string
  26. IndexCode string
  27. DataTime string
  28. Value string
  29. UniqueCode string
  30. CreateTime string
  31. ModifyTime string
  32. DataTimestamp string
  33. }
  34. func (m *BaseFromRadishResearchData) Cols() BaseFromRadishResearchDataCols {
  35. return BaseFromRadishResearchDataCols{
  36. PrimaryId: "base_from_radish_research_data_id",
  37. BaseFromRadishResearchIndexId: "base_from_radish_research_index_id",
  38. IndexCode: "index_code",
  39. DataTime: "data_time",
  40. Value: "value",
  41. UniqueCode: "unique_code",
  42. CreateTime: "create_time",
  43. ModifyTime: "modify_time",
  44. DataTimestamp: "data_timestamp",
  45. }
  46. }
  47. func (m *BaseFromRadishResearchData) Create() (err error) {
  48. o := global.DbMap[utils.DbNameIndex]
  49. err = o.Create(m).Error
  50. return
  51. }
  52. func (m *BaseFromRadishResearchData) CreateMulti(items []*BaseFromRadishResearchData) (err error) {
  53. if len(items) == 0 {
  54. return
  55. }
  56. o := global.DbMap[utils.DbNameIndex]
  57. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  58. return
  59. }
  60. func (m *BaseFromRadishResearchData) Update(cols []string) (err error) {
  61. o := global.DbMap[utils.DbNameIndex]
  62. err = o.Select(cols).Updates(m).Error
  63. return
  64. }
  65. func (m *BaseFromRadishResearchData) Remove() (err error) {
  66. o := global.DbMap[utils.DbNameIndex]
  67. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  68. err = o.Exec(sql, m.BaseFromRadishResearchDataId).Error
  69. return
  70. }
  71. func (m *BaseFromRadishResearchData) MultiRemove(ids []int) (err error) {
  72. if len(ids) == 0 {
  73. return
  74. }
  75. o := global.DbMap[utils.DbNameIndex]
  76. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  77. err = o.Exec(sql, ids).Error
  78. return
  79. }
  80. func (m *BaseFromRadishResearchData) RemoveByCondition(condition string, pars []interface{}) (err error) {
  81. if condition == "" {
  82. return
  83. }
  84. o := global.DbMap[utils.DbNameIndex]
  85. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  86. err = o.Exec(sql, pars...).Error
  87. return
  88. }
  89. func (m *BaseFromRadishResearchData) GetItemById(id int) (item *BaseFromRadishResearchData, err error) {
  90. o := global.DbMap[utils.DbNameIndex]
  91. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  92. err = o.Raw(sql, id).First(&item).Error
  93. return
  94. }
  95. func (m *BaseFromRadishResearchData) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromRadishResearchData, err error) {
  96. o := global.DbMap[utils.DbNameIndex]
  97. order := ``
  98. if orderRule != "" {
  99. order = ` ORDER BY ` + orderRule
  100. }
  101. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  102. err = o.Raw(sql, pars...).First(&item).Error
  103. return
  104. }
  105. func (m *BaseFromRadishResearchData) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  106. o := global.DbMap[utils.DbNameIndex]
  107. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  108. err = o.Raw(sql, pars...).Scan(&count).Error
  109. return
  110. }
  111. func (m *BaseFromRadishResearchData) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromRadishResearchData, err error) {
  112. o := global.DbMap[utils.DbNameIndex]
  113. fields := strings.Join(fieldArr, ",")
  114. if len(fieldArr) == 0 {
  115. fields = `*`
  116. }
  117. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  118. if orderRule != "" {
  119. order = ` ORDER BY ` + orderRule
  120. }
  121. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  122. err = o.Raw(sql, pars...).Find(&items).Error
  123. return
  124. }
  125. func (m *BaseFromRadishResearchData) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromRadishResearchData, err error) {
  126. o := global.DbMap[utils.DbNameIndex]
  127. fields := strings.Join(fieldArr, ",")
  128. if len(fieldArr) == 0 {
  129. fields = `*`
  130. }
  131. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  132. if orderRule != "" {
  133. order = ` ORDER BY ` + orderRule
  134. }
  135. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  136. pars = append(pars, startSize, pageSize)
  137. err = o.Raw(sql, pars...).Find(&items).Error
  138. return
  139. }
  140. // BaseFromRadishResearchDataItem 萝卜投研数据信息
  141. type BaseFromRadishResearchDataItem struct {
  142. DataId int `description:"数据ID"`
  143. IndexId int `description:"指标ID"`
  144. IndexCode string `description:"指标编码"`
  145. DataTime string `description:"数据日期"`
  146. Value float64 `description:"数据值"`
  147. DataTimestamp int64 `description:"数据日期时间戳"`
  148. }
  149. func (m *BaseFromRadishResearchData) Format2Item() (item *BaseFromRadishResearchDataItem) {
  150. item = new(BaseFromRadishResearchDataItem)
  151. item.DataId = m.BaseFromRadishResearchDataId
  152. item.IndexId = m.BaseFromRadishResearchIndexId
  153. item.IndexCode = m.IndexCode
  154. item.DataTime = utils.TimeTransferString(utils.FormatDate, m.DataTime)
  155. item.Value = m.Value
  156. item.DataTimestamp = m.DataTimestamp
  157. return
  158. }
  159. func (m *BaseFromRadishResearchData) GetDataTimeByIndexIds(indexIds []int) (items []string, err error) {
  160. if len(indexIds) == 0 {
  161. return
  162. }
  163. o := global.DbMap[utils.DbNameIndex]
  164. sql := fmt.Sprintf(`SELECT DISTINCT %s FROM %s WHERE %s IN ? ORDER BY %s DESC`, m.Cols().DataTime, m.TableName(), m.Cols().BaseFromRadishResearchIndexId, m.Cols().DataTime)
  165. err = o.Raw(sql, indexIds).Find(&items).Error
  166. if err != nil {
  167. return
  168. }
  169. for i, v := range items {
  170. items[i] = utils.GormDateStrToDateStr(v)
  171. }
  172. return
  173. }