base_from_rzd_index.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Package data_manage
  2. // @Author gmy 2024/8/7 9:38:00
  3. package data_manage
  4. import (
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. )
  10. type BaseFromRzdIndex struct {
  11. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id);pk"`
  12. CreateTime string `orm:"column(create_time)"`
  13. ModifyTime string `orm:"column(modify_time)"`
  14. BaseFromRzdClassifyId int `orm:"column(base_from_rzd_classify_id)"`
  15. IndexCode string `orm:"column(index_code)"`
  16. IndexName string `orm:"column(index_name)"`
  17. Frequency string `orm:"column(frequency)"`
  18. Unit string `orm:"column(unit)"`
  19. }
  20. type BaseFromRzdIndexAndData struct {
  21. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id);pk"`
  22. CreateTime string `orm:"column(create_time)"`
  23. ModifyTime string `orm:"column(modify_time)"`
  24. BaseFromRzdClassifyId int `orm:"column(base_from_rzd_classify_id)"`
  25. IndexCode string `orm:"column(index_code)"`
  26. IndexName string `orm:"column(index_name)"`
  27. Frequency string `orm:"column(frequency)"`
  28. Unit string `orm:"column(unit)"`
  29. ModifyTimeMax string `json:"modify_time_max" description:"最后修改时间"`
  30. Value float64 `orm:"column(value)" description:"数据值"`
  31. }
  32. type BaseFromRzdIndexPage struct {
  33. List []*BaseFromRzdIndexAndData `description:"指标列表"`
  34. Paging *paging.PagingItem `description:"分页数据"`
  35. }
  36. type BaseFromRzdIndexList struct {
  37. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id);pk"`
  38. CreateTime string `orm:"column(create_time)"`
  39. ModifyTime string `orm:"column(modify_time)"`
  40. BaseFromRzdClassifyId int `orm:"column(base_from_rzd_classify_id)"`
  41. IndexCode string `orm:"column(index_code)"`
  42. IndexName string `orm:"column(index_name)"`
  43. Frequency string `orm:"column(frequency)"`
  44. Unit string `orm:"column(unit)"`
  45. DataList []*BaseFromRzdData
  46. Paging *paging.PagingItem `description:"分页数据"`
  47. EdbInfoId int `description:"指标库主键id"`
  48. }
  49. // RzdNameCheckResult 校验名称是否存在
  50. type RzdNameCheckResult struct {
  51. IndexCode string `from:"EdbCode" description:"edb编码"`
  52. IndexName string `from:"EdbName" description:"edb名称"`
  53. Exist bool
  54. }
  55. // RzdIndexCheckData 校验编码是否存在
  56. type RzdIndexCheckData struct {
  57. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  58. IndexName string `orm:"column(index_name)" description:"指标名称"`
  59. Frequency string `orm:"column(frequency)" description:"频度"`
  60. Unit string `orm:"column(unit)" description:"单位"`
  61. EdbInfoId int `json:"edb_info_id" description:"指标库主键id"`
  62. UniqueCode string `json:"unique_code" description:"指标库唯一编码"`
  63. ClassifyId int `json:"classify_id" description:"分类id"`
  64. }
  65. // BaseFromRzdIndexBatchAddCheckReq 校验编码是否存在请求参数
  66. type BaseFromRzdIndexBatchAddCheckReq struct {
  67. IndexCodes []string `form:"IndexCodes" description:"指标编码列表"`
  68. }
  69. func init() {
  70. orm.RegisterModel(new(BaseFromRzdIndex))
  71. }
  72. // GetRzdIndexByClassifyIds 根据分类id获取指标信息
  73. func GetRzdIndexByClassifyIds(classifyIds []int) (items []*BaseFromRzdIndex, err error) {
  74. o := orm.NewOrmUsingDB("data")
  75. sql := fmt.Sprintf(`SELECT * FROM base_from_rzd_index WHERE base_from_rzd_classify_id IN (`+utils.GetOrmInReplace(len(classifyIds))) + `)`
  76. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return items, nil
  81. }
  82. func GetRzdIndex(condition string, pars interface{}) (items []*BaseFromRzdIndexList, err error) {
  83. o := orm.NewOrmUsingDB("data")
  84. sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1 `
  85. if condition != "" {
  86. sql += condition
  87. }
  88. sql += ` ORDER BY base_from_rzd_index_id asc`
  89. _, err = o.Raw(sql, pars).QueryRows(&items)
  90. return
  91. }
  92. // GetRzdIndexFrequency 获取指标频度
  93. func GetRzdIndexFrequency(classifyIdList []int) (items []*string, err error) {
  94. sql := `SELECT DISTINCT frequency
  95. FROM base_from_rzd_index
  96. WHERE frequency is not null`
  97. // 如果 classifyId > 0,则添加该条件
  98. if len(classifyIdList) > 0 {
  99. sql += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  100. }
  101. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  102. o := orm.NewOrmUsingDB("data")
  103. if len(classifyIdList) > 0 {
  104. _, err = o.Raw(sql, classifyIdList).QueryRows(&items)
  105. } else {
  106. _, err = o.Raw(sql).QueryRows(&items)
  107. }
  108. return items, err
  109. }
  110. // GetRzdIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  111. func GetRzdIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromRzdIndex, err error) {
  112. o := orm.NewOrmUsingDB("data")
  113. // SQL 查询语句
  114. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  115. FROM base_from_rzd_index AS a
  116. INNER JOIN base_from_rzd_data AS b ON a.base_from_rzd_index_id = b.base_from_rzd_index_id
  117. WHERE 1=1`
  118. var params []interface{}
  119. if classifyId != 0 {
  120. sql += ` AND a.classify_id = ?`
  121. params = append(params, classifyId)
  122. }
  123. // 如果 indexCode 不为空,增加过滤条件
  124. if indexCode != "" {
  125. sql += ` AND a.index_code = ?`
  126. params = append(params, indexCode)
  127. }
  128. if frequency != nil {
  129. sql += ` AND a.frequency = ?`
  130. params = append(params, *frequency)
  131. }
  132. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  133. _, err = o.Raw(sql, params...).QueryRows(&items)
  134. if err != nil {
  135. return nil, err
  136. }
  137. return
  138. }
  139. // GetRzdIndexInfoCount 分页查询指标信息行数
  140. func GetRzdIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  141. o := orm.NewOrmUsingDB("data")
  142. sql := ` SELECT count(1) FROM base_from_rzd_index WHERE 1=1 `
  143. if condition != "" {
  144. sql += condition
  145. }
  146. err = o.Raw(sql, pars).QueryRow(&count)
  147. return
  148. }
  149. // GetRzdIndexInfoPage 分页查询指标信息
  150. func GetRzdIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  151. o := orm.NewOrmUsingDB("data")
  152. sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1 `
  153. if condition != "" {
  154. sql += condition
  155. }
  156. _, err = o.Raw(sql, pars).QueryRows(&items)
  157. return
  158. }