search_device.go 1.7 KB
package query

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

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

type SearchDeviceQuery 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" valid:"Required"`
	// 页码
	PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
	// 页数
	PageSize int `cname:"页数" json:"pageSize,omitempty"`
	// 设备名称
	DeviceName string `json:"deviceName,omitempty"`
	// 设备状态 1:正常 2:封存 3:报废
	DeviceStatus int `json:"deviceStatus,omitempty"`
	// 排除的设备列表
	IncludeDevices []int `cname:"排除的设备列表" json:"includeDevices"`
}

func (cmd *SearchDeviceQuery) Valid(validation *validation.Validation) {
	cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
}

func (cmd *SearchDeviceQuery) 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
}