package command import ( "errors" "fmt" "regexp" "unicode/utf8" ) type OrderGoodData struct { //货品id Id int64 `json:"id"` //货品名称 长度可能较长 GoodName string `json:"goodName"` //预计的货品数量 PlanGoodNumber int `json:"planGoodNumber"` //货品单价 Price float64 `json:"price"` //合伙人分红比例 PartnerBonusPercent float64 `json:"partnerBonusPercent"` //备注信息 Remark string `json:"remark"` //行号-错误信息返回 LineNumber int `json:"lineNumber"` } func (postData OrderGoodData) Valid() error { lenProductName := utf8.RuneCountInString(postData.GoodName) if lenProductName == 0 { return errors.New("商品名称必填") } if lenProductName > 50 { return errors.New("商品名称最多50位") } if postData.PlanGoodNumber >= 1e16 { return errors.New("商品数量最多16位") } if postData.Price >= 1e16 { return errors.New("商品单价最多16位") } if postData.PartnerBonusPercent > 100 { return errors.New("合伙人分红比例超额") } partnerRatio := fmt.Sprint(postData.PartnerBonusPercent) regexpStr := `^(100|[1-9]\d|\d)(.\d{1,2})?$` ok := regexp.MustCompile(regexpStr).MatchString(partnerRatio) if !ok { return errors.New("合伙人分红比例精确到小数点2位") } return nil }