pg_worker_attendance_report.go 3.1 KB
package domainService

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/dao"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
	"time"
)

type PGWorkerAttendanceReportService struct {
	transactionContext *pgTransaction.TransactionContext
}

// 考勤汇报
func (ptr *PGWorkerAttendanceReportService) Report(cid, oid int, report *domain.DeviceZkTeco) (interface{}, error) {
	var (
		attendanceRecordDao, _ = dao.NewAttendanceRecordDao(ptr.transactionContext)
		isSignIn               = true
		record                 *domain.ProductAttendanceRecord
		workStationId          string //具体工位
		workStation            *domain.WorkStation
		attendanceType         int
		worker                 *domain.User
		org                    *domain.Org
	)

	beginTime := utils.GetZeroTime(time.Now())
	_, records, _ := attendanceRecordDao.WorkerAttendanceRecords(cid, oid, worker.UserId, workStationId, beginTime, time.Now())
	for i := 0; i < len(records); i++ {
		r := records[i]
		// 操作时间 < 签到时间   签退时间 < 操作时间
		if (!r.SignIn.IsZero() && r.SignIn.After(report.ActionTime)) || (!r.SignOut.IsZero() && r.SignOut.Before(report.ActionTime)) {
			continue
		}

		if !r.SignIn.IsZero() && !r.SignOut.IsZero() {
			if r.SignIn.Before(report.ActionTime) && r.SignOut.After(report.ActionTime) {
				log.Logger.Info(fmt.Sprintf("【考勤汇报】 已存在同一时间段的考勤记录 考勤ID:%v   用户:%v  打卡时间:%v", r.ProductAttendanceId, worker.UserId, report.ActionTime))
				return struct{}{}, nil
			}
			continue
		}
		if !r.SignIn.IsZero() && r.SignOut.IsZero() && r.SignIn.Before(report.ActionTime) {
			isSignIn = false
			record = r
			break
		}
	}

	if isSignIn {
		record = &domain.ProductAttendanceRecord{
			//ProductAttendanceId: cmd.ProductAttendanceId,
			CompanyId:      cid,
			OrgId:          oid,
			AttendanceType: attendanceType,
			ProductWorker:  worker,
			WorkStation:    workStation,
			SignIn:         report.ActionTime,
			//SignOut:          cmd.SignOut,
			AttendanceStatus: domain.AttendanceNotApprove,
			WorkTimeBefore:   0,
			WorkTimeAfter:    0,
			CreatedAt:        time.Now(),
			UpdatedAt:        time.Now(),
			Ext:              domain.NewExt(org.OrgName).WithAttendanceExt(&domain.ProductAttendanceRecordExt{
				//GroupName:      productGroup.GroupName,
				//ProductGroupId: productGroup.ProductGroupId,
			}),
		}
	} else {
		record.WorkTimeBefore = record.ComputeWorkTimeBefore()
	}

	return struct{}{}, nil
}

func NewPGWorkerAttendanceReportService(transactionContext *pgTransaction.TransactionContext) (*PGWorkerAttendanceReportService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &PGWorkerAttendanceReportService{
			transactionContext: transactionContext,
		}, nil
	}
}