dao.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "go-common/library/sync/pipeline/fanout"
  8. "net/url"
  9. "reflect"
  10. "strconv"
  11. "go-common/app/interface/bbq/app-bbq/conf"
  12. "go-common/app/interface/bbq/app-bbq/model"
  13. "go-common/app/interface/bbq/app-bbq/model/grpc"
  14. notice "go-common/app/service/bbq/notice-service/api/v1"
  15. recsys "go-common/app/service/bbq/recsys/api/grpc/v1"
  16. user "go-common/app/service/bbq/user/api"
  17. image "go-common/app/service/bbq/video-image/api/grpc/v1"
  18. video "go-common/app/service/bbq/video/api/grpc/v1"
  19. acc "go-common/app/service/main/account/api"
  20. filter "go-common/app/service/main/filter/api/grpc/v1"
  21. "go-common/library/cache/redis"
  22. xsql "go-common/library/database/sql"
  23. "go-common/library/ecode"
  24. "go-common/library/log"
  25. bm "go-common/library/net/http/blademaster"
  26. "go-common/library/net/rpc/warden"
  27. jsoniter "github.com/json-iterator/go"
  28. )
  29. var (
  30. videoPath string
  31. )
  32. // Dao dao
  33. type Dao struct {
  34. c *conf.Config
  35. cache *fanout.Fanout
  36. redis *redis.Pool
  37. db *xsql.DB
  38. dbDM *xsql.DB
  39. httpClient *bm.Client
  40. httpslowClient *bm.Client
  41. accountClient acc.AccountClient
  42. recsysClient recsys.RecsysClient
  43. imageClient image.VideoImageClient
  44. bvcPlayClient grpc.PlayurlServiceClient
  45. redundanceVideos []*model.RVideo
  46. noticeClient notice.NoticeClient
  47. userClient user.UserClient
  48. videoClient video.VideoClient
  49. filterClient filter.FilterClient
  50. }
  51. func init() {
  52. flag.StringVar(&videoPath, "video_json", "./video.json", "接口冗余降级video数据")
  53. }
  54. // New init mysql db
  55. func New(c *conf.Config) (dao *Dao) {
  56. dao = &Dao{
  57. c: c,
  58. cache: fanout.New("cache", fanout.Worker(1), fanout.Buffer(1024)),
  59. redis: redis.NewPool(c.Redis),
  60. db: xsql.NewMySQL(c.MySQL),
  61. dbDM: xsql.NewMySQL(c.DMMySQL),
  62. httpClient: bm.NewClient(c.HTTPClient.Normal),
  63. httpslowClient: bm.NewClient(c.HTTPClient.Slow),
  64. accountClient: newAccountClient(c.GRPCClient["account"]),
  65. recsysClient: newRecsysClient(c.GRPCClient["recsys"]),
  66. imageClient: newVideoImageClient(c.GRPCClient["videoimage"]),
  67. bvcPlayClient: newBVCPlayClient(c.GRPCClient["bvcplay"]),
  68. redundanceVideos: model.RedundanceVideo(),
  69. noticeClient: newNoticeClient(c.GRPCClient["notice"]),
  70. userClient: newUserClient(c.GRPCClient["user"]),
  71. videoClient: newVideoClient(c.GRPCClient["video"]),
  72. filterClient: newFilterClient(c.GRPCClient["filter"]),
  73. }
  74. return
  75. }
  76. // newVideoClient .
  77. func newVideoClient(cfg *conf.GRPCConf) video.VideoClient {
  78. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  79. if err != nil {
  80. panic(err)
  81. }
  82. return video.NewVideoClient(cc)
  83. }
  84. // newUserClient .
  85. func newUserClient(cfg *conf.GRPCConf) user.UserClient {
  86. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  87. if err != nil {
  88. panic(err)
  89. }
  90. return user.NewUserClient(cc)
  91. }
  92. // newNoticeClient .
  93. func newNoticeClient(cfg *conf.GRPCConf) notice.NoticeClient {
  94. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  95. if err != nil {
  96. panic(err)
  97. }
  98. return notice.NewNoticeClient(cc)
  99. }
  100. // newBVCPlayClient .
  101. func newBVCPlayClient(cfg *conf.GRPCConf) grpc.PlayurlServiceClient {
  102. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  103. if err != nil {
  104. panic(err)
  105. }
  106. return grpc.NewPlayurlServiceClient(cc)
  107. }
  108. // newVideoImageClient .
  109. func newVideoImageClient(cfg *conf.GRPCConf) image.VideoImageClient {
  110. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  111. if err != nil {
  112. panic(err)
  113. }
  114. return image.NewVideoImageClient(cc)
  115. }
  116. //newAccountClient .
  117. func newAccountClient(cfg *conf.GRPCConf) acc.AccountClient {
  118. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  119. if err != nil {
  120. panic(err)
  121. }
  122. return acc.NewAccountClient(cc)
  123. }
  124. //newRecsysClient .
  125. func newRecsysClient(cfg *conf.GRPCConf) recsys.RecsysClient {
  126. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  127. if err != nil {
  128. panic(err)
  129. }
  130. return recsys.NewRecsysClient(cc)
  131. }
  132. // newUserClient .
  133. func newFilterClient(cfg *conf.GRPCConf) filter.FilterClient {
  134. cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
  135. if err != nil {
  136. panic(err)
  137. }
  138. return filter.NewFilterClient(cc)
  139. }
  140. // Close close the resource.
  141. func (d *Dao) Close() {
  142. d.redis.Close()
  143. d.db.Close()
  144. }
  145. // Ping dao ping
  146. func (d *Dao) Ping(c context.Context) error {
  147. // TODO: if you need use mc,redis, please add
  148. return d.db.Ping(c)
  149. }
  150. // BeginTran begin mysql transaction
  151. func (d *Dao) BeginTran(c context.Context) (*xsql.Tx, error) {
  152. return d.db.Begin(c)
  153. }
  154. // ReplyHTTPCommon 评论公用请求
  155. func replyHTTPCommon(c context.Context, httpClient *bm.Client, path string, method string, data map[string]interface{}, ip string) (r []byte, err error) {
  156. params := url.Values{}
  157. t := reflect.TypeOf(data).Kind()
  158. if t == reflect.Map {
  159. for k, v := range data {
  160. // params.Set(k, v.(string))
  161. switch reflect.TypeOf(v).Kind() {
  162. case reflect.Int64:
  163. params.Set(k, strconv.FormatInt(v.(int64), 10))
  164. case reflect.Int16:
  165. params.Set(k, strconv.FormatInt(int64(v.(int16)), 10))
  166. case reflect.String:
  167. params.Set(k, v.(string))
  168. case reflect.Int:
  169. params.Set(k, strconv.FormatInt(int64(v.(int)), 10))
  170. }
  171. }
  172. }
  173. log.V(5).Infov(c, log.KV("log", fmt.Sprintf("reply req url(%s)", path+"?"+params.Encode())))
  174. req, err := httpClient.NewRequest(method, path, ip, params)
  175. if err != nil {
  176. log.Errorv(c, log.KV("log", fmt.Sprintf("reply url(%s) error(%v)", path+"?"+params.Encode(), err)))
  177. return
  178. }
  179. var res struct {
  180. Code int `json:"code"`
  181. Msg string `json:"message"`
  182. Data json.RawMessage `json:"data"`
  183. }
  184. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  185. if err = httpClient.Do(c, req, &res); err != nil {
  186. str, _ := json.Marshal(res)
  187. log.Errorv(c, log.KV("log", fmt.Sprintf("reply ret data(%s) err[%v]", str, err)))
  188. return
  189. }
  190. str, _ := json.Marshal(res)
  191. log.V(5).Infov(c, log.KV("log", fmt.Sprintf("reply ret data(%s)", str)))
  192. if res.Code != 0 {
  193. err = ecode.Int(res.Code)
  194. log.Warnv(c, log.KV("log", fmt.Sprintf("reply url(%s) error(%v)", path+"?"+params.Encode(), err)))
  195. }
  196. r = res.Data
  197. return
  198. }