edb_info_relation.go 7.2 KB

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