weaver-hrm-salary/src/com/engine/salary/util/ResponseResult.java

173 lines
4.8 KiB
Java
Raw Normal View History

package com.engine.salary.util;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
2022-04-25 18:06:09 +08:00
import com.engine.common.service.HrmCommonService;
import com.engine.common.service.impl.HrmCommonServiceImpl;
2022-02-28 11:12:55 +08:00
import com.engine.core.exception.ECException;
import com.engine.salary.exception.ExceptionUtil;
import com.engine.salary.exception.SalaryRunTimeException;
2022-04-08 16:30:10 +08:00
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
2022-04-12 19:25:19 +08:00
import lombok.extern.slf4j.Slf4j;
import weaver.general.BaseBean;
2022-04-25 18:06:09 +08:00
import weaver.hrm.User;
import java.util.HashMap;
import java.util.Map;
2022-03-21 09:33:21 +08:00
import java.util.function.Consumer;
import java.util.function.Function;
2022-03-21 09:33:21 +08:00
import java.util.function.Supplier;
/**
* 请求执行器
* <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> {
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 HrmCommonService hrmCommonService = new HrmCommonServiceImpl();
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;
}
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) {
return Error(e.getMessage());
} catch (ECException e) {
2022-05-10 17:26:58 +08:00
log.error("salary api run fail", e);
2022-03-15 09:55:58 +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("系统异常!");
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) {
return Error(e.getMessage());
} catch (ECException e) {
2022-05-10 17:26:58 +08:00
log.error("salary api run fail", e);
return Error(ExceptionUtil.getRealMessage(e));
2022-03-21 09:33:21 +08:00
} 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
}
}
/**
* 统一返回方法无参有返回
*/
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) {
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
2022-04-12 19:25:19 +08:00
private static String getJsonString(Object apidatas) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(apidatas);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "";
}
2022-03-15 09:55:58 +08:00
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-06-09 17:45:28 +08:00
2022-07-01 15:18:29 +08:00
/**
* 成功返回
*/
private String Ok(R r) {
Map<String, Object> apidatas = new HashMap<>();
apidatas.put("status", true);
apidatas.put("data", r);
String success = getJsonString(apidatas);
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);
}
}