base_from_ly_index.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. type IndexCheckData struct {
  53. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  54. IndexName string `orm:"column(index_name)" description:"指标名称"`
  55. Frequency string `orm:"column(frequency)" description:"频度"`
  56. Unit string `orm:"column(unit)" description:"单位"`
  57. EdbInfoId int `json:"edb_info_id" description:"指标库主键id"`
  58. UniqueCode string `json:"unique_code" description:"指标库唯一编码"`
  59. ClassifyId int `json:"classify_id" description:"分类id"`
  60. }
  61. // GetLyIndexByClassifyIds 通过分类ids查询指标列表
  62. func GetLyIndexByClassifyIds(classifyIds []int) (items []*BaseFromLyIndex, err error) {
  63. o := orm.NewOrmUsingDB("data")
  64. // 创建查询条件
  65. qs := o.QueryTable("base_from_ly_index")
  66. // 使用 Filter 进行查询
  67. _, err = qs.Filter("base_from_ly_classify_id__in", classifyIds).All(&items)
  68. return
  69. }
  70. // GetLyIndexCount 获取指标总数
  71. func GetLyIndexCount(classifyId string, searchParam string) (count int, err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. // 构建 SQL 查询语句
  74. sql := `SELECT COUNT(*) FROM base_from_ly_index WHERE 1=1`
  75. var params []interface{}
  76. // 添加 classifyId 过滤条件
  77. if classifyId != "" {
  78. sql += ` AND base_from_ly_classify_id = ?`
  79. params = append(params, classifyId)
  80. }
  81. // 添加搜索条件
  82. if searchParam != "" {
  83. sql += ` AND (index_name LIKE ? OR index_code = ?)`
  84. params = append(params, "%"+searchParam+"%", searchParam)
  85. }
  86. // 执行查询
  87. err = o.Raw(sql, params...).QueryRow(&count)
  88. if err != nil {
  89. return 0, err
  90. }
  91. return count, nil
  92. }
  93. // GetLyIndexPage 获取指标列表
  94. func GetLyIndexPage(classifyId string, searchParam string, currentIndex, pageSize int) (items []*BaseFromLyIndexAndData, err error) {
  95. o := orm.NewOrmUsingDB("data")
  96. // 构建 SQL 查询语句
  97. sql := `SELECT * FROM base_from_ly_index WHERE 1=1`
  98. var params []interface{}
  99. // 添加 classifyId 过滤条件
  100. if classifyId != "" {
  101. sql += ` AND base_from_ly_classify_id = ?`
  102. params = append(params, classifyId)
  103. }
  104. // 添加搜索条件
  105. if searchParam != "" {
  106. sql += ` AND (index_name LIKE ? OR index_code = ?)`
  107. params = append(params, "%"+searchParam+"%", searchParam)
  108. }
  109. // 添加排序和分页条件
  110. sql += ` ORDER BY base_from_ly_index_id DESC LIMIT ?, ?`
  111. params = append(params, (currentIndex-1)*pageSize, pageSize)
  112. // 执行查询
  113. _, err = o.Raw(sql, params...).QueryRows(&items)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return
  118. }
  119. // UpdateLyIndexEdbExist 指标库标记已添加
  120. func UpdateLyIndexEdbExist(indexCode string, isExist int) (err error) {
  121. o := orm.NewOrmUsingDB("data")
  122. // 构建 SQL 更新语句
  123. sql := `UPDATE base_from_ly_index SET edb_exist = ? WHERE index_code = ?`
  124. // 执行 SQL 语句
  125. _, err = o.Raw(sql, isExist, indexCode).Exec()
  126. if err != nil {
  127. return err
  128. }
  129. return nil
  130. }
  131. // GetLyIndexList 根据传入条件查询指标列表
  132. func GetLyIndexList(condition string, pars interface{}) (items []*BaseFromLyIndex, err error) {
  133. o := orm.NewOrmUsingDB("data")
  134. sql := ` SELECT * FROM base_from_ly_index WHERE 1=1 `
  135. if condition != "" {
  136. sql += condition
  137. }
  138. sql += `ORDER BY base_from_ly_index_id ASC `
  139. _, err = o.Raw(sql, pars).QueryRows(&items)
  140. return
  141. }
  142. // GetLyDataMaxCount 获取分类下指标最大数据量
  143. func GetLyDataMaxCount(classifyId int) (count int, err error) {
  144. o := orm.NewOrmUsingDB("data")
  145. sql := `SELECT MAX(t.num) AS count FROM (
  146. SELECT COUNT(1) AS num FROM base_from_ly_index AS a
  147. INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id=b.base_from_ly_index_id
  148. WHERE a.base_from_ly_classify_id=?
  149. GROUP BY a.base_from_ly_index_id
  150. )AS t `
  151. err = o.Raw(sql, classifyId).QueryRow(&count)
  152. return
  153. }
  154. // GetLyIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  155. func GetLyIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromLyIndex, err error) {
  156. o := orm.NewOrmUsingDB("data")
  157. // SQL 查询语句
  158. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  159. FROM base_from_ly_index AS a
  160. INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id = b.base_from_ly_index_id
  161. WHERE 1=1`
  162. var params []interface{}
  163. if classifyId != 0 {
  164. sql += ` AND a.base_from_ly_classify_id = ?`
  165. params = append(params, classifyId)
  166. }
  167. // 如果 indexCode 不为空,增加过滤条件
  168. if indexCode != "" {
  169. sql += ` AND a.index_code = ?`
  170. params = append(params, indexCode)
  171. }
  172. if frequency != nil {
  173. sql += ` AND a.frequency = ?`
  174. params = append(params, *frequency)
  175. }
  176. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  177. _, err = o.Raw(sql, params...).QueryRows(&items)
  178. if err != nil {
  179. return nil, err
  180. }
  181. return
  182. }
  183. // GetLyIndexFrequency 获取指标频度
  184. func GetLyIndexFrequency(classifyId int) (items []*string, err error) {
  185. sql := `SELECT DISTINCT frequency
  186. FROM base_from_ly_index
  187. WHERE frequency IS NOT NULL`
  188. // 如果 classifyId > 0,则添加该条件
  189. if classifyId > 0 {
  190. sql += ` AND base_from_ly_classify_id = ?`
  191. }
  192. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  193. o := orm.NewOrmUsingDB("data")
  194. if classifyId > 0 {
  195. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  196. } else {
  197. _, err = o.Raw(sql).QueryRows(&items)
  198. }
  199. return items, err
  200. }