service.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package faq
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/conf"
  5. "go-common/app/interface/main/creative/dao/faq"
  6. faqMdl "go-common/app/interface/main/creative/model/faq"
  7. "go-common/app/interface/main/creative/service"
  8. "go-common/library/log"
  9. "go-common/library/sync/pipeline/fanout"
  10. )
  11. //Service struct
  12. type Service struct {
  13. c *conf.Config
  14. faq *faq.Dao
  15. cache *fanout.Fanout
  16. }
  17. //New get service
  18. func New(c *conf.Config, rpcdaos *service.RPCDaos) *Service {
  19. s := &Service{
  20. c: c,
  21. faq: faq.New(c),
  22. cache: fanout.New("faq-service"),
  23. }
  24. return s
  25. }
  26. // Pre fn
  27. func (s *Service) Pre(c context.Context) (res map[string]*faqMdl.Faq) {
  28. res = make(map[string]*faqMdl.Faq)
  29. var (
  30. total int
  31. err error
  32. resD []*faqMdl.Detail
  33. )
  34. res["app_edit_faq"] = &faqMdl.Faq{
  35. State: false,
  36. QuestionTypeID: faqMdl.PhoneFaqQuesTypeID,
  37. QuestionTypeName: faqMdl.PhoneFaqName,
  38. URL: s.c.H5Page.FAQVideoEditor,
  39. }
  40. if resD, total, err = s.faq.DetailCache(c, faqMdl.PhoneFaqQuesTypeID, 1, 1, 2); err != nil || len(resD) == 0 {
  41. if _, total, err = s.faq.Detail(context.Background(), faqMdl.PhoneFaqQuesTypeID, 1, 1, 2); err != nil {
  42. log.Error("s.faq.Detail(%s,%d,%d,%d) error(%v)", faqMdl.PhoneFaqQuesTypeID, 1, 1, 2, err)
  43. }
  44. }
  45. if total > 0 {
  46. res["app_edit_faq"].State = true
  47. }
  48. return
  49. }
  50. // Detail fn
  51. func (s *Service) Detail(c context.Context, qTypeID string, keyFlag, pn, ps int) (resD []*faqMdl.Detail, total int, err error) {
  52. if resD, total, err = s.faq.DetailCache(c, qTypeID, keyFlag, pn, ps); err != nil || len(resD) == 0 {
  53. if resD, total, err = s.faq.Detail(context.Background(), qTypeID, keyFlag, pn, ps); err != nil {
  54. log.Error("s.faq.Detail(%s,%d,%d,%d) error(%v)", qTypeID, keyFlag, pn, ps, err)
  55. }
  56. if len(resD) > 0 {
  57. s.cache.Do(c, func(c context.Context) {
  58. s.faq.SetDetailCache(context.Background(), qTypeID, keyFlag, pn, ps, total, resD)
  59. })
  60. }
  61. }
  62. return
  63. }