package com.engine.salary.util; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.engine.common.service.HrmCommonService; import com.engine.common.service.impl.HrmCommonServiceImpl; import com.engine.core.exception.ECException; import com.engine.salary.exception.SalaryRunTimeException; 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.List; import java.util.Map; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @Slf4j public class ResponseResult { private static final long serialVersionUID = 1L; private final User user; private boolean permission = true; private final HrmCommonService hrmCommonService = new HrmCommonServiceImpl(); private final BaseBean baseBean = new BaseBean(); private final Boolean isLog = "ture".equals(baseBean.getPropValue("hrmSalary", "log")); private final String salaryAdminRoleId = baseBean.getPropValue("hrmSalary", "salaryAdminRoleId"); private final Boolean isPermission = "true".equals(baseBean.getPropValue("hrmSalary", "isPermission")); public ResponseResult(User user) { this.user = user; } public ResponseResult(User user, boolean permission) { this.user = user; this.permission = permission; } private void permission() { if (isPermission && permission) { if (user == null) { throw new SalaryRunTimeException("获取登录人员信息失败"); } List roleInfo = hrmCommonService.getRoleInfo(user.getUID()); roleInfo.stream().map(m -> (Map) m).filter(m -> m.get("roleid") != null && m.get("roleid").toString().equals(salaryAdminRoleId)).findFirst().orElseThrow(() -> new SalaryRunTimeException("无权限")); } } /** * 成功返回 */ private static String Ok(Map map) { Map apidatas = new HashMap<>(); apidatas.put("status", true); apidatas.put("data", map); return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect); } /** * 失败返回 */ private static String Error(String message) { Map apidatas = new HashMap<>(); apidatas.put("status", false); apidatas.put("errormsg", message); return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect); } /** * 统一返回方法 */ public String run(Function f, T t) { try { if (isLog) { log.info("run salary api , param {}", t); } permission(); return Ok(f.apply(t)); } catch (SalaryRunTimeException e) { return Error(e.getMessage()); } catch (ECException e) { log.error("salary api run fail", e); Throwable cause = e.getCause(); return Error(cause.getMessage()); } catch (Exception e) { log.error("salary api run fail", e); return Error(e.getMessage()); } } /** * 统一返回方法(有参无返回) */ public String run(Consumer f, T t) { try { if (isLog) { log.info("run salary api , param {}", t); } permission(); f.accept(t); return Ok(); } catch (SalaryRunTimeException e) { return Error(e.getMessage()); } catch (ECException e) { log.error("salary api run fail", e); Throwable cause = e.getCause(); return Error(cause.getMessage()); } catch (Exception e) { log.error("salary api run fail", e); return Error(e.getMessage()); } } /** * 统一返回方法(无参有返回) */ public String run(Supplier f) { try { if (isLog) { log.info("run salary api"); } permission(); return Ok(f.get()); } catch (SalaryRunTimeException e) { return Error(e.getMessage()); } catch (ECException e) { log.error("salary api run fail", e); Throwable cause = e.getCause(); return Error(cause.getMessage()); } catch (Exception e) { log.error("salary api run fail", e); return Error(e.getMessage()); } } /** * 成功返回 */ private String Ok(R r) { Map apidatas = new HashMap<>(); apidatas.put("status", true); apidatas.put("data", r); return getJsonString(apidatas); } 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 apidatas = new HashMap<>(); apidatas.put("status", true); return JSONObject.toJSONString(apidatas, SerializerFeature.DisableCircularReferenceDetect); } }