device_list.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package project
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "smart-site-management/consts"
  8. "smart-site-management/errors"
  9. "smart-site-management/model"
  10. "smart-site-management/pb/v1"
  11. "github.com/jaryhe/gopkgs/logger"
  12. "go.uber.org/zap"
  13. )
  14. func DeviceList(ctx context.Context, req *v1.DeviceListRequest) (reply *v1.DeviceListReply, err error) {
  15. reply = &v1.DeviceListReply{}
  16. if req.ProjectId == 0 {
  17. return nil, errors.ParamsError
  18. }
  19. where := map[string]interface{}{
  20. "project_id":req.ProjectId,
  21. }
  22. if len(req.StatusFilters) > 0 {
  23. where["verify_status in"] = req.StatusFilters
  24. }
  25. p := &model.TDevice{}
  26. if req.Page < 1 {
  27. req.Page = 1
  28. }
  29. if req.PageSize < 1 {
  30. req.PageSize = 10
  31. }
  32. pt := &model.TDeviceTypeItem{}
  33. m, _, err := pt.AllMap(database.DB())
  34. if err != nil {
  35. logger.Error("DeviceList",
  36. zap.String("err", err.Error()))
  37. return nil, errors.DataBaseError
  38. }
  39. dbReq := model.ListRawRequest{
  40. Page:req.Page,
  41. ProjectId:req.ProjectId,
  42. ProviderId:req.ProviderId,
  43. Filter:req.Filter,
  44. StatusFilter:req.StatusFilters,
  45. IsAll:req.IsAll,
  46. CanDel:req.CanDel,
  47. TypeCode:req.TypeCode,
  48. PageSize:req.PageSize,
  49. }
  50. list, total, err := p.ListRaw(database.DB(), dbReq)
  51. if err != nil {
  52. logger.Error("DeviceList",
  53. zap.String("err", err.Error()))
  54. return nil, errors.DataBaseError
  55. }
  56. reply.Page = req.Page
  57. reply.Total = total
  58. reply.PageSize = int32(model.PageSize)
  59. if total == 0 {
  60. return reply, nil
  61. }
  62. reply.List = make([]*v1.DeviceItem, len(list))
  63. for i, v := range list{
  64. item := &v1.DeviceItem{
  65. Id: int64(v.Id),
  66. Sn: v.Sn,
  67. TypeCode: v.TypeCode,
  68. TypeName: m[v.TypeCode],
  69. ProjectName: v.ProjectName,
  70. SafetyRecordNo: v.SafetyRecordNo,
  71. ApplyTime: v.CreatedTime.Format(layout),
  72. Status:v.Status,
  73. State:v.State,
  74. Key:v.Key,
  75. ProviderName:v.ProviderName,
  76. SocialCode:v.SocialCode,
  77. Name:v.Name,
  78. ChannelNo:v.ChannelNo,
  79. ChannelName:v.ChannelName,
  80. }
  81. if item.Status == consts.DeviceStatusAddAuditted ||
  82. item.Status == consts.DeviceStatusAddAudittedFail {
  83. item.ApproveTime = v.ProjectApproveTime.Format(layout)
  84. }
  85. reply.List[i] = item
  86. }
  87. reply.Page = req.Page
  88. reply.Total = total
  89. reply.PageSize = int32(model.PageSize)
  90. return reply, nil
  91. }