device_list.go 2.2 KB

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