base_from_rzd_index.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 BaseFromRzdIndexList 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_ly_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. DataList []*BaseFromRzdData
  30. Paging *paging.PagingItem `description:"分页数据"`
  31. EdbInfoId int `description:"指标库主键id"`
  32. }
  33. // RzdNameCheckResult 校验名称是否存在
  34. type RzdNameCheckResult struct {
  35. IndexCode string `from:"EdbCode" description:"edb编码"`
  36. IndexName string `from:"EdbName" description:"edb名称"`
  37. Exist bool
  38. }
  39. // RzdIndexCheckData 校验编码是否存在
  40. type RzdIndexCheckData struct {
  41. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  42. IndexName string `orm:"column(index_name)" description:"指标名称"`
  43. Frequency string `orm:"column(frequency)" description:"频度"`
  44. Unit string `orm:"column(unit)" description:"单位"`
  45. EdbInfoId int `json:"edb_info_id" description:"指标库主键id"`
  46. UniqueCode string `json:"unique_code" description:"指标库唯一编码"`
  47. ClassifyId int `json:"classify_id" description:"分类id"`
  48. }
  49. // BaseFromRzdIndexBatchAddCheckReq 校验编码是否存在请求参数
  50. type BaseFromRzdIndexBatchAddCheckReq struct {
  51. IndexCodes []string `form:"IndexCodes" description:"指标编码列表"`
  52. }
  53. func init() {
  54. orm.RegisterModel(new(BaseFromRzdIndex))
  55. }
  56. // GetRzdIndexByClassifyIds 根据分类id获取指标信息
  57. func GetRzdIndexByClassifyIds(classifyIds []int) (items []*BaseFromRzdIndex, err error) {
  58. o := orm.NewOrmUsingDB("data")
  59. sql := fmt.Sprintf(`SELECT * FROM base_from_rzd_index WHERE base_from_rzd_classify_id IN (`+utils.GetOrmInReplace(len(classifyIds))) + `)`
  60. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return items, nil
  65. }
  66. func GetRzdIndex(condition string, pars interface{}) (items []*BaseFromRzdIndexList, err error) {
  67. o := orm.NewOrmUsingDB("data")
  68. sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1 `
  69. if condition != "" {
  70. sql += condition
  71. }
  72. sql += ` ORDER BY base_from_rzd_index_id asc`
  73. _, err = o.Raw(sql, pars).QueryRows(&items)
  74. return
  75. }
  76. // GetRzdIndexFrequency 获取指标频度
  77. func GetRzdIndexFrequency(classifyId int) (items []*string, err error) {
  78. sql := `SELECT DISTINCT frequency
  79. FROM base_from_rzd_index
  80. WHERE frequency is not null`
  81. // 如果 classifyId > 0,则添加该条件
  82. if classifyId > 0 {
  83. sql += ` AND base_from_rzd_classify_id = ?`
  84. }
  85. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  86. o := orm.NewOrmUsingDB("data")
  87. if classifyId > 0 {
  88. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  89. } else {
  90. _, err = o.Raw(sql).QueryRows(&items)
  91. }
  92. return items, err
  93. }
  94. // GetRzdIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  95. func GetRzdIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromRzdIndex, err error) {
  96. o := orm.NewOrmUsingDB("data")
  97. // SQL 查询语句
  98. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  99. FROM base_from_rzd_index AS a
  100. INNER JOIN base_from_rzd_data AS b ON a.base_from_rzd_index_id = b.base_from_rzd_index_id
  101. WHERE 1=1`
  102. var params []interface{}
  103. if classifyId != 0 {
  104. sql += ` AND a.classify_id = ?`
  105. params = append(params, classifyId)
  106. }
  107. // 如果 indexCode 不为空,增加过滤条件
  108. if indexCode != "" {
  109. sql += ` AND a.index_code = ?`
  110. params = append(params, indexCode)
  111. }
  112. if frequency != nil {
  113. sql += ` AND a.frequency = ?`
  114. params = append(params, *frequency)
  115. }
  116. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  117. _, err = o.Raw(sql, params...).QueryRows(&items)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return
  122. }
  123. // GetRzdIndexInfoPage 分页查询指标信息
  124. func GetRzdIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndex, err error) {
  125. o := orm.NewOrmUsingDB("data")
  126. sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1 `
  127. if condition != "" {
  128. sql += condition
  129. }
  130. sql += ` ORDER BY base_from_rzd_index_id asc`
  131. _, err = o.Raw(sql, pars).QueryRows(&items)
  132. return
  133. }