search.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package search
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "go-common/app/job/main/reply/conf"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. )
  12. const (
  13. _appIDRecord = "reply_record"
  14. _updateURL = "/x/internal/search/reply/update"
  15. )
  16. // Dao Dao
  17. type Dao struct {
  18. searchHTTPClient *bm.Client
  19. searchUpdateURI string
  20. }
  21. // New New
  22. func New(c *conf.Config) *Dao {
  23. return &Dao{
  24. searchHTTPClient: bm.NewClient(c.HTTPClient),
  25. searchUpdateURI: c.Host.API + _updateURL,
  26. }
  27. }
  28. // DelReply DelReply
  29. func (dao *Dao) DelReply(c context.Context, rpid, oid, mid int64, state int8) (err error) {
  30. return dao.update(c, rpid, oid, mid, state)
  31. }
  32. func (dao *Dao) update(c context.Context, rpid, oid, mid int64, state int8) (err error) {
  33. type updateRecord struct {
  34. ID int64 `json:"id"`
  35. Oid int64 `json:"oid"`
  36. Mid int64 `json:"mid"`
  37. State int8 `json:"state"`
  38. }
  39. var (
  40. res struct {
  41. Code int `json:"code"`
  42. Msg string `json:"message"`
  43. }
  44. )
  45. records := make([]*updateRecord, 0)
  46. record := &updateRecord{}
  47. record.ID = rpid
  48. record.Oid = oid
  49. record.Mid = mid
  50. record.State = state
  51. records = append(records, record)
  52. recordsStr, _ := json.Marshal(records)
  53. params := url.Values{}
  54. params.Set("appid", _appIDRecord)
  55. params.Set("data", string(recordsStr))
  56. if err = dao.searchHTTPClient.Post(c, dao.searchUpdateURI, "", params, &res); err != nil {
  57. log.Error("bm.Post(%s) failed error(%v)", dao.searchUpdateURI+"?"+params.Encode(), err)
  58. return
  59. }
  60. if res.Code != ecode.OK.Code() {
  61. err = fmt.Errorf("update reply es records failed")
  62. }
  63. log.Info("updateSearch: %s post:%s ret:%v", dao.searchUpdateURI, params.Encode(), res)
  64. return
  65. }