edb_info_relation.go 9.6 KB

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