import java.io.Serializable; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; import com.fineone.shelterbelt.comm.util.UtilsHelper; import lombok.Data; import lombok.NoArgsConstructor; @NoArgsConstructor @Data public class ResultTmplateMsg<T> implements Serializable { private static final long serialVersionUID = 1L; /** * 返回代码(成功标记) 成功为0,其余的为失败 */ private int errcode = 0; /** * 返回信息 */ private String errmsg = ""; /** * @JsonProperty 指点转成json的字段字符串,默认为首字母小写 * 返回数据 */ @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8") @JsonProperty(value = "Retdata") private T retdata = null; /** * 日志内容占位符参数 */ private Object[] logParams = null; public ResultTmplateMsg(int errcode, String errmsg, T retdata) { super(); this.errcode = errcode; this.errmsg = errmsg; this.retdata = retdata; } /** * * -判断 map 中是否存在数组 fieldStrings 的字段,不存在则返回错误 * * @param map 前端传来的数据 * @param fieldStrings 需要判断的字段名称 * @param fieldMap 字段名称对应的中文意义 * @return */ public static ResultTmplateMsg checkFieldNull(Map<String, Object> map, String[] fieldStrings, Map<String, String> fieldMap) { ResultTmplateMsg resultMsg = new ResultTmplateMsg(); resultMsg.setErrcode(0); for (int i = 0; i < fieldStrings.length; i++) { if (UtilsHelper.isEmpty(map.get(fieldStrings[i]))) { if (UtilsHelper.isNotEmpty(fieldMap.get(fieldStrings[i]))) { return commResultMsg(-2, "", fieldMap.get(fieldStrings[i])+"为空!"); }else { return commResultMsg(-2, "", fieldStrings[i]+"为空!"); } } } return resultMsg; } /** * * -判断 map 中是否存在数组 fieldStrings 的字段,不存在则返回错误 * * @param map 前端传来的数据 * @param fieldStrings 需要判断的字段名称 * @return */ public static ResultTmplateMsg checkFieldNull(Map<String, Object> map, String[] fieldStrings) { ResultTmplateMsg resultMsg = new ResultTmplateMsg(); resultMsg.setErrcode(0); for (int i = 0; i < fieldStrings.length; i++) { if (UtilsHelper.isEmpty(map.get(fieldStrings[i]))) { return commResultMsg(-2, "", fieldStrings[i]+"为空!"); } } return resultMsg; } /** * * -判断 map 中是否存在数组 fieldStrings 的字段,不存在则返回错误 * * @param map 前端传来的数据 * @param fieldStrings 需要判断的字段名称 * @return */ public static boolean checkFieldNullBoolean(Map<String, Object> map, String[] fieldStrings) { ResultTmplateMsg resultMsg = new ResultTmplateMsg(); resultMsg.setErrcode(0); for (int i = 0; i < fieldStrings.length; i++) { if (UtilsHelper.isEmpty(map.get(fieldStrings[i]))) { return true; } } return false; } /** * 用于处理分页查询,返回前端值的方法 * * @param map1 前端的的输入值 * @param vlist 数据查到的数据 * @param totalSize 数据的总条数 * @return */ public static ResultTmplateMsg commResultMsg(Map<String, Object> map1, List<?> vlist, int totalSize) { int start = 0; int limit = 20; if (UtilsHelper.isNotEmpty(map1)) { if (UtilsHelper.isNotEmpty(map1.get("start"))) { start = Integer.parseInt(map1.get("start").toString()); } if (UtilsHelper.isNotEmpty(map1.get("limit"))) { limit = Integer.parseInt(map1.get("limit").toString()); } } int pageCount = totalSize % limit == 0 ? totalSize / limit : totalSize / limit + 1; Map<String, Object> pageMap = new HashMap<String, Object>(); pageMap.put("start", start); pageMap.put("limit", limit); pageMap.put("dataList", vlist); pageMap.put("totalSize", totalSize); pageMap.put("success", true); pageMap.put("pageCount", pageCount); ResultTmplateMsg resultMsg = new ResultTmplateMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), pageMap); return resultMsg; } /** * 用于处理数据的增删改,返回前端的方法 * * @param count 增删改的条数 * @param okmsg 增删改==>成功的信息 * @param errmsg 增删改==>失败的信息 * @return */ public static ResultTmplateMsg commResultMsg(int count,String errmsg) { Map<String, Object> mapResult = new HashMap<String, Object>(); mapResult.put("result", count); ResultTmplateMsg resultMsg = null; if (count > 0) { mapResult.put("message", errmsg); mapResult.put("result", 1); resultMsg = new ResultTmplateMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), mapResult); return resultMsg; } else { mapResult.put("message", errmsg); if (count == 0) { mapResult.put("result", -1); resultMsg = new ResultTmplateMsg(ResultStatusCode.ERR.getErrcode(), ResultStatusCode.ERR.getErrmsg(), mapResult); }else { resultMsg = new ResultTmplateMsg(ResultStatusCode.ERR.getErrcode(), ResultStatusCode.ERR.getErrmsg(), mapResult); } return resultMsg; } } /** * 用于处理数据的增删改,返回前端的方法 * * @param count * @param okmsg 增删改==>成功的信息 * @param errmsg 增删改==>失败的信息 * @return */ public static ResultTmplateMsg commResultMsg(int count, String okmsg, String errmsg) { Map<String, Object> mapResult = new HashMap<String, Object>(); mapResult.put("result", count); ResultTmplateMsg resultMsg = null; if (count > 0) { mapResult.put("message", okmsg); mapResult.put("result", 1); resultMsg = new ResultTmplateMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), mapResult); return resultMsg; } else { mapResult.put("message", errmsg); if (count == 0) { mapResult.put("result", -1); resultMsg = new ResultTmplateMsg(ResultStatusCode.ERR.getErrcode(), ResultStatusCode.ERR.getErrmsg(), mapResult); }else { resultMsg = new ResultTmplateMsg(ResultStatusCode.ERR.getErrcode(), ResultStatusCode.ERR.getErrmsg(), mapResult); } return resultMsg; } } /** * 用于处理数据的增删改,返回前端的方法 * @param count * @return ResultMsg 增加,删除(pc,app返回值) */ public static ResultTmplateMsg commResultMsg(int count, Map<String, Object> mapResult) { mapResult.put("result", count); ResultTmplateMsg resultMsg = null; if (count > 0) { mapResult.put("result", 1); if (UtilsHelper.isEmpty(mapResult.get("message"))) { mapResult.put("message", "成功"); } resultMsg = new ResultTmplateMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), mapResult); }else{ if (UtilsHelper.isEmpty(mapResult.get("message"))) { mapResult.put("message", "失败"); } resultMsg = new ResultTmplateMsg(0, ResultStatusCode.ERR.getErrmsg(), mapResult); } return resultMsg; } /** * 用于处理公共,返回前端的值的方法 * * @param object * @return */ public static ResultTmplateMsg commResultMsg(Object object) { return new ResultTmplateMsg(ResultStatusCode.OK.getErrcode(), ResultStatusCode.OK.getErrmsg(), object); } /** * @去除map中值为null '' 'null'的键值对 */ public static Map<String, Object> mapCleanNull(Map<String, Object> map) { if (map == null || map.size() == 0) { return new HashMap<String, Object>(); } Map<String, Object> passdata = (Map<String, Object>) map.get("passdata"); map.put("passdata", null); if (passdata != null) { map.putAll(passdata); } Map<String, Object> cleanMap = new HashMap<>(); Iterator<Entry<String, Object>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, Object> entry = iterator.next(); String key = entry.getKey(); Object value = entry.getValue(); if (value != null) { if (value instanceof String) { String strValue = (String) value; if (strValue != null && !strValue.trim().isEmpty() && !strValue.trim().equals("null")&& !strValue.trim().equals("undefined") && !strValue.equals("[]")) { cleanMap.put(key, value); } } else { if (value instanceof List) { List list = (List) value; if (list.size() <= 0) { continue; } } cleanMap.put(key, value); } } } return cleanMap; } /** * 对象转为map */ public static Map<String, Object> objectToMap(Object obj) { Map<String, Object> map = new HashMap<String, Object>(); if (obj == null) { return map; } Field[] declaredFields = obj.getClass().getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); try { map.put(field.getName(), field.get(obj)); } catch (Exception e) { throw new RuntimeException(e); } } return map; } /** * list转化为字符串数组 */ public static String[] listToArrString(List<Map<String, Object>> list , String fieldName) { String[] strings = new String[list.size()]; for (int i = 0; i < list.size(); i++) { Map<String, Object> carMap = list.get(i); strings[i] = (String) carMap.get(fieldName); } return strings; } public ResultTmplateMsg changeMessage() { Map<String, Object> resultMap = (Map<String, Object>) this.retdata; String message = (String) resultMap.get("message"); this.errmsg = message; return this; } }
ResultTmplateMsg通用返回
于 2023-05-18 15:04:55 首次发布