flow_resource.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/log"
  7. )
  8. // FRByFlow .
  9. func (d *Dao) FRByFlow(c context.Context, flowID []int64) (fr *net.FlowResource, err error) {
  10. fr = &net.FlowResource{}
  11. if err = d.orm.Where("flow_id in (?)", flowID).First(fr).Error; err == gorm.ErrRecordNotFound {
  12. fr = nil
  13. err = nil
  14. return
  15. }
  16. if err != nil {
  17. log.Error("FRByFlow error(%v) flowid(%v)", err, flowID)
  18. }
  19. return
  20. }
  21. // FRByNetRID .
  22. func (d *Dao) FRByNetRID(c context.Context, netID []int64, rids []int64, onlyRunning bool) (frs []*net.FlowResource, err error) {
  23. frs = []*net.FlowResource{}
  24. db := d.orm.Where("rid in (?) AND net_id in (?)", rids, netID)
  25. if onlyRunning {
  26. db = db.Scopes(running)
  27. }
  28. if err = db.Find(&frs).Error; err != nil {
  29. log.Error("FRByNetRID error(%v) netid(%v) rids(%v)", err, netID, rids)
  30. }
  31. return
  32. }
  33. // FRByUniques .
  34. func (d *Dao) FRByUniques(c context.Context, rids []int64, flowID []int64, onlyRunning bool) (frs []*net.FlowResource, err error) {
  35. frs = []*net.FlowResource{}
  36. db := d.orm
  37. if len(rids) > 0 {
  38. db = db.Where("rid in (?)", rids)
  39. }
  40. if len(flowID) > 0 {
  41. db = db.Where("flow_id in (?)", flowID)
  42. }
  43. if onlyRunning {
  44. db = db.Scopes(running)
  45. }
  46. if err = db.Find(&frs).Error; err != nil {
  47. log.Error("FRByUniques error(%v) rids(%+v) flowid(%d)", err, rids, flowID)
  48. }
  49. return
  50. }
  51. // CancelFlowResource .
  52. func (d *Dao) CancelFlowResource(c context.Context, tx *gorm.DB, rids []int64) (err error) {
  53. fields := map[string]interface{}{"state": net.FRStateDeleted}
  54. if err = tx.Table(net.TableFlowResource).Where("rid in (?)", rids).Updates(fields).Error; err != nil {
  55. log.Error("CancelFlowResource error(%v) rids(%+v)", err, rids)
  56. }
  57. return
  58. }
  59. func running(db *gorm.DB) *gorm.DB {
  60. return db.Where("state!=?", net.FRStateDeleted)
  61. }