device_list.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "smart-enterprise-management/consts"
  8. "smart-enterprise-management/errors"
  9. "smart-enterprise-management/model"
  10. "smart-enterprise-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.Cid < 1 {
  17. return nil, errors.DataBaseError
  18. }
  19. p := &model.TDevice{}
  20. if req.Page < 1 {
  21. req.Page = 1
  22. }
  23. pt := &model.TDeviceTypeItem{}
  24. m, _, err := pt.AllMap(database.DB())
  25. if err != nil {
  26. logger.Error("DeviceList",
  27. zap.String("err", err.Error()))
  28. return nil, errors.DataBaseError
  29. }
  30. dbReq := model.ListRawRequest{
  31. Page:req.Page,
  32. ProjectId:req.ProjectId,
  33. ProviderId:req.ProviderId,
  34. Filter:req.Filter,
  35. StatusFilter:req.StatusFilters,
  36. IsAll:req.IsAll,
  37. CanDel:req.CanDel,
  38. TypeCode:req.TypeCode,
  39. Cid:req.Cid,
  40. }
  41. list, total, err := p.ListRaw(database.DB(), dbReq)
  42. if err != nil {
  43. logger.Error("DeviceList",
  44. zap.String("err", err.Error()))
  45. return nil, errors.DataBaseError
  46. }
  47. reply.Page = req.Page
  48. reply.Total = total
  49. reply.PageSize = int32(model.PageSize)
  50. if total == 0 {
  51. return reply, nil
  52. }
  53. reply.List = make([]*v1.DeviceItem, len(list))
  54. for i, v := range list{
  55. item := &v1.DeviceItem{
  56. Id: int64(v.Id),
  57. Sn: v.Sn,
  58. TypeCode: v.TypeCode,
  59. TypeName: m[v.TypeCode],
  60. ProjectName: v.ProjectName,
  61. SafetyRecordNo: v.SafetyRecordNo,
  62. ApplyTime: v.CreatedTime.Format(layout),
  63. Status:v.Status,
  64. State:v.State,
  65. Key:v.Key,
  66. ProviderName:v.ProviderName,
  67. SocialCode:v.SocialCode,
  68. Name:v.Name,
  69. }
  70. if item.Status == consts.DeviceStatusAddAuditted ||
  71. item.Status == consts.DeviceStatusAddAudittedFail {
  72. item.ApproveTime = v.ProjectApproveTime.Format(layout)
  73. }
  74. reply.List[i] = item
  75. }
  76. reply.Page = req.Page
  77. reply.Total = total
  78. reply.PageSize = int32(model.PageSize)
  79. return reply, nil
  80. }