baiinfo_data.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package data_manage
  2. import (
  3. "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. )
  8. type BaiinfoClassify struct {
  9. TypeName string `orm:"column(type_name)" description:"分类名称"`
  10. TypeCode string `orm:"column(type_code)" description:"分类名称编码"`
  11. }
  12. func GetBaiinfoClassify() (items []*BaiinfoClassify, err error) {
  13. sql := `SELECT CONCAT(type_2,type_3) AS type_name,CONCAT(type_2,'#',type_3) type_code FROM base_from_baiinfo_index GROUP BY CONCAT(type_2,type_3) ORDER BY CONCAT(type_2,type_3) ASC `
  14. err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&items).Error
  15. return
  16. }
  17. type BaiinfoFrequency struct {
  18. Frequency string `description:"频度"`
  19. }
  20. func GetBaiinfoFrequencyByClassifyId(classifyId int) (items []*GlFrequency, err error) {
  21. sql := ` SELECT frequency FROM base_from_baiinfo_index WHERE classify_id = ? `
  22. sql += ` GROUP BY frequency ORDER BY frequency ASC `
  23. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).Find(&items).Error
  24. return
  25. }
  26. type BaiinfoIndex struct {
  27. BaseFromBaiinfoIndexId int `orm:"column(base_from_baiinfo_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 GetBaiinfoIndex(condition string, pars interface{}) (items []*BaiinfoIndex, err error) {
  45. sql := ` SELECT * FROM base_from_baiinfo_index WHERE 1=1 `
  46. if condition != "" {
  47. sql += condition
  48. }
  49. sql += `ORDER BY sort ASC, base_from_baiinfo_index_id asc`
  50. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars).Find(&items).Error
  51. return
  52. }
  53. type BaiinfoExportIndex struct {
  54. TypeName string
  55. IndexCode string
  56. IndexName string
  57. Type1 string `orm:"column(type_1)"`
  58. Type2 string `orm:"column(type_2)"`
  59. Type3 string `orm:"column(type_3)"`
  60. Frequency string
  61. Unit string
  62. ModifyTime string
  63. }
  64. func GetExportBaiinfoIndex(typeCodes []string) (items []*BaiinfoExportIndex, err error) {
  65. if len(typeCodes) == 0 {
  66. return
  67. }
  68. sql := ` SELECT *,CONCAT(type_2, "#", type_3) AS type_name FROM base_from_baiinfo_index WHERE CONCAT(type_2, "#", type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `) ORDER BY frequency ASC,index_code ASC`
  69. err = global.DbMap[utils.DbNameIndex].Raw(sql, typeCodes).Find(&items).Error
  70. return
  71. }
  72. func GetBaiinfoFrequency(classifyId int) (items []*string, err error) {
  73. sql := `SELECT DISTINCT frequency FROM base_from_baiinfo_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  74. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).Find(&items).Error
  75. return
  76. }
  77. func GetBaiinfoFrequencyByCode(code string) (items []*string, err error) {
  78. sql := `SELECT DISTINCT frequency FROM base_from_baiinfo_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  79. err = global.DbMap[utils.DbNameIndex].Raw(sql, code).Find(&items).Error
  80. return
  81. }
  82. type BaiinfoIndexList struct {
  83. BaseFromBaiinfoIndexId int `orm:"column(base_from_baiinfo_index_id);pk"`
  84. Interface string
  85. Name string
  86. IndexCode string
  87. IndexName string
  88. Type1 string `orm:"column(type_1)"`
  89. Type2 string `orm:"column(type_2)"`
  90. Type3 string `orm:"column(type_3)"`
  91. Frequency string
  92. Unit string
  93. ApiStartTime string
  94. ApiUpdateTime string
  95. StartTime string
  96. FinishTime string
  97. ModifyTime string
  98. DataList []*BaiinfoIndexData
  99. Paging *paging.PagingItem `description:"分页数据"`
  100. }
  101. type BaiinfoIndexData struct {
  102. Value string `orm:"column(value)" description:"日期"`
  103. DataTime string `orm:"column(data_time)" description:"值"`
  104. }
  105. func GetBaiinfoIndexData(indexCode string, startSize, pageSize int) (items []*BaiinfoIndexData, err error) {
  106. sql := ` SELECT * FROM base_from_baiinfo_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  107. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  108. return
  109. }
  110. func GetBaiinfoIndexDataCount(indexCode string) (count int, err error) {
  111. sqlStr := ` SELECT COUNT(1) AS count FROM base_from_baiinfo_data WHERE index_code=? `
  112. var totalNull sql.NullInt64
  113. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, indexCode).Scan(&totalNull).Error
  114. if !totalNull.Valid {
  115. count = 0
  116. } else {
  117. count = int(totalNull.Int64)
  118. }
  119. return
  120. }
  121. // GetBaiinfoItemList 模糊查询Baiinfo数据库指标列表
  122. func GetBaiinfoItemList(keyword string) (items []*BaiinfoIndex, err error) {
  123. sql := "SELECT * FROM base_from_baiinfo_index WHERE CONCAT(index_name,index_code) LIKE ? "
  124. err = global.DbMap[utils.DbNameIndex].Raw(sql, utils.GetLikeKeyword(keyword)).Find(&items).Error
  125. return
  126. }
  127. func GetBaiinfoIndexDataByCode(indexCode string) (items []*BaiinfoIndexData, err error) {
  128. sql := ` SELECT * FROM base_from_baiinfo_data WHERE index_code=? ORDER BY data_time DESC `
  129. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Find(&items).Error
  130. return
  131. }
  132. func GetBaiinfoDataMaxCount(classifyId int) (count int, err error) {
  133. sqlStr := `SELECT MAX(t.num) AS count FROM (
  134. SELECT COUNT(1) AS num FROM base_from_baiinfo_index AS a
  135. INNER JOIN base_from_baiinfo_data AS b ON a.index_code=b.index_code
  136. WHERE a.classify_id=?
  137. GROUP BY a.base_from_baiinfo_index_id
  138. )AS t `
  139. var totalNull sql.NullInt64
  140. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, classifyId).Scan(&count).Error
  141. if !totalNull.Valid {
  142. count = 0
  143. } else {
  144. count = int(totalNull.Int64)
  145. }
  146. return
  147. }
  148. type ExportBaiinfoDataMaxCount struct {
  149. TypeName string
  150. Count int
  151. }
  152. func GetExportBaiinfoDataMaxCount(typeCodes []string) (items []*ExportBaiinfoDataMaxCount, err error) {
  153. if len(typeCodes) == 0 {
  154. return
  155. }
  156. sql := ` SELECT
  157. MAX(t.num) AS count,
  158. t.type_name
  159. FROM
  160. (
  161. SELECT
  162. COUNT(1) AS num,
  163. CONCAT(a.type_2, "#", a.type_3) AS type_name
  164. FROM
  165. base_from_baiinfo_index AS a
  166. INNER JOIN base_from_baiinfo_data AS b ON a.index_code = b.index_code
  167. WHERE
  168. CONCAT(a.type_2, "#", a.type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `)
  169. GROUP BY
  170. a.base_from_baiinfo_index_id
  171. ) AS t
  172. GROUP BY
  173. type_name `
  174. err = global.DbMap[utils.DbNameIndex].Raw(sql, typeCodes).Find(&items).Error
  175. return
  176. }
  177. type ExportBaiinfoIndexData struct {
  178. Value string `orm:"column(value)" description:"日期"`
  179. DataTime string `orm:"column(data_time)" description:"值"`
  180. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  181. }
  182. func GetExportBaiinfoIndexDataByCodes(indexCodes []string) (items []*ExportBaiinfoIndexData, err error) {
  183. if len(indexCodes) == 0 {
  184. return
  185. }
  186. sql := ` SELECT index_code,data_time,value FROM base_from_baiinfo_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) ORDER BY data_time DESC `
  187. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCodes).Find(&items).Error
  188. return
  189. }