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

178 lines
5.4 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.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;
2022-04-25 18:06:09 +08:00
import java.util.List;
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;
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;
private boolean permission = true;
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-05-08 12:34:05 +08:00
private final Boolean isLog = "ture".equals(baseBean.getPropValue("hrmSalary", "log"));
2022-04-26 11:26:13 +08:00
private final String salaryAdminRoleId = baseBean.getPropValue("hrmSalary", "salaryAdminRoleId");
2022-05-16 17:03:17 +08:00
private final Boolean isPermission = "true".equals(baseBean.getPropValue("hrmSalary", "isPermission"));
2022-04-25 18:06:09 +08:00
public ResponseResult(User user) {
this.user = user;
}
2022-04-26 11:26:13 +08:00
public ResponseResult(User user, boolean permission) {
this.user = user;
this.permission = permission;
}
2022-04-25 18:06:09 +08:00
private void permission() {
2022-04-26 11:26:13 +08:00
if (isPermission && permission) {
2022-04-26 16:39:41 +08:00
if (user == null) {
throw new SalaryRunTimeException("获取登录人员信息失败");
}
2022-04-25 18:06:09 +08:00
List<Object> roleInfo = hrmCommonService.getRoleInfo(user.getUID());
2022-04-26 11:26:13 +08:00
roleInfo.stream().map(m -> (Map) m).filter(m -> m.get("roleid") != null && m.get("roleid").toString().equals(salaryAdminRoleId)).findFirst().orElseThrow(() -> new SalaryRunTimeException("无权限"));
2022-04-25 18:06:09 +08:00
}
}
/**
* 成功返回
*/
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);
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
/**
* 统一返回方法
*/
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);
}
2022-04-25 18:06:09 +08:00
permission();
2022-05-08 12:34:05 +08:00
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-03-15 09:55:58 +08:00
return Error(e.getMessage());
}
}
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-04-25 18:06:09 +08:00
permission();
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);
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-03-21 09:33:21 +08:00
return Error(e.getMessage());
}
}
/**
* 统一返回方法无参有返回
*/
public String run(Supplier<R> f) {
try {
2022-05-08 12:34:05 +08:00
if (isLog) {
log.info("run salary api");
}
2022-04-25 18:06:09 +08:00
permission();
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-03-21 09:33:21 +08:00
return Error(e.getMessage());
}
}
2022-03-15 09:55:58 +08:00
/**
* 成功返回
*/
2022-04-12 19:25:19 +08:00
private String Ok(R r) {
2022-03-15 09:55:58 +08:00
Map<String, Object> apidatas = new HashMap<>();
apidatas.put("status", true);
apidatas.put("data", r);
2022-04-12 19:25:19 +08:00
return getJsonString(apidatas);
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);
}
}