base_from_rzd_index.go 8.2 KB

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