base_from_rzd_index.go 8.1 KB

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