edb_info_relation.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package data
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. "eta/eta_api/models/fe_calendar"
  5. "eta/eta_api/services/alarm_msg"
  6. "eta/eta_api/services/sandbox"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "strings"
  10. "time"
  11. )
  12. // SaveChartEdbInfoRelation 添加/编辑图表指标引用关联记录
  13. func SaveChartEdbInfoRelation(edbInfoIds []int, chartInfo *data_manage.ChartInfo) (err error) {
  14. //更新指标刷新状态为启用
  15. err = saveEdbInfoRelation(edbInfoIds, chartInfo.ChartInfoId, utils.EDB_RELATION_CHART, chartInfo.Source)
  16. return
  17. }
  18. // saveEdbInfoRelation 添加/编辑图表指标引用关联记录
  19. func saveEdbInfoRelation(edbInfoIds []int, objectId, objectType, objectSubType int) (err error) {
  20. // 实现添加引用记录的逻辑
  21. if len(edbInfoIds) == 0 {
  22. return
  23. }
  24. defer func() {
  25. if err != nil {
  26. tips := "实现添加引用记录的逻辑-添加/编辑图表指标引用关联记录失败, ErrMsg:\n" + err.Error()
  27. utils.FileLog.Info(tips)
  28. go alarm_msg.SendAlarmMsg(tips, 3)
  29. }
  30. }()
  31. refreshIds := make([]int, 0)
  32. indexCodeList := make([]string, 0)
  33. // 查询指标信息
  34. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  35. if e != nil {
  36. err = fmt.Errorf("查询指标信息失败,%s", e.Error())
  37. return
  38. }
  39. // 只统计钢联化工和wind来源的指标
  40. for _, edbInfo := range edbInfoList {
  41. if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  42. continue
  43. }
  44. refreshIds = append(refreshIds, edbInfo.EdbInfoId)
  45. if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  46. indexCodeList = append(indexCodeList, edbInfo.EdbCode)
  47. }
  48. }
  49. // 循转组装引用
  50. // 查询已有的引用关系
  51. existList, e := data_manage.GetEdbInfoRelationByReferObjectId(objectId, objectType)
  52. if e != nil {
  53. err = fmt.Errorf("查询已有的引用关系失败,%s", e.Error())
  54. return
  55. }
  56. deleteMap := make(map[int]bool)
  57. relationMap := make(map[int]bool)
  58. for _, exist := range existList {
  59. deleteMap[exist.EdbInfoId] = true
  60. relationMap[exist.EdbInfoId] = true
  61. }
  62. // 新增不存在的引用关系
  63. // 删除不再需要的引用关系
  64. nowTime := time.Now()
  65. addList := make([]*data_manage.EdbInfoRelation, 0)
  66. deleteRelationIds := make([]int, 0)
  67. for _, edbInfo := range edbInfoList {
  68. if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  69. continue
  70. }
  71. if _, ok := relationMap[edbInfo.EdbInfoId]; ok {
  72. delete(deleteMap, edbInfo.EdbInfoId)
  73. } else {
  74. tmp := &data_manage.EdbInfoRelation{
  75. ReferObjectId: objectId,
  76. ReferObjectType: objectType,
  77. ReferObjectSubType: objectSubType,
  78. EdbInfoId: edbInfo.EdbInfoId,
  79. EdbName: edbInfo.EdbName,
  80. Source: edbInfo.Source,
  81. EdbCode: edbInfo.EdbCode,
  82. CreateTime: nowTime,
  83. ModifyTime: nowTime,
  84. RelationTime: nowTime,
  85. }
  86. addList = append(addList, tmp)
  87. }
  88. }
  89. // 删除不再需要的引用关系
  90. for deleteId, _ := range deleteMap {
  91. deleteRelationIds = append(deleteRelationIds, deleteId)
  92. }
  93. //更新指标刷新状态为启用
  94. err = data_manage.AddOrUpdateEdbInfoRelation(addList, deleteRelationIds, refreshIds, indexCodeList)
  95. if err != nil {
  96. err = fmt.Errorf("删除不再需要的引用关系失败,%s", err.Error())
  97. return
  98. }
  99. return
  100. }
  101. // SaveSandBoxEdbInfoRelation 添加/编辑 eta逻辑图指标引用
  102. func SaveSandBoxEdbInfoRelation(sandBoxId int, sandBoxContent string) (err error) {
  103. edbInfoIds, err := sandbox.GetSandBoxEdbIdsByContent(sandBoxContent)
  104. if err != nil {
  105. return
  106. }
  107. if len(edbInfoIds) == 0 {
  108. return
  109. }
  110. //更新指标刷新状态为启用
  111. err = saveEdbInfoRelation(edbInfoIds, sandBoxId, utils.EDB_RELATION_SANDBOX, 0)
  112. return
  113. }
  114. // SaveCalendarEdbInfoRelation 添加/编辑 事件日历指标引用
  115. func SaveCalendarEdbInfoRelation(chartPermissionId int, matterDate string, editMatters, removeMatters []*fe_calendar.FeCalendarMatter) (err error) {
  116. //整理相关的事件ID
  117. matterIds := make([]int, 0)
  118. updateMatterMap := make(map[int]*fe_calendar.FeCalendarMatter)
  119. deleteMatterMap := make(map[int]struct{})
  120. for _, matter := range removeMatters {
  121. deleteMatterMap[matter.FeCalendarMatterId] = struct{}{}
  122. matterIds = append(matterIds, matter.FeCalendarMatterId)
  123. }
  124. for _, matter := range editMatters {
  125. updateMatterMap[matter.FeCalendarMatterId] = matter
  126. matterIds = append(matterIds, matter.FeCalendarMatterId)
  127. }
  128. //删除ID,先删除
  129. deleteRelationIds := make([]int, 0)
  130. if len(matterIds) > 0 {
  131. relationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(matterIds, utils.EDB_RELATION_CALENDAR)
  132. if e != nil {
  133. err = fmt.Errorf("查询事件日历指标引用失败,%s", e.Error())
  134. return
  135. }
  136. for _, relation := range relationList {
  137. if _, ok := deleteMatterMap[relation.ReferObjectId]; ok {
  138. deleteRelationIds = append(deleteRelationIds, relation.EdbInfoRelationId)
  139. }
  140. if newMatter, ok := updateMatterMap[relation.ReferObjectId]; ok {
  141. if relation.EdbInfoId != newMatter.EdbInfoId {
  142. deleteRelationIds = append(deleteRelationIds, relation.EdbInfoRelationId)
  143. }
  144. }
  145. }
  146. }
  147. if len(deleteRelationIds) > 0 {
  148. err = data_manage.DeleteEdbRelationByObjectIds(deleteRelationIds, utils.EDB_RELATION_CALENDAR)
  149. if err != nil {
  150. err = fmt.Errorf("删除事件日历指标引用失败,%s", err.Error())
  151. return
  152. }
  153. deleteRelationIds = make([]int, 0)
  154. }
  155. // 获取已有事项
  156. matterOb := new(fe_calendar.FeCalendarMatter)
  157. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, fe_calendar.FeCalendarMatterCols.ChartPermissionId, fe_calendar.FeCalendarMatterCols.MatterDate)
  158. pars := make([]interface{}, 0)
  159. pars = append(pars, chartPermissionId, matterDate)
  160. order := fmt.Sprintf(`%s ASC`, fe_calendar.FeCalendarMatterCols.Sort)
  161. matters, e := matterOb.GetItemsByCondition(cond, pars, []string{}, order)
  162. if e != nil {
  163. err = fmt.Errorf("查询事件日历事项失败,%s", e.Error())
  164. return
  165. }
  166. // 循环查询matters
  167. edbInfoIds := make([]int, 0)
  168. refreshIds := make([]int, 0)
  169. indexCodeList := make([]string, 0)
  170. newMatterIds := make([]int, 0)
  171. for _, matter := range matters {
  172. newMatterIds = append(newMatterIds, matter.FeCalendarMatterId)
  173. edbInfoIds = append(edbInfoIds, matter.EdbInfoId)
  174. }
  175. // 查询指标信息
  176. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  177. if e != nil {
  178. err = fmt.Errorf("查询指标信息失败,%s", e.Error())
  179. return
  180. }
  181. // 只统计钢联化工和wind来源的指标
  182. addEdbInfoIdMap := make(map[int]*data_manage.EdbInfo)
  183. for _, edbInfo := range edbInfoList {
  184. if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  185. continue
  186. }
  187. refreshIds = append(refreshIds, edbInfo.EdbInfoId)
  188. addEdbInfoIdMap[edbInfo.EdbInfoId] = edbInfo
  189. if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  190. indexCodeList = append(indexCodeList, edbInfo.EdbCode)
  191. }
  192. }
  193. relationMap := make(map[int]struct{})
  194. if len(newMatterIds) > 0 {
  195. //查询已有的matters
  196. relationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(newMatterIds, utils.EDB_RELATION_CALENDAR)
  197. if e != nil {
  198. err = fmt.Errorf("查询事件日历指标引用失败,%s", e.Error())
  199. return
  200. }
  201. for _, relation := range relationList {
  202. relationMap[relation.ReferObjectId] = struct{}{}
  203. }
  204. }
  205. addList := make([]*data_manage.EdbInfoRelation, 0)
  206. nowTime := time.Now()
  207. for _, matter := range matters {
  208. _, ok1 := relationMap[matter.FeCalendarMatterId]
  209. edbInfo, ok2 := addEdbInfoIdMap[matter.EdbInfoId]
  210. if !ok1 && ok2 {
  211. tmp := &data_manage.EdbInfoRelation{
  212. ReferObjectId: matter.FeCalendarMatterId,
  213. ReferObjectType: utils.EDB_RELATION_CALENDAR,
  214. ReferObjectSubType: 0,
  215. EdbInfoId: edbInfo.EdbInfoId,
  216. EdbName: edbInfo.EdbName,
  217. Source: edbInfo.Source,
  218. EdbCode: edbInfo.EdbCode,
  219. CreateTime: nowTime,
  220. ModifyTime: nowTime,
  221. }
  222. addList = append(addList, tmp)
  223. }
  224. }
  225. //更新指标刷新状态为启用
  226. err = data_manage.AddOrUpdateEdbInfoRelation(addList, deleteRelationIds, refreshIds, indexCodeList)
  227. if err != nil {
  228. err = fmt.Errorf("删除不再需要的引用关系失败,%s", err.Error())
  229. return
  230. }
  231. return
  232. }
  233. // GetEdbRelationList 获取指标引用列表
  234. func GetEdbRelationList(source int, classifyId, sysUserId, frequency, keyword, status string, startSize, pageSize int, sortParam, sortType string) (total int, list []*data_manage.BaseRelationEdbInfo, err error) {
  235. var pars []interface{}
  236. var condition string
  237. list = make([]*data_manage.BaseRelationEdbInfo, 0)
  238. isStop := -1
  239. if status == `暂停` {
  240. isStop = 1
  241. } else if status == "启用" {
  242. isStop = 0
  243. }
  244. switch source {
  245. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND:
  246. condition += ` AND e.source = ? `
  247. pars = append(pars, source)
  248. if isStop >= 0 {
  249. condition += " AND e.no_update = ? "
  250. pars = append(pars, isStop)
  251. }
  252. if classifyId != `` {
  253. classifyIdSlice := strings.Split(classifyId, ",")
  254. condition += ` AND e.classify_id IN (` + utils.GetOrmInReplace(len(classifyIdSlice)) + `)`
  255. pars = append(pars, classifyIdSlice)
  256. }
  257. if sysUserId != `` {
  258. sysUserIdSlice := strings.Split(sysUserId, ",")
  259. condition += ` AND e.sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
  260. pars = append(pars, sysUserIdSlice)
  261. }
  262. if frequency != `` {
  263. frequencySlice := strings.Split(frequency, ",")
  264. condition += ` AND e.frequency IN (` + utils.GetOrmInReplace(len(frequencySlice)) + `)`
  265. pars = append(pars, frequencySlice)
  266. }
  267. if keyword != `` {
  268. keywordSlice := strings.Split(keyword, " ")
  269. if len(keywordSlice) > 0 {
  270. tmpConditionSlice := make([]string, 0)
  271. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  272. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  273. for _, v := range keywordSlice {
  274. if v == ` ` || v == `` {
  275. continue
  276. }
  277. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  278. pars = utils.GetLikeKeywordPars(pars, v, 2)
  279. }
  280. condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
  281. } else {
  282. condition += ` AND (e.edb_name like ? or e.edb_code like ? )`
  283. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  284. }
  285. }
  286. sortStr := ``
  287. if sortParam != `` {
  288. sortStr = fmt.Sprintf("%s %s,e.edb_info_id desc ", sortParam, sortType)
  289. }
  290. total, list, err = data_manage.GetEdbInfoRelationList(condition, pars, sortStr, startSize, pageSize)
  291. }
  292. return
  293. }