java使用Enum枚举做多字段匹配配置表
Posted XYLANCC
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java使用Enum枚举做多字段匹配配置表相关的知识,希望对你有一定的参考价值。
根据运费名称、税率、币种匹配获取物料编码(税率null匹配所有)
import cn.hutool.core.util.EnumUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@Getter
@AllArgsConstructor
public enum ProductCodeConvertEnum
InternalNoTaxRate(Arrays.asList("国内运费", "其他费用"), 0D, "CNY", "10356232"),
InternalWithTaxRate(Arrays.asList("国内运费", "其他费用"), 0.09D, "CNY", "10356543"),
Sea(Arrays.asList("海运费", "其他费用"), null, "USD", "10356876"),
Insure(Arrays.asList("保险费"), null, "CNY", "10359064");
//费用名称
List<String> label;
//税率
Double TaxRate;
//币种
String currency;
//物料编码
String code;
public String getValue()
return this.name();
/**
* 根据运费名称、税率、币种匹配获取物料编码(税率null匹配所有)
*
* @param label
* @param TaxRate
* @param currency
* @return
*/
public static String getCode(String label, double TaxRate, String currency)
ProductCodeConvertEnum productCodeEnum = EnumUtil.getBy(ProductCodeConvertEnum.class, e -> e.getLabel().contains(label)
&& (Objects.isNull(e.getTaxRate()) || e.getTaxRate() == TaxRate)
&& e.getCurrency().equals(currency));
return Objects.nonNull(productCodeEnum) ? productCodeEnum.getCode() : null;
根据外部编码获取对应默认工厂外部编码
import cn.hutool.core.util.EnumUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.List;
@Getter
@AllArgsConstructor
public enum DefaultFactoryEnum
GUDY("测试1", "1200", "1201"),
FGHJ("测试2", "1260", "1261"),
YUIO("测试3", "3070", "3071");
String label;
//外部编码
String referCode;
//默认工厂外部编码
String factoryReferCode;
public String getValue()
return this.name();
/**
* 根据外部编码获取对应默认工厂外部编码
*
* @param referCode
* @return
*/
public static String getDefaultFactoryReferCode(String referCode)
return EnumUtil.getFieldBy(DefaultFactoryEnum::getFactoryReferCode, DefaultFactoryEnum::getReferCode, referCode);
/**
* 获取所有外部编码
*
* @return
*/
public static List<Object> getReferCodes()
return EnumUtil.getFieldValues(DefaultFactoryEnum.class, "referCode");
Flask 学习-71.Flask-RESTX 枚举类型Enum字段
前言
model 中有枚举类型字段使用示例
模型设计
class Teachers(db.Model):
__tablename__ = 'teacher' # 数据库表名
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(50), unique=True, nullable=False)
size = db.Column(db.Enum("SMALL", "MEDIUM", "LARGE"), default="MEDIUM")
address = db.Column(db.String(150), nullable=True)
def save(self):
"""保存数据"""
db.session.add(self)
db.session.commit()
def __repr__(self):
return f"<Teachers(id='self.id', name='self.name'...)>"
同步到数据库
flask db migrate
flask db upgrade
同步后数据库里面可以看到 size 字段是枚举类型
校验枚举类型
请求入参校验枚举类型, 添加enum 参数
teacher_model = api.model('TeacherModel',
'name': fields.String(
max_length=50, required=True, description='姓名'),
'size': fields.String(
enum=["SMALL", "MEDIUM", "LARGE"], required=False, description='型号'),
'address': fields.String(max_length=150, description='邮箱')
)
视图
@api.route('/teacher')
class TeacherView(Resource):
@api.doc(description='新增老师')
@api.marshal_with(teacher_model)
@api.expect(teacher_model, validate=True)
def post(self):
"""
add teacher
"""
api.logger.info(f"获取请求参数: api.payload")
t = Teachers(
name=api.payload.get('name'),
size=api.payload.get('size'),
address=api.payload.get('address'),
)
t.save() # 保存数据
return t, HTTPStatus.CREATED
测试接口
POST http://127.0.0.1:5000/api/v1/teacher HTTP/1.1
User-Agent: Fiddler
Host: 127.0.0.1:5000
Content-Length: 33
Content-Type: application/json
"name": "yoyo", "size": "SMALL"
HTTP/1.0 201 CREATED
Content-Type: application/json
Content-Length: 65
Server: Werkzeug/2.0.2 Python/3.8.5
Date: Thu, 22 Sep 2022 01:22:52 GMT
"name": "yoyo",
"size": "SMALL",
"address": null
参数不合法,返回400 BAD REQUEST
以上是关于java使用Enum枚举做多字段匹配配置表的主要内容,如果未能解决你的问题,请参考以下文章