| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package cn.com.goldenwater.dcproj.utils.export;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Date;
- /**
- * <p>
- *
- * </p>
- *
- * @author liyz
- * @date 2019/4/13 13:45
- **/
- public class FieldFormatDispatch {
- private static final String SK = "sk";
- private static final String RY = "ry";
- private static final String SH = "sh";
- /**
- * 获取格式化后的值
- *
- * @param type 表格类型,是sk,ry,sh ?
- * @param name 字段名称,
- * @param value 原始字段值
- * @return 格式化的字段值
- */
- public static Object getFormatValue(String type, String name, Object value, boolean isImp) {
- if (type == null || name == null) {
- return null;
- }
- switch (type) {
- case SK:
- return getSkFormatValue(name, value, isImp);
- case RY:
- return getRyFormatValue(name, value);
- case SH:
- return getShFormatValue(name, value);
- default:
- return "";
- }
- }
- private static Object getSkFormatValue(String name, Object value, boolean isImp) {
- Object formatObj;
- if (isImp) {
- formatObj = new FieldFormatSkImp();
- } else {
- formatObj = new FieldFormatSkExp();
- }
- Method[] methods = formatObj.getClass().getMethods();
- for (Method method : methods) {
- if (("format" + name).toLowerCase().equals(method.getName().toLowerCase())) {
- try {
- Class<?>[] parameterC = method.getParameterTypes();
- if (parameterC[0] == Date.class) {
- // checkNull 之后 日期有可能会变成 空字符 所以要判断一下
- if (value instanceof Date) {
- return method.invoke(formatObj, (Date) value);
- } else {
- return "";
- }
- } else {
- // 找到对应的方法,并把原始值传进去,返回格式化后的值
- return method.invoke(formatObj, value.toString());
- }
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
- }
- return value;
- }
- private static Object getRyFormatValue(String name, Object value) {
- if (name == null) {
- return null;
- }
- return "";
- }
- private static Object getShFormatValue(String name, Object value) {
- if (name == null) {
- return null;
- }
- return "";
- }
- }
|