list.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package staff
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "smart-site-management/errors"
  8. "smart-site-management/model"
  9. "smart-site-management/pb/v1"
  10. "smart-site-management/utils"
  11. )
  12. func StaffList(ctx context.Context, req *v1.StaffListRequest) (reply *v1.StaffListReply, err error) {
  13. reply = &v1.StaffListReply{}
  14. if req.ProjectId == 0 {
  15. return nil,errors.ParamsError
  16. }
  17. if req.Page < 1 {
  18. req.Page =1
  19. }
  20. if req.PageSize == 0 {
  21. req.PageSize = 10
  22. }
  23. p := &model.TStaff{}
  24. list, total, err := p.List(database.DB(), req.Filter, req.ProjectId, req.Page, req.PageSize)
  25. if err != nil {
  26. return nil, errors.DataBaseError
  27. }
  28. reply.Total = total
  29. reply.PageSize = req.PageSize
  30. reply.Page = req.Page
  31. if total == 0 {
  32. return reply, nil
  33. }
  34. workTypeMap, err := model.GetWorkTypeMap(database.DB())
  35. if err != nil {
  36. return nil, errors.DataBaseError
  37. }
  38. staffTypeMap, err := model.GetStaffTypeMap(database.DB())
  39. if err != nil {
  40. return nil, errors.DataBaseError
  41. }
  42. reply.List = make([]*v1.StaffListItem, len(list))
  43. for i, v := range list {
  44. item := &v1.StaffListItem{}
  45. utils.StructCopy(item, &list[i], "")
  46. item.Id = v.Id
  47. item.CreatedAt = v.CreatedAt.Format("2006-01-02 15:04:05")
  48. item.WorkType = workTypeMap[v.WorkType]
  49. item.StaffType = staffTypeMap[v.StaffType]
  50. item.Gender = "男"
  51. if v.Gender == 2 {
  52. item.Gender = "女"
  53. }
  54. reply.List[i] = item
  55. }
  56. return reply, nil
  57. }