AdmCreateResponse.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package com.persagy.proxy.adm.request;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.persagy.proxy.adm.utils.AdmEntityTransferUtil;
  4. import com.persagy.proxy.common.entity.DmpResult;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * ADM 通用创建返回结果
  12. * @author Charlie Yu
  13. * @date 2021-08-16
  14. */
  15. @Data
  16. @NoArgsConstructor
  17. @AllArgsConstructor
  18. public class AdmCreateResponse {
  19. private final static String SUCCESS = "success";
  20. private final static String FAILURE = "failure";
  21. /** 结果 */
  22. private String result;
  23. /** 消息 */
  24. private String message;
  25. /** 错误数据主从对象 1 主对象 2 从对象 */
  26. private String errorType;
  27. /** 查询到的数据列表 */
  28. private List<?> entityList;
  29. public static AdmCreateResponse success() {
  30. return new AdmCreateResponse(SUCCESS, null, null, null);
  31. }
  32. public static AdmCreateResponse failure(String message) {
  33. return new AdmCreateResponse(FAILURE, message, null, new ArrayList<>());
  34. }
  35. public static AdmCreateResponse failure(String message,String errorType) {
  36. return new AdmCreateResponse(FAILURE, message, errorType, new ArrayList<>());
  37. }
  38. public static AdmCreateResponse success(List<?> content) {
  39. return new AdmCreateResponse(SUCCESS, null, null, content);
  40. }
  41. /**
  42. * 从数据中台结果转换
  43. * @param admRequest
  44. * @param dmpResult
  45. * @param clazz
  46. * @param <T>
  47. * @return
  48. */
  49. public static <T> AdmCreateResponse fromDmp(AdmQueryCriteria admRequest, DmpResult<JSONArray> dmpResult, Class<T> clazz) {
  50. if(!DmpResult.SUCCESS.equals(dmpResult.getResult())) {
  51. return failure("调用中台查询接口出错:" + dmpResult.getMessage());
  52. }
  53. // 转换为结果
  54. List<T> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), admRequest.getProjection(), clazz);
  55. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  56. return new AdmCreateResponse(SUCCESS, null, null, admVOs);
  57. }
  58. }