attendance_list.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "google.golang.org/grpc/status"
  8. "smart-site-management/errors"
  9. "smart-site-management/model"
  10. "smart-site-management/pb/v1"
  11. "strings"
  12. "time"
  13. )
  14. func getStartAndEnd(reqStart int64, reqEnd int64)(start string, end string, month int, err error) {
  15. layout := "2006-01-02"
  16. now := time.Now()
  17. monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  18. monthEnd := now
  19. if reqStart > 0 && reqEnd > 0 {
  20. if time.Unix(reqStart, 0).Month() != time.Unix(reqEnd, 0).Month() {
  21. return "", "", 0, status.Error(10003, "不支持跨月搜索")
  22. }
  23. }
  24. switch {
  25. case reqStart > 0 && reqEnd > 0:
  26. monthStart = time.Unix(reqStart, 0)
  27. monthEnd = time.Unix(reqEnd, 0)
  28. return monthStart.Format(layout), monthEnd.Format(layout), int(monthStart.Month()), nil
  29. case reqStart > 0 && reqEnd == 0:
  30. monthStart = time.Unix(reqStart, 0)
  31. return monthStart.Format(layout), "", int(monthStart.Month()), nil
  32. case reqStart == 0 && reqEnd > 0 :
  33. monthEnd = time.Unix(reqEnd, 0)
  34. return "", monthEnd.Format(layout), int(monthEnd.Month()), nil
  35. }
  36. return monthStart.Format(layout), monthEnd.Format(layout), int(monthStart.Month()), nil
  37. }
  38. func parseAttendance(data string)(string, string, bool) {
  39. if len(data) != 21 {
  40. return "", "", false
  41. }
  42. length := len(data)
  43. atime := data[:length - 2]
  44. inout := data[length-2:]
  45. if string(inout) == "2" {
  46. return string(atime), "出", false
  47. }
  48. return string(atime), "进", true
  49. }
  50. func StaffAttendance(ctx context.Context, req *v1.StaffAttendanceRequest) (reply *v1.StaffAttendanceReply, err error) {
  51. reply = &v1.StaffAttendanceReply{}
  52. if req.ProjectId == 0 {
  53. return nil,errors.ParamsError
  54. }
  55. if req.Page < 1 {
  56. req.Page =1
  57. }
  58. if req.PageSize == 0 {
  59. req.PageSize = 10
  60. }
  61. p := &model.TStaffAttendance{}
  62. dreq := model.StaffAttendanceRequest{
  63. Page:req.Page,
  64. PageSize:req.PageSize,
  65. Filter:req.Filter,
  66. ProjectId:req.ProjectId,
  67. }
  68. start, end, month, err := getStartAndEnd(req.Start, req.End)
  69. if err != nil {
  70. return nil, err
  71. }
  72. p.TabType = month
  73. dreq.Start = start
  74. dreq.End = end
  75. list, total, err := p.List(database.DB(), dreq)
  76. if err != nil {
  77. return nil, errors.DataBaseError
  78. }
  79. reply.Total = total
  80. reply.PageSize = req.PageSize
  81. reply.Page = req.Page
  82. if total == 0 {
  83. return reply, nil
  84. }
  85. reply.List = make([]*v1.StaffAttendanceItem, len(list))
  86. for i, v := range list {
  87. item := &v1.StaffAttendanceItem{}
  88. item.Id = v.Id
  89. item.Date = v.DayTime.Format("2006-01-02")
  90. item.WorkType = v.WorkType
  91. item.StaffType = v.StaffType
  92. item.Gender = "男"
  93. if v.Gender == 2 {
  94. item.Gender = "女"
  95. }
  96. item.Name = v.Name
  97. item.WorkNo = v.WorkNo
  98. if v.Attendance != "" {
  99. item.List = []*v1.StaffAttendanceDesc{}
  100. array := strings.Split(v.Attendance, "/")
  101. for _, sub := range array {
  102. atime, inout, isIn:= parseAttendance(sub)
  103. if atime != "" {
  104. item.List = append(item.List, &v1.StaffAttendanceDesc{Time:atime, InOut:inout, IsIn:isIn})
  105. }
  106. }
  107. }
  108. reply.List[i] = item
  109. }
  110. return reply, nil
  111. }