config_business.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "time"
  7. "go-common/app/job/main/search/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _getBusinessSQL = "SELECT business, app_ids, asset_db, asset_es, asset_dtb FROM digger_business WHERE business=?"
  13. )
  14. type bns struct {
  15. d *Dao
  16. business string
  17. bInfo *model.Bsn
  18. }
  19. func newBusiness(d *Dao, business string) (bs *bns, err error) {
  20. bs = &bns{
  21. d: d,
  22. business: business,
  23. bInfo: new(model.Bsn),
  24. }
  25. if err = bs.initBusiness(); err != nil {
  26. log.Error("d.initBusiness error (%v)", err)
  27. }
  28. return
  29. }
  30. func (bs *bns) initBusiness() (err error) {
  31. var sqlBusiness *model.SQLBusiness
  32. for {
  33. if sqlBusiness, err = bs.getBusiness(context.TODO()); err != nil {
  34. log.Error("initBusiness error (%v)", err)
  35. time.Sleep(time.Second * 3)
  36. continue
  37. }
  38. break
  39. }
  40. if sqlBusiness == nil {
  41. err = errors.New("initBusiness: " + bs.business + " not found in `digger_business`")
  42. return
  43. }
  44. bs.bInfo.Business = sqlBusiness.Business
  45. bs.bInfo.AppInfo = make([]model.BsnAppInfo, 0)
  46. // business-appinfo
  47. if sqlBusiness.AppIds != "" {
  48. err = json.Unmarshal([]byte(sqlBusiness.AppIds), &bs.bInfo.AppInfo)
  49. }
  50. // business-assetdb
  51. // business-assetes
  52. // business-assedtb
  53. return
  54. }
  55. func (bs *bns) getBusiness(c context.Context) (res *model.SQLBusiness, err error) {
  56. res = new(model.SQLBusiness)
  57. row := bs.d.SearchDB.QueryRow(c, _getBusinessSQL, bs.business)
  58. if err = row.Scan(&res.Business, &res.AppIds, &res.AssetDB, &res.AssetES, &res.AssetDtb); err != nil {
  59. log.Error("business row.Scan error(%v)", err)
  60. if err == sql.ErrNoRows {
  61. err = nil
  62. res = nil
  63. return
  64. }
  65. }
  66. return
  67. }