base_from_rzd_index.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Package data_manage
  2. // @Author gmy 2024/8/7 9:38:00
  3. package data_manage
  4. import (
  5. "eta/eta_api/global"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/beego/beego/v2/client/orm"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. )
  11. type BaseFromRzdIndex struct {
  12. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id);pk" gorm:"primaryKey"`
  13. CreateTime string `orm:"column(create_time)"`
  14. ModifyTime string `orm:"column(modify_time)"`
  15. BaseFromRzdClassifyId int `orm:"column(base_from_rzd_classify_id)"`
  16. IndexCode string `orm:"column(index_code)"`
  17. IndexName string `orm:"column(index_name)"`
  18. Frequency string `orm:"column(frequency)"`
  19. Unit string `orm:"column(unit)"`
  20. }
  21. type BaseFromRzdIndexAndData struct {
  22. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id);pk" gorm:"primaryKey"`
  23. CreateTime string `orm:"column(create_time)"`
  24. ModifyTime string `orm:"column(modify_time)"`
  25. BaseFromRzdClassifyId int `orm:"column(base_from_rzd_classify_id)"`
  26. IndexCode string `orm:"column(index_code)"`
  27. IndexName string `orm:"column(index_name)"`
  28. Frequency string `orm:"column(frequency)"`
  29. Unit string `orm:"column(unit)"`
  30. ModifyTimeMax string `json:"modify_time_max" description:"最后修改时间"`
  31. Value float64 `orm:"column(value)" description:"数据值"`
  32. }
  33. type BaseFromRzdIndexPage struct {
  34. List []*BaseFromRzdIndexAndData `description:"指标列表"`
  35. Paging *paging.PagingItem `description:"分页数据"`
  36. }
  37. type BaseFromRzdIndexList struct {
  38. BaseFromRzdIndexId int `gorm:"column:base_from_rzd_index_id;primaryKey"`
  39. CreateTime string `orm:"column(create_time)"`
  40. ModifyTime string `orm:"column(modify_time)"`
  41. BaseFromRzdClassifyId int `orm:"column(base_from_rzd_classify_id)"`
  42. IndexCode string `orm:"column(index_code)"`
  43. IndexName string `orm:"column(index_name)"`
  44. Frequency string `orm:"column(frequency)"`
  45. Unit string `orm:"column(unit)"`
  46. DataList []*BaseFromRzdData `gorm:"-"`
  47. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  48. EdbInfoId int `description:"指标库主键id"`
  49. }
  50. // RzdNameCheckResult 校验名称是否存在
  51. type RzdNameCheckResult struct {
  52. IndexCode string `from:"EdbCode" description:"edb编码"`
  53. IndexName string `from:"EdbName" description:"edb名称"`
  54. Exist bool
  55. }
  56. // RzdIndexCheckData 校验编码是否存在
  57. type RzdIndexCheckData struct {
  58. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  59. IndexName string `orm:"column(index_name)" description:"指标名称"`
  60. Frequency string `orm:"column(frequency)" description:"频度"`
  61. Unit string `orm:"column(unit)" description:"单位"`
  62. EdbInfoId int `json:"edb_info_id" description:"指标库主键id"`
  63. UniqueCode string `json:"unique_code" description:"指标库唯一编码"`
  64. ClassifyId int `json:"classify_id" description:"分类id"`
  65. }
  66. // BaseFromRzdIndexBatchAddCheckReq 校验编码是否存在请求参数
  67. type BaseFromRzdIndexBatchAddCheckReq struct {
  68. IndexCodes []string `form:"IndexCodes" description:"指标编码列表"`
  69. ClassifyIdList []int `description:"分类id"`
  70. FrequencyList []string `description:"频度"`
  71. SearchParams string `description:"搜索参数 指标编码/指标名称"`
  72. IsCheckAll bool `description:"是否全选"`
  73. }
  74. func init() {
  75. orm.RegisterModel(new(BaseFromRzdIndex))
  76. }
  77. // GetRzdIndexByClassifyIds 根据分类id获取指标信息
  78. func GetRzdIndexByClassifyIds(classifyIds []int) (items []*BaseFromRzdIndex, err error) {
  79. o := global.DbMap[utils.DbNameIndex]
  80. sql := fmt.Sprintf(`SELECT * FROM base_from_rzd_index WHERE base_from_rzd_classify_id IN (`+utils.GetOrmInReplace(len(classifyIds))) + `)`
  81. err = o.Raw(sql, classifyIds).Find(&items).Error
  82. if err != nil {
  83. return nil, err
  84. }
  85. return items, nil
  86. }
  87. func GetRzdIndex(condition string, pars []interface{}) (items []*BaseFromRzdIndexList, err error) {
  88. o := global.DbMap[utils.DbNameIndex]
  89. sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1 `
  90. if condition != "" {
  91. sql += condition
  92. }
  93. sql += ` ORDER BY base_from_rzd_index_id asc`
  94. err = o.Raw(sql, pars...).Find(&items).Error
  95. return
  96. }
  97. func GetRzdIndexNotExistEdbInfoCount(condition string, pars []interface{}) (count int, err error) {
  98. o := global.DbMap[utils.DbNameIndex]
  99. sql := ` SELECT count(1) FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  100. if condition != "" {
  101. sql += condition
  102. }
  103. err = o.Raw(sql, pars...).Scan(&count).Error
  104. return
  105. }
  106. func GetRzdIndexNotExistEdbInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  107. o := global.DbMap[utils.DbNameIndex]
  108. sql := ` SELECT * FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  109. if condition != "" {
  110. sql += condition
  111. }
  112. err = o.Raw(sql, pars...).Find(&items).Error
  113. return
  114. }
  115. // GetRzdIndexFrequency 获取指标频度
  116. func GetRzdIndexFrequency(classifyIdList []int) (items []*string, err error) {
  117. sql := `SELECT DISTINCT frequency
  118. FROM base_from_rzd_index
  119. WHERE frequency is not null`
  120. // 如果 classifyId > 0,则添加该条件
  121. if len(classifyIdList) > 0 {
  122. sql += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  123. }
  124. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  125. o := global.DbMap[utils.DbNameIndex]
  126. if len(classifyIdList) > 0 {
  127. err = o.Raw(sql, classifyIdList).Find(&items).Error
  128. } else {
  129. err = o.Raw(sql).Find(&items).Error
  130. }
  131. return items, err
  132. }
  133. // GetRzdIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  134. func GetRzdIndexByCodeAndClassify(indexCode string, classifyIdList []int, frequency *string) (items []*BaseFromRzdIndex, err error) {
  135. o := global.DbMap[utils.DbNameIndex]
  136. // SQL 查询语句
  137. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  138. FROM base_from_rzd_index AS a
  139. INNER JOIN base_from_rzd_data AS b ON a.base_from_rzd_index_id = b.base_from_rzd_index_id
  140. WHERE 1=1`
  141. var params []interface{}
  142. if len(classifyIdList) > 0 {
  143. sql += ` AND a.base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  144. for _, id := range classifyIdList {
  145. params = append(params, id)
  146. }
  147. }
  148. // 如果 indexCode 不为空,增加过滤条件
  149. if indexCode != "" {
  150. sql += ` AND a.index_code = ?`
  151. params = append(params, indexCode)
  152. }
  153. if frequency != nil {
  154. sql += ` AND a.frequency = ?`
  155. params = append(params, *frequency)
  156. }
  157. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  158. err = o.Raw(sql, params...).Find(&items).Error
  159. if err != nil {
  160. return nil, err
  161. }
  162. return
  163. }
  164. // GetRzdIndexInfoCount 分页查询指标信息行数
  165. func GetRzdIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  166. o := global.DbMap[utils.DbNameIndex]
  167. sql := ` SELECT count(1) FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  168. if condition != "" {
  169. sql += condition
  170. }
  171. err = o.Raw(sql, pars...).Scan(&count).Error
  172. return
  173. }
  174. // GetRzdIndexInfoPage 分页查询指标信息
  175. func GetRzdIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  176. o := global.DbMap[utils.DbNameIndex]
  177. sql := ` SELECT * FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  178. if condition != "" {
  179. sql += condition
  180. }
  181. err = o.Raw(sql, pars...).Find(&items).Error
  182. return
  183. }