service.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package version
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/conf"
  5. "go-common/app/interface/main/creative/dao/creative"
  6. vsMdl "go-common/app/interface/main/creative/model/version"
  7. "go-common/app/interface/main/creative/service"
  8. "go-common/library/log"
  9. "time"
  10. )
  11. //Service struct
  12. type Service struct {
  13. c *conf.Config
  14. Creative *creative.Dao
  15. // cache
  16. VersionCache []*vsMdl.Version
  17. VersionMapCache map[string][]*vsMdl.Version
  18. WebManagerTip *vsMdl.Version // WebTip
  19. AppManagerTip *vsMdl.Version // APPTip
  20. CusManagerTip *vsMdl.Version // 客服Tip
  21. }
  22. //New get service
  23. func New(c *conf.Config, rpcdaos *service.RPCDaos) *Service {
  24. s := &Service{
  25. c: c,
  26. Creative: creative.New(c),
  27. }
  28. s.loadVersion()
  29. go s.loadproc()
  30. return s
  31. }
  32. func (s *Service) loadVersion() {
  33. var (
  34. vss []*vsMdl.Version
  35. vsWebTip *vsMdl.Version
  36. vsAppTip *vsMdl.Version
  37. vsCustomTip *vsMdl.Version
  38. err error
  39. )
  40. vss, err = s.Creative.AllByTypes(context.TODO(), vsMdl.FullVersions())
  41. if err != nil {
  42. log.Error("s.Version.versions error(%v)", err)
  43. return
  44. }
  45. s.VersionCache = vss
  46. s.VersionMapCache, _ = s.versionMap(context.TODO())
  47. vsWebTip, err = s.Creative.LatestByType(context.TODO(), 7)
  48. if err != nil {
  49. log.Error("s.Creative.LatestByType, type=7, error(%v)", err)
  50. return
  51. }
  52. s.WebManagerTip = vsWebTip
  53. vsAppTip, err = s.Creative.LatestByType(context.TODO(), 8)
  54. if err != nil {
  55. log.Error("s.Creative.LatestByType, type=8, error(%v)", err)
  56. return
  57. }
  58. s.AppManagerTip = vsAppTip
  59. vsCustomTip, err = s.Creative.LatestByType(context.TODO(), 9)
  60. if err != nil {
  61. log.Error("s.Creative.LatestByType, type=9, error(%v)", err)
  62. return
  63. }
  64. s.CusManagerTip = vsCustomTip
  65. }
  66. // loadproc
  67. func (s *Service) loadproc() {
  68. for {
  69. time.Sleep(2 * time.Minute)
  70. s.loadVersion()
  71. }
  72. }
  73. // Ping service
  74. func (s *Service) Ping(c context.Context) (err error) {
  75. if err = s.Creative.Ping(c); err != nil {
  76. log.Error("s.versionDao.PingDb err(%v)", err)
  77. }
  78. return
  79. }
  80. // Close dao
  81. func (s *Service) Close() {
  82. s.Creative.Close()
  83. }