product_attendance_record.go 5.1 KB
package domain

import (
	"errors"
	"github.com/linmadan/egglib-go/utils/xtime"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
	"time"
)

const (
	AttendanceNotApprove   = 1 // 未审核
	AttendanceApproved     = 2 // 已审核
	AttendanceAutoApproved = 3 // 自动审核
)

// 生产考勤记录
type ProductAttendanceRecord struct {
	// 考勤记录ID
	ProductAttendanceId int `json:"productAttendanceId,omitempty"`
	// 企业id
	CompanyId int `json:"companyId,omitempty"`
	// 组织ID
	OrgId int `json:"orgId,omitempty"`
	// 考勤类型 1.正常 2.支援
	AttendanceType int `json:"attendanceType,omitempty"`
	// 生产工人
	ProductWorker *User `json:"productWorker,omitempty"`
	// 工作位置
	WorkStation *WorkStation `json:"workStation,omitempty"`
	// 签到
	SignIn time.Time `json:"signIn,omitempty"`
	// 签退
	SignOut time.Time `json:"signOut,omitempty"`
	// 考勤状态 1.未审核 2:已审核 3.自动审核
	AttendanceStatus int `json:"attendanceStatus,omitempty"`
	// 工时(审核前)
	WorkTimeBefore float64 `json:"workTimeBefore,omitempty"`
	// 工时(审核后)
	WorkTimeAfter float64 `json:"workTimeAfter,omitempty"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt,omitempty"`
	// 更新时间
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
	// 删除时间
	DeletedAt time.Time `json:"deletedAt,omitempty"`
	// 扩展数据
	Ext *Ext `json:"ext,omitempty"`
	// 生产日期
	ProductDate time.Time `json:"productDate,omitempty"`
}

type ProductAttendanceRecordRepository interface {
	Save(productAttendanceRecord *ProductAttendanceRecord) (*ProductAttendanceRecord, error)
	Remove(productAttendanceRecord *ProductAttendanceRecord) (*ProductAttendanceRecord, error)
	FindOne(queryOptions map[string]interface{}) (*ProductAttendanceRecord, error)
	Find(queryOptions map[string]interface{}) (int64, []*ProductAttendanceRecord, error)
}

func (productAttendanceRecord *ProductAttendanceRecord) Identify() interface{} {
	if productAttendanceRecord.ProductAttendanceId == 0 {
		return nil
	}
	return productAttendanceRecord.ProductAttendanceId
}

func (productAttendanceRecord *ProductAttendanceRecord) Update(data map[string]interface{}) error {
	return nil
}

// 计算审核前工时
func (productAttendanceRecord *ProductAttendanceRecord) ComputeWorkTimeBefore(productCalendar *ProductCalendar) float64 {
	if productAttendanceRecord.SignIn.IsZero() {
		return 0
	}
	if productAttendanceRecord.SignOut.IsZero() {
		return 0
	}
	if !productAttendanceRecord.SignOut.After(productAttendanceRecord.SignIn) {
		return 0
	}
	wt := utils.Round(productAttendanceRecord.SignOut.Sub(productAttendanceRecord.SignIn).Hours(), 1)
	if productCalendar == nil {
		return wt
	}
	wt = wt - productCalendar.BreakTime
	if wt < 0 {
		return 0
	}
	return wt
}

func (productAttendanceRecord *ProductAttendanceRecord) SetProductTimeByProductCalendar(productCalendar *ProductCalendar) error {
	now := xtime.New(productAttendanceRecord.SignIn)
	productAttendanceRecord.ProductDate = now.BeginningOfDay()
	if productCalendar == nil {
		return nil
	}
	overDay, err := productCalendar.CheckOverDay(productAttendanceRecord.SignIn)
	if err != nil {
		return nil
	}
	newNow := now.BeginningOfDay().Add(-time.Hour * 24)
	if overDay && productCalendar.MatchCalendarSelected(newNow) {
		productAttendanceRecord.ProductDate = newNow
	}
	return nil
}

func (productAttendanceRecord *ProductAttendanceRecord) Approve(approveUser *User, workTimeAfter float64, status int) error {
	if productAttendanceRecord.AttendanceStatus == AttendanceApproved || productAttendanceRecord.AttendanceStatus == AttendanceAutoApproved {
		return errors.New("已审核")
	}
	productAttendanceRecord.AttendanceStatus = status
	productAttendanceRecord.WorkTimeAfter = workTimeAfter
	if productAttendanceRecord.Ext != nil {
		if productAttendanceRecord.Ext.AttendanceExt == nil {
			productAttendanceRecord.Ext.AttendanceExt = &ProductAttendanceRecordExt{}
		}
		productAttendanceRecord.Ext.AttendanceExt.ApproveUserId = approveUser.UserId
		productAttendanceRecord.Ext.AttendanceExt.ApproveUserName = approveUser.UserName
		productAttendanceRecord.Ext.AttendanceExt.ApproveAt = time.Now().Unix()
	}
	return nil
}

func (productAttendanceRecord *ProductAttendanceRecord) ProductTime() time.Time {
	attendanceExt := productAttendanceRecord.Ext.AttendanceExt
	if attendanceExt != nil && !xtime.IsZero(productAttendanceRecord.ProductDate) {
		return productAttendanceRecord.ProductDate
	}
	return productAttendanceRecord.SignIn
}

func (productAttendanceRecord *ProductAttendanceRecord) GroupName() string {
	if productAttendanceRecord.Ext != nil {
		return ""
	}
	if productAttendanceRecord.Ext.AttendanceExt != nil {
		return ""
	}
	return productAttendanceRecord.Ext.AttendanceExt.GroupName
}

func (productAttendanceRecord *ProductAttendanceRecord) ApproveUser() *User {
	if productAttendanceRecord.Ext != nil {
		return nil
	}
	if productAttendanceRecord.Ext.AttendanceExt != nil {
		return nil
	}
	if productAttendanceRecord.Ext.AttendanceExt.ApproveUserId == 0 {
		return nil
	}
	return &User{
		UserId:   productAttendanceRecord.Ext.AttendanceExt.ApproveUserId,
		UserName: productAttendanceRecord.Ext.AttendanceExt.ApproveUserName,
	}
}