b6e62c4a54f0029eb94ba198a9e1bed8907fc1dd.svn-base 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package cn.com.goldenwater.dcproj.utils.export;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import java.util.Date;
  5. /**
  6. * <p>
  7. *
  8. * </p>
  9. *
  10. * @author liyz
  11. * @date 2019/4/13 13:45
  12. **/
  13. public class FieldFormatDispatch {
  14. private static final String SK = "sk";
  15. private static final String RY = "ry";
  16. private static final String SH = "sh";
  17. /**
  18. * 获取格式化后的值
  19. *
  20. * @param type 表格类型,是sk,ry,sh ?
  21. * @param name 字段名称,
  22. * @param value 原始字段值
  23. * @return 格式化的字段值
  24. */
  25. public static Object getFormatValue(String type, String name, Object value, boolean isImp) {
  26. if (type == null || name == null) {
  27. return null;
  28. }
  29. switch (type) {
  30. case SK:
  31. return getSkFormatValue(name, value, isImp);
  32. case RY:
  33. return getRyFormatValue(name, value);
  34. case SH:
  35. return getShFormatValue(name, value);
  36. default:
  37. return "";
  38. }
  39. }
  40. private static Object getSkFormatValue(String name, Object value, boolean isImp) {
  41. Object formatObj;
  42. if (isImp) {
  43. formatObj = new FieldFormatSkImp();
  44. } else {
  45. formatObj = new FieldFormatSkExp();
  46. }
  47. Method[] methods = formatObj.getClass().getMethods();
  48. for (Method method : methods) {
  49. if (("format" + name).toLowerCase().equals(method.getName().toLowerCase())) {
  50. try {
  51. Class<?>[] parameterC = method.getParameterTypes();
  52. if (parameterC[0] == Date.class) {
  53. // checkNull 之后 日期有可能会变成 空字符 所以要判断一下
  54. if (value instanceof Date) {
  55. return method.invoke(formatObj, (Date) value);
  56. } else {
  57. return "";
  58. }
  59. } else {
  60. // 找到对应的方法,并把原始值传进去,返回格式化后的值
  61. return method.invoke(formatObj, value.toString());
  62. }
  63. } catch (IllegalAccessException e) {
  64. e.printStackTrace();
  65. } catch (InvocationTargetException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. }
  70. return value;
  71. }
  72. private static Object getRyFormatValue(String name, Object value) {
  73. if (name == null) {
  74. return null;
  75. }
  76. return "";
  77. }
  78. private static Object getShFormatValue(String name, Object value) {
  79. if (name == null) {
  80. return null;
  81. }
  82. return "";
  83. }
  84. }