create_product_plan.go 2.6 KB
package command

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

type CreateProductPlanCommand struct {
	// 企业id
	CompanyId int `cname:"企业id" json:"companyId" valid:"Required"`
	// 组织ID
	OrgId int `cname:"组织ID" json:"orgId" valid:"Required"`
	// 车间ID
	WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"`
	// 批号
	BatchNumber string `cname:"批号" json:"batchNumber"`
	// 生产日期
	ProductDate string `cname:"生产日期" json:"productDate" valid:"Required"`
	// 上班班次 1:全天  2:白班 4:中班  8:夜班
	WorkOn int `cname:"上班班次 1:全天  2:白班 4:中班  8:夜班" json:"workOn"`
	// 机台 (A、B、C、D 区分机器大小)
	Machine string `cname:"机台 (A、B、C、D 区分机器大小)" json:"machine"`
	// 计划的产品名称
	//PlanProductName string `cname:"计划的产品名称" json:"planProductName"`
	// 产品ID
	ProductId int `cname:"产品ID" json:"productId" valid:"Required"`
	// 投入量规格 默认份
	DevotedUnit string `cname:"投入量规格" json:"devotedUnit" `
	// 数量(保留两位小数)
	Quantity float64 `cname:"数量(保留两位小数)" json:"quantity" valid:"Required"`
	// 重量
	Weight float64 `cname:"重量" json:"weight" valid:"Required"`
	// 备注
	Remark string `cname:"备注" json:"remark"`
	// 生产日期
	ProductDateTime time.Time `cname:"生产日期" json:"-" `
}

func (createProductPlanCommand *CreateProductPlanCommand) Valid(validation *validation.Validation) {
	//validation.SetError("CustomValid", "未实现的自定义认证")
	if createProductPlanCommand.WorkOn > 0 {
		if err := domain.ValidWorkOn(createProductPlanCommand.WorkOn); err != nil {
			validation.Error(err.Error())
			return
		}
	}
	if t, err := time.Parse("2006-01-02", createProductPlanCommand.ProductDate); err != nil {
		validation.Error("时间格式有误" + createProductPlanCommand.ProductDate)
		return
	} else {
		createProductPlanCommand.ProductDateTime = t
	}
}

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