flow.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package gorm
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/jinzhu/gorm"
  6. "go-common/app/admin/main/aegis/model/net"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. // FlowByID .
  11. func (d *Dao) FlowByID(c context.Context, id int64) (f *net.Flow, err error) {
  12. f = &net.Flow{}
  13. err = d.orm.Where("id=?", id).First(f).Error
  14. if err == gorm.ErrRecordNotFound {
  15. err = ecode.AegisFlowNotFound
  16. return
  17. }
  18. if err != nil {
  19. log.Error("FlowByID(%+v) error(%v)", id, err)
  20. }
  21. return
  22. }
  23. // FlowList .
  24. func (d *Dao) FlowList(c context.Context, pm *net.ListNetElementParam) (result *net.ListFlowRes, err error) {
  25. result = &net.ListFlowRes{
  26. Pager: net.Pager{
  27. Num: pm.Pn,
  28. Size: pm.Ps,
  29. },
  30. }
  31. db := d.orm.Table(net.TableFlow).Where("net_id=?", pm.NetID)
  32. if len(pm.ID) > 0 {
  33. db = db.Where("id in (?)", pm.ID)
  34. }
  35. pm.Name = strings.TrimSpace(pm.Name)
  36. if pm.Name != "" {
  37. db = db.Where("name=?", pm.Name)
  38. }
  39. err = db.Scopes(state(pm.State)).Count(&result.Pager.Total).Scopes(pager(pm.Ps, pm.Pn, pm.Sort)).Find(&result.Result).Error
  40. if err != nil {
  41. log.Error("FlowList find error(%v) params(%+v)", err, pm)
  42. }
  43. return
  44. }
  45. // FlowByUnique .
  46. func (d *Dao) FlowByUnique(c context.Context, netID int64, name string) (f *net.Flow, err error) {
  47. f = &net.Flow{}
  48. err = d.orm.Where("net_id=? AND name=?", netID, name).First(f).Error
  49. if err == gorm.ErrRecordNotFound {
  50. err = nil
  51. f = nil
  52. return
  53. }
  54. if err != nil {
  55. log.Error("FlowByUnique(%d,%s) error(%v)", netID, name, err)
  56. }
  57. return
  58. }
  59. // Flows .
  60. func (d *Dao) Flows(c context.Context, ids []int64) (fs []*net.Flow, err error) {
  61. fs = []*net.Flow{}
  62. err = d.orm.Where("id in (?)", ids).Find(&fs).Error
  63. if err != nil {
  64. log.Error("Flows(%v) error(%v)", ids, err)
  65. }
  66. return
  67. }
  68. // FlowsByNet .
  69. func (d *Dao) FlowsByNet(c context.Context, netID []int64) (fs []*net.Flow, err error) {
  70. fs = []*net.Flow{}
  71. err = d.orm.Scopes(Available).Where("net_id in (?)", netID).Order("id ASC").Find(&fs).Error
  72. if err != nil {
  73. log.Error("Flows(%d) error(%v)", netID, err)
  74. }
  75. return
  76. }
  77. func (d *Dao) FlowIDByNet(c context.Context, nid []int64) (res map[int64][]int64, err error) {
  78. res = map[int64][]int64{}
  79. listi := []struct {
  80. ID int64 `gorm:"column:id"`
  81. NetID int64 `gorm:"column:net_id"`
  82. }{}
  83. if err = d.orm.Table(net.TableFlow).Select("id,net_id").
  84. Where("net_id in (?)", nid).Scopes(Available).Scan(&listi).Error; err != nil {
  85. log.Error("FlowIDByNet error(%v) nid(%v)", err, nid)
  86. return
  87. }
  88. for _, item := range listi {
  89. res[item.NetID] = append(res[item.NetID], item.ID)
  90. }
  91. return
  92. }