edb_info_relation.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "time"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. )
  8. type EdbInfoRelation struct {
  9. EdbInfoRelationId int `orm:"column(edb_info_relation_id);pk" gorm:"primaryKey" `
  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. RelationCode string `description:"引用标识"`
  24. ParentRelationId int `description:"间接引用关联的直接引用的ID"`
  25. }
  26. func (e *EdbInfoRelation) TableName() string {
  27. return "edb_info_relation"
  28. }
  29. func GetEdbInfoRelationByRelationIds(ids []int) (items []*EdbInfoRelation, err error) {
  30. o := global.DmSQL["data"]
  31. msql := ` SELECT * FROM edb_info_relation WHERE edb_info_relation_id in (` + utils.GetOrmInReplace(len(ids)) + `) `
  32. err = o.Raw(msql, ids).Find(&items).Error
  33. return
  34. }
  35. func GetEdbInfoRelationByReferObjectId(referObjectId int, referObjectType int) (items []*EdbInfoRelation, err error) {
  36. o := global.DmSQL["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,edb_info_relation_id, source, edb_name, edb_code, refer_object_id, refer_object_type, refer_object_sub_type,create_time,modify_time,relation_time,relation_type, root_edb_info_id, child_edb_info_id,relation_code,parent_relation_id `
  38. err = o.Raw(msql, referObjectId, referObjectType).Find(&items).Error
  39. return
  40. }
  41. func GetEdbInfoRelationByReferObjectIds(referObjectIds []int, referObjectType int) (items []*EdbInfoRelation, err error) {
  42. o := global.DmSQL["data"]
  43. msql := ` SELECT * FROM edb_info_relation WHERE refer_object_id in (` + utils.GetOrmInReplace(len(referObjectIds)) + `) AND refer_object_type=? and relation_type=0`
  44. err = o.Raw(msql, referObjectIds, referObjectType).Find(&items).Error
  45. return
  46. }
  47. func GetEdbInfoRelationAllByReferObjectIds(referObjectIds []int, referObjectType int) (items []*EdbInfoRelation, err error) {
  48. o := global.DmSQL["data"]
  49. msql := ` SELECT * FROM edb_info_relation WHERE refer_object_id in (` + utils.GetOrmInReplace(len(referObjectIds)) + `) AND refer_object_type=?`
  50. err = o.Raw(msql, referObjectIds, referObjectType).Find(&items).Error
  51. return
  52. }
  53. func AddOrUpdateEdbInfoRelation(objectId, objectType int, relationList []*EdbInfoRelation, deleteEdbInfoIds []int, refreshEdbInfoIds []int, indexCodeList []string) (err error) {
  54. o := global.DmSQL["data"].Begin()
  55. defer func() {
  56. if err != nil {
  57. _ = o.Rollback()
  58. return
  59. }
  60. _ = o.Commit()
  61. }()
  62. if len(deleteEdbInfoIds) > 0 {
  63. 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`
  64. err = o.Exec(sql, objectId, objectType, deleteEdbInfoIds).Error
  65. if err != nil {
  66. return
  67. }
  68. sql = ` DELETE FROM edb_info_relation WHERE refer_object_id = ? AND refer_object_type=? AND root_edb_info_id in (` + utils.GetOrmInReplace(len(deleteEdbInfoIds)) + `) AND relation_type=1 `
  69. err = o.Exec(sql, objectId, objectType, deleteEdbInfoIds).Error
  70. if err != nil {
  71. return
  72. }
  73. }
  74. relationCodesMap := make(map[string]struct{}, 0)
  75. if len(relationList) > 0 {
  76. for _, relation := range relationList {
  77. if relation.RelationType == 1 {
  78. relationCodesMap[relation.RelationCode] = struct{}{}
  79. }
  80. }
  81. err = o.CreateInBatches(relationList, utils.MultiAddNum).Error
  82. if err != nil {
  83. return
  84. }
  85. }
  86. if len(refreshEdbInfoIds) > 0 {
  87. sql := ` UPDATE edb_info SET no_update = 0 WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(refreshEdbInfoIds)) + `) AND no_update = 1`
  88. err = o.Exec(sql, refreshEdbInfoIds).Error
  89. if err != nil {
  90. return
  91. }
  92. }
  93. if len(indexCodeList) > 0 {
  94. sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = 0 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=1`
  95. err = o.Exec(sql, indexCodeList).Error
  96. if err != nil {
  97. return
  98. }
  99. }
  100. if len(relationList) > 0 {
  101. relationCodes := make([]string, 0)
  102. for relationCode := range relationCodesMap {
  103. relationCodes = append(relationCodes, relationCode)
  104. }
  105. if len(relationCodes) > 0 {
  106. sql := ` UPDATE edb_info_relation e1
  107. JOIN edb_info_relation e2 ON e1.relation_code = e2.relation_code
  108. SET e1.parent_relation_id = e2.edb_info_relation_id
  109. WHERE
  110. e1.relation_type = 1
  111. AND e2.relation_type = 0 AND e1.parent_relation_id !=e2.edb_info_relation_id AND e1.relation_code in (` + utils.GetOrmInReplace(len(relationCodes)) + `)`
  112. err = o.Exec(sql, relationCodes).Error
  113. if err != nil {
  114. return
  115. }
  116. }
  117. }
  118. return
  119. }
  120. func AddOrUpdateEdbInfoRelationMulti(relationList []*EdbInfoRelation, refreshEdbInfoIds []int, indexCodeList []string) (err error) {
  121. o := global.DmSQL["data"].Begin()
  122. defer func() {
  123. if err != nil {
  124. _ = o.Rollback()
  125. return
  126. }
  127. _ = o.Commit()
  128. }()
  129. relationCodesMap := make(map[string]struct{}, 0)
  130. if len(relationList) > 0 {
  131. for _, relation := range relationList {
  132. if relation.RelationType == 1 {
  133. relationCodesMap[relation.RelationCode] = struct{}{}
  134. }
  135. }
  136. err = o.CreateInBatches(relationList, utils.MultiAddNum).Error
  137. if err != nil {
  138. return
  139. }
  140. }
  141. if len(refreshEdbInfoIds) > 0 {
  142. sql := ` UPDATE edb_info SET no_update = 0 WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(refreshEdbInfoIds)) + `) AND no_update = 1`
  143. err = o.Exec(sql, refreshEdbInfoIds).Error
  144. if err != nil {
  145. return
  146. }
  147. }
  148. if len(indexCodeList) > 0 {
  149. sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = 0 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=1`
  150. err = o.Exec(sql, indexCodeList).Error
  151. if err != nil {
  152. return
  153. }
  154. }
  155. if len(relationList) > 0 {
  156. relationCodes := make([]string, 0)
  157. for relationCode := range relationCodesMap {
  158. relationCodes = append(relationCodes, relationCode)
  159. }
  160. if len(relationCodes) > 0 {
  161. sql := ` UPDATE edb_info_relation e1
  162. JOIN edb_info_relation e2 ON e1.relation_code = e2.relation_code
  163. SET e1.parent_relation_id = e2.edb_info_relation_id
  164. WHERE
  165. e1.relation_type = 1
  166. AND e2.relation_type = 0 AND e1.parent_relation_id !=e2.edb_info_relation_id AND e1.relation_code in (` + utils.GetOrmInReplace(len(relationCodes)) + `)`
  167. err = o.Exec(sql, relationCodes).Error
  168. if err != nil {
  169. return
  170. }
  171. }
  172. }
  173. return
  174. }
  175. func DeleteEdbRelationByObjectIds(referObjectIds []int, referObjectType int) (err error) {
  176. sql := ` DELETE FROM edb_info_relation WHERE refer_object_id in (` + utils.GetOrmInReplace(len(referObjectIds)) + `) AND refer_object_type=?`
  177. err = global.DmSQL["data"].Exec(sql, referObjectIds, referObjectType).Error
  178. return
  179. }
  180. func DeleteEdbRelationByObjectId(referObjectId int, referObjectType int) (err error) {
  181. o := global.DmSQL["data"]
  182. sql := ` DELETE FROM edb_info_relation WHERE refer_object_id =? AND refer_object_type=?`
  183. err = o.Exec(sql, referObjectId, referObjectType).Error
  184. return
  185. }
  186. type BaseRelationEdbInfo struct {
  187. EdbInfoId int
  188. ClassifyId int `description:"指标分类id"`
  189. EdbName string `description:"指标名称"`
  190. EdbCode string `description:"指标编码"`
  191. SysUserId int `description:"创建人id"`
  192. SysUserRealName string `description:"创建人姓名"`
  193. Frequency string `description:"频度"`
  194. IsStop int `description:"是否停更:1:停更,0:未停更"`
  195. IsSupplierStop int `description:"是否供应商停更:1:停更,0:未停更"`
  196. RelationNum int `description:"引用次数"`
  197. RelationTime string `description:"引用时间"`
  198. }
  199. type BaseRelationEdbInfoResp struct {
  200. Paging *paging.PagingItem
  201. List []*BaseRelationEdbInfo
  202. }
  203. type EdbInfoRelationDetail struct {
  204. EdbInfoRelationId int `orm:"column(edb_info_relation_id);pk" gorm:"primaryKey" `
  205. EdbInfoId int `description:"指标id"`
  206. ReferObjectId int `description:"引用对象ID(图表ID,ETA逻辑ID等)"`
  207. ReferObjectTypeName string `description:"引用对象类型"`
  208. ReferObjectType int `description:"引用对象ID类型(1.图表,2.ETA逻辑)"`
  209. ReferObjectSubType int `description:"引用对象子类"`
  210. RelationTime string `description:"引用时间"`
  211. ReferObjectName string `description:"引用对象名称"`
  212. }
  213. type BaseRelationEdbInfoDetailResp struct {
  214. Paging *paging.PagingItem
  215. List []*EdbInfoRelationDetail
  216. }
  217. func GetEdbInfoRelationList(condition string, pars []interface{}, addFieldStr, joinTableStr, orderBy string, startSize, pageSize int) (total int, items []*BaseRelationEdbInfo, err error) {
  218. o := global.DmSQL["data"]
  219. totalSql := ` SELECT count(1) FROM edb_info e LEFT JOIN (
  220. 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 `
  221. if joinTableStr != "" {
  222. totalSql += joinTableStr
  223. }
  224. totalSql += ` WHERE 1=1 `
  225. if condition != "" {
  226. totalSql += condition
  227. }
  228. err = o.Raw(totalSql, pars...).Scan(&total).Error
  229. if err != nil {
  230. return
  231. }
  232. fieldStr := ` 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 ` + addFieldStr
  233. sql := ` SELECT ` + fieldStr + ` from edb_info e LEFT JOIN (
  234. 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 `
  235. if joinTableStr != "" {
  236. sql += joinTableStr
  237. }
  238. sql += ` WHERE 1=1 `
  239. if condition != "" {
  240. sql += condition
  241. }
  242. if orderBy != "" {
  243. sql += ` ORDER BY ` + orderBy
  244. } else {
  245. sql += ` ORDER BY edb_info_id ASC `
  246. }
  247. sql += ` LIMIT ?,? `
  248. pars = append(pars, startSize)
  249. pars = append(pars, pageSize)
  250. err = o.Raw(sql, pars...).Scan(&items).Error
  251. return
  252. }
  253. func GetEdbInfoRelationDetailList(edbInfoId int, startSize, pageSize int) (total int, items []*EdbInfoRelation, err error) {
  254. o := global.DmSQL["data"]
  255. totalSql := ` SELECT count(*) FROM edb_info_relation where edb_info_id=?`
  256. err = o.Raw(totalSql, edbInfoId).Scan(&total).Error
  257. if err != nil {
  258. return
  259. }
  260. sql := ` SELECT * FROM edb_info_relation where edb_info_id=? ORDER BY relation_time desc, edb_info_id ASC `
  261. sql += ` LIMIT ?,? `
  262. err = o.Raw(sql, edbInfoId, startSize, pageSize).Find(&items).Error
  263. return
  264. }
  265. func GetReplaceChildEdbInfoRelationTotal(edbInfoId int) (total int, err error) {
  266. o := global.DmSQL["data"]
  267. totalSql := ` SELECT count(*) FROM edb_info_relation where relation_type=1 and (child_edb_info_id=? or edb_info_id=? ) group by parent_relation_id`
  268. err = o.Raw(totalSql, edbInfoId, edbInfoId).Scan(&total).Error
  269. if err != nil {
  270. return
  271. }
  272. return
  273. }
  274. func GetReplaceChildEdbInfoRelationList(edbInfoId int, startSize, pageSize int) (items []*EdbInfoRelation, err error) {
  275. o := global.DmSQL["data"]
  276. sql := ` SELECT * FROM edb_info_relation where relation_type=1 and (child_edb_info_id=? or edb_info_id=? ) group by parent_relation_id ORDER BY edb_info_relation_id ASC LIMIT ?,? `
  277. err = o.Raw(sql, edbInfoId, edbInfoId, startSize, pageSize).Scan(&items).Error
  278. return
  279. }
  280. func GetReplaceEdbInfoRelationTotal(edbInfoId int) (total int, err error) {
  281. o := global.DmSQL["data"]
  282. totalSql := ` SELECT count(*) FROM edb_info_relation where edb_info_id=? and relation_type = 0`
  283. err = o.Raw(totalSql, edbInfoId).Scan(&total).Error
  284. if err != nil {
  285. return
  286. }
  287. return
  288. }
  289. func GetReplaceEdbInfoRelationList(edbInfoId int, startSize, pageSize int) (items []*EdbInfoRelation, err error) {
  290. o := global.DmSQL["data"]
  291. sql := ` SELECT * FROM edb_info_relation where edb_info_id=? and relation_type = 0 ORDER BY edb_info_relation_id ASC LIMIT ?,? `
  292. err = o.Raw(sql, edbInfoId, startSize, pageSize).Scan(&items).Error
  293. return
  294. }
  295. func ReplaceRelationEdbInfoId(oldEdbInfo, newEdbInfo *EdbInfo, edbRelationIds []int, relationList []*EdbInfoRelation, refreshEdbInfoIds []int, indexCodeList []string) (err error) {
  296. o := global.DmSQL["data"].Begin()
  297. defer func() {
  298. if err != nil {
  299. _ = o.Rollback()
  300. return
  301. }
  302. _ = o.Commit()
  303. }()
  304. now := time.Now()
  305. sql := ` DELETE FROM edb_info_relation WHERE root_edb_info_id=? and relation_type=1 and parent_relation_id in (` + utils.GetOrmInReplace(len(edbRelationIds)) + `)`
  306. err = o.Exec(sql, oldEdbInfo.EdbInfoId, edbRelationIds).Error
  307. if err != nil {
  308. return
  309. }
  310. sourceWhere := ` and (refer_object_type in (1,2 ) or (refer_object_type=4 and refer_object_sub_type !=5) )` //平衡表和事件日历中的直接引用无需替换,
  311. sql = ` UPDATE edb_info_relation SET edb_info_id=?, source=?, edb_name=?, edb_code=?, modify_time=?, relation_time=? WHERE edb_info_id=? ` + sourceWhere + ` and relation_type=0 and edb_info_relation_id in (` + utils.GetOrmInReplace(len(edbRelationIds)) + `)`
  312. err = o.Exec(sql, newEdbInfo.EdbInfoId, newEdbInfo.Source, newEdbInfo.EdbName, newEdbInfo.EdbCode, now, now, oldEdbInfo.EdbInfoId, edbRelationIds).Error
  313. if err != nil {
  314. return
  315. }
  316. sql = ` UPDATE edb_info_relation SET relation_code=CONCAT_WS("_", edb_info_id,refer_object_id,refer_object_type,refer_object_sub_type) WHERE relation_type=0 ` + sourceWhere + ` and edb_info_relation_id in (` + utils.GetOrmInReplace(len(edbRelationIds)) + `)`
  317. err = o.Exec(sql, edbRelationIds).Error
  318. if err != nil {
  319. return
  320. }
  321. relationCodesMap := make(map[string]struct{}, 0)
  322. if len(relationList) > 0 {
  323. for _, relation := range relationList {
  324. if relation.RelationType == 1 {
  325. relationCodesMap[relation.RelationCode] = struct{}{}
  326. }
  327. }
  328. err = o.CreateInBatches(relationList, utils.MultiAddNum).Error
  329. if err != nil {
  330. return
  331. }
  332. }
  333. if len(refreshEdbInfoIds) > 0 {
  334. sql := ` UPDATE edb_info SET no_update = 0 WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(refreshEdbInfoIds)) + `) AND no_update = 1`
  335. err = o.Exec(sql, refreshEdbInfoIds).Error
  336. if err != nil {
  337. return
  338. }
  339. }
  340. if len(indexCodeList) > 0 {
  341. sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = 0 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=1`
  342. err = o.Exec(sql, indexCodeList).Error
  343. if err != nil {
  344. return
  345. }
  346. }
  347. if len(relationList) > 0 {
  348. relationCodes := make([]string, 0)
  349. for relationCode := range relationCodesMap {
  350. relationCodes = append(relationCodes, relationCode)
  351. }
  352. if len(relationCodes) > 0 {
  353. sql := ` UPDATE edb_info_relation e1
  354. JOIN edb_info_relation e2 ON e1.relation_code = e2.relation_code
  355. SET e1.parent_relation_id = e2.edb_info_relation_id
  356. WHERE
  357. e1.relation_type = 1
  358. AND e2.relation_type = 0 AND e1.parent_relation_id !=e2.edb_info_relation_id AND e1.relation_code in (` + utils.GetOrmInReplace(len(relationCodes)) + `)`
  359. err = o.Exec(sql, relationCodes).Error
  360. if err != nil {
  361. return
  362. }
  363. }
  364. }
  365. return
  366. }
  367. func UpdateSecondRelationEdbInfoId(edbRelationIds []int, relationList []*EdbInfoRelation, refreshEdbInfoIds []int, indexCodeList []string) (err error) {
  368. o := global.DmSQL["data"].Begin()
  369. defer func() {
  370. if err != nil {
  371. _ = o.Rollback()
  372. return
  373. }
  374. _ = o.Commit()
  375. }()
  376. sql := ` DELETE FROM edb_info_relation WHERE relation_type=1 and parent_relation_id in (` + utils.GetOrmInReplace(len(edbRelationIds)) + `)`
  377. err = o.Exec(sql, edbRelationIds).Error
  378. if err != nil {
  379. return
  380. }
  381. relationCodesMap := make(map[string]struct{}, 0)
  382. if len(relationList) > 0 {
  383. for _, relation := range relationList {
  384. if relation.RelationType == 1 {
  385. relationCodesMap[relation.RelationCode] = struct{}{}
  386. }
  387. }
  388. err = o.CreateInBatches(relationList, utils.MultiAddNum).Error
  389. if err != nil {
  390. return
  391. }
  392. }
  393. if len(refreshEdbInfoIds) > 0 {
  394. sql := ` UPDATE edb_info SET no_update = 0 WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(refreshEdbInfoIds)) + `) AND no_update = 1`
  395. err = o.Exec(sql, refreshEdbInfoIds).Error
  396. if err != nil {
  397. return
  398. }
  399. }
  400. if len(indexCodeList) > 0 {
  401. sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = 0 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=1`
  402. err = o.Exec(sql, indexCodeList).Error
  403. if err != nil {
  404. return
  405. }
  406. }
  407. if len(relationList) > 0 {
  408. relationCodes := make([]string, 0)
  409. for relationCode := range relationCodesMap {
  410. relationCodes = append(relationCodes, relationCode)
  411. }
  412. if len(relationCodes) > 0 {
  413. sql := ` UPDATE edb_info_relation e1
  414. JOIN edb_info_relation e2 ON e1.relation_code = e2.relation_code
  415. SET e1.parent_relation_id = e2.edb_info_relation_id
  416. WHERE
  417. e1.relation_type = 1
  418. AND e2.relation_type = 0 AND e1.parent_relation_id !=e2.edb_info_relation_id AND e1.relation_code in (` + utils.GetOrmInReplace(len(relationCodes)) + `)`
  419. err = o.Exec(sql, relationCodes).Error
  420. if err != nil {
  421. return
  422. }
  423. }
  424. }
  425. return
  426. }