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;
|
2023-12-22 14:50:16 +08:00
|
|
|
|
import com.engine.salary.common.SalaryContext;
|
2022-08-01 14:15:09 +08:00
|
|
|
|
import com.engine.salary.exception.ExceptionUtil;
|
2022-02-25 19:41:22 +08:00
|
|
|
|
import com.engine.salary.exception.SalaryRunTimeException;
|
2022-04-08 16:30:10 +08:00
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2025-08-25 16:40:47 +08:00
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
2023-03-02 10:41:21 +08:00
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2022-04-12 19:25:19 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2022-02-25 19:41:22 +08:00
|
|
|
|
import weaver.general.BaseBean;
|
2022-04-25 18:06:09 +08:00
|
|
|
|
import weaver.hrm.User;
|
2022-02-25 19:41:22 +08:00
|
|
|
|
|
2023-12-22 14:50:16 +08:00
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2025-08-25 16:40:47 +08:00
|
|
|
|
import java.io.IOException;
|
2022-02-25 19:41:22 +08:00
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
import java.util.Map;
|
2022-03-21 09:33:21 +08:00
|
|
|
|
import java.util.function.Consumer;
|
2022-02-25 19:41:22 +08:00
|
|
|
|
import java.util.function.Function;
|
2022-03-21 09:33:21 +08:00
|
|
|
|
import java.util.function.Supplier;
|
2022-02-25 19:41:22 +08:00
|
|
|
|
|
2022-07-27 16:56:13 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 请求执行器
|
|
|
|
|
|
* <p>Copyright: Copyright (c) 2022</p>
|
|
|
|
|
|
* <p>Company: 泛微软件</p>
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author qiantao
|
|
|
|
|
|
* @version 1.0
|
|
|
|
|
|
**/
|
2022-04-12 19:25:19 +08:00
|
|
|
|
@Slf4j
|
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-04-26 11:26:13 +08:00
|
|
|
|
private final User user;
|
2022-04-25 18:06:09 +08:00
|
|
|
|
|
2022-04-26 11:26:13 +08:00
|
|
|
|
private final BaseBean baseBean = new BaseBean();
|
|
|
|
|
|
|
2022-06-23 17:15:39 +08:00
|
|
|
|
private final Boolean isLog = "true".equals(baseBean.getPropValue("hrmSalary", "log"));
|
2022-04-25 18:06:09 +08:00
|
|
|
|
|
|
|
|
|
|
public ResponseResult(User user) {
|
|
|
|
|
|
this.user = user;
|
2023-12-22 14:50:16 +08:00
|
|
|
|
SalaryContext.get().setValue("user", user);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ResponseResult(HttpServletRequest request, HttpServletResponse response, User user) {
|
|
|
|
|
|
this.user = user;
|
|
|
|
|
|
SalaryContext.get().setValue("user", user);
|
|
|
|
|
|
SalaryContext.get().setValue("request", request);
|
|
|
|
|
|
SalaryContext.get().setValue("response", response);
|
2022-04-25 18:06:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-25 16:40:47 +08:00
|
|
|
|
public T parseParams(HttpServletRequest request, Class<T> clazz) {
|
|
|
|
|
|
T t = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
|
t = mapper.readValue(request.getInputStream(), clazz);
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
log.error("salary api parseParams fail , param {}", t, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
return t;
|
|
|
|
|
|
}
|
2022-03-15 09:55:58 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 统一返回方法
|
|
|
|
|
|
*/
|
2022-05-08 12:34:05 +08:00
|
|
|
|
public String run(Function<T, R> f, T t) {
|
2022-03-15 09:55:58 +08:00
|
|
|
|
try {
|
2022-05-08 12:34:05 +08:00
|
|
|
|
if (isLog) {
|
|
|
|
|
|
log.info("run salary api , param {}", t);
|
|
|
|
|
|
}
|
|
|
|
|
|
return Ok(f.apply(t));
|
2022-03-15 09:55:58 +08:00
|
|
|
|
} catch (SalaryRunTimeException e) {
|
2023-02-16 10:47:30 +08:00
|
|
|
|
log.error("salary api run fail , param {}", t, e);
|
2022-03-15 09:55:58 +08:00
|
|
|
|
return Error(e.getMessage());
|
|
|
|
|
|
} catch (ECException e) {
|
2023-02-16 10:47:30 +08:00
|
|
|
|
log.error("salary api run fail , param {}", t, e);
|
2022-03-15 09:55:58 +08:00
|
|
|
|
Throwable cause = e.getCause();
|
2023-06-08 10:14:37 +08:00
|
|
|
|
String message = "";
|
|
|
|
|
|
while (cause != null) {
|
|
|
|
|
|
Throwable causeTmp = cause.getCause();
|
|
|
|
|
|
if (causeTmp == null) {
|
|
|
|
|
|
message = cause.getMessage();
|
|
|
|
|
|
}
|
|
|
|
|
|
cause = causeTmp;
|
|
|
|
|
|
}
|
|
|
|
|
|
return Error(message);
|
2022-03-15 09:55:58 +08:00
|
|
|
|
} catch (Exception e) {
|
2023-02-16 10:47:30 +08:00
|
|
|
|
log.error("salary api run fail , param {}", t, e);
|
2022-07-01 15:18:29 +08:00
|
|
|
|
return Error("系统异常!");
|
2022-03-15 09:55:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-21 09:33:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 统一返回方法(有参无返回)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public String run(Consumer<T> f, T t) {
|
|
|
|
|
|
try {
|
2022-05-08 12:34:05 +08:00
|
|
|
|
if (isLog) {
|
|
|
|
|
|
log.info("run salary api , param {}", t);
|
|
|
|
|
|
}
|
2022-03-21 09:33:21 +08:00
|
|
|
|
f.accept(t);
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
} catch (SalaryRunTimeException e) {
|
2023-02-16 10:47:30 +08:00
|
|
|
|
log.error("salary api run fail , param {}", t, e);
|
2022-03-21 09:33:21 +08:00
|
|
|
|
return Error(e.getMessage());
|
|
|
|
|
|
} catch (ECException e) {
|
2023-02-16 10:47:30 +08:00
|
|
|
|
log.error("salary api run fail , param {}", t, e);
|
2022-08-01 14:15:09 +08:00
|
|
|
|
return Error(ExceptionUtil.getRealMessage(e));
|
2022-03-21 09:33:21 +08:00
|
|
|
|
} catch (Exception e) {
|
2023-02-16 10:47:30 +08:00
|
|
|
|
log.error("salary api run fail , param {}", t, e);
|
2022-07-01 15:18:29 +08:00
|
|
|
|
return Error("系统异常!", e);
|
2022-03-21 09:33:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 统一返回方法(无参有返回)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public String run(Supplier<R> f) {
|
|
|
|
|
|
try {
|
2022-05-08 12:34:05 +08:00
|
|
|
|
if (isLog) {
|
|
|
|
|
|
log.info("run salary api");
|
|
|
|
|
|
}
|
2022-03-21 09:33:21 +08:00
|
|
|
|
return Ok(f.get());
|
|
|
|
|
|
} catch (SalaryRunTimeException e) {
|
2022-11-03 21:37:25 +08:00
|
|
|
|
log.error("salary api run fail", e);
|
2022-03-21 09:33:21 +08:00
|
|
|
|
return Error(e.getMessage());
|
|
|
|
|
|
} catch (ECException e) {
|
2022-05-10 17:26:58 +08:00
|
|
|
|
log.error("salary api run fail", e);
|
2022-03-21 09:33:21 +08:00
|
|
|
|
Throwable cause = e.getCause();
|
|
|
|
|
|
return Error(cause.getMessage());
|
|
|
|
|
|
} catch (Exception e) {
|
2022-05-10 17:26:58 +08:00
|
|
|
|
log.error("salary api run fail", e);
|
2022-07-01 15:18:29 +08:00
|
|
|
|
return Error("系统异常!", e);
|
2022-03-21 09:33:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 09:55:58 +08:00
|
|
|
|
|
2023-03-02 10:41:21 +08:00
|
|
|
|
private static String getJsonString(Object apidatas) throws JsonProcessingException {
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
return mapper.writeValueAsString(apidatas);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-21 09:33:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 成功返回
|
|
|
|
|
|
*/
|
|
|
|
|
|
private String Ok() {
|
|
|
|
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
|
|
|
|
apidatas.put("status", true);
|
|
|
|
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
|
|
|
|
}
|
2022-04-11 14:30:30 +08:00
|
|
|
|
|
2022-06-09 17:45:28 +08:00
|
|
|
|
|
2022-07-01 15:18:29 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 成功返回
|
|
|
|
|
|
*/
|
2023-03-02 10:12:00 +08:00
|
|
|
|
private String Ok(R r) throws JsonProcessingException {
|
2022-07-01 15:18:29 +08:00
|
|
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
|
|
|
|
apidatas.put("status", true);
|
|
|
|
|
|
apidatas.put("data", r);
|
2023-03-02 10:41:21 +08:00
|
|
|
|
String success = getJsonString(apidatas);
|
2022-07-01 15:18:29 +08:00
|
|
|
|
if (isLog) {
|
|
|
|
|
|
log.info("run salary api success return {}", success);
|
|
|
|
|
|
}
|
|
|
|
|
|
return success;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-09 17:45:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 失败返回
|
|
|
|
|
|
*/
|
|
|
|
|
|
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-07-01 15:18:29 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 系统异常失败返回
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static String Error(String message, Exception e) {
|
|
|
|
|
|
Map<String, Object> apidatas = new HashMap<>();
|
|
|
|
|
|
apidatas.put("status", false);
|
|
|
|
|
|
apidatas.put("errormsg", message);
|
|
|
|
|
|
apidatas.put("error", e.getMessage());
|
|
|
|
|
|
return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-25 19:41:22 +08:00
|
|
|
|
}
|