net.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package gorm
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/aegis/model/net"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "github.com/jinzhu/gorm"
  9. )
  10. // NetByID .
  11. func (d *Dao) NetByID(c context.Context, id int64) (n *net.Net, err error) {
  12. n = &net.Net{}
  13. err = d.orm.Where("id=?", id).First(n).Error
  14. if err == gorm.ErrRecordNotFound {
  15. err = ecode.NothingFound
  16. return
  17. }
  18. if err != nil {
  19. log.Error("NetByID(%+v) error(%v)", id, err)
  20. }
  21. return
  22. }
  23. func (d *Dao) Nets(c context.Context, ids []int64) (n []*net.Net, err error) {
  24. n = []*net.Net{}
  25. if err = d.orm.Where("id in (?)", ids).Find(&n).Error; err != nil {
  26. log.Error("Nets error(%v) ids(%v)", err, ids)
  27. }
  28. return
  29. }
  30. // NetByUnique .
  31. func (d *Dao) NetByUnique(c context.Context, name string) (n *net.Net, err error) {
  32. n = &net.Net{}
  33. err = d.orm.Where("ch_name=?", name).First(n).Error
  34. if err == gorm.ErrRecordNotFound {
  35. err = nil
  36. n = nil
  37. return
  38. }
  39. if err != nil {
  40. log.Error("NetByUnique(%+v) error(%v)", name, err)
  41. }
  42. return
  43. }
  44. // NetList .
  45. func (d *Dao) NetList(c context.Context, pm *net.ListNetParam) (result *net.ListNetRes, err error) {
  46. result = &net.ListNetRes{
  47. Pager: net.Pager{
  48. Num: pm.Pn,
  49. Size: pm.Ps,
  50. },
  51. }
  52. db := d.orm.Table(net.TableNet)
  53. if len(pm.ID) > 0 {
  54. db = db.Where("id in (?)", pm.ID)
  55. }
  56. if pm.BusinessID > 0 {
  57. db = db.Where("business_id=?", pm.BusinessID)
  58. }
  59. err = db.Scopes(state(pm.State)).Count(&result.Pager.Total).Scopes(pager(pm.Ps, pm.Pn, pm.Sort)).Find(&result.Result).Error
  60. if err != nil {
  61. log.Error("NetList find error(%v) params(%+v)", err, pm)
  62. }
  63. return
  64. }
  65. // NetBindStartFlow .
  66. func (d *Dao) NetBindStartFlow(c context.Context, tx *gorm.DB, id int64, flowID int64) (err error) {
  67. if err = d.UpdateFields(c, tx, net.TableNet, id, map[string]interface{}{"start_flow_id": flowID}); err != nil {
  68. log.Error("NetBindStartFlow d.UpdateFields error(%v) id(%d) flowid(%d)", err, id, flowID)
  69. }
  70. return
  71. }
  72. // NetIDByBusiness .
  73. func (d *Dao) NetIDByBusiness(c context.Context, businessID []int64) (bizmap map[int64][]int64, err error) {
  74. res := []struct {
  75. ID int64 `gorm:"column:id"`
  76. BusinessID int64 `gorm:"column:business_id"`
  77. }{}
  78. list := []*net.Net{}
  79. bizmap = map[int64][]int64{}
  80. if err = d.orm.Select("id, business_id").Where("business_id in (?)", businessID).
  81. Scopes(Available).Find(&list).Scan(&res).Error; err != nil {
  82. return
  83. }
  84. for _, item := range res {
  85. bizmap[item.BusinessID] = append(bizmap[item.BusinessID], item.ID)
  86. }
  87. return
  88. }
  89. // NetsByBusiness .
  90. func (d *Dao) NetsByBusiness(c context.Context, businessID []int64, onlyAvailable bool) (list []*net.Net, err error) {
  91. list = []*net.Net{}
  92. db := d.orm
  93. if len(businessID) > 0 {
  94. db = db.Where("business_id in (?)", businessID)
  95. }
  96. if onlyAvailable {
  97. db = db.Scopes(Available)
  98. }
  99. if err = db.Find(&list).Error; err != nil {
  100. log.Error("NetsByBusiness(%v) error(%v) onlyavailable(%v)", businessID, err, onlyAvailable)
  101. }
  102. return
  103. }
  104. // DisableNet .
  105. func (d *Dao) DisableNet(c context.Context, tx *gorm.DB, id int64) (err error) {
  106. err = d.UpdateFields(c, tx, net.TableNet, id, map[string]interface{}{"disable_time": time.Now()})
  107. return
  108. }