base_from_rzd_index.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "gorm.io/gorm"
  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. func (baseFromRzdIndexList *BaseFromRzdIndexList) AfterFind(tx *gorm.DB) (err error) {
  51. baseFromRzdIndexList.ModifyTime = utils.GormDateStrToDateTimeStr(baseFromRzdIndexList.ModifyTime)
  52. baseFromRzdIndexList.CreateTime = utils.GormDateStrToDateTimeStr(baseFromRzdIndexList.CreateTime)
  53. return
  54. }
  55. // RzdNameCheckResult 校验名称是否存在
  56. type RzdNameCheckResult struct {
  57. IndexCode string `from:"EdbCode" description:"edb编码"`
  58. IndexName string `from:"EdbName" description:"edb名称"`
  59. Exist bool
  60. }
  61. // RzdIndexCheckData 校验编码是否存在
  62. type RzdIndexCheckData struct {
  63. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  64. IndexName string `orm:"column(index_name)" description:"指标名称"`
  65. Frequency string `orm:"column(frequency)" description:"频度"`
  66. Unit string `orm:"column(unit)" description:"单位"`
  67. EdbInfoId int `json:"edb_info_id" description:"指标库主键id"`
  68. UniqueCode string `json:"unique_code" description:"指标库唯一编码"`
  69. ClassifyId int `json:"classify_id" description:"分类id"`
  70. }
  71. // BaseFromRzdIndexBatchAddCheckReq 校验编码是否存在请求参数
  72. type BaseFromRzdIndexBatchAddCheckReq struct {
  73. IndexCodes []string `form:"IndexCodes" description:"指标编码列表"`
  74. ClassifyIdList []int `description:"分类id"`
  75. FrequencyList []string `description:"频度"`
  76. SearchParams string `description:"搜索参数 指标编码/指标名称"`
  77. IsCheckAll bool `description:"是否全选"`
  78. }
  79. // GetRzdIndexByClassifyIds 根据分类id获取指标信息
  80. func GetRzdIndexByClassifyIds(classifyIds []int) (items []*BaseFromRzdIndex, err error) {
  81. o := global.DbMap[utils.DbNameIndex]
  82. sql := fmt.Sprintf(`SELECT * FROM base_from_rzd_index WHERE base_from_rzd_classify_id IN (`+utils.GetOrmInReplace(len(classifyIds))) + `)`
  83. err = o.Raw(sql, classifyIds).Find(&items).Error
  84. if err != nil {
  85. return nil, err
  86. }
  87. return items, nil
  88. }
  89. func GetRzdIndex(condition string, pars []interface{}) (items []*BaseFromRzdIndexList, err error) {
  90. o := global.DbMap[utils.DbNameIndex]
  91. sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1 `
  92. if condition != "" {
  93. sql += condition
  94. }
  95. sql += ` ORDER BY base_from_rzd_index_id asc`
  96. err = o.Raw(sql, pars...).Find(&items).Error
  97. return
  98. }
  99. func GetRzdIndexNotExistEdbInfoCount(condition string, pars []interface{}) (count int, err error) {
  100. o := global.DbMap[utils.DbNameIndex]
  101. sql := ` SELECT count(1) FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  102. if condition != "" {
  103. sql += condition
  104. }
  105. err = o.Raw(sql, pars...).Scan(&count).Error
  106. return
  107. }
  108. func GetRzdIndexNotExistEdbInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  109. o := global.DbMap[utils.DbNameIndex]
  110. sql := ` SELECT * FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  111. if condition != "" {
  112. sql += condition
  113. }
  114. err = o.Raw(sql, pars...).Find(&items).Error
  115. return
  116. }
  117. // GetRzdIndexFrequency 获取指标频度
  118. func GetRzdIndexFrequency(classifyIdList []int) (items []*string, err error) {
  119. sql := `SELECT DISTINCT frequency
  120. FROM base_from_rzd_index
  121. WHERE frequency is not null`
  122. // 如果 classifyId > 0,则添加该条件
  123. if len(classifyIdList) > 0 {
  124. sql += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  125. }
  126. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  127. o := global.DbMap[utils.DbNameIndex]
  128. if len(classifyIdList) > 0 {
  129. err = o.Raw(sql, classifyIdList).Find(&items).Error
  130. } else {
  131. err = o.Raw(sql).Find(&items).Error
  132. }
  133. return items, err
  134. }
  135. // GetRzdIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  136. func GetRzdIndexByCodeAndClassify(indexCode string, classifyIdList []int, frequency *string) (items []*BaseFromRzdIndex, err error) {
  137. o := global.DbMap[utils.DbNameIndex]
  138. // SQL 查询语句
  139. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  140. FROM base_from_rzd_index AS a
  141. INNER JOIN base_from_rzd_data AS b ON a.base_from_rzd_index_id = b.base_from_rzd_index_id
  142. WHERE 1=1`
  143. var params []interface{}
  144. if len(classifyIdList) > 0 {
  145. sql += ` AND a.base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  146. //for _, id := range classifyIdList {
  147. // params = append(params, id)
  148. //}
  149. params = append(params, classifyIdList)
  150. }
  151. // 如果 indexCode 不为空,增加过滤条件
  152. if indexCode != "" {
  153. sql += ` AND a.index_code = ?`
  154. params = append(params, indexCode)
  155. }
  156. if frequency != nil {
  157. sql += ` AND a.frequency = ?`
  158. params = append(params, *frequency)
  159. }
  160. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  161. err = o.Raw(sql, params...).Find(&items).Error
  162. if err != nil {
  163. return nil, err
  164. }
  165. return
  166. }
  167. // GetRzdIndexInfoCount 分页查询指标信息行数
  168. func GetRzdIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  169. o := global.DbMap[utils.DbNameIndex]
  170. sql := ` SELECT count(1) FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  171. if condition != "" {
  172. sql += condition
  173. }
  174. err = o.Raw(sql, pars...).Scan(&count).Error
  175. return
  176. }
  177. // GetRzdIndexInfoPage 分页查询指标信息
  178. func GetRzdIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  179. o := global.DbMap[utils.DbNameIndex]
  180. sql := ` SELECT * FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  181. if condition != "" {
  182. sql += condition
  183. }
  184. err = o.Raw(sql, pars...).Find(&items).Error
  185. return
  186. }