create_attendance.go 2.5 KB
package command

import (
	"fmt"
	"github.com/beego/beego/v2/core/validation"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
	"reflect"
	"strings"
)

type CreateAttendanceCommand struct {
	// 考勤记录ID
	//ProductAttendanceId int `cname:"考勤记录ID" json:"productAttendanceId" valid:"Required"`
	// 考勤类型 1.正常 2.支援
	AttendanceType int `cname:"考勤类型" json:"attendanceType,omitempty"`
	// 生产班组Id
	ProductGroupId int `cname:"生产班组Id" json:"productGroupId,omitempty" valid:"Required"`
	// 生产工人
	ProductWorkerId int `cname:"生产工人" json:"productWorkerId,omitempty" valid:"Required"`
	// 车间ID
	WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"`
	// 生产线ID
	LineId int `cname:"生产线ID" json:"lineId" valid:"Required"`
	// 工段ID
	SectionId int `cname:"工段ID" json:"sectionId" valid:"Required"`
	// 生产日期
	ProductDate string `cname:"生产日期"  json:"productDate,omitempty" valid:"Required"`
	// 签到
	SignIn string `cname:"上岗时间" json:"signIn,omitempty" valid:"Required"`
	// 签退
	SignOut string `cname:"下岗时间" json:"signOut,omitempty" valid:"Required"`
	// 考勤状态 1.未审核 2:审核
	AttendanceStatus int `cname:"考勤状态" json:"attendanceStatus,omitempty"`
	// 休息时长
	BreakTime float64 `cname:"休息时长" json:"breakTime"`
	// 工时
	WorkTime float64 `cname:"工时"  json:"workTime"`
	// 准备(true:准备完毕 false:进行预查)
	Prepared bool `json:"prepared"`
}

func (createAttendanceCommand *CreateAttendanceCommand) Valid(validation *validation.Validation) {
	if err := utils.ValidWorkTime(createAttendanceCommand.SignIn); err != nil {
		validation.Error(err.Error())
		return
	}
	if err := utils.ValidWorkTime(createAttendanceCommand.SignOut); err != nil {
		validation.Error(err.Error())
		return
	}
	validation.Range(createAttendanceCommand.AttendanceStatus, 1, 2, "attendanceStatus")
}

func (createAttendanceCommand *CreateAttendanceCommand) ValidateCommand() error {
	valid := validation.Validation{}
	b, err := valid.Valid(createAttendanceCommand)
	if err != nil {
		return err
	}
	if !b {
		elem := reflect.TypeOf(createAttendanceCommand).Elem()
		for _, validErr := range valid.Errors {
			field, isExist := elem.FieldByName(validErr.Field)
			if isExist {
				return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
			} else {
				return fmt.Errorf(validErr.Message)
			}
		}
	}
	return nil
}