list.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "property-company/errors"
  13. dbmodel "property-company/model"
  14. pb_v1 "property-company/pb/v1"
  15. )
  16. //
  17. func CompanyUserList(ctx context.Context, req *pb_v1.CompanyUserListRequest) (reply *pb_v1.CompanyUserListReply, err error) {
  18. reply = &pb_v1.CompanyUserListReply{}
  19. // 捕获各个task中的异常并返回给调用者
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err = fmt.Errorf("%+v", r)
  23. e := &status.Status{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. logger.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. if req.Cid == 0 {
  32. return nil, errors.ParamsError
  33. }
  34. if req.PageSize == 0 {
  35. req.PageSize = 10
  36. }
  37. if req.Page == 0 {
  38. req.Page = 1
  39. }
  40. reply.Page = req.Page
  41. p := &dbmodel.TUser{}
  42. where := map[string]interface{}{}
  43. if req.Cid > 0 {
  44. where["cid"] = req.Cid
  45. }
  46. reply.Total, err = p.Count(database.DB(), where, nil)
  47. if err != nil {
  48. return nil, errors.DataBaseError
  49. }
  50. if reply.Total == 0 {
  51. return reply, nil
  52. }
  53. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  54. if err != nil {
  55. return nil, errors.DataBaseError
  56. }
  57. reply.List = make([]*pb_v1.CompanyUserItem, len(list))
  58. for i, v := range list {
  59. item := &pb_v1.CompanyUserItem{
  60. Phone: v.Phone,
  61. UserName: v.User,
  62. Email: v.Email,
  63. RealName: v.RealName,
  64. CreatedAt: v.CreatedAt.Unix(),
  65. Id: v.ID,
  66. }
  67. reply.List[i] = item
  68. }
  69. return reply, nil
  70. }