You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
5.2 KiB
Java
186 lines
5.2 KiB
Java
package com.engine.sship.util;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
|
import com.engine.core.exception.ECException;
|
|
import com.engine.sship.exception.CustomizeRunTimeException;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import weaver.general.BaseBean;
|
|
import weaver.hrm.User;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Function;
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
@Slf4j
|
|
public class ResponseResult<T, R> {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private final User user;
|
|
|
|
private final BaseBean baseBean = new BaseBean();
|
|
|
|
private final Boolean isLog = "true".equals(baseBean.getPropValue("hrmSalary", "log"));
|
|
|
|
public ResponseResult(User user) {
|
|
this.user = user;
|
|
}
|
|
|
|
/**
|
|
* 统一返回方法(自定义返回格式)
|
|
*/
|
|
public String customRun(Function<T, R> f, T t) {
|
|
try {
|
|
if (isLog) {
|
|
log.info("run api , param {}", t);
|
|
}
|
|
return getJsonString(f.apply(t));
|
|
} catch (CustomizeRunTimeException e) {
|
|
log.error("api run fail", e);
|
|
return Error(e.getMessage());
|
|
} catch (ECException e) {
|
|
log.error("api run fail", e);
|
|
Throwable cause = e.getCause();
|
|
return Error(cause.getMessage());
|
|
} catch (Exception e) {
|
|
log.error("api run fail", e);
|
|
return Error("系统异常!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 统一返回方法
|
|
*/
|
|
public String run(Function<T, R> f, T t) {
|
|
try {
|
|
if (isLog) {
|
|
log.info("run api , param {}", t);
|
|
}
|
|
return Ok(f.apply(t));
|
|
} catch (CustomizeRunTimeException e) {
|
|
log.error("api run fail", e);
|
|
return Error(e.getMessage());
|
|
} catch (ECException e) {
|
|
log.error("api run fail", e);
|
|
Throwable cause = e.getCause();
|
|
return Error(cause.getMessage());
|
|
} catch (Exception e) {
|
|
log.error("api run fail", e);
|
|
return Error("系统异常!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 统一返回方法(有参无返回)
|
|
*/
|
|
public String run(Consumer<T> f, T t) {
|
|
try {
|
|
if (isLog) {
|
|
log.info("run api , param {}", t);
|
|
}
|
|
f.accept(t);
|
|
return Ok();
|
|
} catch (CustomizeRunTimeException e) {
|
|
log.error("api run fail", e);
|
|
return Error(e.getMessage());
|
|
} catch (ECException e) {
|
|
log.error("api run fail", e);
|
|
return Error(ExceptionUtil.getRealMessage(e));
|
|
} catch (Exception e) {
|
|
log.error("api run fail", e);
|
|
return Error("系统异常!", e);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 统一返回方法(无参有返回)
|
|
*/
|
|
public String run(Supplier<R> f) {
|
|
try {
|
|
if (isLog) {
|
|
log.info("run api");
|
|
}
|
|
return Ok(f.get());
|
|
} catch (CustomizeRunTimeException e) {
|
|
log.error("api run fail", e);
|
|
return Error(e.getMessage());
|
|
} catch (ECException e) {
|
|
log.error("api run fail", e);
|
|
Throwable cause = e.getCause();
|
|
return Error(cause.getMessage());
|
|
} catch (Exception e) {
|
|
log.error("api run fail", e);
|
|
return Error("系统异常!", e);
|
|
}
|
|
}
|
|
|
|
|
|
private static String getJsonString(Object apidatas) {
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
try {
|
|
return mapper.writeValueAsString(apidatas);
|
|
} catch (JsonProcessingException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
|
|
/**
|
|
* 成功返回
|
|
*/
|
|
private String Ok() {
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
apidatas.put("api_status", true);
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
}
|
|
|
|
|
|
/**
|
|
* 成功返回
|
|
*/
|
|
private String Ok(R r) {
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
apidatas.put("api_status", true);
|
|
apidatas.put("data", r);
|
|
String success = getJsonString(apidatas);
|
|
if (isLog) {
|
|
log.info("run salary api success return {}", success);
|
|
}
|
|
return success;
|
|
}
|
|
|
|
|
|
/**
|
|
* 失败返回
|
|
*/
|
|
private static String Error(String message) {
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
apidatas.put("api_status", false);
|
|
apidatas.put("msg", message);
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
}
|
|
|
|
|
|
/**
|
|
* 系统异常失败返回
|
|
*/
|
|
private static String Error(String message, Exception e) {
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
apidatas.put("api_status", false);
|
|
apidatas.put("msg", message);
|
|
apidatas.put("error", e.getMessage());
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
}
|
|
|
|
}
|