base_from_ly_index.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Package data_manage
  2. // @Author gmy 2024/8/7 9:38:00
  3. package data_manage
  4. import (
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. )
  8. type BaseFromLyIndex struct {
  9. BaseFromLyIndexId int `orm:"column(base_from_ly_index_id);pk" description:"指标ID"`
  10. CreateTime string `orm:"column(create_time)" description:"创建时间"`
  11. ModifyTime string `orm:"column(modify_time)" description:"修改时间"`
  12. BaseFromLyClassifyId int `orm:"column(base_from_ly_classify_id)" description:"原始数据指标分类id"`
  13. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  14. IndexName string `orm:"column(index_name)" description:"指标名称"`
  15. Frequency string `orm:"column(frequency)" description:"频度"`
  16. Unit string `orm:"column(unit)" description:"单位"`
  17. EdbExist int `orm:"column(edb_exist)" description:"指标库是否已添加:0-否;1-是"`
  18. }
  19. // 在 init 函数中注册模型
  20. func init() {
  21. orm.RegisterModel(new(BaseFromLyIndex))
  22. }
  23. type BaseFromLyIndexPage struct {
  24. List []*BaseFromLyIndex `description:"指标列表"`
  25. Paging *paging.PagingItem `description:"分页数据"`
  26. }
  27. type BaseFromLyIndexBatchAddCheckReq struct {
  28. IndexCodes []string `form:"IndexCodes" description:"指标编码列表"`
  29. }
  30. type BaseFromLyIndexNameCheck struct {
  31. IndexCode string `from:"IndexCode" description:"指标编码"`
  32. IndexName string `from:"IndexName" description:"指标名称"`
  33. }
  34. type NameCheckResult struct {
  35. IndexCode string `from:"EdbCode" description:"edb编码"`
  36. IndexName string `from:"EdbName" description:"edb名称"`
  37. Exist bool
  38. }
  39. // GetLyIndexByClassifyIds 通过分类ids查询指标列表
  40. func GetLyIndexByClassifyIds(classifyIds []int) (items []*BaseFromLyIndex, err error) {
  41. o := orm.NewOrmUsingDB("data")
  42. // 创建查询条件
  43. qs := o.QueryTable("base_from_ly_index")
  44. // 使用 Filter 进行查询
  45. _, err = qs.Filter("base_from_ly_classify_id__in", classifyIds).All(&items)
  46. return
  47. }
  48. // GetLyIndexCount 获取指标总数
  49. func GetLyIndexCount(classifyId string, searchParam string) (count int, err error) {
  50. o := orm.NewOrmUsingDB("data")
  51. qs := o.QueryTable("base_from_ly_index")
  52. // 精确匹配 classifyId
  53. if classifyId != "" {
  54. qs = qs.Filter("base_from_ly_classify_id", classifyId)
  55. }
  56. if searchParam != "" {
  57. qs = qs.Filter("index_name__icontains", searchParam).
  58. Filter("index_code", searchParam)
  59. }
  60. number, err := qs.Count()
  61. if err != nil {
  62. return 0, err
  63. }
  64. return int(number), nil
  65. }
  66. // GetLyIndexPage 获取指标列表
  67. func GetLyIndexPage(classifyId string, searchParam string, currentIndex, pageSize int) (items []*BaseFromLyIndex, err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. qs := o.QueryTable("base_from_ly_index")
  70. // 精确匹配 classifyId
  71. if classifyId != "" {
  72. qs = qs.Filter("base_from_ly_classify_id", classifyId)
  73. }
  74. // 添加搜索条件
  75. if searchParam != "" {
  76. cond := orm.NewCondition()
  77. cond = cond.Or("index_name__icontains", searchParam).Or("index_code", searchParam)
  78. qs = qs.SetCond(cond)
  79. }
  80. // id降序排序与分页
  81. qs = qs.OrderBy("-base_from_ly_index_id")
  82. qs = qs.Limit(pageSize, (currentIndex-1)*pageSize)
  83. // 执行查询
  84. _, err = qs.All(&items)
  85. return
  86. }
  87. // UpdateLyIndexEdbExist 指标库标记已添加
  88. func UpdateLyIndexEdbExist(indexCode string) (err error) {
  89. o := orm.NewOrmUsingDB("data")
  90. lyIndex := BaseFromLyIndex{IndexCode: indexCode}
  91. lyIndex.EdbExist = 1
  92. _, err = o.Update(&lyIndex, "edb_exist")
  93. if err != nil {
  94. return err
  95. }
  96. return
  97. }
  98. // GetLyIndexList 根据传入条件查询指标列表
  99. func GetLyIndexList(condition string, pars interface{}) (items []*BaseFromLyIndex, err error) {
  100. o := orm.NewOrmUsingDB("data")
  101. sql := ` SELECT * FROM base_from_ly_index WHERE 1=1 `
  102. if condition != "" {
  103. sql += condition
  104. }
  105. sql += `ORDER BY base_from_ly_index_id ASC `
  106. _, err = o.Raw(sql, pars).QueryRows(&items)
  107. return
  108. }
  109. // GetLyDataMaxCount 获取分类下指标最大数据量
  110. func GetLyDataMaxCount(classifyId int) (count int, err error) {
  111. o := orm.NewOrmUsingDB("data")
  112. sql := `SELECT MAX(t.num) AS count FROM (
  113. SELECT COUNT(1) AS num FROM base_from_ly_index AS a
  114. INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id=b.base_from_ly_index_id
  115. WHERE a.base_from_ly_classify_id=?
  116. GROUP BY a.base_from_ly_index_id
  117. )AS t `
  118. err = o.Raw(sql, classifyId).QueryRow(&count)
  119. return
  120. }