search_unit_conversion.go 1.5 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 SearchUnitConversionQuery 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" valid:"Required"`
	// 页码
	PageNumber int `cname:"页码" json:"pageNumber,omitempty"`
	// 页数
	PageSize int `cname:"页数" json:"pageSize,omitempty"`
	// 物料名称
	MaterialName string `cname:"物料名称" json:"materialName"`
	// 智能称重标识 0:全部 1:是 2:否
	IntelligentWeighingFlag int `cname:"智能称重标识" json:"intelligentWeighingFlag"`
}

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

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