base_from_ly_index.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 []*BaseFromLyIndexAndData `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. type BaseFromLyIndexAndData struct {
  40. BaseFromLyIndexId int `orm:"column(base_from_ly_index_id);pk" description:"指标ID"`
  41. CreateTime string `orm:"column(create_time)" description:"创建时间"`
  42. ModifyTime string `orm:"column(modify_time)" description:"修改时间"`
  43. BaseFromLyClassifyId int `orm:"column(base_from_ly_classify_id)" description:"原始数据指标分类id"`
  44. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  45. IndexName string `orm:"column(index_name)" description:"指标名称"`
  46. Frequency string `orm:"column(frequency)" description:"频度"`
  47. Unit string `orm:"column(unit)" description:"单位"`
  48. EdbExist int `orm:"column(edb_exist)" description:"指标库是否已添加:0-否;1-是"`
  49. ModifyTimeMax string `json:"modify_time_max" description:"最后修改时间"`
  50. Value float64 `orm:"column(value)" description:"数据值"`
  51. }
  52. // GetLyIndexByClassifyIds 通过分类ids查询指标列表
  53. func GetLyIndexByClassifyIds(classifyIds []int) (items []*BaseFromLyIndex, err error) {
  54. o := orm.NewOrmUsingDB("data")
  55. // 创建查询条件
  56. qs := o.QueryTable("base_from_ly_index")
  57. // 使用 Filter 进行查询
  58. _, err = qs.Filter("base_from_ly_classify_id__in", classifyIds).All(&items)
  59. return
  60. }
  61. // GetLyIndexCount 获取指标总数
  62. func GetLyIndexCount(classifyId string, searchParam string) (count int, err error) {
  63. o := orm.NewOrmUsingDB("data")
  64. // 构建 SQL 查询语句
  65. sql := `SELECT COUNT(*) FROM base_from_ly_index WHERE 1=1`
  66. var params []interface{}
  67. // 添加 classifyId 过滤条件
  68. if classifyId != "" {
  69. sql += ` AND base_from_ly_classify_id = ?`
  70. params = append(params, classifyId)
  71. }
  72. // 添加搜索条件
  73. if searchParam != "" {
  74. sql += ` AND (index_name LIKE ? OR index_code = ?)`
  75. params = append(params, "%"+searchParam+"%", searchParam)
  76. }
  77. // 执行查询
  78. err = o.Raw(sql, params...).QueryRow(&count)
  79. if err != nil {
  80. return 0, err
  81. }
  82. return count, nil
  83. }
  84. // GetLyIndexPage 获取指标列表
  85. func GetLyIndexPage(classifyId string, searchParam string, currentIndex, pageSize int) (items []*BaseFromLyIndexAndData, err error) {
  86. o := orm.NewOrmUsingDB("data")
  87. // 构建 SQL 查询语句
  88. sql := `SELECT * FROM base_from_ly_index WHERE 1=1`
  89. var params []interface{}
  90. // 添加 classifyId 过滤条件
  91. if classifyId != "" {
  92. sql += ` AND base_from_ly_classify_id = ?`
  93. params = append(params, classifyId)
  94. }
  95. // 添加搜索条件
  96. if searchParam != "" {
  97. sql += ` AND (index_name LIKE ? OR index_code = ?)`
  98. params = append(params, "%"+searchParam+"%", searchParam)
  99. }
  100. // 添加排序和分页条件
  101. sql += ` ORDER BY base_from_ly_index_id DESC LIMIT ?, ?`
  102. params = append(params, (currentIndex-1)*pageSize, pageSize)
  103. // 执行查询
  104. _, err = o.Raw(sql, params...).QueryRows(&items)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return
  109. }
  110. // UpdateLyIndexEdbExist 指标库标记已添加
  111. func UpdateLyIndexEdbExist(indexCode string) (err error) {
  112. o := orm.NewOrmUsingDB("data")
  113. lyIndex := BaseFromLyIndex{IndexCode: indexCode}
  114. lyIndex.EdbExist = 1
  115. _, err = o.Update(&lyIndex, "edb_exist")
  116. if err != nil {
  117. return err
  118. }
  119. return
  120. }
  121. // GetLyIndexList 根据传入条件查询指标列表
  122. func GetLyIndexList(condition string, pars interface{}) (items []*BaseFromLyIndex, err error) {
  123. o := orm.NewOrmUsingDB("data")
  124. sql := ` SELECT * FROM base_from_ly_index WHERE 1=1 `
  125. if condition != "" {
  126. sql += condition
  127. }
  128. sql += `ORDER BY base_from_ly_index_id ASC `
  129. _, err = o.Raw(sql, pars).QueryRows(&items)
  130. return
  131. }
  132. // GetLyDataMaxCount 获取分类下指标最大数据量
  133. func GetLyDataMaxCount(classifyId int) (count int, err error) {
  134. o := orm.NewOrmUsingDB("data")
  135. sql := `SELECT MAX(t.num) AS count FROM (
  136. SELECT COUNT(1) AS num FROM base_from_ly_index AS a
  137. INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id=b.base_from_ly_index_id
  138. WHERE a.base_from_ly_classify_id=?
  139. GROUP BY a.base_from_ly_index_id
  140. )AS t `
  141. err = o.Raw(sql, classifyId).QueryRow(&count)
  142. return
  143. }
  144. // GetLyIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  145. func GetLyIndexByCodeAndClassify(indexCode string, classifyId int) (items []*BaseFromLyIndex, err error) {
  146. o := orm.NewOrmUsingDB("data")
  147. // SQL 查询语句
  148. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  149. FROM base_from_ly_index AS a
  150. INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id = b.base_from_ly_index_id
  151. WHERE a.base_from_ly_classify_id = ?`
  152. var params []interface{}
  153. params = append(params, classifyId)
  154. // 如果 indexCode 不为空,增加过滤条件
  155. if indexCode != "" {
  156. sql += ` AND a.index_code = ?`
  157. params = append(params, indexCode)
  158. }
  159. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  160. _, err = o.Raw(sql, params...).QueryRows(&items)
  161. if err != nil {
  162. return nil, err
  163. }
  164. return
  165. }
  166. // GetLyIndexFrequency 获取指标频度
  167. func GetLyIndexFrequency(classifyId int) (items []*string, err error) {
  168. sql := `SELECT DISTINCT frequency FROM base_from_ly_index where base_from_ly_classify_id=? AND frequency IS NOT NULL ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  169. o := orm.NewOrmUsingDB("edb")
  170. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  171. return
  172. }