base_from_usda_fas.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package data_manage
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_task/global"
  5. "eta/eta_task/utils"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type BaseFromUsdaFasIndex struct {
  11. BaseFromUsdaFasIndexId int `gorm:"column:base_from_usda_fas_index_id;primaryKey;autoIncrement"`
  12. ClassifyId int
  13. IndexCode string
  14. IndexName string
  15. Frequency string
  16. Unit string
  17. Sort int
  18. StartDate string `description:"开始日期"`
  19. EndDate string `description:"结束日期"`
  20. EndValue float64
  21. Country string `description:"国家"`
  22. Commodity string `description:"属性"`
  23. CreateTime time.Time
  24. ModifyTime time.Time
  25. }
  26. func (m *BaseFromUsdaFasIndex) AfterFind(db *gorm.DB) (err error) {
  27. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  28. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  29. return
  30. }
  31. // BaseFromUsdaFasClassify UsdaFas原始数据分类表
  32. type BaseFromUsdaFasClassify struct {
  33. ClassifyId int `gorm:"column:classify_id;primaryKey;autoIncrement"`
  34. ClassifyName string `description:"分类名称"`
  35. ParentId int `description:"父级id"`
  36. SysUserId int `description:"创建人id"`
  37. SysUserRealName string `description:"创建人姓名"`
  38. Level int `description:"层级"`
  39. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  40. ModifyTime time.Time `description:"修改时间"`
  41. CreateTime time.Time `description:"创建时间"`
  42. }
  43. func GetBaseFromUsdaFasIndexMaxDate() (maxDate time.Time, err error) {
  44. o := global.DbMap[utils.DbNameIndex]
  45. sql := ` SELECT max(a.modify_time) as max_date FROM base_from_usda_fas_data as a `
  46. var timeNull sql2.NullTime
  47. err = o.Raw(sql).Scan(&timeNull).Error
  48. if err != nil {
  49. return
  50. }
  51. if timeNull.Valid {
  52. maxDate = timeNull.Time
  53. }
  54. return
  55. }
  56. type UsdaFasIndexResp struct {
  57. Ret int
  58. Msg string
  59. ErrMsg string
  60. ErrCode string
  61. Data []*BaseFromUsdaFasIndex
  62. }
  63. func GetBaseFromUsdaFasIndexAll(dateStr string) (list []*BaseFromUsdaFasIndex, err error) {
  64. o := global.DbMap[utils.DbNameIndex]
  65. sql := `SELECT * FROM base_from_usda_fas_index WHERE end_date>=?`
  66. err = o.Raw(sql, dateStr).Find(&list).Error
  67. return
  68. }
  69. func (obj *BaseFromUsdaFasIndex) AddBaseFromUsdaFasIndex(item *BaseFromUsdaFasIndex) (lastId int64, err error) {
  70. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  71. if err != nil {
  72. return
  73. }
  74. lastId = int64(item.BaseFromUsdaFasIndexId)
  75. return
  76. }
  77. func (obj *BaseFromUsdaFasIndex) InsertOrUpdateBaseFromUsdaFasIndex(item *BaseFromUsdaFasIndex) (lastId int64, err error) {
  78. saveItem := *item
  79. o := global.DbMap[utils.DbNameIndex]
  80. //lastId, err = o.InsertOrUpdate(item)
  81. result := o.FirstOrCreate(item, "base_from_usda_fas_index_id = ?", item.BaseFromUsdaFasIndexId)
  82. if result.Error != nil {
  83. err = result.Error
  84. return
  85. }
  86. if result.RowsAffected == 0 {
  87. saveItem.BaseFromUsdaFasIndexId = 0
  88. result = o.Model(item).Where("base_from_usda_fas_index_id = ?", item.BaseFromUsdaFasIndexId).Updates(saveItem)
  89. if result.Error != nil {
  90. err = result.Error
  91. return
  92. }
  93. }
  94. return int64(item.BaseFromUsdaFasIndexId), nil
  95. }
  96. type UsdaFasClassifyResp struct {
  97. Ret int
  98. Msg string
  99. ErrMsg string
  100. ErrCode string
  101. Data []*BaseFromUsdaFasClassify
  102. }
  103. func GetBaseFromUsdaFasClassifyAll() (list []*BaseFromUsdaFasClassify, err error) {
  104. o := global.DbMap[utils.DbNameIndex]
  105. sql := `SELECT * FROM base_from_usda_fas_classify `
  106. err = o.Raw(sql).Find(&list).Error
  107. return
  108. }
  109. func AddBaseFromUsdaFasClassify(item *BaseFromUsdaFasClassify) (lastId int64, err error) {
  110. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  111. if err != nil {
  112. return
  113. }
  114. lastId = int64(item.ClassifyId)
  115. return
  116. }
  117. func InsertOrUpdateBaseFromUsdaFasClassify(item *BaseFromUsdaFasClassify) (lastId int64, err error) {
  118. saveItem := *item
  119. o := global.DbMap[utils.DbNameIndex]
  120. //lastId, err = o.InsertOrUpdate(item)
  121. result := o.FirstOrCreate(item, "classify_id = ?", item.ClassifyId)
  122. if result.Error != nil {
  123. err = result.Error
  124. return
  125. }
  126. if result.RowsAffected == 0 {
  127. saveItem.ClassifyId = 0
  128. result = o.Model(item).Where("classify_id = ?", item.ClassifyId).Updates(saveItem)
  129. if result.Error != nil {
  130. err = result.Error
  131. return
  132. }
  133. }
  134. return int64(item.ClassifyId), nil
  135. }
  136. // GetBaseFromUsdaFasMaxDate 获取UsdaFas消费者指数最大数据
  137. func GetBaseFromUsdaFasMaxDate() (maxDate time.Time, err error) {
  138. o := global.DbMap[utils.DbNameIndex]
  139. sql := ` SELECT max(a.modify_time) as max_date FROM base_from_usda_fas_data as a `
  140. var timeNull sql2.NullTime
  141. err = o.Raw(sql).Scan(&timeNull).Error
  142. if err != nil {
  143. return
  144. }
  145. if timeNull.Valid {
  146. maxDate = timeNull.Time
  147. }
  148. return
  149. }
  150. type BaseFromUsdaFasData struct {
  151. BaseFromUsdaFasDataId int `gorm:"column:base_from_usda_fas_data_id;primaryKey;autoIncrement"`
  152. BaseFromUsdaFasIndexId int `description:"指标id"`
  153. IndexCode string `description:"指标编码"`
  154. DataTime string `description:"日期"`
  155. Value string `description:"值"`
  156. CreateTime time.Time `description:"创建时间"`
  157. ModifyTime time.Time `description:"修改时间"`
  158. }
  159. func (m *BaseFromUsdaFasData) AfterFind(db *gorm.DB) (err error) {
  160. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  161. return
  162. }
  163. // GetAllBaseFromUsdaFasDataList 获取UsdaFas消费者指数数据
  164. func GetAllBaseFromUsdaFasDataList(startDate string) (list []*BaseFromUsdaFasData, err error) {
  165. o := global.DbMap[utils.DbNameIndex]
  166. sql := `SELECT * FROM base_from_usda_fas_data WHERE modify_time>=? ORDER BY base_from_usda_fas_data_id ASC `
  167. err = o.Raw(sql, startDate).Find(&list).Error
  168. return
  169. }
  170. type BaseFromUsdaFasDataResp struct {
  171. Ret int
  172. Msg string
  173. ErrMsg string
  174. ErrCode string
  175. Data BaseFromUsdaFasDataIndexAndDataResp
  176. }
  177. // BaseFromUsdaFasDataIndexAndDataResp 分页列表响应体
  178. type BaseFromUsdaFasDataIndexAndDataResp struct {
  179. List []*BaseFromUsdaFasData
  180. Paging *paging.PagingItem `description:"分页数据"`
  181. }
  182. // MultiAddBaseFromUsdaFasDataIndex 批量添加数据
  183. func MultiAddBaseFromUsdaFasDataIndex(items []*BaseFromUsdaFasData) (lastId int64, err error) {
  184. num := len(items)
  185. if num <= 0 {
  186. return
  187. }
  188. o := global.DbMap[utils.DbNameIndex]
  189. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  190. return
  191. }