base_from_ly_index.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. "github.com/beego/beego/v2/client/orm"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. )
  10. type BaseFromLyIndex struct {
  11. BaseFromLyIndexId int `orm:"column(base_from_ly_index_id);pk" gorm:"primaryKey" description:"指标ID"`
  12. CreateTime string `orm:"column(create_time)" description:"创建时间"`
  13. ModifyTime string `orm:"column(modify_time)" description:"修改时间"`
  14. BaseFromLyClassifyId int `orm:"column(base_from_ly_classify_id)" description:"原始数据指标分类id"`
  15. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  16. IndexName string `orm:"column(index_name)" description:"指标名称"`
  17. Frequency string `orm:"column(frequency)" description:"频度"`
  18. Unit string `orm:"column(unit)" description:"单位"`
  19. EdbExist int `orm:"column(edb_exist)" description:"指标库是否已添加:0-否;1-是"`
  20. }
  21. // 在 init 函数中注册模型
  22. func init() {
  23. orm.RegisterModel(new(BaseFromLyIndex))
  24. }
  25. type BaseFromLyIndexPage struct {
  26. List []*BaseFromLyIndexAndData `description:"指标列表"`
  27. Paging *paging.PagingItem `description:"分页数据"`
  28. }
  29. type BaseFromLyIndexBatchAddCheckReq struct {
  30. IndexCodes []string `form:"IndexCodes" description:"指标编码列表"`
  31. }
  32. type BaseFromLyIndexNameCheck struct {
  33. IndexCode string `from:"IndexCode" description:"指标编码"`
  34. IndexName string `from:"IndexName" description:"指标名称"`
  35. }
  36. type NameCheckResult struct {
  37. IndexCode string `from:"EdbCode" description:"edb编码"`
  38. IndexName string `from:"EdbName" description:"edb名称"`
  39. Exist bool
  40. }
  41. type BaseFromLyIndexAndData struct {
  42. BaseFromLyIndexId int `orm:"column(base_from_ly_index_id);pk" gorm:"primaryKey" description:"指标ID"`
  43. CreateTime string `orm:"column(create_time)" description:"创建时间"`
  44. ModifyTime string `orm:"column(modify_time)" description:"修改时间"`
  45. BaseFromLyClassifyId int `orm:"column(base_from_ly_classify_id)" description:"原始数据指标分类id"`
  46. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  47. IndexName string `orm:"column(index_name)" description:"指标名称"`
  48. Frequency string `orm:"column(frequency)" description:"频度"`
  49. Unit string `orm:"column(unit)" description:"单位"`
  50. EdbExist int `orm:"column(edb_exist)" description:"指标库是否已添加:0-否;1-是"`
  51. ModifyTimeMax string `json:"modify_time_max" description:"最后修改时间"`
  52. Value float64 `orm:"column(value)" description:"数据值"`
  53. }
  54. type IndexCheckData struct {
  55. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  56. IndexName string `orm:"column(index_name)" description:"指标名称"`
  57. Frequency string `orm:"column(frequency)" description:"频度"`
  58. Unit string `orm:"column(unit)" description:"单位"`
  59. EdbInfoId int `json:"edb_info_id" description:"指标库主键id"`
  60. UniqueCode string `json:"unique_code" description:"指标库唯一编码"`
  61. ClassifyId int `json:"classify_id" description:"分类id"`
  62. }
  63. // GetLyIndexByClassifyIds 通过分类ids查询指标列表
  64. func GetLyIndexByClassifyIds(classifyIds []int) (items []*BaseFromLyIndex, err error) {
  65. o := global.DbMap[utils.DbNameIndex]
  66. sql := `SELECT * FROM base_from_ly_index WHERE base_from_ly_classify_id in (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  67. // 使用 Filter 进行查询
  68. err = o.Raw(sql, classifyIds).Find(&items).Error
  69. return
  70. }
  71. // GetLyIndexCount 获取指标总数
  72. func GetLyIndexCount(classifyId string, searchParam string) (count int, err error) {
  73. o := global.DbMap[utils.DbNameIndex]
  74. // 构建 SQL 查询语句
  75. sql := `SELECT COUNT(*) FROM base_from_ly_index WHERE 1=1`
  76. var params []interface{}
  77. // 添加 classifyId 过滤条件
  78. if classifyId != "" {
  79. sql += ` AND base_from_ly_classify_id = ?`
  80. params = append(params, classifyId)
  81. }
  82. // 添加搜索条件
  83. if searchParam != "" {
  84. sql += ` AND (index_name LIKE ? OR index_code = ?)`
  85. params = append(params, "%"+searchParam+"%", searchParam)
  86. }
  87. // 执行查询
  88. err = o.Raw(sql, params...).Scan(&count).Error
  89. if err != nil {
  90. return 0, err
  91. }
  92. return count, nil
  93. }
  94. // GetLyIndexPage 获取指标列表
  95. func GetLyIndexPage(classifyId string, searchParam string, currentIndex, pageSize int) (items []*BaseFromLyIndexAndData, err error) {
  96. o := global.DbMap[utils.DbNameIndex]
  97. // 构建 SQL 查询语句
  98. sql := `SELECT * FROM base_from_ly_index WHERE 1=1`
  99. var params []interface{}
  100. // 添加 classifyId 过滤条件
  101. if classifyId != "" {
  102. sql += ` AND base_from_ly_classify_id = ?`
  103. params = append(params, classifyId)
  104. }
  105. // 添加搜索条件
  106. if searchParam != "" {
  107. sql += ` AND (index_name LIKE ? OR index_code = ?)`
  108. params = append(params, "%"+searchParam+"%", searchParam)
  109. }
  110. // 添加排序和分页条件
  111. sql += ` ORDER BY base_from_ly_index_id DESC LIMIT ?, ?`
  112. params = append(params, (currentIndex-1)*pageSize, pageSize)
  113. // 执行查询
  114. err = o.Raw(sql, params...).Find(&items).Error
  115. if err != nil {
  116. return nil, err
  117. }
  118. return
  119. }
  120. // UpdateLyIndexEdbExist 指标库标记已添加
  121. func UpdateLyIndexEdbExist(indexCode string, isExist int) (err error) {
  122. o := global.DbMap[utils.DbNameIndex]
  123. // 构建 SQL 更新语句
  124. sql := `UPDATE base_from_ly_index SET edb_exist = ? WHERE index_code = ?`
  125. // 执行 SQL 语句
  126. err = o.Exec(sql, isExist, indexCode).Error
  127. if err != nil {
  128. return err
  129. }
  130. return nil
  131. }
  132. // GetLyIndexList 根据传入条件查询指标列表
  133. func GetLyIndexList(condition string, pars interface{}) (items []*BaseFromLyIndex, err error) {
  134. o := global.DbMap[utils.DbNameIndex]
  135. sql := ` SELECT * FROM base_from_ly_index WHERE 1=1 `
  136. if condition != "" {
  137. sql += condition
  138. }
  139. sql += `ORDER BY base_from_ly_index_id ASC `
  140. err = o.Raw(sql, pars).Find(&items).Error
  141. return
  142. }
  143. // GetLyDataMaxCount 获取分类下指标最大数据量
  144. func GetLyDataMaxCount(classifyId int) (count int, err error) {
  145. o := global.DbMap[utils.DbNameIndex]
  146. sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM (
  147. SELECT COUNT(1) AS num FROM base_from_ly_index AS a
  148. INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id=b.base_from_ly_index_id
  149. WHERE a.base_from_ly_classify_id=?
  150. GROUP BY a.base_from_ly_index_id
  151. )AS t `
  152. err = o.Raw(sql, classifyId).Scan(&count).Error
  153. return
  154. }
  155. // GetLyIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  156. func GetLyIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromLyIndex, err error) {
  157. o := global.DbMap[utils.DbNameIndex]
  158. // SQL 查询语句
  159. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  160. FROM base_from_ly_index AS a
  161. INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id = b.base_from_ly_index_id
  162. WHERE 1=1`
  163. var params []interface{}
  164. if classifyId != 0 {
  165. sql += ` AND a.base_from_ly_classify_id = ?`
  166. params = append(params, classifyId)
  167. }
  168. // 如果 indexCode 不为空,增加过滤条件
  169. if indexCode != "" {
  170. sql += ` AND a.index_code = ?`
  171. params = append(params, indexCode)
  172. }
  173. if frequency != nil {
  174. sql += ` AND a.frequency = ?`
  175. params = append(params, *frequency)
  176. }
  177. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  178. err = o.Raw(sql, params...).Find(&items).Error
  179. if err != nil {
  180. return nil, err
  181. }
  182. return
  183. }
  184. // GetLyIndexFrequency 获取指标频度
  185. func GetLyIndexFrequency(classifyId int) (items []*string, err error) {
  186. sql := `SELECT DISTINCT frequency
  187. FROM base_from_ly_index
  188. WHERE frequency IS NOT NULL`
  189. // 如果 classifyId > 0,则添加该条件
  190. if classifyId > 0 {
  191. sql += ` AND base_from_ly_classify_id = ?`
  192. }
  193. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  194. o := global.DbMap[utils.DbNameIndex]
  195. if classifyId > 0 {
  196. err = o.Raw(sql, classifyId).Find(&items).Error
  197. } else {
  198. err = o.Raw(sql).Find(&items).Error
  199. }
  200. return items, err
  201. }