transition.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/aegis/model"
  7. "go-common/app/admin/main/aegis/model/net"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "github.com/jinzhu/gorm"
  11. )
  12. func (s *Service) prepareBeforeTrigger(c context.Context, rids []int64, flowID, bizid int64) (err error) {
  13. var (
  14. enableDir []*net.Direction
  15. tids []int64
  16. transition []*net.Transition
  17. )
  18. //以flow为起点的有向线
  19. if enableDir, err = s.fetchFlowNextEnableDirs(c, flowID); err != nil {
  20. log.Error("prepareBeforeTrigger s.fetchFlowNextEnableDirs error(%v) flowid(%d) rids(%v)", err, flowID, rids)
  21. return
  22. }
  23. //flow下游没有变迁,属于正常情况
  24. if len(enableDir) == 0 {
  25. return
  26. }
  27. //所有可用有向线的变迁限制
  28. tids = []int64{}
  29. for _, dir := range enableDir {
  30. tids = append(tids, dir.TransitionID)
  31. }
  32. if transition, err = s.transitions(c, tids, true); err != nil {
  33. log.Error("prepareBeforeTrigger s.transitions(%+v) error(%v) flowid(%d) rids(%v)", tids, err, flowID, rids)
  34. return
  35. }
  36. for _, item := range transition {
  37. if item.Trigger != net.TriggerManual || item.Limit <= 0 {
  38. continue
  39. }
  40. for _, rid := range rids {
  41. s.sendCreateTaskMsg(c, rid, flowID, item.Limit, bizid)
  42. }
  43. }
  44. return
  45. }
  46. //ShowTransition .
  47. func (s *Service) ShowTransition(c context.Context, id int64) (r *net.ShowTransitionResult, err error) {
  48. var (
  49. t *net.Transition
  50. tks map[int64][]*net.TokenBind
  51. )
  52. if t, err = s.gorm.TransitionByID(c, id); err != nil {
  53. return
  54. }
  55. if tks, err = s.gorm.TokenBindByElement(c, []int64{id}, net.BindTranType, true); err != nil {
  56. return
  57. }
  58. r = &net.ShowTransitionResult{
  59. Transition: t,
  60. Tokens: tks[id],
  61. }
  62. return
  63. }
  64. //GetTranByNet .
  65. func (s *Service) GetTranByNet(c context.Context, netID int64) (result map[int64]string, err error) {
  66. var (
  67. trans []*net.Transition
  68. )
  69. result = map[int64]string{}
  70. if trans, err = s.gorm.TranByNet(c, netID, true); err != nil {
  71. log.Error("GetTranByNet s.gorm.TranByNet(%d) error(%v)", netID, err)
  72. return
  73. }
  74. for _, item := range trans {
  75. result[item.ID] = item.ChName
  76. }
  77. return
  78. }
  79. //GetTransitionList .
  80. func (s *Service) GetTransitionList(c context.Context, pm *net.ListNetElementParam) (result *net.ListTransitionRes, err error) {
  81. var (
  82. transitionID []int64
  83. tks map[int64][]*net.TokenBind
  84. uid = []int64{}
  85. unames map[int64]string
  86. )
  87. if result, err = s.gorm.TransitionList(c, pm); err != nil {
  88. return
  89. }
  90. if len(result.Result) == 0 {
  91. return
  92. }
  93. for _, item := range result.Result {
  94. transitionID = append(transitionID, item.ID)
  95. uid = append(uid, item.UID)
  96. }
  97. if tks, err = s.gorm.TokenBindByElement(c, transitionID, net.BindTranType, true); err != nil {
  98. return
  99. }
  100. if unames, err = s.http.GetUnames(c, uid); err != nil {
  101. log.Error("GetTransitionList s.http.GetUnames error(%v)", err)
  102. err = nil
  103. }
  104. for _, item := range result.Result {
  105. item.Username = unames[item.UID]
  106. for _, bd := range tks[item.ID] {
  107. item.Tokens = append(item.Tokens, bd.ChName)
  108. }
  109. }
  110. return
  111. }
  112. func (s *Service) checkTransitionUnique(c context.Context, netID int64, name string) (err error, msg string) {
  113. var exist *net.Transition
  114. if exist, err = s.gorm.TransitionByUnique(c, netID, name); err != nil {
  115. log.Error("checkTransitionUnique s.gorm.TransitionByUnique(%d,%s) error(%v)", netID, name, err)
  116. return
  117. }
  118. if exist != nil {
  119. err = ecode.AegisUniqueAlreadyExist
  120. msg = fmt.Sprintf(ecode.AegisUniqueAlreadyExist.Message(), "变化", name)
  121. }
  122. return
  123. }
  124. //AddTransition .
  125. func (s *Service) AddTransition(c context.Context, uid int64, f *net.TransitionEditParam) (id int64, err error, msg string) {
  126. var (
  127. tx *gorm.DB
  128. diff = []string{}
  129. diffBind string
  130. )
  131. if err, msg = s.checkTransitionUnique(c, f.NetID, f.Name); err != nil {
  132. return
  133. }
  134. tran := &net.Transition{
  135. NetID: f.NetID,
  136. Trigger: f.Trigger,
  137. Limit: f.Limit,
  138. Name: f.Name,
  139. ChName: f.ChName,
  140. Description: f.Description,
  141. UID: uid,
  142. }
  143. //db update
  144. tx, err = s.gorm.BeginTx(c)
  145. if err != nil {
  146. log.Error("AddTransition s.gorm.BeginTx error(%v)", err)
  147. return
  148. }
  149. if err = s.gorm.AddItem(c, tx, tran); err != nil {
  150. tx.Rollback()
  151. return
  152. }
  153. if diffBind, _, err, msg = s.compareTranBind(c, tx, tran.ID, f.TokenList, false); err != nil {
  154. log.Error("AddTransition s.compareTranBind error(%v) params(%+v)", err, f)
  155. tx.Rollback()
  156. return
  157. }
  158. if diffBind != "" {
  159. diff = append(diff, diffBind)
  160. }
  161. if err = tx.Commit().Error; err != nil {
  162. log.Error("AddTransition tx.Commit error(%v)", err)
  163. return
  164. }
  165. id = tran.ID
  166. //日志
  167. diff = append(diff, model.LogFieldTemp(model.LogFieldChName, f.ChName, "", false))
  168. diff = append(diff, model.LogFieldTemp(model.LogFieldName, f.Name, "", false))
  169. diff = append(diff, model.LogFieldTemp(model.LogFieldLimit, f.Limit, "", false))
  170. diff = append(diff, model.LogFieldTemp(model.LogFieldTrigger, net.TriggerDesc[f.Trigger], "", false))
  171. oper := &model.NetConfOper{
  172. OID: tran.ID,
  173. Action: model.LogNetActionNew,
  174. UID: tran.UID,
  175. NetID: tran.NetID,
  176. ChName: tran.ChName,
  177. TranID: tran.ID,
  178. Diff: diff,
  179. }
  180. s.sendNetConfLog(c, model.LogTypeTranConf, oper)
  181. return
  182. }
  183. //UpdateTransition .
  184. func (s *Service) UpdateTransition(c context.Context, uid int64, f *net.TransitionEditParam) (err error, msg string) {
  185. var (
  186. old *net.Transition
  187. updates = map[string]interface{}{}
  188. tx *gorm.DB
  189. diff = []string{}
  190. diffBind string
  191. changedBind []int64
  192. )
  193. if old, err = s.gorm.TransitionByID(c, f.ID); err != nil {
  194. log.Error("UpdateTransition s.gorm.TransitionByID(%d) error(%v)", f.ID, err)
  195. return
  196. }
  197. if f.Name != old.Name {
  198. if err, msg = s.checkTransitionUnique(c, f.NetID, f.Name); err != nil {
  199. return
  200. }
  201. diff = append(diff, model.LogFieldTemp(model.LogFieldName, f.Name, old.Name, true))
  202. old.Name = f.Name
  203. updates["name"] = f.Name
  204. }
  205. if f.ChName != old.ChName {
  206. diff = append(diff, model.LogFieldTemp(model.LogFieldChName, f.ChName, old.ChName, true))
  207. old.ChName = f.ChName
  208. updates["ch_name"] = f.ChName
  209. }
  210. if f.Description != old.Description {
  211. old.Description = f.Description
  212. updates["description"] = f.Description
  213. }
  214. if f.Limit != old.Limit {
  215. diff = append(diff, model.LogFieldTemp(model.LogFieldLimit, f.Limit, old.Limit, true))
  216. old.Limit = f.Limit
  217. updates["limit"] = f.Limit
  218. }
  219. if f.Trigger != old.Trigger {
  220. diff = append(diff, model.LogFieldTemp(model.LogFieldTrigger, net.TriggerDesc[f.Trigger], net.TriggerDesc[old.Trigger], true))
  221. old.Trigger = f.Trigger
  222. updates["trigger"] = f.Trigger
  223. }
  224. //db update
  225. tx, err = s.gorm.BeginTx(c)
  226. if err != nil {
  227. log.Error("UpdateTransition s.gorm.BeginTx error(%v)", err)
  228. return
  229. }
  230. if len(updates) > 0 {
  231. if err = s.gorm.UpdateFields(c, tx, net.TableTransition, old.ID, updates); err != nil {
  232. tx.Rollback()
  233. return
  234. }
  235. }
  236. if diffBind, changedBind, err, msg = s.compareTranBind(c, tx, old.ID, f.TokenList, true); err != nil {
  237. log.Error("UpdateTransition s.compareTranBind error(%v) params(%+v)", err, f)
  238. tx.Rollback()
  239. return
  240. }
  241. if diffBind != "" {
  242. diff = append(diff, diffBind)
  243. }
  244. if err = tx.Commit().Error; err != nil {
  245. log.Error("UpdateTransition tx.Commit error(%v)", err)
  246. return
  247. }
  248. s.delTranCache(c, old, changedBind)
  249. //日志
  250. if len(diff) == 0 {
  251. return
  252. }
  253. oper := &model.NetConfOper{
  254. OID: old.ID,
  255. Action: model.LogNetActionUpdate,
  256. UID: uid,
  257. NetID: old.NetID,
  258. ChName: old.ChName,
  259. TranID: old.ID,
  260. Diff: diff,
  261. }
  262. s.sendNetConfLog(c, model.LogTypeTranConf, oper)
  263. return
  264. }
  265. //SwitchTransition .
  266. func (s *Service) SwitchTransition(c context.Context, id int64, needDisable bool) (err error) {
  267. var (
  268. old *net.Transition
  269. dirs []*net.Direction
  270. action string
  271. )
  272. if old, err = s.gorm.TransitionByID(c, id); err != nil {
  273. log.Error("SwitchTransition s.gorm.TransitionByID(%d) error(%v) needDisable(%v)", id, err, needDisable)
  274. return
  275. }
  276. available := old.IsAvailable()
  277. if available == !needDisable {
  278. return
  279. }
  280. if needDisable {
  281. if dirs, err = s.gorm.DirectionByTransitionID(c, []int64{id}, 0, true); err != nil {
  282. log.Error("SwitchTransition s.gorm.DirectionByTransitionID(%d) error(%v)", id, err)
  283. return
  284. }
  285. if len(dirs) > 0 {
  286. log.Error("SwitchTransition dir by transition(%d) founded", id)
  287. err = ecode.AegisTranBinded
  288. return
  289. }
  290. old.DisableTime = time.Now()
  291. action = model.LogNetActionDisable
  292. } else {
  293. old.DisableTime = net.Recovered
  294. action = model.LogNetActionAvailable
  295. }
  296. if err = s.gorm.UpdateFields(c, nil, net.TableTransition, id, map[string]interface{}{"disable_time": old.DisableTime}); err != nil {
  297. return
  298. }
  299. s.delTranCache(c, old, nil)
  300. //日志
  301. oper := &model.NetConfOper{
  302. OID: old.ID,
  303. Action: action,
  304. UID: old.UID,
  305. NetID: old.NetID,
  306. ChName: old.ChName,
  307. TranID: old.ID,
  308. }
  309. s.sendNetConfLog(c, model.LogTypeTranConf, oper)
  310. return
  311. }