edb_info_relation.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. }
  85. addList = append(addList, tmp)
  86. }
  87. }
  88. // 删除不再需要的引用关系
  89. for deleteId, _ := range deleteMap {
  90. deleteRelationIds = append(deleteRelationIds, deleteId)
  91. }
  92. //更新指标刷新状态为启用
  93. err = data_manage.AddOrUpdateEdbInfoRelation(addList, deleteRelationIds, refreshIds, indexCodeList)
  94. if err != nil {
  95. err = fmt.Errorf("删除不再需要的引用关系失败,%s", err.Error())
  96. return
  97. }
  98. return
  99. }
  100. // SaveSandBoxEdbInfoRelation 添加/编辑 eta逻辑图指标引用
  101. func SaveSandBoxEdbInfoRelation(sandBoxId int, sandBoxContent string) (err error) {
  102. edbInfoIds, err := sandbox.GetSandBoxEdbIdsByContent(sandBoxContent)
  103. if err != nil {
  104. return
  105. }
  106. if len(edbInfoIds) == 0 {
  107. return
  108. }
  109. //更新指标刷新状态为启用
  110. err = saveEdbInfoRelation(edbInfoIds, sandBoxId, utils.EDB_RELATION_SANDBOX, 0)
  111. return
  112. }
  113. // SaveCalendarEdbInfoRelation 添加/编辑 事件日历指标引用
  114. func SaveCalendarEdbInfoRelation(chartPermissionId int, matterDate string, editMatters, removeMatters []*fe_calendar.FeCalendarMatter) (err error) {
  115. //整理相关的事件ID
  116. matterIds := make([]int, 0)
  117. updateMatterMap := make(map[int]*fe_calendar.FeCalendarMatter)
  118. deleteMatterMap := make(map[int]struct{})
  119. for _, matter := range removeMatters {
  120. deleteMatterMap[matter.FeCalendarMatterId] = struct{}{}
  121. matterIds = append(matterIds, matter.FeCalendarMatterId)
  122. }
  123. for _, matter := range editMatters {
  124. updateMatterMap[matter.FeCalendarMatterId] = matter
  125. matterIds = append(matterIds, matter.FeCalendarMatterId)
  126. }
  127. //删除ID,先删除
  128. deleteRelationIds := make([]int, 0)
  129. if len(matterIds) > 0 {
  130. relationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(matterIds, utils.EDB_RELATION_CALENDAR)
  131. if e != nil {
  132. err = fmt.Errorf("查询事件日历指标引用失败,%s", e.Error())
  133. return
  134. }
  135. for _, relation := range relationList {
  136. if _, ok := deleteMatterMap[relation.ReferObjectId]; ok {
  137. deleteRelationIds = append(deleteRelationIds, relation.EdbInfoRelationId)
  138. }
  139. if newMatter, ok := updateMatterMap[relation.ReferObjectId]; ok {
  140. if relation.EdbInfoId != newMatter.EdbInfoId {
  141. deleteRelationIds = append(deleteRelationIds, relation.EdbInfoRelationId)
  142. }
  143. }
  144. }
  145. }
  146. if len(deleteRelationIds) > 0 {
  147. err = data_manage.DeleteEdbRelationByObjectIds(deleteRelationIds, utils.EDB_RELATION_CALENDAR)
  148. if err != nil {
  149. err = fmt.Errorf("删除事件日历指标引用失败,%s", err.Error())
  150. return
  151. }
  152. deleteRelationIds = make([]int, 0)
  153. }
  154. // 获取已有事项
  155. matterOb := new(fe_calendar.FeCalendarMatter)
  156. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, fe_calendar.FeCalendarMatterCols.ChartPermissionId, fe_calendar.FeCalendarMatterCols.MatterDate)
  157. pars := make([]interface{}, 0)
  158. pars = append(pars, chartPermissionId, matterDate)
  159. order := fmt.Sprintf(`%s ASC`, fe_calendar.FeCalendarMatterCols.Sort)
  160. matters, e := matterOb.GetItemsByCondition(cond, pars, []string{}, order)
  161. if e != nil {
  162. err = fmt.Errorf("查询事件日历事项失败,%s", e.Error())
  163. return
  164. }
  165. // 循环查询matters
  166. edbInfoIds := make([]int, 0)
  167. refreshIds := make([]int, 0)
  168. indexCodeList := make([]string, 0)
  169. newMatterIds := make([]int, 0)
  170. for _, matter := range matters {
  171. newMatterIds = append(newMatterIds, matter.FeCalendarMatterId)
  172. edbInfoIds = append(edbInfoIds, matter.EdbInfoId)
  173. }
  174. // 查询指标信息
  175. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  176. if e != nil {
  177. err = fmt.Errorf("查询指标信息失败,%s", e.Error())
  178. return
  179. }
  180. // 只统计钢联化工和wind来源的指标
  181. addEdbInfoIdMap := make(map[int]*data_manage.EdbInfo)
  182. for _, edbInfo := range edbInfoList {
  183. if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  184. continue
  185. }
  186. refreshIds = append(refreshIds, edbInfo.EdbInfoId)
  187. addEdbInfoIdMap[edbInfo.EdbInfoId] = edbInfo
  188. if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  189. indexCodeList = append(indexCodeList, edbInfo.EdbCode)
  190. }
  191. }
  192. relationMap := make(map[int]struct{})
  193. if len(newMatterIds) > 0 {
  194. //查询已有的matters
  195. relationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(newMatterIds, utils.EDB_RELATION_CALENDAR)
  196. if e != nil {
  197. err = fmt.Errorf("查询事件日历指标引用失败,%s", e.Error())
  198. return
  199. }
  200. for _, relation := range relationList {
  201. relationMap[relation.ReferObjectId] = struct{}{}
  202. }
  203. }
  204. addList := make([]*data_manage.EdbInfoRelation, 0)
  205. nowTime := time.Now()
  206. for _, matter := range matters {
  207. _, ok1 := relationMap[matter.FeCalendarMatterId]
  208. edbInfo, ok2 := addEdbInfoIdMap[matter.EdbInfoId]
  209. if !ok1 && ok2 {
  210. tmp := &data_manage.EdbInfoRelation{
  211. ReferObjectId: matter.FeCalendarMatterId,
  212. ReferObjectType: utils.EDB_RELATION_CALENDAR,
  213. ReferObjectSubType: 0,
  214. EdbInfoId: edbInfo.EdbInfoId,
  215. EdbName: edbInfo.EdbName,
  216. Source: edbInfo.Source,
  217. EdbCode: edbInfo.EdbCode,
  218. CreateTime: nowTime,
  219. ModifyTime: nowTime,
  220. }
  221. addList = append(addList, tmp)
  222. }
  223. }
  224. //更新指标刷新状态为启用
  225. err = data_manage.AddOrUpdateEdbInfoRelation(addList, deleteRelationIds, refreshIds, indexCodeList)
  226. if err != nil {
  227. err = fmt.Errorf("删除不再需要的引用关系失败,%s", err.Error())
  228. return
  229. }
  230. return
  231. }
  232. // GetEdbRelationList 获取指标引用列表
  233. func GetEdbRelationList(source int, classifyId, sysUserId, frequency, keyword, status string, startSize, pageSize int, sortParam, sortType string) (total int, list []*data_manage.BaseRelationEdbInfo, err error) {
  234. var pars []interface{}
  235. var condition string
  236. list = make([]*data_manage.BaseRelationEdbInfo, 0)
  237. isStop := -1
  238. if status == `暂停` {
  239. isStop = 1
  240. } else if status == "启用" {
  241. isStop = 0
  242. }
  243. switch source {
  244. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND:
  245. condition += ` AND e.source = ? `
  246. pars = append(pars, source)
  247. if isStop >= 0 {
  248. condition += " AND e.no_update = ? "
  249. pars = append(pars, isStop)
  250. }
  251. if classifyId != `` {
  252. classifyIdSlice := strings.Split(classifyId, ",")
  253. condition += ` AND e.classify_id IN (` + utils.GetOrmInReplace(len(classifyIdSlice)) + `)`
  254. pars = append(pars, classifyIdSlice)
  255. }
  256. if sysUserId != `` {
  257. sysUserIdSlice := strings.Split(sysUserId, ",")
  258. condition += ` AND e.sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
  259. pars = append(pars, sysUserIdSlice)
  260. }
  261. if frequency != `` {
  262. frequencySlice := strings.Split(frequency, ",")
  263. condition += ` AND e.frequency IN (` + utils.GetOrmInReplace(len(frequencySlice)) + `)`
  264. pars = append(pars, frequencySlice)
  265. }
  266. if keyword != `` {
  267. keywordSlice := strings.Split(keyword, " ")
  268. if len(keywordSlice) > 0 {
  269. tmpConditionSlice := make([]string, 0)
  270. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  271. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  272. for _, v := range keywordSlice {
  273. if v == ` ` || v == `` {
  274. continue
  275. }
  276. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  277. pars = utils.GetLikeKeywordPars(pars, v, 2)
  278. }
  279. condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
  280. } else {
  281. condition += ` AND (e.edb_name like ? or e.edb_code like ? )`
  282. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  283. }
  284. }
  285. sortStr := ``
  286. if sortParam != `` {
  287. sortStr = fmt.Sprintf("%s %s,e.edb_info_id desc ", sortParam, sortType)
  288. }
  289. total, list, err = data_manage.GetEdbInfoRelationList(condition, pars, sortStr, startSize, pageSize)
  290. }
  291. return
  292. }