app.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package v2
  2. import (
  3. "go-common/app/infra/config/model"
  4. "go-common/library/database/sql"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. )
  8. // AppByTree get token by Name.
  9. func (d *Dao) AppByTree(zone, env string, treeID int64) (app *model.App, err error) {
  10. app = &model.App{}
  11. row := d.DB.Select("id,token").Where("tree_id = ? AND env=? AND zone=?", treeID, env, zone).Model(&model.DBApp{}).Row()
  12. if err = row.Scan(&app.ID, &app.Token); err != nil {
  13. log.Error("AppByTree(%v) error(%v)", treeID, err)
  14. if err == sql.ErrNoRows {
  15. err = ecode.NothingFound
  16. }
  17. }
  18. return
  19. }
  20. // AppsByNameEnv get token by Name.
  21. func (d *Dao) AppsByNameEnv(name, env string) (apps []*model.DBApp, err error) {
  22. if err = d.DB.Where("env = ? and name like ?", env, "%"+name).Find(&apps).Error; err != nil {
  23. log.Error("AppsByNameEnv(%v) error(%v)", name, env)
  24. return
  25. }
  26. if len(apps) == 0 {
  27. err = ecode.NothingFound
  28. }
  29. return
  30. }
  31. // AppGet ...
  32. func (d *Dao) AppGet(zone, env, token string) (app *model.App, err error) {
  33. app = &model.App{}
  34. row := d.DB.Select("id,token,env,zone,tree_id").Where("token = ? AND env= ? AND zone= ?", token, env, zone).Model(&model.DBApp{}).Row()
  35. if err = row.Scan(&app.ID, &app.Token, &app.Env, &app.Zone, &app.TreeID); err != nil {
  36. log.Error("AppGet zone(%v) env(%v) token(%v) error(%v)", zone, env, token, err)
  37. if err == sql.ErrNoRows {
  38. err = ecode.NothingFound
  39. }
  40. }
  41. return
  42. }