base_from_eia_steo.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package data_manage
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_task/global"
  5. "eta/eta_task/utils"
  6. "time"
  7. )
  8. // BaseFromEiaSteoIndex EiaSteo指标
  9. type BaseFromEiaSteoIndex struct {
  10. BaseFromEiaSteoIndexId int `gorm:"column:base_from_eia_steo_index_id;primaryKey;autoIncrement"`
  11. BaseFromEiaSteoClassifyId int `description:"指标分类id"`
  12. IndexCode string `description:"指标编码"`
  13. IndexName string `description:"指标名称(中文名称)"`
  14. IndexNameOriginal string `description:"指标名称(原始名称)"`
  15. Frequency string `description:"频度"`
  16. Level int `description:"指标层级(原始层级)"`
  17. Unit string `description:"单位"`
  18. Super string `description:"我也不知道是个啥,反正先存起来,万一用到了呢"`
  19. Precision int `description:"精度,我也不知道会不会用到,反正先存起来,万一用到了呢"`
  20. LastHistorical string `description:"最近的历史记录,我也不知道会不会用到,反正先存起来,万一用到了呢"`
  21. Description string `description:"备注信息,我也不知道会不会用到,反正先存起来,万一用到了呢"`
  22. IsMappable int `description:"是否可映射,我也不知道会不会用到,反正先存起来,万一用到了呢"`
  23. StartDate string `description:"开始日期"`
  24. EndDate string `description:"结束日期"`
  25. ModifyTime string `description:"最新更新时间"`
  26. CreateTime string `description:"创建时间"`
  27. LatestValue float64 `description:"最新值"`
  28. }
  29. func AddBaseFromEiaSteoIndex(item *BaseFromEiaSteoIndex) (lastId int64, err error) {
  30. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  31. if err != nil {
  32. return
  33. }
  34. lastId = int64(item.BaseFromEiaSteoIndexId)
  35. return
  36. }
  37. func InsertOrUpdateBaseFromEiaSteoIndex(item *BaseFromEiaSteoIndex) (lastId int64, err error) {
  38. saveItem := *item
  39. o := global.DbMap[utils.DbNameIndex]
  40. result := o.FirstOrCreate(item, "base_from_eia_steo_index_id = ?", item.BaseFromEiaSteoIndexId)
  41. if result.Error != nil {
  42. err = result.Error
  43. return
  44. }
  45. if result.RowsAffected == 0 {
  46. saveItem.BaseFromEiaSteoIndexId = 0
  47. result = o.Model(item).Where("base_from_eia_steo_index_id = ?", item.BaseFromEiaSteoIndexId).Updates(saveItem)
  48. if result.Error != nil {
  49. err = result.Error
  50. return
  51. }
  52. }
  53. return int64(item.BaseFromEiaSteoIndexId), nil
  54. }
  55. // Add 新增指标
  56. func (item *BaseFromEiaSteoIndex) Add() (err error) {
  57. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  58. return
  59. }
  60. func GetBaseFromEiaSteoIndexAll(dateStr string) (list []*BaseFromEiaSteoIndex, err error) {
  61. o := global.DbMap[utils.DbNameIndex]
  62. sql := `SELECT * FROM base_from_eia_steo_index WHERE create_time>=?`
  63. err = o.Raw(sql, dateStr).Find(&list).Error
  64. return
  65. }
  66. func GetBaseFromEiaSteoIndexCodeListByDate(dateStr string) (list []string, err error) {
  67. o := global.DbMap[utils.DbNameIndex]
  68. sql := `SELECT index_code FROM base_from_eia_steo_index WHERE modify_time>=?`
  69. err = o.Raw(sql, dateStr).Find(&list).Error
  70. return
  71. }
  72. // Update 更新作者基础信息
  73. func (item *BaseFromEiaSteoIndex) Update(cols []string) (err error) {
  74. err = global.DbMap[utils.DbNameIndex].Select(cols).Updates(item).Error
  75. return
  76. }
  77. // BaseFromEiaSteoClassify EiaSteo分类
  78. type BaseFromEiaSteoClassify struct {
  79. BaseFromEiaSteoClassifyId int `gorm:"column:base_from_eia_steo_classify_id;primaryKey;autoIncrement"`
  80. ClassifyName string `description:"分类名称(中文名称)"`
  81. ClassifyNameOriginal string `description:"分类名称(原始名称)"`
  82. ParentId int `description:"父级id"`
  83. Level int `description:"层级"`
  84. ModifyTime time.Time `description:"最新更新时间"`
  85. CreateTime time.Time `description:"创建时间"`
  86. }
  87. func (item *BaseFromEiaSteoClassify) AddBaseFromEiaSteoClassify() (err error) {
  88. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  89. return
  90. }
  91. // BaseFromEiaSteoData EiaSteo数据表
  92. type BaseFromEiaSteoData struct {
  93. BaseFromEiaSteoDataId int `gorm:"column:base_from_eia_steo_data_id;primaryKey;autoIncrement"`
  94. BaseFromEiaSteoIndexId int `description:"指标id"`
  95. IndexCode string `description:"指标编码"`
  96. DataTime time.Time `description:"数据日期"`
  97. Value float64 `description:"数据值"`
  98. ModifyTime time.Time `description:"最新更新时间"`
  99. CreateTime time.Time `description:"创建时间"`
  100. }
  101. func (b *BaseFromEiaSteoData) Update(cols []string) (err error) {
  102. err = global.DbMap[utils.DbNameIndex].Select(cols).Updates(b).Error
  103. return
  104. }
  105. func AddBaseFromEiaSteoData(item *BaseFromEiaSteoData) (lastId int64, err error) {
  106. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  107. if err != nil {
  108. return
  109. }
  110. lastId = int64(item.BaseFromEiaSteoDataId)
  111. return
  112. }
  113. // GetBaseFromEiaSteoClassifyAll 获取所有的分类
  114. func GetBaseFromEiaSteoClassifyAll() (list []*BaseFromEiaSteoClassify, err error) {
  115. o := global.DbMap[utils.DbNameIndex]
  116. sql := `SELECT * FROM base_from_eia_steo_classify`
  117. err = o.Raw(sql).Find(&list).Error
  118. return
  119. }
  120. func AddBaseEiaSteoClassify(item *BaseFromEiaSteoClassify) (lastId int64, err error) {
  121. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  122. if err != nil {
  123. return
  124. }
  125. lastId = int64(item.BaseFromEiaSteoClassifyId)
  126. return
  127. }
  128. func (item *BaseFromEiaSteoClassify) Update(cols []string) (err error) {
  129. err = global.DbMap[utils.DbNameIndex].Select(cols).Updates(item).Error
  130. return
  131. }
  132. func BatchDeleteBaseFromEiaSteoClassify(id []int) (err error) {
  133. if len(id) == 0 {
  134. return
  135. }
  136. o := global.DbMap[utils.DbNameIndex]
  137. sql := `DELETE FROM base_from_eia_steo_classify WHERE base_from_eia_steo_classify_id IN (` + utils.GetOrmInReplace(len(id)) + `)`
  138. err = o.Exec(sql, id).Error
  139. return
  140. }
  141. type EiaSteoIndexResp struct {
  142. Ret int
  143. Msg string
  144. ErrMsg string
  145. ErrCode string
  146. Data []*BaseFromEiaSteoIndex
  147. }
  148. type EiaSteoIndexDataResp struct {
  149. Ret int
  150. Msg string
  151. ErrMsg string
  152. ErrCode string
  153. Data []*BaseFromEiaSteoData
  154. }
  155. type EiaSteoClassifyDataResp struct {
  156. Ret int
  157. Msg string
  158. ErrMsg string
  159. ErrCode string
  160. Data []*BaseFromEiaSteoClassify
  161. }
  162. func GetBaseFromEiaSteoIndexDataAll(dateStr string) (list []*BaseFromEiaSteoData, err error) {
  163. o := global.DbMap[utils.DbNameIndex]
  164. sql := `SELECT * FROM base_from_eia_steo_data WHERE create_time>=?`
  165. err = o.Raw(sql, dateStr).Find(&list).Error
  166. return
  167. }
  168. func GetBaseFromEiaSteoIndexDataByIndexCode(indexCode, dateStr string) (list []*BaseFromEiaSteoData, err error) {
  169. o := global.DbMap[utils.DbNameIndex]
  170. sql := `SELECT * FROM base_from_eia_steo_data WHERE index_code=? AND modify_time>=?`
  171. err = o.Raw(sql, indexCode, dateStr).Find(&list).Error
  172. return
  173. }
  174. func GetBaseFromEiaSteoIndexDataByDate(dateStr string) (list []*BaseFromEiaSteoData, err error) {
  175. o := global.DbMap[utils.DbNameIndex]
  176. sql := `SELECT * FROM base_from_eia_steo_data WHERE modify_time>=?`
  177. err = o.Raw(sql, dateStr).Find(&list).Error
  178. return
  179. }
  180. func GetBaseFromEiaSteoIndexMaxDate() (maxDate time.Time, err error) {
  181. o := global.DbMap[utils.DbNameIndex]
  182. sql := ` SELECT MAX(a.data_time) AS max_date FROM base_from_eia_steo_data AS a `
  183. var timeNull sql2.NullTime
  184. err = o.Raw(sql).Scan(&timeNull).Error
  185. if err != nil {
  186. return
  187. }
  188. if timeNull.Valid {
  189. maxDate = timeNull.Time
  190. }
  191. return
  192. }
  193. func GetBaseFromEiaSteoIndexMaxCreateDate() (maxDate time.Time, err error) {
  194. o := global.DbMap[utils.DbNameIndex]
  195. sql := ` SELECT MAX(a.create_time) AS max_date FROM base_from_eia_steo_index AS a `
  196. var timeNull sql2.NullTime
  197. err = o.Raw(sql).Scan(&timeNull).Error
  198. if err != nil {
  199. return
  200. }
  201. if timeNull.Valid {
  202. maxDate = timeNull.Time
  203. }
  204. return
  205. }