AjaxJson.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.hywa.banktest.common;
  2. import com.alibaba.fastjson.JSON;
  3. import java.util.Collection;
  4. /**
  5. * 请求返回 数据封装
  6. * by yfw
  7. * */
  8. public class AjaxJson {
  9. public static final Integer SUCCESS = 0;
  10. public static final Integer FAILURE = 1;
  11. public static final String MSGSUCCESS = "操作成功!";
  12. public static final String MSGFAILURE = "操作失败!";
  13. public static final String DEAL_CLEARING_APPLY_SUCCESS="处理客户打款请求成功!";
  14. /**
  15. * code 0:成功 ,1:失败
  16. * */
  17. private Integer code = SUCCESS;
  18. /**
  19. * 返回提示消息
  20. * **/
  21. private String msg = "success";
  22. /**
  23. * 返回对象数据
  24. *
  25. * */
  26. private Object data;
  27. /**
  28. *
  29. * 总条数
  30. * 选填
  31. * */
  32. private Integer total =SUCCESS;
  33. public void setTotal(Integer total) {
  34. this.total = total;
  35. }
  36. public AjaxJson(Object data){
  37. this.data = data;
  38. }
  39. public AjaxJson(Object data, Integer total){
  40. this.data = data;
  41. this.total = total;
  42. }
  43. public AjaxJson(Integer code, String msg, Object data) {
  44. super();
  45. this.code = code;
  46. this.msg = msg;
  47. this.data = data;
  48. }
  49. public AjaxJson(Integer code, String msg) {
  50. super();
  51. this.code = code;
  52. this.msg = msg;
  53. }
  54. public AjaxJson(ResponseEnum responseEnum){
  55. super();
  56. this.code= responseEnum.getCode();
  57. this.msg = responseEnum.getMsg();
  58. }
  59. public AjaxJson(){}
  60. @Override
  61. public String toString() {
  62. if(null ==this.data) {
  63. this.data = new StringBuilder("当前数据为空!");
  64. }
  65. return "AjaxJson [code=" + code + ", msg=" + msg + ", data=" +JSON.toJSONString(data) + ", total=" + total + "]";
  66. }
  67. public Integer getCode() {
  68. return code;
  69. }
  70. public String getMsg() {
  71. return msg;
  72. }
  73. public Object getData() {
  74. return data;
  75. }
  76. public Integer getTotal() {
  77. //外部赋值的情况
  78. if(this.total > 0 ) {
  79. return this.total;
  80. }
  81. //TODO 集合数据处理
  82. if(data instanceof Collection<?>){
  83. Collection<?> collection = (Collection<?>)data;
  84. if(null !=collection && !collection.isEmpty()){
  85. int size = collection.size();
  86. this.total = size;
  87. }
  88. }
  89. return this.total;
  90. }
  91. }