状态码表对应数据返回

1.创建注解类

package com.panda.emo.commons.annotations;

import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperDict {
/**
* 列名 ,返回map中的key,多个以,分隔
* @return
*/
String columns();

String operKey();

String newKeys();
}
2./**
 * 切面操作
*
* @Author Zhou
* @Date 2019/12/1315:07
*/
@Component
@Aspect
public class OperateAspect {
@Resource
private OperateDao operateDao;

@Pointcut("@annotation(com.panda.emo.commons.annotations.OperDict)") //内外部状态码表切点
public void pointDict() {}

@AfterReturning(pointcut = "pointDict()", returning = "oper")
public void daoReturn(JoinPoint point, Object oper) {
if (oper == null)
return;
try{
if (Map.class.isAssignableFrom(oper.getClass())) {
processAnnotation(point,(Map<String, Object>) oper);
} else {
if (List.class.isAssignableFrom(oper.getClass())) {
List os = (List) oper;
for (Object o : os) {
daoReturn(point, o);
}
}
}
} catch (Exception e) {
e.printStackTrace();
TLog.println("error:" + e.getMessage());
}
}
private void processAnnotation(JoinPoint point, Map<String, Object> oper) throws Exception {
if (oper.isEmpty())
return;
Annotation[] ans = AOPUtil.getQueryMethodAnnotations(point);

OperDict operDict = AOPUtil.getAnnotation(OperDict.class, ans);
processDataDict(operDict, oper);
}
private void processDataDict(OperDict operDict, Map<String, Object> oper) throws Exception {
if (operDict == null)
return;
String[] columns = operDict.columns().split(",");
String[] operKeys = operDict.operKey().split(",");
String[] newKeys = operDict.newKeys().split(",");
for (int i=0;i<columns.length;i++){
String column=columns[i];
Object value = oper.get(column);
if (value == null)
continue;
String newKey = newKeys[i];
if (oper.containsKey(newKey)) {
throw new Exception(newKey + "已经在结果集中存在");
}
String code = value.toString();
String operKey = operKeys[i];
String name = OperDictionary.getName(operKey, code);
oper.put(newKey, name);
}
}
3.创建.json文件
{
"status":{
"name":"内部状态",
"items": [
{
"code": "0",
"name": "待提交"
},
{
"code": "1",
"name": "已提交待客户经理审 "
},
{
"code": "2",
"name": "客户经理已审"
},
{
"code": "3",
"name": "客户经理驳回"
},
{
"code": "4",
"name": "内审被驳回"
},
{
"code": "5",
"name": "内审拒绝"
},
{
"code": "6",
"name": "内审通过"
},
{
"code": "7",
"name": "预审中"
},
{
"code": "8",
"name": "复审中"
},
{
"code": "9",
"name": "已完成"
}
]
}
}
4.解析json文件的类
public class OperDictionary {
private static String oper;
private static Map<String,Object> opers;

public static void init(){
oper=Application.readPackageFile(OperDictionary.class,"oper.json");
opers=JSONUtil.toObjectMap(oper);
}

public static Map<String,Object> get(String key){
return (Map<String,Object>) opers.get(key);
}

public static Map<String,Object> allOper(){
return opers;
}

public static String getName(String key,String code){
Map<String, Object> data = get(key);
System.out.println(get(key).getClass().getName());
List<Map<String, Object>> items = (List<Map<String, Object>>) data.get("items");
for (Map<String, Object> item : items) {
String cv = (String) item.get("code");
if (cv.equals(code)) {
return (String) item.get("name");
}
}
return "";
}

public static String operToString(){
return oper;
}


原文地址:https://www.cnblogs.com/zhou-tt/p/12097746.html