2022-02-25 19:41:22 +08:00
|
|
|
package com.engine.salary.util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
2022-02-28 11:12:55 +08:00
|
|
|
import com.engine.core.exception.ECException;
|
2022-02-25 19:41:22 +08:00
|
|
|
import com.engine.salary.exception.SalaryRunTimeException;
|
|
|
|
|
import weaver.general.BaseBean;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 09:55:58 +08:00
|
|
|
public class ResponseResult<T, R> {
|
2022-02-25 19:41:22 +08:00
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 统一返回方法
|
|
|
|
|
*/
|
2022-02-28 11:12:55 +08:00
|
|
|
public static String run(Function<Map<String, Object>, Map<String, Object>> f, Map<String, Object> m) {
|
2022-02-25 19:41:22 +08:00
|
|
|
try {
|
|
|
|
|
return Ok(f.apply(m));
|
|
|
|
|
} catch (SalaryRunTimeException e) {
|
|
|
|
|
return Error(e.getMessage());
|
2022-02-28 11:12:55 +08:00
|
|
|
} catch (ECException e) {
|
|
|
|
|
BaseBean b = new BaseBean();
|
|
|
|
|
b.writeLog(e);
|
|
|
|
|
Throwable cause = e.getCause();
|
|
|
|
|
return Error(cause.getMessage());
|
2022-02-25 19:41:22 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
BaseBean b = new BaseBean();
|
|
|
|
|
b.writeLog(e);
|
|
|
|
|
return Error(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 成功返回
|
|
|
|
|
*/
|
|
|
|
|
private static String Ok(Map<String, Object> map) {
|
|
|
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
|
|
|
apidatas.put("status", true);
|
2022-02-28 11:12:55 +08:00
|
|
|
apidatas.put("data", map);
|
2022-02-25 19:41:22 +08:00
|
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 失败返回
|
|
|
|
|
*/
|
|
|
|
|
private static String Error(String message) {
|
|
|
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
|
|
|
apidatas.put("status", false);
|
|
|
|
|
apidatas.put("errormsg", message);
|
|
|
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
|
|
|
}
|
2022-03-15 09:55:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 统一返回方法
|
|
|
|
|
*/
|
|
|
|
|
public String run(Function<T, R> f, T m) {
|
|
|
|
|
try {
|
|
|
|
|
return Ok(f.apply(m));
|
|
|
|
|
} catch (SalaryRunTimeException e) {
|
|
|
|
|
return Error(e.getMessage());
|
|
|
|
|
} catch (ECException e) {
|
|
|
|
|
BaseBean b = new BaseBean();
|
|
|
|
|
b.writeLog(e);
|
|
|
|
|
Throwable cause = e.getCause();
|
|
|
|
|
return Error(cause.getMessage());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
BaseBean b = new BaseBean();
|
|
|
|
|
b.writeLog(e);
|
|
|
|
|
return Error(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 成功返回
|
|
|
|
|
*/
|
|
|
|
|
private String Ok(R r) {
|
|
|
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
|
|
|
apidatas.put("status", true);
|
|
|
|
|
apidatas.put("data", r);
|
|
|
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-02-25 19:41:22 +08:00
|
|
|
}
|