channel_list.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package device
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "github.com/jinzhu/gorm"
  8. "smart-government-management/consts"
  9. "smart-government-management/errors"
  10. "smart-government-management/model"
  11. "smart-government-management/pb/v1"
  12. "github.com/jaryhe/gopkgs/logger"
  13. "go.uber.org/zap"
  14. )
  15. func ChannelList(ctx context.Context, req *v1.ChannelListRequest) (reply *v1.ChannelListReply, err error) {
  16. if req.Sn == "" {
  17. return nil, errors.ParamsError
  18. }
  19. if req.Page < 1 {
  20. req.Page = 1
  21. }
  22. p := model.TDevice{}
  23. where := map[string]interface{}{
  24. "sn":req.Sn,
  25. }
  26. err = p.Find(database.DB(), where)
  27. if err != nil && err != gorm.ErrRecordNotFound{
  28. logger.Error("ChannelList",
  29. zap.String("err", err.Error()))
  30. return nil, errors.DataBaseError
  31. }
  32. if p.Id == 0 {
  33. return nil, errors.NoRecordError
  34. }
  35. if p.VerifyStatus != consts.DeviceStatusAddAuditted {
  36. return nil, errors.VssStatusError
  37. }
  38. pc := model.Vsschanneltbl{}
  39. where = map[string]interface{}{
  40. "DevPubID":req.Sn,
  41. }
  42. if req.ChannelNo != "" {
  43. where["ChanPubID like"] = "%"+req.ChannelNo+"%"
  44. }
  45. count, err := pc.Count(database.DB(), where)
  46. if err != nil && err != gorm.ErrRecordNotFound{
  47. logger.Error("ChannelList",
  48. zap.String("err", err.Error()))
  49. return nil, errors.DataBaseError
  50. }
  51. reply = &v1.ChannelListReply{
  52. Page:req.Page,
  53. PageSize:int32(model.PageSize),
  54. Total:count,
  55. }
  56. if count == 0 {
  57. return reply, nil
  58. }
  59. list, err := pc.List(database.DB(), where, req.Page)
  60. if err != nil && err != gorm.ErrRecordNotFound{
  61. logger.Error("ChannelList",
  62. zap.String("err", err.Error()))
  63. return nil, errors.DataBaseError
  64. }
  65. reply.List = make([]*v1.ChannelItem, len(list))
  66. for i, v := range list {
  67. item := &v1.ChannelItem{
  68. Name:v.Nickname,
  69. Id:v.ID,
  70. ChannelNo:v.Chanpubid,
  71. State:int32(v.Alive),
  72. }
  73. reply.List[i] = item
  74. }
  75. return reply, nil
  76. }