base_from_smm.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package data_manage
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_task/global"
  5. "eta/eta_task/utils"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. type BaseFromSmm struct {
  10. BaseFromSmmId int `gorm:"column:base_from_smm_id;primaryKey;autoIncrement"`
  11. Aid int
  12. Name string
  13. Interface string
  14. ApiUpdate int
  15. ApiUpdateType string
  16. ApiType int
  17. ApiTypeAll string
  18. Type1 string `gorm:"column:type_1"`
  19. Type2 string `gorm:"column:type_2"`
  20. Type3 string `gorm:"column:type_3"`
  21. ApiStartTime string
  22. ApiUpdateTime string
  23. StartTime string
  24. FinishTime string
  25. AuthModule string
  26. AuthLang string
  27. ApiLabel string
  28. Enable string
  29. EditPerson string
  30. EditTime string
  31. AuthDur int
  32. AuthDurType string
  33. StartDate string
  34. EndDate string
  35. }
  36. func (m *BaseFromSmm) AfterFind(db *gorm.DB) (err error) {
  37. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  38. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  39. m.ApiStartTime = utils.GormDateStrToDateStr(m.ApiStartTime)
  40. m.ApiUpdateTime = utils.GormDateStrToDateStr(m.ApiUpdateTime)
  41. return
  42. }
  43. func AddBaseFromSmm(item *BaseFromSmm) (lastId int64, err error) {
  44. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  45. if err != nil {
  46. return
  47. }
  48. lastId = int64(item.BaseFromSmmId)
  49. return
  50. }
  51. func GetBaseFromSmmList() (list []*BaseFromSmm, err error) {
  52. o := global.DbMap[utils.DbNameIndex]
  53. //sql := `SELECT * FROM base_from_smm WHERE interface='cu_annual_copper_cathode_demand_supply_balance_in_china' `
  54. sql := `SELECT * FROM base_from_smm WHERE end_date IS NOT NULL `
  55. err = o.Raw(sql).Find(&list).Error
  56. return
  57. }
  58. type BaseFromSmmIndex struct {
  59. BaseFromSmmIndexId int `gorm:"column:base_from_smm_index_id;primaryKey;autoIncrement"`
  60. Interface string
  61. Name string
  62. IndexCode string
  63. IndexName string
  64. Type1 string `gorm:"column:type_1"`
  65. Type2 string `gorm:"column:type_2"`
  66. Type3 string `gorm:"column:type_3"`
  67. Frequency string
  68. Unit string
  69. ApiStartTime string
  70. ApiUpdateTime string
  71. StartTime string
  72. FinishTime string
  73. CreateTime time.Time
  74. ModifyTime time.Time
  75. }
  76. func (m *BaseFromSmmIndex) AfterFind(db *gorm.DB) (err error) {
  77. m.StartTime = utils.GormDateStrToDateStr(m.StartTime)
  78. m.FinishTime = utils.GormDateStrToDateStr(m.FinishTime)
  79. m.ApiStartTime = utils.GormDateStrToDateStr(m.ApiStartTime)
  80. m.ApiUpdateTime = utils.GormDateStrToDateStr(m.ApiUpdateTime)
  81. return
  82. }
  83. func AddBaseFromSmmIndex(item *BaseFromSmmIndex) (lastId int64, err error) {
  84. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  85. return
  86. }
  87. func GetBaseFromSmmIndex() (list []*BaseFromSmmIndex, err error) {
  88. o := global.DbMap[utils.DbNameIndex]
  89. sql := `SELECT * FROM base_from_smm_index `
  90. err = o.Raw(sql).Find(&list).Error
  91. return
  92. }
  93. type BaseFromSmmData struct {
  94. SmmDataId int `gorm:"column:smm_data_id;primaryKey;autoIncrement"`
  95. BaseFromSmmIndexId int
  96. IndexCode string
  97. DataTime string
  98. Value string
  99. CreateTime time.Time
  100. ModifyTime time.Time
  101. DataTimestamp int64
  102. }
  103. func (m *BaseFromSmmData) AfterFind(db *gorm.DB) (err error) {
  104. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  105. return
  106. }
  107. func AddBaseFromSmmData(item *BaseFromSmmData) (lastId int64, err error) {
  108. err = global.DbMap[utils.DbNameIndex].Create(item).Error
  109. return
  110. }
  111. func GetBaseFromSmmDataAll(indexCode string) (list []*BaseFromSmmData, err error) {
  112. o := global.DbMap[utils.DbNameIndex]
  113. sql := `SELECT * FROM base_from_smm_data WHERE index_code=?`
  114. err = o.Raw(sql, indexCode).Find(&list).Error
  115. return
  116. }
  117. func ModifyBaseFromSmmData(smmDataId int, value string) (err error) {
  118. o := global.DbMap[utils.DbNameIndex]
  119. sql := `UPDATE base_from_smm_data SET value=?,modify_time=NOW() WHERE smm_data_id=? `
  120. err = o.Exec(sql, value, smmDataId).Error
  121. return
  122. }
  123. func GetBaseFromSmmIndexByCode(smmCode string) (list []*BaseFromSmmIndex, err error) {
  124. o := global.DbMap[utils.DbNameIndex]
  125. sql := ` SELECT * FROM base_from_smm_index WHERE interface=? `
  126. err = o.Raw(sql, smmCode).Find(&list).Error
  127. return
  128. }
  129. func GetBaseFromSmmMaxOrMinDate(indexCode string) (minDate, maxDate string, err error) {
  130. o := global.DbMap[utils.DbNameIndex]
  131. sql := ` SELECT MIN(data_time) AS min_date, MAX(data_time) AS max_date FROM base_from_smm_data WHERE index_code=? `
  132. var minNull, maxNull sql2.NullString
  133. err = o.Raw(sql, indexCode).Row().Scan(&minNull, &maxNull)
  134. if err != nil {
  135. return
  136. }
  137. if minNull.Valid {
  138. minDate = minNull.String
  139. }
  140. if maxNull.Valid {
  141. maxDate = maxNull.String
  142. }
  143. return
  144. }
  145. func ModifyBaseFromSmmSmmType(baseFromSmmId int, smmType string) (err error) {
  146. o := global.DbMap[utils.DbNameIndex]
  147. sql := ` UPDATE base_from_smm SET smm_type=? WHERE base_from_smm_id=? `
  148. err = o.Exec(sql, smmType, baseFromSmmId).Error
  149. return
  150. }
  151. func ModifyBaseFromSmmMinDateAndMaxDate(baseFromSmmIndexId, baseFromSmmId int, minDate, maxDate string) (err error) {
  152. o := global.DbMap[utils.DbNameIndex]
  153. sql := ` UPDATE base_from_smm_index SET start_date=?,end_date=?,modify_time=NOW() WHERE base_from_smm_index_id=? `
  154. err = o.Exec(sql, minDate, maxDate, baseFromSmmIndexId).Error
  155. sql = ` UPDATE base_from_smm SET start_date=?,end_date=?,modify_time=NOW() WHERE base_from_smm_id=? `
  156. err = o.Exec(sql, minDate, maxDate, baseFromSmmId).Error
  157. return
  158. }
  159. type BaseFromSmmDataSimple struct {
  160. SmmDataId int `gorm:"column:smm_data_id;primaryKey;autoIncrement"`
  161. BaseFromSmmIndexId int
  162. IndexCode string
  163. DataTime string
  164. Value float64
  165. }
  166. func (m *BaseFromSmmDataSimple) AfterFind(db *gorm.DB) (err error) {
  167. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  168. return
  169. }
  170. func GetBaseFromSmmDataByCondition(condition string, pars []interface{}) (list []*BaseFromSmmDataSimple, err error) {
  171. o := global.DbMap[utils.DbNameIndex]
  172. sql := `SELECT * FROM base_from_smm_data WHERE 1=1 `
  173. if condition != "" {
  174. sql += condition
  175. }
  176. err = o.Raw(sql, pars...).Find(&list).Error
  177. return
  178. }