smm_data.go 6.5 KB

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