resource.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package v1
  2. import (
  3. "context"
  4. "strings"
  5. v1pb "go-common/app/admin/live/live-admin/api/http/v1"
  6. "go-common/app/admin/live/live-admin/conf"
  7. rspb "go-common/app/service/live/resource/api/grpc/v1"
  8. "go-common/library/log"
  9. )
  10. // ResourceService struct
  11. type ResourceService struct {
  12. conf *conf.Config
  13. // optionally add other properties here, such as dao
  14. // dao *dao.Dao
  15. rsCli *rspb.Client
  16. }
  17. //NewResourceService init
  18. func NewResourceService(c *conf.Config) (s *ResourceService) {
  19. s = &ResourceService{
  20. conf: c,
  21. }
  22. var svc *rspb.Client
  23. var err error
  24. log.Info("ResourceService Init: %+v", s.conf.ResourceClient)
  25. if svc, err = rspb.NewClient(s.conf.ResourceClient); err != nil {
  26. panic(err)
  27. }
  28. s.rsCli = svc
  29. return s
  30. }
  31. // Add implementation
  32. // Add 添加资源接口
  33. // `method:"POST" internal:"true"
  34. func (s *ResourceService) Add(ctx context.Context, req *v1pb.AddReq) (resp *v1pb.AddResp, err error) {
  35. respRPC, error := s.rsCli.Add(ctx, &rspb.AddReq{
  36. Platform: req.Platform,
  37. Title: req.Title,
  38. JumpPath: req.JumpPath,
  39. JumpPathType: req.JumpPathType,
  40. JumpTime: req.JumpTime,
  41. Type: req.Type,
  42. Device: req.Device,
  43. StartTime: req.StartTime,
  44. EndTime: req.EndTime,
  45. ImageUrl: req.ImageUrl,
  46. })
  47. err = error
  48. if error == nil {
  49. resp = &v1pb.AddResp{
  50. Id: respRPC.Id,
  51. }
  52. }
  53. return
  54. }
  55. // AddEx implementation
  56. // AddEx 添加资源接口
  57. // `method:"POST" internal:"true"
  58. func (s *ResourceService) AddEx(ctx context.Context, req *v1pb.AddReq) (resp *v1pb.AddResp, err error) {
  59. respRPC, error := s.rsCli.AddEx(ctx, &rspb.AddReq{
  60. Platform: req.Platform,
  61. Title: req.Title,
  62. JumpPath: req.JumpPath,
  63. JumpPathType: req.JumpPathType,
  64. JumpTime: req.JumpTime,
  65. Type: req.Type,
  66. Device: req.Device,
  67. StartTime: req.StartTime,
  68. EndTime: req.EndTime,
  69. ImageUrl: req.ImageUrl,
  70. })
  71. err = error
  72. if error == nil {
  73. resp = &v1pb.AddResp{
  74. Id: respRPC.Id,
  75. }
  76. }
  77. return
  78. }
  79. // Edit implementation
  80. // Edit 编辑资源接口
  81. // `method:"POST" internal:"true" `
  82. func (s *ResourceService) Edit(ctx context.Context, req *v1pb.EditReq) (resp *v1pb.EditResp, err error) {
  83. _, err = s.rsCli.Edit(ctx, &rspb.EditReq{
  84. Platform: req.Platform,
  85. Id: req.Id,
  86. Title: req.Title,
  87. JumpPath: req.JumpPath,
  88. JumpTime: req.JumpTime,
  89. StartTime: req.StartTime,
  90. EndTime: req.EndTime,
  91. ImageUrl: req.ImageUrl,
  92. JumpPathType: req.JumpPathType,
  93. })
  94. return
  95. }
  96. // Offline implementation
  97. // Offline 下线资源接口
  98. // `method:"POST" internal:"true" `
  99. func (s *ResourceService) Offline(ctx context.Context, req *v1pb.OfflineReq) (resp *v1pb.OfflineResp, err error) {
  100. _, err = s.rsCli.Offline(ctx, &rspb.OfflineReq{
  101. Platform: req.Platform,
  102. Id: req.Id,
  103. })
  104. if err == nil {
  105. resp = &v1pb.OfflineResp{}
  106. }
  107. return
  108. }
  109. // GetList implementation
  110. // GetList 获取资源列表
  111. // `method:"GET" internal:"true" `
  112. func (s *ResourceService) GetList(ctx context.Context, req *v1pb.GetListReq) (resp *v1pb.GetListResp, err error) {
  113. RPCReq := &rspb.GetListReq{
  114. Platform: req.Platform,
  115. Type: req.Type,
  116. Page: req.Page,
  117. PageSize: req.PageSize,
  118. }
  119. if RPCReq.Page == 0 {
  120. RPCReq.Page = 1
  121. }
  122. if RPCReq.PageSize == 0 {
  123. RPCReq.PageSize = 50
  124. }
  125. var RPCResp *rspb.GetListResp
  126. RPCResp, err = s.rsCli.GetList(ctx, RPCReq)
  127. if err == nil {
  128. resp = &v1pb.GetListResp{
  129. CurrentPage: RPCResp.CurrentPage,
  130. TotalCount: RPCResp.TotalCount,
  131. List: convertRPCList2HttpList(RPCResp.List),
  132. }
  133. }
  134. return
  135. }
  136. // GetPlatformList implementation
  137. // 获取平台列表
  138. // `method:"GET" internal:"true" `
  139. func (s *ResourceService) GetPlatformList(ctx context.Context, req *v1pb.GetPlatformListReq) (resp *v1pb.GetPlatformListResp, err error) {
  140. var RPCResp *rspb.GetPlatformListResp
  141. RPCResp, err = s.rsCli.GetPlatformList(ctx, &rspb.GetPlatformListReq{
  142. Type: req.Type,
  143. })
  144. if err == nil {
  145. resp = &v1pb.GetPlatformListResp{
  146. Platform: RPCResp.Platform,
  147. }
  148. }
  149. return
  150. }
  151. // GetListEx implementation
  152. // GetListEx 获取资源列表
  153. // `method:"GET" internal:"true" `
  154. func (s *ResourceService) GetListEx(ctx context.Context, req *v1pb.GetListExReq) (resp *v1pb.GetListExResp, err error) {
  155. var RPCResp *rspb.GetListExResp
  156. // 默认分页参数
  157. if req.Page == 0 {
  158. req.Page = 1
  159. }
  160. if req.PageSize == 0 {
  161. req.PageSize = 50
  162. }
  163. if len(req.Type) > 0 {
  164. req.Type = strings.Split(req.Type[0], ",")
  165. }
  166. RPCResp, err = s.rsCli.GetListEx(ctx, &rspb.GetListExReq{
  167. Platform: req.Platform,
  168. Page: req.Page,
  169. PageSize: req.PageSize,
  170. Type: req.Type,
  171. DevicePlatform: req.DevicePlatform,
  172. Status: req.Status,
  173. StartTime: req.StartTime,
  174. EndTime: req.EndTime,
  175. })
  176. if err == nil {
  177. resp = &v1pb.GetListExResp{
  178. CurrentPage: RPCResp.CurrentPage,
  179. TotalCount: RPCResp.TotalCount,
  180. List: convertRPCListEx2HttpListEx(RPCResp.List),
  181. }
  182. }
  183. return
  184. }
  185. func convertRPCList2HttpList(RPCList []*rspb.GetListResp_List) (HTTPList []*v1pb.GetListResp_List) {
  186. HTTPList = make([]*v1pb.GetListResp_List, len(RPCList))
  187. for index, RPCRespItem := range RPCList {
  188. HTTPList[index] = &v1pb.GetListResp_List{
  189. Id: RPCRespItem.Id,
  190. Title: RPCRespItem.Title,
  191. JumpPath: RPCRespItem.JumpPath,
  192. DevicePlatform: RPCRespItem.DevicePlatform,
  193. DeviceBuild: RPCRespItem.DeviceBuild,
  194. StartTime: RPCRespItem.StartTime,
  195. EndTime: RPCRespItem.EndTime,
  196. Status: RPCRespItem.Status,
  197. DeviceLimit: RPCRespItem.DeviceLimit,
  198. ImageUrl: RPCRespItem.ImageUrl,
  199. JumpPathType: RPCRespItem.JumpPathType,
  200. JumpTime: RPCRespItem.JumpTime,
  201. }
  202. }
  203. return
  204. }
  205. func convertRPCListEx2HttpListEx(RPCList []*rspb.GetListExResp_List) (HTTPList []*v1pb.GetListExResp_List) {
  206. HTTPList = make([]*v1pb.GetListExResp_List, len(RPCList))
  207. for index, RPCRespItem := range RPCList {
  208. HTTPList[index] = &v1pb.GetListExResp_List{
  209. Id: RPCRespItem.Id,
  210. Title: RPCRespItem.Title,
  211. JumpPath: RPCRespItem.JumpPath,
  212. DevicePlatform: RPCRespItem.DevicePlatform,
  213. DeviceBuild: RPCRespItem.DeviceBuild,
  214. StartTime: RPCRespItem.StartTime,
  215. EndTime: RPCRespItem.EndTime,
  216. Status: RPCRespItem.Status,
  217. DeviceLimit: RPCRespItem.DeviceLimit,
  218. ImageUrl: RPCRespItem.ImageUrl,
  219. JumpPathType: RPCRespItem.JumpPathType,
  220. JumpTime: RPCRespItem.JumpTime,
  221. Type: RPCRespItem.Type,
  222. }
  223. }
  224. return
  225. }