base_from_ly_index.go 8.4 KB

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