sci_data.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "gorm.io/gorm"
  7. )
  8. type SciClassify struct {
  9. TypeName string `orm:"column(type_name)" description:"分类名称"`
  10. TypeCode string `orm:"column(type_code)" description:"分类名称编码"`
  11. }
  12. func GetSciClassify() (items []*SciClassify, err error) {
  13. sql := `SELECT CONCAT(type_2,type_3) AS type_name,CONCAT(type_2,'#',type_3) type_code FROM base_from_sci_index GROUP BY CONCAT(type_2,type_3) ORDER BY CONCAT(type_2,type_3) ASC `
  14. o := global.DbMap[utils.DbNameIndex]
  15. err = o.Raw(sql).Find(&items).Error
  16. return
  17. }
  18. type SciFrequency struct {
  19. Frequency string `description:"频度"`
  20. }
  21. func GetSciFrequencyByClassifyId(classifyId int) (items []*GlFrequency, err error) {
  22. o := global.DbMap[utils.DbNameIndex]
  23. sql := ` SELECT frequency FROM base_from_sci_index WHERE classify_id = ? `
  24. sql += ` GROUP BY frequency ORDER BY frequency ASC `
  25. err = o.Raw(sql, classifyId).Find(&items).Error
  26. return
  27. }
  28. type SciIndex struct {
  29. BaseFromSciIndexId int `orm:"column(base_from_sci_index_id);pk" gorm:"primaryKey"`
  30. Interface string
  31. Name string
  32. IndexCode string
  33. IndexName string
  34. Type1 string `gorm:"column:type_1"`
  35. Type2 string `gorm:"column:type_2"`
  36. Type3 string `gorm:"column:type_3"`
  37. Frequency string
  38. Unit string
  39. ApiStartTime string
  40. ApiUpdateTime string
  41. StartTime string
  42. FinishTime string
  43. CreateTime string
  44. ModifyTime string
  45. }
  46. func (sciIndex *SciIndex) AfterFind(tx *gorm.DB) (err error) {
  47. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  48. if sciIndex.CreateTime != "" {
  49. sciIndex.CreateTime = utils.GormDateStrToDateTimeStr(sciIndex.CreateTime)
  50. }
  51. if sciIndex.ModifyTime != "" {
  52. sciIndex.ModifyTime = utils.GormDateStrToDateTimeStr(sciIndex.ModifyTime)
  53. }
  54. if sciIndex.ApiStartTime != "" {
  55. sciIndex.ApiStartTime = utils.GormDateStrToDateStr(sciIndex.ApiStartTime)
  56. }
  57. if sciIndex.ApiUpdateTime != "" {
  58. sciIndex.ApiUpdateTime = utils.GormDateStrToDateTimeStr(sciIndex.ApiUpdateTime)
  59. }
  60. if sciIndex.StartTime != "" {
  61. sciIndex.StartTime = utils.GormDateStrToDateTimeStr(sciIndex.StartTime)
  62. }
  63. if sciIndex.FinishTime != "" {
  64. sciIndex.FinishTime = utils.GormDateStrToDateTimeStr(sciIndex.FinishTime)
  65. }
  66. }
  67. return
  68. }
  69. func GetSciIndex(condition string, pars []interface{}) (items []*SciIndex, err error) {
  70. o := global.DbMap[utils.DbNameIndex]
  71. sql := ` SELECT * FROM base_from_sci_index WHERE 1=1 `
  72. if condition != "" {
  73. sql += condition
  74. }
  75. sql += `ORDER BY sort ASC, base_from_sci_index_id asc`
  76. err = o.Raw(sql, pars...).Find(&items).Error
  77. return
  78. }
  79. type SciExportIndex struct {
  80. TypeName string
  81. IndexCode string
  82. IndexName string
  83. Type1 string `gorm:"column:type_1"`
  84. Type2 string `gorm:"column:type_2"`
  85. Type3 string `gorm:"column:type_3"`
  86. Frequency string
  87. Unit string
  88. ModifyTime string
  89. }
  90. func GetExportSciIndex(typeCodes []string) (items []*SciExportIndex, err error) {
  91. if len(typeCodes) == 0 {
  92. return
  93. }
  94. o := global.DbMap[utils.DbNameIndex]
  95. sql := ` SELECT *,CONCAT(type_2, "#", type_3) AS type_name FROM base_from_sci_index WHERE CONCAT(type_2, "#", type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `) ORDER BY frequency ASC,index_code ASC`
  96. err = o.Raw(sql, typeCodes).Find(&items).Error
  97. return
  98. }
  99. func GetSciFrequency(classifyId int) (items []*string, err error) {
  100. sql := `SELECT DISTINCT frequency FROM base_from_sci_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  101. o := global.DbMap[utils.DbNameIndex]
  102. err = o.Raw(sql, classifyId).Find(&items).Error
  103. return
  104. }
  105. func GetSciFrequencyByCode(code string) (items []*string, err error) {
  106. sql := `SELECT DISTINCT frequency FROM base_from_sci_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  107. o := global.DbMap[utils.DbNameIndex]
  108. err = o.Raw(sql, code).Find(&items).Error
  109. return
  110. }
  111. type SciIndexList struct {
  112. BaseFromSciIndexId int `orm:"column(base_from_sci_index_id);pk" gorm:"primaryKey"`
  113. Interface string
  114. Name string
  115. IndexCode string
  116. IndexName string
  117. Type1 string `gorm:"column:type_1"`
  118. Type2 string `gorm:"column:type_2"`
  119. Type3 string `gorm:"column:type_3"`
  120. Frequency string
  121. Unit string
  122. ApiStartTime string
  123. ApiUpdateTime string
  124. StartTime string
  125. FinishTime string
  126. ModifyTime string
  127. DataList []*SciIndexData `gorm:"-"`
  128. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  129. }
  130. type SciIndexData struct {
  131. Value string `orm:"column(value)" description:"日期"`
  132. DataTime string `orm:"column(data_time)" description:"值"`
  133. }
  134. func (sciIndexData *SciIndexData) AfterFind(tx *gorm.DB) (err error) {
  135. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  136. if sciIndexData.DataTime != "" {
  137. sciIndexData.DataTime = utils.GormDateStrToDateStr(sciIndexData.DataTime)
  138. }
  139. }
  140. return
  141. }
  142. func GetSciIndexData(indexCode string, startSize, pageSize int) (items []*SciIndexData, err error) {
  143. o := global.DbMap[utils.DbNameIndex]
  144. sql := ` SELECT * FROM base_from_sci_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  145. err = o.Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  146. return
  147. }
  148. func GetSciIndexDataCount(indexCode string) (count int, err error) {
  149. o := global.DbMap[utils.DbNameIndex]
  150. sql := ` SELECT COUNT(1) AS count FROM base_from_sci_data WHERE index_code=? `
  151. err = o.Raw(sql, indexCode).Scan(&count).Error
  152. return
  153. }
  154. // GetSciItemList 模糊查询Sci数据库指标列表
  155. func GetSciItemList(keyword string) (items []*SciIndex, err error) {
  156. o := global.DbMap[utils.DbNameIndex]
  157. sql := "SELECT * FROM base_from_sci_index WHERE CONCAT(index_name,index_code) LIKE ? "
  158. err = o.Raw(sql, utils.GetLikeKeyword(keyword)).Find(&items).Error
  159. return
  160. }
  161. func GetSciIndexDataByCode(indexCode string) (items []*SciIndexData, err error) {
  162. o := global.DbMap[utils.DbNameIndex]
  163. sql := ` SELECT * FROM base_from_sci_data WHERE index_code=? ORDER BY data_time DESC `
  164. err = o.Raw(sql, indexCode).Find(&items).Error
  165. return
  166. }
  167. func GetSciDataMaxCount(classifyId int) (count int, err error) {
  168. o := global.DbMap[utils.DbNameIndex]
  169. sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM (
  170. SELECT COUNT(1) AS num FROM base_from_sci_index AS a
  171. INNER JOIN base_from_sci_data AS b ON a.index_code=b.index_code
  172. WHERE a.classify_id=?
  173. GROUP BY a.base_from_sci_index_id
  174. )AS t `
  175. err = o.Raw(sql, classifyId).Scan(&count).Error
  176. return
  177. }
  178. type ExportSciDataMaxCount struct {
  179. TypeName string
  180. Count int
  181. }
  182. func GetExportSciDataMaxCount(typeCodes []string) (items []*ExportSciDataMaxCount, err error) {
  183. if len(typeCodes) == 0 {
  184. return
  185. }
  186. o := global.DbMap[utils.DbNameIndex]
  187. sql := ` SELECT
  188. COALESCE(MAX(t.num), 0) AS count,
  189. t.type_name
  190. FROM
  191. (
  192. SELECT
  193. COUNT(1) AS num,
  194. CONCAT(a.type_2, "#", a.type_3) AS type_name
  195. FROM
  196. base_from_sci_index AS a
  197. INNER JOIN base_from_sci_data AS b ON a.index_code = b.index_code
  198. WHERE
  199. CONCAT(a.type_2, "#", a.type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `)
  200. GROUP BY
  201. a.base_from_sci_index_id
  202. ) AS t
  203. GROUP BY
  204. type_name `
  205. err = o.Raw(sql, typeCodes).Find(&items).Error
  206. return
  207. }
  208. type ExportSciIndexData struct {
  209. Value string `orm:"column(value)" description:"日期"`
  210. DataTime string `orm:"column(data_time)" description:"值"`
  211. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  212. }
  213. func GetExportSciIndexDataByCodes(indexCodes []string) (items []*ExportSciIndexData, err error) {
  214. if len(indexCodes) == 0 {
  215. return
  216. }
  217. o := global.DbMap[utils.DbNameIndex]
  218. sql := ` SELECT index_code,data_time,value FROM base_from_sci_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) ORDER BY data_time DESC `
  219. err = o.Raw(sql, indexCodes).Find(&items).Error
  220. return
  221. }