dao.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package relation
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/service/main/videoup/conf"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. const (
  11. _add_black = "/x/internal/relation/black/add"
  12. _get_relation = "/x/internal/relation"
  13. )
  14. // Dao is redis dao.
  15. type Dao struct {
  16. c *conf.Config
  17. httpW *bm.Client
  18. addBlackURL string
  19. getRelationURL string
  20. }
  21. // New new a dao.
  22. func New(c *conf.Config) (d *Dao) {
  23. d = &Dao{
  24. c: c,
  25. httpW: bm.NewClient(c.HTTPClient.Write),
  26. addBlackURL: c.Host.APICO + _add_black,
  27. getRelationURL: c.Host.APICO + _get_relation,
  28. }
  29. return d
  30. }
  31. // Ping ping cpdb
  32. func (d *Dao) Ping(c context.Context) (err error) {
  33. return
  34. }
  35. //拉黑 http://info.bilibili.co/pages/viewpage.action?pageId=1742202#id-%E5%85%B3%E7%B3%BB%E6%9C%8D%E5%8A%A1%E5%86%85%E7%BD%91%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3-%E8%8E%B7%E5%8F%96%E9%BB%91%E5%90%8D%E5%8D%95%E5%88%97%E8%A1%A8
  36. //网关层 两者关系 http://info.bilibili.co/pages/viewpage.action?pageId=1742202#id-%E5%85%B3%E7%B3%BB%E6%9C%8D%E5%8A%A1%E5%86%85%E7%BD%91%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3-%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E4%B8%8E%E5%85%B6%E4%BB%96%E7%94%A8%E6%88%B7%E5%85%B3%E7%B3%BB
  37. // Bind aid,sid,cid bind in one
  38. func (d *Dao) AddBalck(c context.Context, mid, fid, aid int64) (err error) {
  39. params := url.Values{}
  40. params.Set("mid", strconv.FormatInt(mid, 10))
  41. params.Set("fid", strconv.FormatInt(fid, 10))
  42. params.Set("src", strconv.Itoa(221))
  43. var res struct {
  44. Code int `json:"code"`
  45. }
  46. if err = d.httpW.Post(c, d.addBlackURL, "", params, &res); err != nil {
  47. log.Error("d.httpW.Post(%s) error(%v)", d.addBlackURL+"?"+params.Encode(), err)
  48. return
  49. }
  50. if res.Code != 0 {
  51. log.Error("url(%s) code(%d)", d.addBlackURL+"?"+params.Encode(), res.Code)
  52. }
  53. log.Info("aid(%d) AddBalck mid(%d) url(%s) code(%d)", aid, mid, d.addBlackURL+"?"+params.Encode(), res.Code)
  54. return
  55. }
  56. // Relation aid,sid,cid bind in one
  57. func (d *Dao) Relation(c context.Context, mid, fid, aid int64) (attribute int64, err error) {
  58. params := url.Values{}
  59. params.Set("mid", strconv.FormatInt(mid, 10))
  60. params.Set("fid", strconv.FormatInt(fid, 10))
  61. var res struct {
  62. Code int `json:"code"`
  63. Data struct {
  64. MID int64 `json:"mid"`
  65. //大于等于128表示拉黑
  66. Attribute int64 `json:"attribute"`
  67. Mtime int64 `json:"mtime"`
  68. }
  69. }
  70. if err = d.httpW.Get(c, d.getRelationURL, "", params, &res); err != nil {
  71. log.Error("d.httpW.Get(%s) error(%v)", d.getRelationURL+"?"+params.Encode(), err)
  72. return
  73. }
  74. if res.Code != 0 {
  75. log.Error("url(%s) code(%d)", d.getRelationURL+"?"+params.Encode(), res.Code)
  76. }
  77. attribute = res.Data.Attribute
  78. log.Info("aid(%d) Relation mid(%d) url(%s) code(%d) data(%+v)", aid, mid, d.getRelationURL+"?"+params.Encode(), res.Code, res)
  79. return
  80. }