sci_data.go 6.8 KB

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