123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package staff
- import (
- "context"
- "github.com/jaryhe/gopkgs/database"
- "google.golang.org/grpc/status"
- "smart-site-management/errors"
- "smart-site-management/model"
- "smart-site-management/pb/v1"
- "strings"
- "time"
- )
- func getStartAndEnd(reqStart int64, reqEnd int64)(start string, end string, month int, err error) {
- layout := "2006-01-02"
- now := time.Now()
- monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
- monthEnd := now
- if reqStart > 0 && reqEnd > 0 {
- if time.Unix(reqStart, 0).Month() != time.Unix(reqEnd, 0).Month() {
- return "", "", 0, status.Error(10003, "不支持跨月搜索")
- }
- }
- switch {
- case reqStart > 0 && reqEnd > 0:
- monthStart = time.Unix(reqStart, 0)
- monthEnd = time.Unix(reqEnd, 0)
- return monthStart.Format(layout), monthEnd.Format(layout), int(monthStart.Month()), nil
- case reqStart > 0 && reqEnd == 0:
- monthStart = time.Unix(reqStart, 0)
- return monthStart.Format(layout), "", int(monthStart.Month()), nil
- case reqStart == 0 && reqEnd > 0 :
- monthEnd = time.Unix(reqEnd, 0)
- return "", monthEnd.Format(layout), int(monthEnd.Month()), nil
- }
- return monthStart.Format(layout), monthEnd.Format(layout), int(monthStart.Month()), nil
- }
- func parseAttendance(data string)(string, string, bool) {
- if len(data) != 21 {
- return "", "", false
- }
- length := len(data)
- atime := data[:length - 2]
- inout := data[length-2:]
- if string(inout) == "2" {
- return string(atime), "出", false
- }
- return string(atime), "进", true
- }
- func StaffAttendance(ctx context.Context, req *v1.StaffAttendanceRequest) (reply *v1.StaffAttendanceReply, err error) {
- reply = &v1.StaffAttendanceReply{}
- if req.ProjectId == 0 {
- return nil,errors.ParamsError
- }
- if req.Page < 1 {
- req.Page =1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- p := &model.TStaffAttendance{}
- dreq := model.StaffAttendanceRequest{
- Page:req.Page,
- PageSize:req.PageSize,
- Filter:req.Filter,
- ProjectId:req.ProjectId,
- }
- start, end, month, err := getStartAndEnd(req.Start, req.End)
- if err != nil {
- return nil, err
- }
- p.TabType = month
- dreq.Start = start
- dreq.End = end
- list, total, err := p.List(database.DB(), dreq)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.Total = total
- reply.PageSize = req.PageSize
- reply.Page = req.Page
- if total == 0 {
- return reply, nil
- }
- reply.List = make([]*v1.StaffAttendanceItem, len(list))
- for i, v := range list {
- item := &v1.StaffAttendanceItem{}
- item.Id = v.Id
- item.Date = v.DayTime.Format("2006-01-02")
- item.WorkType = v.WorkType
- item.StaffType = v.StaffType
- item.Gender = "男"
- if v.Gender == 2 {
- item.Gender = "女"
- }
- item.Name = v.Name
- item.WorkNo = v.WorkNo
- if v.Attendance != "" {
- item.List = []*v1.StaffAttendanceDesc{}
- array := strings.Split(v.Attendance, "/")
- for _, sub := range array {
- atime, inout, isIn:= parseAttendance(sub)
- if atime != "" {
- item.List = append(item.List, &v1.StaffAttendanceDesc{Time:atime, InOut:inout, IsIn:isIn})
- }
- }
- }
- reply.List[i] = item
- }
- return reply, nil
- }
|