package com.manage.controller; /**
 * <p>Title: LogAopAction.java</p>
 * <p>Description: 操作日志Controller</p>
 */

import com.manage.annotation.OptionalLog;
import com.manage.entity.Power_Log;
import com.manage.service.LogService;
import com.manage.util.ExceptionPrintUtil;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
public class LogAopAction {
    // 注入service,用来将日志信息保存在数据库
    @Resource
    private LogService logService;

    // 配置接入点，即为所要记录的action操作目录
    @Pointcut("@annotation(com.manage.annotation.OptionalLog)")
    private void controllerAspect() {
    }

    @Around("controllerAspect()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        // 拦截的实体类，就是当前正在执行的controller
        Object target = pjp.getTarget();
        // 拦截的方法名称。当前正在执行的方法
        String methodName = pjp.getSignature().getName();
        // 拦截的放参数类型
        Signature sig = pjp.getSignature();
        MethodSignature msig = null;
        if (!(sig instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        msig = (MethodSignature) sig;
        Class[] parameterTypes = msig.getMethod().getParameterTypes();

        Object object = null;
        // 获得被拦截的方法
        Method method = null;
        try {
            method = target.getClass().getMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        }
        if (null != method) {
            //插入表操作
            insertLog(method,pjp);
        }

        object = pjp.proceed();
        //接受客户端的数据
        Map<String,String[]> map = request.getParameterMap();
         // 解决获取参数乱码
        Map<String,String[]> newmap = new HashMap<String,String[]>();
        for(Map.Entry<String, String[]> entry : map.entrySet()){
            String name = entry.getKey();
            String[] values = entry.getValue();

            if(values==null){
                newmap.put(name, new String[]{});
                continue;
            }
            String[] newvalues = new String[values.length];
            for(int i=0; i<values.length;i++){
                String value = values[i];
                value = new String(value.getBytes("iso8859-1"),request.getCharacterEncoding());
                newvalues[i] = value; //解决乱码后封装到Map中
            }
            newmap.put(name, newvalues);
        }
        return object;
    }

    //对象字段转数据库字段
    private static String propertyToField(String property) {
        if (StringUtils.isEmpty(property)) {
            return "";
        }
        if (property.length() == 1) {
            return property.toLowerCase();
        }

        char[] chars = property.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (i == 0) {
                sb.append(StringUtils.lowerCase(CharUtils.toString(c)));
            } else {
                if (CharUtils.isAsciiAlphaUpper(c)) {
                    sb.append("_").append(StringUtils.lowerCase(CharUtils.toString(c)));
                } else {
                    sb.append(c);
                }
            }
        }
        return sb.toString();
    }
    private void insertLog(Method method,ProceedingJoinPoint pjp) {
        //日志实体对象
        Power_Log logBo = new Power_Log();
        // 获取方法（此为自定义注解）
        OptionalLog op = method.getAnnotation(OptionalLog.class);
        String module = op.module();
        // 拦截的方法参数
        //判断操作类型
        if (StringUtils.isNoneBlank(module)) {
            Object[] args = pjp.getArgs();
            try {
                if (null != args && StringUtils.isNotBlank(op.fieldName())) {
                    if (("新增".equals(module)) || "修改".equals(module) || "保存".equals(module)) {
                        Object obj = args[0];
                        //通过反射获取值
                        Class c = obj.getClass();
                        // 获取该类的成员变量
                        Field f = c.getDeclaredField(op.fieldName());
                        // 取消语言访问检查
                        f.setAccessible(true);
                        logBo.setRemark(f.get(obj).toString());
                    }
                    if ("删除".equals(module) || "重置密码".equals(module) || "修改分配".equals(module)) {
                        String tableName = op.tableName();
                        String fieldName = op.fieldName();
                        fieldName = propertyToField(fieldName);
                        DefaultParameterNameDiscoverer discover = new DefaultParameterNameDiscoverer();
                        String[] parameterNames = discover.getParameterNames(method);
                        //拼接查询语句
                        String sqlWhere = "";
                        //一个参数如id
                        sqlWhere = " " + propertyToField(parameterNames[0]) + " = '" + args[0] +"'";
                        Map<String, Object> obj = logService.selectObjectByKey(tableName, sqlWhere);
                        Object o = null;
                        if(obj != null){
                            o = obj.get(fieldName);
                            String remark = "";
                            //第一个字段
                            if(o != null){
                                remark = obj.get(fieldName).toString();
                            }else{
                                //否则取备用字段
                                fieldName = propertyToField(op.fieldName1());
                                if(StringUtils.isNotBlank(fieldName)){
                                    o = obj.get(fieldName);
                                    if(o != null){
                                        remark = obj.get(fieldName).toString();
                                    }
                                }
                            }
                            logBo.setRemark(remark);
                        }
                    }
                }
                // 获取注解的modules 设为操作模块
                logBo.setLogTitle(module);
                // 获取注解的methods 设为执行方法
                logBo.setLogContent(op.methods());
                // 添加到数据库
                logService.insert(logBo);
                } catch (Exception e) {
                    ExceptionPrintUtil.printException(e);
                    e.printStackTrace();
                    throw new RuntimeException();
                }
        }
    }
}