direction.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package gorm
  2. import (
  3. "context"
  4. "github.com/jinzhu/gorm"
  5. "go-common/app/admin/main/aegis/model/net"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. // DirectionByFlowID .
  10. func (d *Dao) DirectionByFlowID(c context.Context, flowID []int64, direction int8) (dirs []*net.Direction, err error) {
  11. dirs = []*net.Direction{}
  12. db := d.orm.Where("flow_id in (?)", flowID)
  13. if direction == net.DirInput || direction == net.DirOutput {
  14. db = db.Where("direction=?", direction)
  15. }
  16. err = db.Scopes(Available).Find(&dirs).Error
  17. if err != nil {
  18. log.Error("DirectionByFlowID find error(%v) flowid(%v) direction(%d)", err, flowID, direction)
  19. }
  20. return
  21. }
  22. // DirectionByTransitionID .
  23. func (d *Dao) DirectionByTransitionID(c context.Context, transitionID []int64, direction int8, onlyAvailable bool) (dirs []*net.Direction, err error) {
  24. dirs = []*net.Direction{}
  25. db := d.orm.Where("transition_id in (?)", transitionID)
  26. if direction == net.DirInput || direction == net.DirOutput {
  27. db = db.Where("direction=?", direction)
  28. }
  29. if onlyAvailable {
  30. db = db.Scopes(Available)
  31. }
  32. err = db.Find(&dirs).Error
  33. if err != nil {
  34. log.Error("DirectionByTransitionID find error(%v) transitionid(%v) direction(%d)", err, transitionID, direction)
  35. }
  36. return
  37. }
  38. // DirectionByID .
  39. func (d *Dao) DirectionByID(c context.Context, id int64) (n *net.Direction, err error) {
  40. n = &net.Direction{}
  41. err = d.orm.Where("id=?", id).First(n).Error
  42. if err == gorm.ErrRecordNotFound {
  43. err = ecode.NothingFound
  44. return
  45. }
  46. if err != nil {
  47. log.Error("DirectionByID(%+v) error(%v)", id, err)
  48. }
  49. return
  50. }
  51. func (d *Dao) Directions(c context.Context, ids []int64) (n []*net.Direction, err error) {
  52. n = []*net.Direction{}
  53. if err = d.orm.Where("id in (?)", ids).Find(&n).Error; err != nil {
  54. log.Error("Directions error(%v) ids(%v)", err, ids)
  55. }
  56. return
  57. }
  58. // DirectionList .
  59. func (d *Dao) DirectionList(c context.Context, pm *net.ListDirectionParam) (result *net.ListDirectionRes, err error) {
  60. result = &net.ListDirectionRes{
  61. Pager: net.Pager{
  62. Num: pm.Pn,
  63. Size: pm.Ps,
  64. },
  65. }
  66. db := d.orm.Table(net.TableDirection).Where("net_id=?", pm.NetID)
  67. if len(pm.ID) > 0 {
  68. db = db.Where("id in (?)", pm.ID)
  69. }
  70. if pm.FlowID > 0 {
  71. db = db.Where("flow_id=?", pm.FlowID)
  72. }
  73. if pm.TransitionID > 0 {
  74. db = db.Where("transition_id=?", pm.TransitionID)
  75. }
  76. if pm.Direction == net.DirInput || pm.Direction == net.DirOutput {
  77. db = db.Where("direction=?", pm.Direction)
  78. }
  79. err = db.Scopes(state(pm.State)).Count(&result.Pager.Total).Scopes(pager(pm.Ps, pm.Pn, pm.Sort)).Find(&result.Result).Error
  80. if err != nil {
  81. log.Error("DirectionList find error(%v) params(%+v)", err, pm)
  82. }
  83. return
  84. }
  85. // DirectionByUnique .
  86. func (d *Dao) DirectionByUnique(c context.Context, netID int64, flowID int64, transitionID int64, direction int8) (t *net.Direction, err error) {
  87. t = &net.Direction{}
  88. err = d.orm.Where("net_id=? AND flow_id=? AND transition_id=? AND direction=?", netID, flowID, transitionID, direction).
  89. First(t).Error
  90. if err == gorm.ErrRecordNotFound {
  91. err = nil
  92. t = nil
  93. return
  94. }
  95. if err != nil {
  96. log.Error("DirectionByUnique(%d,%d,%d,%d) error(%v)", netID, flowID, transitionID, direction, err)
  97. }
  98. return
  99. }
  100. // DirectionByNet .
  101. func (d *Dao) DirectionByNet(c context.Context, netID int64) (n []*net.Direction, err error) {
  102. n = []*net.Direction{}
  103. err = d.orm.Where("net_id=?", netID).Scopes(Available).Find(&n).Error
  104. if err != nil {
  105. log.Error("DirectionByNet(%d) error(%v)", netID, err)
  106. }
  107. return
  108. }