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