base_from_ly_index.go 8.5 KB

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