pg_approve_attendance_records.go 2.0 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/repository"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
)

type PGApproveAttendanceRecordsService struct {
	transactionContext *pgTransaction.TransactionContext
}

// 批量审核考勤记录
func (ptr *PGApproveAttendanceRecordsService) BatchApproveAttendanceRecords(opt *domain.OperateInfo, list []*domain.ProductAttendanceRecord, approveUserId int, workTime float64, status int) (interface{}, error) {
	var productAttendanceRecordRepository, _ = repository.NewProductAttendanceRecordRepository(ptr.transactionContext)
	var attendance *domain.ProductAttendanceRecord
	var err error
	log.Logger.Debug("【自动审核考勤记录任务】 启动")
	var user *domain.User = &domain.User{}
	userService := NewUserService()
	if approveUserId > 0 {
		user, err = userService.User(approveUserId)
		if err != nil {
			return nil, err
		}
	}

	for i := range list {
		attendance = list[i]
		t := attendance.WorkTimeBefore
		if workTime > 0 {
			t = workTime
		}
		if t <= 0 && status == domain.AttendanceAutoApproved {
			continue
		}
		if err = attendance.Approve(user, t, status); err != nil {
			return nil, err
		}
		if _, err := productAttendanceRecordRepository.Save(attendance); err != nil {
			return nil, err
		}
	}

	for i := range list {
		attendance = list[i]
		if e := SendWorkshopWorkTimeStaticJob(attendance); e != nil {
			log.Logger.Error("【发送工时统计任务失败】" + e.Error())
		}
	}
	return struct{}{}, nil
}

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