zoukankan      html  css  js  c++  java
  • 状态码表对应数据返回

    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;
    }


  • 相关阅读:
    关于在ubuntu12.04图形界面下不能从root用户直接登录的问题
    error: stray '357' in program
    关于gcc -o 的使用问题
    如何解决程序退出重启后不能绑定端口的问题?
    使用Ubuntu12.04登陆账户时,输入密码是正确的,但是图形界面闪一下后就又回到登陆页面了
    如何在linux系统中设置严密的密码策略(译文)
    sqlite3数据库归纳
    Bing地图切片原理
    CSS技巧
    jQuery.extend方法和开发中变量的复用
  • 原文地址:https://www.cnblogs.com/zhou-tt/p/12097746.html
Copyright © 2011-2022 走看看