edb_info_relation.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "time"
  7. )
  8. type EdbInfoRelation struct {
  9. EdbInfoRelationId int `orm:"column(edb_info_relation_id);pk"`
  10. EdbInfoId int `description:"指标id"`
  11. Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,9:手工指标,10:隆众"`
  12. EdbName string `description:"指标名称"`
  13. EdbCode string `description:"指标编码"`
  14. ReferObjectId int `description:"引用对象ID(图表ID,ETA逻辑ID等)"`
  15. ReferObjectType int `description:"引用对象ID类型(1.图表,2.ETA逻辑)"`
  16. ReferObjectSubType int `description:"引用对象子类"`
  17. CreateTime time.Time `description:"创建时间"`
  18. ModifyTime time.Time `description:"修改时间"`
  19. }
  20. func (e *EdbInfoRelation) TableName() string {
  21. return "edb_info_relation"
  22. }
  23. // GetEdbInfoRelationByEdbInfoIds 查询引用的指标ID
  24. func GetEdbInfoRelationByEdbInfoIds(edbInfoIds []int) (edbIds []int, err error) {
  25. o := orm.NewOrmUsingDB("data")
  26. msql := ` SELECT edb_info_id FROM edb_info_relation WHERE edb_info_id in (` + utils.GetOrmInReplace(len(edbInfoIds)) + `) GROUP BY edb_info_id `
  27. _, err = o.Raw(msql, edbInfoIds).QueryRows(&edbIds)
  28. return
  29. }
  30. // GetEdbInfoRelationByReferObjectId 查询引用的指标ID
  31. func GetEdbInfoRelationByReferObjectId(referObjectId int, referObjectType int) (items []*EdbInfoRelation, err error) {
  32. o := orm.NewOrmUsingDB("data")
  33. msql := ` SELECT * FROM edb_info_relation WHERE refer_object_id =? AND refer_object_type=? GROUP BY edb_info_id `
  34. _, err = o.Raw(msql, referObjectId, referObjectType).QueryRows(&items)
  35. return
  36. }
  37. // GetEdbInfoRelationByReferObjectIds 查询引用的指标ID
  38. func GetEdbInfoRelationByReferObjectIds(referObjectIds []int, referObjectType int) (items []*EdbInfoRelation, err error) {
  39. o := orm.NewOrmUsingDB("data")
  40. msql := ` SELECT * FROM edb_info_relation WHERE refer_object_id in (` + utils.GetOrmInReplace(len(referObjectIds)) + `) AND refer_object_type=?`
  41. _, err = o.Raw(msql, referObjectIds, referObjectType).QueryRows(&items)
  42. return
  43. }
  44. // 新增记录
  45. func AddOrUpdateEdbInfoRelation(relationList []*EdbInfoRelation, deleteIds []int, refreshEdbInfoIds []int, indexCodeList []string) (err error) {
  46. o, err := orm.NewOrmUsingDB("data").Begin()
  47. if err != nil {
  48. return
  49. }
  50. defer func() {
  51. if err != nil {
  52. _ = o.Rollback()
  53. return
  54. }
  55. _ = o.Commit()
  56. }()
  57. if len(deleteIds) > 0 {
  58. sql := ` DELETE FROM edb_info_relation WHERE edb_info_relation_id in (` + utils.GetOrmInReplace(len(deleteIds)) + `) `
  59. _, err = o.Raw(sql, deleteIds).Exec()
  60. if err != nil {
  61. return
  62. }
  63. }
  64. if len(relationList) > 0 {
  65. _, err = o.InsertMulti(len(relationList), relationList)
  66. if err != nil {
  67. return
  68. }
  69. }
  70. if len(refreshEdbInfoIds) > 0 {
  71. //更新指标的刷新状态
  72. sql := ` UPDATE edb_info SET no_update = 0 WHERE source in (?, ?) AND edb_info_id IN (` + utils.GetOrmInReplace(len(refreshEdbInfoIds)) + `) AND no_update = 1`
  73. _, err = o.Raw(sql, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND, refreshEdbInfoIds).Exec()
  74. if err != nil {
  75. return
  76. }
  77. }
  78. //更新数据源钢联化工指标
  79. if len(indexCodeList) > 0 {
  80. // 更改数据源的更新状态
  81. sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = 0 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=1`
  82. _, err = o.Raw(sql, indexCodeList).Exec()
  83. if err != nil {
  84. return
  85. }
  86. }
  87. // todo 由此被禁用的计算指标是否能恢复刷新
  88. return
  89. }
  90. // 删除指标引用内容
  91. func DeleteEdbRelationByObjectIds(referObjectIds []int, referObjectType int) (err error) {
  92. o := orm.NewOrm()
  93. sql := ` DELETE FROM edb_info_relation WHERE refer_object_id in (` + utils.GetOrmInReplace(len(referObjectIds)) + `) AND refer_object_type=?`
  94. _, err = o.Raw(sql, referObjectIds, referObjectType).Exec()
  95. return
  96. }
  97. type BaseRelationEdbInfo struct {
  98. EdbInfoId int
  99. ClassifyId int `description:"指标分类id"`
  100. EdbName string `description:"指标名称"`
  101. SysUserId int `description:"创建人id"`
  102. SysUserRealName string `description:"创建人姓名"`
  103. Frequency string `description:"频度"`
  104. IsStop int `description:"是否停更:1:停更,0:未停更"`
  105. RelationNum int `description:"引用次数"`
  106. RelationTime string `description:"引用时间"`
  107. }
  108. type BaseRelationEdbInfoResp struct {
  109. Paging *paging.PagingItem
  110. List []*BaseRelationEdbInfo
  111. }
  112. type EdbInfoRelationDetail struct {
  113. EdbInfoRelationId int `orm:"column(edb_info_relation_id);pk"`
  114. EdbInfoId int `description:"指标id"`
  115. ReferObjectId int `description:"引用对象ID(图表ID,ETA逻辑ID等)"`
  116. ReferObjectTypeName string `description:"引用对象类型"`
  117. ReferObjectType int `description:"引用对象ID类型(1.图表,2.ETA逻辑)"`
  118. ReferObjectSubType int `description:"引用对象子类"`
  119. RelationTime string `description:"引用时间"`
  120. }
  121. type BaseRelationEdbInfoDetailResp struct {
  122. Paging *paging.PagingItem
  123. List []*EdbInfoRelationDetail
  124. }
  125. // 查询指标引用列表
  126. func GetEdbInfoRelationList(condition string, pars []interface{}, orderBy string, startSize, pageSize int) (total int, items []*BaseRelationEdbInfo, err error) {
  127. o := orm.NewOrmUsingDB("data")
  128. // 数量汇总
  129. totalSql := ` SELECT count(1) FROM edb_info e LEFT JOIN (
  130. SELECT count(edb_info_id) as relation_num, edb_info_id, max(modify_time) as relation_time FROM edb_info_relation GROUP BY edb_info_id) r on e.edb_info_id=r.edb_info_id WHERE e.source in (2,34) `
  131. if condition != "" {
  132. totalSql += condition
  133. }
  134. err = o.Raw(totalSql, pars).QueryRow(&total)
  135. if err != nil {
  136. return
  137. }
  138. // 列表数据
  139. sql := ` SELECT e.edb_info_id, e.classify_id,e.edb_code,e.edb_name,e.sys_user_id,e.sys_user_real_name,e.frequency,e.no_update as is_stop, r.relation_num, r.relation_time from edb_info e LEFT JOIN (
  140. SELECT count(edb_info_id) as relation_num, edb_info_id, max(modify_time) as relation_time FROM edb_info_relation GROUP BY edb_info_id) r on e.edb_info_id=r.edb_info_id WHERE e.source in (2,34)
  141. `
  142. if condition != "" {
  143. sql += condition
  144. }
  145. if orderBy != "" {
  146. sql += ` ORDER BY ` + orderBy
  147. } else {
  148. sql += ` ORDER BY edb_info_id ASC `
  149. }
  150. sql += ` LIMIT ?,? `
  151. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  152. return
  153. }
  154. // GetEdbInfoRelationDetailList 查询指标引用详情列表
  155. func GetEdbInfoRelationDetailList(edbInfoId int, startSize, pageSize int) (total int, items []*EdbInfoRelation, err error) {
  156. o := orm.NewOrmUsingDB("data")
  157. // 数量汇总
  158. totalSql := ` SELECT count(*) FROM edb_info_relation where edb_info_id=?`
  159. err = o.Raw(totalSql, edbInfoId).QueryRow(&total)
  160. if err != nil {
  161. return
  162. }
  163. // 列表数据
  164. sql := ` SELECT *FROM edb_info_relation where edb_info_id=? ORDER BY modify_time, edb_info_id ASC `
  165. sql += ` LIMIT ?,? `
  166. _, err = o.Raw(sql, edbInfoId, startSize, pageSize).QueryRows(&items)
  167. return
  168. }