| /** | 
|  * Copyright (c) 2018 人人开源 All rights reserved. | 
|  * | 
|  * https://www.renren.io | 
|  * | 
|  * 版权所有,侵权必究! | 
|  */ | 
|   | 
| package com.zt.core.db.annotation; | 
|   | 
| import cn.hutool.core.date.DateUtil; | 
| import com.zt.common.annotation.QueryParam; | 
| import com.zt.common.constant.Constant; | 
| import com.zt.common.db.query.QueryFilter; | 
| import io.swagger.annotations.ApiImplicitParam; | 
| import io.swagger.annotations.ApiImplicitParams; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.core.MethodParameter; | 
| import org.springframework.stereotype.Component; | 
| import org.springframework.web.bind.support.WebDataBinderFactory; | 
| import org.springframework.web.context.request.NativeWebRequest; | 
| import org.springframework.web.method.annotation.RequestParamMapMethodArgumentResolver; | 
| import org.springframework.web.method.support.ModelAndViewContainer; | 
|   | 
| import java.lang.annotation.Annotation; | 
| import java.util.ArrayList; | 
| import java.util.Arrays; | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * 有@LoginUser注解的方法参数,注入当前登录用户 | 
|  * | 
|  * @author hehz | 
|  */ | 
| @Component("queryParamResolver") | 
| public class QueryParamMethodArgumentResolver extends RequestParamMapMethodArgumentResolver { | 
|   | 
|     @Override | 
|     public boolean supportsParameter(MethodParameter parameter) { | 
|         return parameter.getParameterType().isAssignableFrom(QueryFilter.class) | 
|                 && parameter.hasParameterAnnotation(QueryParam.class); | 
|     } | 
|   | 
|     @Override | 
|     public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest request, | 
|             WebDataBinderFactory factory) throws Exception { | 
|         Map<String, Object> map = (Map<String, Object>) super.resolveArgument(parameter, container, request, factory); | 
|   | 
|         List<com.zt.common.db.query.QueryParam> params = new ArrayList<>(); | 
|   | 
|         List<ApiImplicitParam> apiImplicitParamList = new ArrayList<>(); | 
|         // 1、获取ApiImplicitParams和ApiImplicitParam注解 | 
|         for (Annotation annotation : parameter.getMethod().getDeclaredAnnotations()) { | 
|             if (annotation instanceof ApiImplicitParams) { | 
|                 ApiImplicitParams apiImplicitParams = (ApiImplicitParams) annotation; | 
|                 apiImplicitParamList.addAll(Arrays.asList(apiImplicitParams.value())); | 
|                 break; | 
|             } else if (annotation instanceof ApiImplicitParam) { | 
|                 apiImplicitParamList.add((ApiImplicitParam) annotation); | 
|             } | 
|         } | 
|         // 2、获取ApiImplicitParam指定的参数 | 
|         for (ApiImplicitParam implicitParam : apiImplicitParamList) { | 
|             String type = implicitParam.dataType(); | 
|             Object value = map.get(implicitParam.name()); | 
|             if (value != null && StringUtils.isNotBlank(value.toString())) { | 
|                 value = getValue(type, value.toString().trim()); | 
|             } | 
|             params.add(new com.zt.common.db.query.QueryParam(implicitParam.name(), value, implicitParam.format())); | 
|         } | 
|         return new QueryFilter(params); | 
|     } | 
|   | 
|     private static Object getValue(String type, String valStr) { | 
|         Object o = null; | 
|         if (type.equals(Constant.QT.INT)) { | 
|             o = Integer.parseInt(valStr); | 
|         } else if (type.equalsIgnoreCase("short")) { | 
|             o = Short.parseShort(valStr); | 
|         } else if (type.equalsIgnoreCase(Constant.QT.LONG)) { | 
|             o = Long.parseLong(valStr); | 
|         } else if (type.equalsIgnoreCase("float")) { | 
|             o = Float.parseFloat(valStr); | 
|         } else if (type.equalsIgnoreCase(Constant.QT.DOUBLE)) { | 
|             o = Double.parseDouble(valStr); | 
|         } else if (type.equalsIgnoreCase("boolean")) { | 
|             o = Boolean.parseBoolean(valStr); | 
|         } else if (type.equals(Constant.QT.STRING)) { | 
|             o = valStr; | 
|         } else if ("varchar".equalsIgnoreCase(type)) { | 
|             o = valStr; | 
|         } else if ("number".equalsIgnoreCase(type)) { | 
|             o = Double.parseDouble(valStr); | 
|         } else if ("date".equalsIgnoreCase(type)) { | 
|             try { | 
|                 o = DateUtil.parse(valStr); | 
|             } catch (Exception e) { | 
|             } | 
|         } else { | 
|             o = valStr; | 
|         } | 
|         return o; | 
|     } | 
| } |