search_product_plan.go 1.6 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 SearchProductPlanQuery 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"`
	// 批号
	BatchNumber string `cname:"批号" json:"batchNumber"`
	// 车间名称
	WorkshopName string `cname:"车间名称" json:"workshopName"`
}

func (cmd *SearchProductPlanQuery) Valid(validation *validation.Validation) {
	//validation.SetError("CustomValid", "未实现的自定义认证")
	cmd.Offset, cmd.Limit = domain.Pagination(cmd.PageNumber, cmd.PageSize)
}

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