废话少说,放码出来。
and(不得不说,我这种菜鸟备甩了三条街啊)
因为存在多个字段,而且每个字段的校验规则不一样,正常的思路是写多个方法多每一个字段进行校验。这样就会调用太多方法。而通过重写方法则可以让方法自动适应,在程序上调用同一个方法即可。
//字段校验总引用
public BaseResp validateFileUploadReq(Integer initDate, String exchangeCode,
String fileType, String filePath, String fileName,
String reissueFlag, String fileMd5, String busiDatetime){
BaseResp resp = new BaseResp();
ValidateParamResult paramResult = new ValidateParamResult();
paramResult.excValidate("业务日期", initDate);
paramResult.excValidate("交易所编号", exchangeCode, 32);
paramResult.excValidate("文件类型", fileType, 1);
paramResult.excValidate("文件路径", filePath, 255);
paramResult.excValidate("文件名称", fileName, 255);
paramResult.excValidate("重发标志", reissueFlag, 1);
paramResult.excValidate("文件MD5码", fileMd5, 64);
paramResult.excValidate("业务日期时间", busiDatetime, 14,14);
if (!paramResult.isValidateSuccess()) {
resp.setErrorNo(ClearExceptionConstants.ERROR_NO_PARAMS_ERROR, paramResult.toString());
}
return resp;
}
//默认的多个字段校验方法(通过重写)
public void putErrorField(String field, Boolean isEmpty, Integer minLen, Integer maxLen) {
if (StringUtils.isBlank(field)) {
return;
}
if (errorFields == null) {
errorFields = new ArrayList<Map<String,Object>>();
}
Map<String, Object> map = new HashMap<String, Object>();
errorFields.add(map);
if (isEmpty) {
map.put("field", field);
map.put("emptyValidate", "该值不能为空");
}
if (minLen != null) {
map.put("field", field);
map.put("minValidate", "该值长度不能小于" + minLen + "位");
}
if (maxLen != null) {
map.put("field", field);
map.put("maxValidate", "该值长度不能大于" + maxLen + "位");
}
if (map.containsKey("field")) {
this.flag = Integer.valueOf(FundDictEntryConstants.RESP_FLAG_SYSEXCEPTION);
}
}
/**
*
* @param field 校验字段别名
* @param val 校验字段值
* @param isValidateEmpty 是否做非空校验
* @param minLen 最小长度值
* @param maxLen 最大长度值
*/
public void excValidate(String field, Object val, Boolean isValidateEmpty, Integer minLen, Integer maxLen){
if (isValidateEmpty == null) {
isValidateEmpty = false;
}
if (isValidateEmpty && (val == null || (val instanceof String && StringUtils.isEmpty((String)val)))) {
putErrorField(field);
return;
}
if (!isValidateEmpty && val == null) {
minLen = null;
maxLen = null;
}else if (val instanceof String) {
if (minLen != null && ((String) val).length() >= minLen) {
minLen = null;
}
if (maxLen != null && ((String) val).length() <= maxLen) {
maxLen = null;
}
}else if (val instanceof Long) {
if (minLen != null
&& ((Long) val).compareTo(Long.valueOf((long) Math.pow(10, minLen - 1))) >= 0) {
minLen = null;
}
if (maxLen != null
&& ((Long) val).compareTo(Long.valueOf((long) Math.pow(10, maxLen))) < 0) {
maxLen = null;
}
}else {
minLen = null;
maxLen = null;
}
putErrorField(field, false, minLen, maxLen);
}
/**
*
* @param field 校验字段别名
* @param val 校验字段值
* @param isValidateEmpty 是否做非空校验
* @param maxLen 最大长度值
*/
public void excValidate(String field, Object val, Boolean isValidateEmpty, Integer maxLen){
excValidate(field, val, isValidateEmpty, null, maxLen);
}
/**
* 默认加上非空校验
* @param field 校验字段别名
* @param val 校验字段值
* @param minLen 最小长度值
* @param maxLen 最大长度值
*/
public void excValidate(String field, Object val, Integer minLen, Integer maxLen){
excValidate(field, val, true, minLen, maxLen);
}
/**
* 默认加上非空校验
* @param field 校验字段别名
* @param val 校验字段值
* @param maxLen 最大长度值
*/
public void excValidate(String field, Object val, Integer maxLen){
excValidate(field, val, true, null, maxLen);
}
/**
* 做非空校验
* @param field 校验字段别名
* @param val 校验字段值
*/
public void excValidate(String field, Object val){
excValidate(field, val, true, null, null);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
if (CollectionUtils.isNotEmpty(errorFields)) {
stringBuilder.append("[");
for (Map<String, Object> temp : errorFields) {
if (!temp.containsKey("field")) {
continue;
}
stringBuilder.append("|" + temp.get("field") + ":");
if (temp.containsKey("emptyValidate")) {
stringBuilder.append(temp.get("emptyValidate") + ";");
}
if (temp.containsKey("minValidate")) {
stringBuilder.append(temp.get("minValidate") + ";");
}
if (temp.containsKey("maxValidate")) {
stringBuilder.append(temp.get("maxValidate") + ";");
}
stringBuilder.append("|");
}
stringBuilder.append("]");
}
return stringBuilder.toString();
}