search_employee_attendance.go 3.3 KB
package query

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
	"reflect"
	"strings"
	"time"

	"github.com/beego/beego/v2/core/validation"
)

type SearchEmployeeAttendanceQuery struct {
	// 查询偏离量
	Offset int `cname:"查询偏离量" json:"offset"`
	// 查询限制
	Limit int `cname:"查询限制" json:"limit"`
	// 当前公司
	CompanyId int `cname:"当前公司" json:"companyId,omitempty" valid:"Required"`
	// 当前登录的组织
	OrgId int `cname:"当前登录的组织" json:"orgId,omitempty" `
	// 匹配多个组织
	InOrgIds []int `cname:"匹配多个组织" json:"inOrgIds,omitempty" `
	// 页码
	PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
	// 页数
	PageSize int `cname:"页数" json:"pageSize,omitempty"`
	// 车间名称
	WorkshopName string `cname:"车间名称" json:"workshopName,omitempty"`
	// 生产线名称
	LineName string `cname:"生产线名称" json:"lineName,omitempty"`
	// 工段名称
	SectionName string `cname:"工段名称" json:"sectionName,omitempty"`
	// 姓名
	UserName string `cname:"姓名" json:"userName,omitempty"`
	// 考勤状态 1.未审核 2:已审核 3.自动审核
	AttendanceStatus int `cname:"考勤状态 1.未审核 2:已审核 3.自动审核" json:"attendanceStatus,omitempty"`
	// 员工类型  1:固定 2:派遣 3.临时
	EmployeeType int `cname:"1:固定 2:派遣 3.临时" json:"employeeType,omitempty"`
	// 开始时间
	BeginTime string `cname:"开始时间" json:"beginTime"`
	// 结束时间
	EndTime string `cname:"结束时间" json:"endTime"`
	// 考勤状态 1.未审核 2:已审核 3.自动审核
	InAttendanceStatus []int `cname:"考勤状态 1.未审核 2:已审核 3.自动审核" json:"inAttendanceStatus,omitempty"`

	// 开始时间
	SignBeginTime time.Time `cname:"开始时间" json:"signBeginTime"`
	// 结束时间
	SignEndTime time.Time `cname:"结束时间" json:"signEndTime"`
}

func (cmd *SearchEmployeeAttendanceQuery) Valid(validation *validation.Validation) {
	cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
	var err error
	if len(cmd.BeginTime) > 0 {
		if cmd.SignBeginTime, err = time.ParseInLocation("2006-01-02", cmd.BeginTime, time.Local); err != nil {
			log.Logger.Error(err.Error())
			validation.Error("开始时间有误")
			return
		}
	}
	if len(cmd.EndTime) > 0 {
		if cmd.SignEndTime, err = time.ParseInLocation("2006-01-02", cmd.EndTime, time.Local); err != nil {
			log.Logger.Error(err.Error())
			validation.Error("结束时间有误")
			return
		}
		cmd.SignEndTime = cmd.SignEndTime.AddDate(0, 0, 1)
		//if cmd.SignBeginTime.Equal(cmd.SignEndTime) {
		//	cmd.SignEndTime = cmd.SignEndTime.Add(time.Hour * 24)
		//}
	}
	//cmd.AttendanceStatus = 6 // 审核 + 自动审核
	cmd.InAttendanceStatus = []int{2, 3}
}

func (cmd *SearchEmployeeAttendanceQuery) ValidateQuery() error {
	valid := validation.Validation{}
	b, err := valid.Valid(cmd)
	if err != nil {
		return err
	}
	if !b {
		elem := reflect.TypeOf(cmd).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
}