token.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // TokenByID .
  11. func (d *Dao) TokenByID(c context.Context, id int64) (t *net.Token, err error) {
  12. t = &net.Token{}
  13. err = d.orm.Where("id=?", id).First(t).Error
  14. if err == gorm.ErrRecordNotFound {
  15. err = ecode.AegisTokenNotFound
  16. return
  17. }
  18. if err != nil {
  19. log.Error("TokenByID(%+v) error(%v)", id, err)
  20. }
  21. return
  22. }
  23. // Tokens .
  24. func (d *Dao) Tokens(c context.Context, ids []int64) (list []*net.Token, err error) {
  25. list = []*net.Token{}
  26. err = d.orm.Where("id in (?)", ids).Find(&list).Error
  27. if err != nil {
  28. log.Error("Tokens(%v) error(%v)", ids, err)
  29. }
  30. return
  31. }
  32. func (d *Dao) tokenListDB(netID []int64, id []int64, name string, onlyAssign bool) (db *gorm.DB) {
  33. db = d.orm.Table(net.TableToken).Where("net_id in (?)", netID)
  34. if len(id) > 0 {
  35. db = db.Where("id in (?)", id)
  36. }
  37. name = strings.TrimSpace(name)
  38. if name != "" {
  39. db = db.Where("name=?", name)
  40. }
  41. if onlyAssign {
  42. db = db.Where("compare=?", net.TokenCompareAssign)
  43. }
  44. return
  45. }
  46. // TokenListWithPager .
  47. func (d *Dao) TokenListWithPager(c context.Context, pm *net.ListTokenParam) (result *net.ListTokenRes, err error) {
  48. result = &net.ListTokenRes{
  49. Pager: net.Pager{
  50. Num: pm.Pn,
  51. Size: pm.Ps,
  52. },
  53. }
  54. db := d.tokenListDB([]int64{pm.NetID}, pm.ID, pm.Name, pm.Assign)
  55. err = db.Count(&result.Pager.Total).Scopes(pager(pm.Ps, pm.Pn, pm.Sort)).Find(&result.Result).Error
  56. if err != nil {
  57. log.Error("TokenListWithPager find error(%v) params(%+v)", err, pm)
  58. }
  59. return
  60. }
  61. // TokenList .
  62. func (d *Dao) TokenList(c context.Context, netID []int64, id []int64, name string, onlyAssign bool) (list []*net.Token, err error) {
  63. err = d.tokenListDB(netID, id, name, onlyAssign).Find(&list).Error
  64. if err != nil {
  65. log.Error("TokenList find error(%v)", err)
  66. }
  67. return
  68. }
  69. // TokenByUnique .
  70. func (d *Dao) TokenByUnique(c context.Context, netID int64, name string, compare int8, value string) (t *net.Token, err error) {
  71. t = &net.Token{}
  72. err = d.orm.Where("net_id=? AND name=? AND compare=? AND value=?", netID, name, compare, value).First(t).Error
  73. if err == gorm.ErrRecordNotFound {
  74. err = nil
  75. t = nil
  76. return
  77. }
  78. if err != nil {
  79. log.Error("TokenByUnique(%d,%s,%d,%s) error(%v)", netID, name, compare, value, err)
  80. }
  81. return
  82. }
  83. // TokenBinds .
  84. func (d *Dao) TokenBinds(c context.Context, id []int64) (t []*net.TokenBind, err error) {
  85. t = []*net.TokenBind{}
  86. err = d.orm.Where("id in (?)", id).Find(&t).Error
  87. if err != nil {
  88. log.Error("TokenBinds(%+v) error(%v)", id, err)
  89. }
  90. return
  91. }
  92. // TokenBindByElement .
  93. func (d *Dao) TokenBindByElement(c context.Context, elementID []int64, tp []int8, onlyAvailable bool) (binds map[int64][]*net.TokenBind, err error) {
  94. binds = map[int64][]*net.TokenBind{}
  95. list := []*net.TokenBind{}
  96. db := d.orm.Where("element_id in (?) AND type in (?)", elementID, tp)
  97. if onlyAvailable {
  98. db = db.Scopes(Available)
  99. }
  100. if err = db.Find(&list).Error; err != nil {
  101. log.Error("TokenBindByElement error(%v) elementid(%d) type(%v) onlyavailable(%v)", err, elementID, tp, onlyAvailable)
  102. return
  103. }
  104. for _, item := range list {
  105. if _, exist := binds[item.ElementID]; !exist {
  106. binds[item.ElementID] = []*net.TokenBind{item}
  107. continue
  108. }
  109. binds[item.ElementID] = append(binds[item.ElementID], item)
  110. }
  111. return
  112. }