net.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/jinzhu/gorm"
  7. "go-common/app/admin/main/aegis/model"
  8. "go-common/app/admin/main/aegis/model/net"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. //GetNetList .
  13. func (s *Service) GetNetList(c context.Context, pm *net.ListNetParam) (result *net.ListNetRes, err error) {
  14. var (
  15. unames map[int64]string
  16. )
  17. if result, err = s.gorm.NetList(c, pm); err != nil {
  18. return
  19. }
  20. if len(result.Result) == 0 {
  21. return
  22. }
  23. //get username
  24. uid := []int64{}
  25. for _, item := range result.Result {
  26. if item.UID > 0 {
  27. uid = append(uid, item.UID)
  28. }
  29. }
  30. if len(uid) == 0 {
  31. return
  32. }
  33. if unames, err = s.http.GetUnames(c, uid); err != nil || len(unames) == 0 {
  34. log.Error("GetNetList s.http.GetUnames error(%v) or empty uid(%+v)", err, uid)
  35. err = nil
  36. }
  37. for _, item := range result.Result {
  38. item.Username = unames[item.UID]
  39. }
  40. return
  41. }
  42. //GetNetByBusiness .
  43. func (s *Service) GetNetByBusiness(c context.Context, businessID int64) (res map[int64]string, err error) {
  44. var (
  45. list []*net.Net
  46. )
  47. res = map[int64]string{}
  48. if list, err = s.gorm.NetsByBusiness(c, []int64{businessID}, true); err != nil {
  49. log.Error("GetNetByBusiness s.gorm.NetsByBusiness(%d) error(%v)", businessID, err)
  50. return
  51. }
  52. for _, item := range list {
  53. res[item.ID] = item.ChName
  54. }
  55. return
  56. }
  57. //ShowNet .
  58. func (s *Service) ShowNet(c context.Context, id int64) (r *net.Net, err error) {
  59. if r, err = s.gorm.NetByID(c, id); err != nil {
  60. log.Error("ShowNet s.gorm.NetByID(%d) error(%v)", id, err)
  61. }
  62. return
  63. }
  64. func (s *Service) netCheckUnique(c context.Context, chName string) (err error, msg string) {
  65. var exist *net.Net
  66. if exist, err = s.gorm.NetByUnique(c, chName); err != nil && err != gorm.ErrRecordNotFound {
  67. log.Error("netCheckUnique s.gorm.NetByID(%s) error(%v)", chName, err)
  68. return
  69. }
  70. if exist != nil {
  71. err = ecode.AegisUniqueAlreadyExist
  72. msg = fmt.Sprintf(ecode.AegisUniqueAlreadyExist.Message(), "网", chName)
  73. }
  74. return
  75. }
  76. //AddNet .
  77. func (s *Service) AddNet(c context.Context, n *net.Net) (id int64, err error, msg string) {
  78. if err, msg = s.netCheckUnique(c, n.ChName); err != nil {
  79. return
  80. }
  81. //网初建为禁用状态
  82. n.DisableTime = time.Now()
  83. if err = s.gorm.AddItem(c, nil, n); err != nil {
  84. return
  85. }
  86. id = n.ID
  87. //日志
  88. oper := &model.NetConfOper{
  89. OID: n.ID,
  90. Action: model.LogNetActionNew,
  91. UID: n.UID,
  92. NetID: n.ID,
  93. ChName: n.ChName,
  94. FlowID: n.StartFlowID,
  95. Diff: []string{model.LogFieldTemp(model.LogFieldPID, n.PID, 0, false)},
  96. }
  97. s.sendNetConfLog(c, model.LogTypeNetConf, oper)
  98. return
  99. }
  100. //UpdateNet .
  101. func (s *Service) UpdateNet(c context.Context, uid int64, n *net.NetEditParam) (err error, msg string) {
  102. var (
  103. old *net.Net
  104. updates = map[string]interface{}{}
  105. diff = []string{}
  106. )
  107. if old, err = s.gorm.NetByID(c, n.ID); err != nil {
  108. log.Error("UpdateNet s.gorm.NetByID(%d) error(%v)", n.ID, err)
  109. return
  110. }
  111. if n.ChName != old.ChName {
  112. if err, msg = s.netCheckUnique(c, n.ChName); err != nil {
  113. return
  114. }
  115. diff = append(diff, model.LogFieldTemp(model.LogFieldChName, n.ChName, old.ChName, true))
  116. old.ChName = n.ChName
  117. updates["ch_name"] = n.ChName
  118. }
  119. if n.Description != old.Description {
  120. old.Description = n.Description
  121. updates["description"] = n.Description
  122. }
  123. if err = s.gorm.UpdateFields(c, nil, net.TableNet, old.ID, updates); err != nil {
  124. return
  125. }
  126. s.delNetCache(c, old)
  127. //日志
  128. if len(diff) == 0 {
  129. return
  130. }
  131. oper := &model.NetConfOper{
  132. OID: old.ID,
  133. Action: model.LogNetActionUpdate,
  134. UID: uid,
  135. NetID: old.ID,
  136. ChName: old.ChName,
  137. FlowID: old.StartFlowID,
  138. Diff: diff,
  139. }
  140. s.sendNetConfLog(c, model.LogTypeNetConf, oper)
  141. return
  142. }
  143. //SwitchNet .
  144. func (s *Service) SwitchNet(c context.Context, id int64, needDisable bool) (err error) {
  145. var (
  146. n *net.Net
  147. action string
  148. valid bool
  149. )
  150. if n, err = s.gorm.NetByID(c, id); err != nil {
  151. log.Error("SwitchNet s.gorm.NetByID(%d) error(%v) needDisable(%v)", id, err, needDisable)
  152. return
  153. }
  154. available := n.IsAvailable()
  155. if available == !needDisable {
  156. return
  157. }
  158. if needDisable {
  159. n.DisableTime = time.Now()
  160. action = model.LogNetActionDisable
  161. } else {
  162. if valid, err = s.checkNetValid(c, id); err != nil {
  163. log.Error("SwitchNet s.checkNetValid error(%v) id(%d)", err, id)
  164. return
  165. }
  166. if !valid {
  167. log.Error("SwitchNet id(%d) isn't valid, can't be available", id)
  168. err = ecode.AegisNetErr
  169. return
  170. }
  171. n.DisableTime = net.Recovered
  172. action = model.LogNetActionAvailable
  173. }
  174. if err = s.gorm.UpdateFields(c, nil, net.TableNet, id, map[string]interface{}{"disable_time": n.DisableTime}); err != nil {
  175. return
  176. }
  177. s.delNetCache(c, n)
  178. //日志
  179. oper := &model.NetConfOper{
  180. OID: n.ID,
  181. Action: action,
  182. UID: n.UID,
  183. NetID: n.ID,
  184. ChName: n.ChName,
  185. FlowID: n.StartFlowID,
  186. }
  187. s.sendNetConfLog(c, model.LogTypeNetConf, oper)
  188. return
  189. }
  190. //初步检查流程网的可用性:流转完整性
  191. func (s *Service) checkNetValid(c context.Context, netID int64) (valid bool, err error) {
  192. var (
  193. n *net.Net
  194. flow *net.Flow
  195. dirs []*net.Direction
  196. )
  197. if n, err = s.netByID(c, netID); err != nil || n == nil || n.StartFlowID <= 0 {
  198. log.Error("checkNetValid s.netByID(%d) error(%v)/not found/start_flow_id=0, net(%+v)", netID, err, n)
  199. return
  200. }
  201. if flow, err = s.flowByID(c, n.StartFlowID); err != nil || flow == nil || !flow.IsAvailable() {
  202. log.Error("checkNetValid s.flowByID(%d) error(%v)/flow not found/disabled, netid(%d), flow(%+v)", n.StartFlowID, err, netID, flow)
  203. return
  204. }
  205. if dirs, err = s.gorm.DirectionByNet(c, netID); err != nil {
  206. log.Error("checkNetValid s.gorm.DirectionByNet(%d) error(%v)", netID, err)
  207. return
  208. }
  209. tranPrevMap := map[int64][]int64{}
  210. tranNextMap := map[int64][]int64{}
  211. flowPrevMap := map[int64][]int64{}
  212. for _, item := range dirs {
  213. if item.Direction == net.DirInput {
  214. tranPrevMap[item.TransitionID] = append(tranPrevMap[item.TransitionID], item.FlowID)
  215. continue
  216. }
  217. flowPrevMap[item.FlowID] = append(flowPrevMap[item.FlowID], item.TransitionID)
  218. tranNextMap[item.TransitionID] = append(tranNextMap[item.TransitionID], item.FlowID)
  219. }
  220. /**
  221. flow next empty/trans---dir=input
  222. flow prev empty(start)/trans---dir=output
  223. tran next flows---dir=output
  224. tran prev flows---dir=input
  225. */
  226. for flowID, trans := range flowPrevMap {
  227. if len(trans) == 0 && flowID != n.StartFlowID {
  228. log.Error("checkNetValid flow(%d) no previous transition", flowID)
  229. return
  230. }
  231. }
  232. for tranID, flows := range tranPrevMap {
  233. prv := len(flows)
  234. nxt := len(tranNextMap[tranID])
  235. if prv == 0 || nxt == 0 {
  236. log.Error("checkNetValid transition(%d) no prev(%d)/next(%d) flow", tranID, prv, nxt)
  237. return
  238. }
  239. }
  240. for tranID, flows := range tranNextMap {
  241. prv := len(tranPrevMap[tranID])
  242. nxt := len(flows)
  243. if prv == 0 || nxt == 0 {
  244. log.Error("checkNetValid transition(%d) no prev(%d)/next(%d) flow", tranID, prv, nxt)
  245. return
  246. }
  247. }
  248. valid = true
  249. return
  250. }