attendance_record_dao.go 3.6 KB
package dao

import (
	"fmt"
	"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
	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/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
	"time"
)

type AttendanceRecordDao struct {
	transactionContext *pgTransaction.TransactionContext
}

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

// 生产工段的考勤记录
func (dao *AttendanceRecordDao) ProductWorkStationAttendanceRecord(companyId, orgId int, workStationId string, productTime time.Time) (int64, []*domain.ProductAttendanceRecord, error) {
	tx := dao.transactionContext.PgTx
	var productAttendanceRecordModels []*models.ProductAttendanceRecord
	productAttendanceRecords := make([]*domain.ProductAttendanceRecord, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&productAttendanceRecordModels), map[string]interface{}{})
	query.Where("company_id = ?", companyId)
	query.Where("org_id = ?", orgId)
	query.Where("work_station->>'workStationId' = ?", workStationId)
	query.Where("sign_in <= ?", productTime)
	query.Where("sign_out >= ?", productTime)
	query.SetOffsetAndLimit(domain.MaxQueryRow)
	query.SetOrderDirect("product_attendance_id", "DESC")
	if count, err := query.SelectAndCount(); err != nil {
		return 0, productAttendanceRecords, err
	} else {
		for _, productAttendanceRecordModel := range productAttendanceRecordModels {
			if productAttendanceRecord, err := transform.TransformToProductAttendanceRecordDomainModelFromPgModels(productAttendanceRecordModel); err != nil {
				return 0, productAttendanceRecords, err
			} else {
				productAttendanceRecords = append(productAttendanceRecords, productAttendanceRecord)
			}
		}
		return int64(count), productAttendanceRecords, nil
	}
}

// 近n天未审核的考勤记录
// fromLastHour 从前 n小时开始
// recentDay 近 n天未审核的数据
func (dao *AttendanceRecordDao) RecentUnApprovedAttendanceRecord(fromLastHour int, recentDay int) (int64, []*domain.ProductAttendanceRecord, error) {
	tx := dao.transactionContext.PgTx
	endTime := time.Now().Add(-(time.Hour * time.Duration(fromLastHour)))
	beginTime := endTime.Add(-(time.Hour * time.Duration(recentDay*24)))
	var productAttendanceRecordModels []*models.ProductAttendanceRecord
	productAttendanceRecords := make([]*domain.ProductAttendanceRecord, 0)
	query := sqlbuilder.BuildQuery(tx.Model(&productAttendanceRecordModels), map[string]interface{}{})
	query.Where("sign_in <= ?", endTime)
	query.Where("sign_in >= ?", beginTime)
	query.Where("attendance_status = ?", domain.AttendanceNotApprove)
	query.Where("work_time_before>0")
	query.SetOffsetAndLimit(domain.MaxQueryRow)
	query.SetOrderDirect("product_attendance_id", "DESC")
	if count, err := query.SelectAndCount(); err != nil {
		return 0, productAttendanceRecords, err
	} else {
		for _, productAttendanceRecordModel := range productAttendanceRecordModels {
			if productAttendanceRecord, err := transform.TransformToProductAttendanceRecordDomainModelFromPgModels(productAttendanceRecordModel); err != nil {
				return 0, productAttendanceRecords, err
			} else {
				productAttendanceRecords = append(productAttendanceRecords, productAttendanceRecord)
			}
		}
		return int64(count), productAttendanceRecords, nil
	}
}