baiinfo_data.go 8.0 KB

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