base_from_ly_index.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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:"最后修改时间"`
  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. b.ModifyTimeMax = utils.GormDateStrToDateStr(b.ModifyTimeMax)
  64. return
  65. }
  66. type IndexCheckData struct {
  67. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  68. IndexName string `orm:"column(index_name)" description:"指标名称"`
  69. Frequency string `orm:"column(frequency)" description:"频度"`
  70. Unit string `orm:"column(unit)" description:"单位"`
  71. EdbInfoId int `json:"edb_info_id" description:"指标库主键id"`
  72. UniqueCode string `json:"unique_code" description:"指标库唯一编码"`
  73. ClassifyId int `json:"classify_id" description:"分类id"`
  74. }
  75. // GetLyIndexByClassifyIds 通过分类ids查询指标列表
  76. func GetLyIndexByClassifyIds(classifyIds []int) (items []*BaseFromLyIndex, err error) {
  77. o := global.DbMap[utils.DbNameIndex]
  78. sql := `SELECT * FROM base_from_ly_index WHERE base_from_ly_classify_id in (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  79. // 使用 Filter 进行查询
  80. err = o.Raw(sql, classifyIds).Find(&items).Error
  81. return
  82. }
  83. // GetLyIndexCount 获取指标总数
  84. func GetLyIndexCount(classifyId string, searchParam string) (count int, err error) {
  85. o := global.DbMap[utils.DbNameIndex]
  86. // 构建 SQL 查询语句
  87. sql := `SELECT COUNT(*) FROM base_from_ly_index WHERE 1=1`
  88. var params []interface{}
  89. // 添加 classifyId 过滤条件
  90. if classifyId != "" {
  91. sql += ` AND base_from_ly_classify_id = ?`
  92. params = append(params, classifyId)
  93. }
  94. // 添加搜索条件
  95. if searchParam != "" {
  96. sql += ` AND (index_name LIKE ? OR index_code = ?)`
  97. params = append(params, "%"+searchParam+"%", searchParam)
  98. }
  99. // 执行查询
  100. err = o.Raw(sql, params...).Scan(&count).Error
  101. if err != nil {
  102. return 0, err
  103. }
  104. return count, nil
  105. }
  106. // GetLyIndexPage 获取指标列表
  107. func GetLyIndexPage(classifyId string, searchParam string, currentIndex, pageSize int) (items []*BaseFromLyIndexAndData, err error) {
  108. o := global.DbMap[utils.DbNameIndex]
  109. // 构建 SQL 查询语句
  110. sql := `SELECT * FROM base_from_ly_index WHERE 1=1`
  111. var params []interface{}
  112. // 添加 classifyId 过滤条件
  113. if classifyId != "" {
  114. sql += ` AND base_from_ly_classify_id = ?`
  115. params = append(params, classifyId)
  116. }
  117. // 添加搜索条件
  118. if searchParam != "" {
  119. sql += ` AND (index_name LIKE ? OR index_code = ?)`
  120. params = append(params, "%"+searchParam+"%", searchParam)
  121. }
  122. // 添加排序和分页条件
  123. sql += ` ORDER BY base_from_ly_index_id DESC LIMIT ?, ?`
  124. params = append(params, (currentIndex-1)*pageSize, pageSize)
  125. // 执行查询
  126. err = o.Raw(sql, params...).Find(&items).Error
  127. if err != nil {
  128. return nil, err
  129. }
  130. return
  131. }
  132. // UpdateLyIndexEdbExist 指标库标记已添加
  133. func UpdateLyIndexEdbExist(indexCode string, isExist int) (err error) {
  134. o := global.DbMap[utils.DbNameIndex]
  135. // 构建 SQL 更新语句
  136. sql := `UPDATE base_from_ly_index SET edb_exist = ? WHERE index_code = ?`
  137. // 执行 SQL 语句
  138. err = o.Exec(sql, isExist, indexCode).Error
  139. if err != nil {
  140. return err
  141. }
  142. return nil
  143. }
  144. // GetLyIndexList 根据传入条件查询指标列表
  145. func GetLyIndexList(condition string, pars []interface{}) (items []*BaseFromLyIndex, err error) {
  146. o := global.DbMap[utils.DbNameIndex]
  147. sql := ` SELECT * FROM base_from_ly_index WHERE 1=1 `
  148. if condition != "" {
  149. sql += condition
  150. }
  151. sql += `ORDER BY base_from_ly_index_id ASC `
  152. err = o.Raw(sql, pars...).Find(&items).Error
  153. return
  154. }
  155. // GetLyDataMaxCount 获取分类下指标最大数据量
  156. func GetLyDataMaxCount(classifyId int) (count int, err error) {
  157. o := global.DbMap[utils.DbNameIndex]
  158. sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM (
  159. SELECT COUNT(1) AS num FROM base_from_ly_index AS a
  160. INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id=b.base_from_ly_index_id
  161. WHERE a.base_from_ly_classify_id=?
  162. GROUP BY a.base_from_ly_index_id
  163. )AS t `
  164. err = o.Raw(sql, classifyId).Scan(&count).Error
  165. return
  166. }
  167. // GetLyIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  168. func GetLyIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromLyIndex, err error) {
  169. o := global.DbMap[utils.DbNameIndex]
  170. // SQL 查询语句
  171. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  172. FROM base_from_ly_index AS a
  173. INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id = b.base_from_ly_index_id
  174. WHERE 1=1`
  175. var params []interface{}
  176. if classifyId != 0 {
  177. sql += ` AND a.base_from_ly_classify_id = ?`
  178. params = append(params, classifyId)
  179. }
  180. // 如果 indexCode 不为空,增加过滤条件
  181. if indexCode != "" {
  182. sql += ` AND a.index_code = ?`
  183. params = append(params, indexCode)
  184. }
  185. if frequency != nil {
  186. sql += ` AND a.frequency = ?`
  187. params = append(params, *frequency)
  188. }
  189. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  190. err = o.Raw(sql, params...).Find(&items).Error
  191. if err != nil {
  192. return nil, err
  193. }
  194. return
  195. }
  196. // GetLyIndexFrequency 获取指标频度
  197. func GetLyIndexFrequency(classifyId int) (items []*string, err error) {
  198. sql := `SELECT DISTINCT frequency
  199. FROM base_from_ly_index
  200. WHERE frequency IS NOT NULL`
  201. // 如果 classifyId > 0,则添加该条件
  202. if classifyId > 0 {
  203. sql += ` AND base_from_ly_classify_id = ?`
  204. }
  205. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  206. o := global.DbMap[utils.DbNameIndex]
  207. if classifyId > 0 {
  208. err = o.Raw(sql, classifyId).Find(&items).Error
  209. } else {
  210. err = o.Raw(sql).Find(&items).Error
  211. }
  212. return items, err
  213. }