dao.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package resource
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/conf"
  5. resmdl "go-common/app/service/main/resource/model"
  6. resrpc "go-common/app/service/main/resource/rpc/client"
  7. "go-common/library/log"
  8. "strconv"
  9. )
  10. // Dao str
  11. type Dao struct {
  12. c *conf.Config
  13. // rpc
  14. resRPC *resrpc.Service
  15. }
  16. // New str
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. c: c,
  20. resRPC: resrpc.New(c.ResourceRPC),
  21. }
  22. return
  23. }
  24. // Banner get search banner
  25. func (d *Dao) Banner(c context.Context, mobiApp, device, network, channel, ip, buvid, adExtra, resIDStr string, build int, plat int8, mid int64, isAd bool) (res map[int][]*resmdl.Banner, err error) {
  26. var bs *resmdl.Banners
  27. arg := &resmdl.ArgBanner{
  28. MobiApp: mobiApp,
  29. Device: device,
  30. Network: network,
  31. Channel: channel,
  32. IP: ip,
  33. Buvid: buvid,
  34. AdExtra: adExtra,
  35. ResIDs: resIDStr,
  36. Build: build,
  37. Plat: plat,
  38. MID: mid,
  39. IsAd: isAd,
  40. }
  41. if bs, err = d.resRPC.Banners(c, arg); err != nil || bs == nil {
  42. log.Error("d.resRPC.Banners(%v) error(%v) or bs is nil", arg, err)
  43. return
  44. }
  45. if bs == nil {
  46. return
  47. }
  48. if len(bs.Banner) > 0 {
  49. res = bs.Banner
  50. }
  51. return
  52. }
  53. // SimpleResource simple resource
  54. func (d *Dao) SimpleResource(c context.Context, resID int) (res *resmdl.Resource, err error) {
  55. arg := &resmdl.ArgRes{ResID: resID}
  56. if res, err = d.resRPC.Resource(c, arg); err != nil || res == nil {
  57. log.Error("d.resRPC.Resource(%v) error(%v) or bs is nil", arg, err)
  58. return
  59. }
  60. return
  61. }
  62. // Resource get resource
  63. func (d *Dao) Resource(c context.Context, resID int) (aidMap map[int64]struct{}, err error) {
  64. var rs *resmdl.Resource
  65. arg := &resmdl.ArgRes{ResID: resID}
  66. if rs, err = d.resRPC.Resource(c, arg); err != nil || rs == nil {
  67. log.Error("d.resRPC.Resource(%v) error(%v) or bs is nil", arg, err)
  68. return
  69. }
  70. aidMap = make(map[int64]struct{})
  71. for _, ass := range rs.Assignments {
  72. aid, _ := strconv.ParseInt(ass.URL, 10, 64)
  73. if aid > 0 {
  74. aidMap[aid] = struct{}{}
  75. }
  76. }
  77. return
  78. }