base_from_rzd_index.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. ClassifyIdList []int `description:"分类id"`
  69. Frequency string `description:"频度"`
  70. SearchParams string `description:"搜索参数 指标编码/指标名称"`
  71. IsCheckAll bool `description:"是否全选"`
  72. CurrentIndex int `description:"起始页"`
  73. PageSize int `description:"每页记录数"`
  74. }
  75. func init() {
  76. orm.RegisterModel(new(BaseFromRzdIndex))
  77. }
  78. // GetRzdIndexByClassifyIds 根据分类id获取指标信息
  79. func GetRzdIndexByClassifyIds(classifyIds []int) (items []*BaseFromRzdIndex, err error) {
  80. o := orm.NewOrmUsingDB("data")
  81. sql := fmt.Sprintf(`SELECT * FROM base_from_rzd_index WHERE base_from_rzd_classify_id IN (`+utils.GetOrmInReplace(len(classifyIds))) + `)`
  82. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return items, nil
  87. }
  88. func GetRzdIndex(condition string, pars interface{}) (items []*BaseFromRzdIndexList, err error) {
  89. o := orm.NewOrmUsingDB("data")
  90. sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1 `
  91. if condition != "" {
  92. sql += condition
  93. }
  94. sql += ` ORDER BY base_from_rzd_index_id asc`
  95. _, err = o.Raw(sql, pars).QueryRows(&items)
  96. return
  97. }
  98. func GetRzdIndexNotExistEdbInfoCount(condition string, pars interface{}) (count int, err error) {
  99. o := orm.NewOrmUsingDB("data")
  100. sql := ` SELECT count(1) FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  101. if condition != "" {
  102. sql += condition
  103. }
  104. err = o.Raw(sql, pars).QueryRow(&count)
  105. return
  106. }
  107. func GetRzdIndexNotExistEdbInfoPage(condition string, pars interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := ` SELECT * FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  110. if condition != "" {
  111. sql += condition
  112. }
  113. _, err = o.Raw(sql, pars).QueryRows(&items)
  114. return
  115. }
  116. // GetRzdIndexFrequency 获取指标频度
  117. func GetRzdIndexFrequency(classifyIdList []int) (items []*string, err error) {
  118. sql := `SELECT DISTINCT frequency
  119. FROM base_from_rzd_index
  120. WHERE frequency is not null`
  121. // 如果 classifyId > 0,则添加该条件
  122. if len(classifyIdList) > 0 {
  123. sql += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  124. }
  125. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  126. o := orm.NewOrmUsingDB("data")
  127. if len(classifyIdList) > 0 {
  128. _, err = o.Raw(sql, classifyIdList).QueryRows(&items)
  129. } else {
  130. _, err = o.Raw(sql).QueryRows(&items)
  131. }
  132. return items, err
  133. }
  134. // GetRzdIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  135. func GetRzdIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromRzdIndex, err error) {
  136. o := orm.NewOrmUsingDB("data")
  137. // SQL 查询语句
  138. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  139. FROM base_from_rzd_index AS a
  140. INNER JOIN base_from_rzd_data AS b ON a.base_from_rzd_index_id = b.base_from_rzd_index_id
  141. WHERE 1=1`
  142. var params []interface{}
  143. if classifyId != 0 {
  144. sql += ` AND a.classify_id = ?`
  145. params = append(params, classifyId)
  146. }
  147. // 如果 indexCode 不为空,增加过滤条件
  148. if indexCode != "" {
  149. sql += ` AND a.index_code = ?`
  150. params = append(params, indexCode)
  151. }
  152. if frequency != nil {
  153. sql += ` AND a.frequency = ?`
  154. params = append(params, *frequency)
  155. }
  156. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  157. _, err = o.Raw(sql, params...).QueryRows(&items)
  158. if err != nil {
  159. return nil, err
  160. }
  161. return
  162. }
  163. // GetRzdIndexInfoCount 分页查询指标信息行数
  164. func GetRzdIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  165. o := orm.NewOrmUsingDB("data")
  166. sql := ` SELECT count(1) FROM base_from_rzd_index WHERE 1=1 `
  167. if condition != "" {
  168. sql += condition
  169. }
  170. err = o.Raw(sql, pars).QueryRow(&count)
  171. return
  172. }
  173. // GetRzdIndexInfoPage 分页查询指标信息
  174. func GetRzdIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  175. o := orm.NewOrmUsingDB("data")
  176. sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1 `
  177. if condition != "" {
  178. sql += condition
  179. }
  180. _, err = o.Raw(sql, pars).QueryRows(&items)
  181. return
  182. }