package com.njcn.advice;

import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.AppConfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import javax.security.auth.login.AccountException;


/**
 * @author hongawen
 * @Date: 2019/6/4 16:27
 */
@ControllerAdvice
public class RecordLogByResultAdvice implements ResponseBodyAdvice<Object> {

    private static final Logger logger = LoggerFactory.getLogger(RecordLogByResultAdvice.class);

    @Autowired
    private UserLogDetailService userLogDetailService;

    @Autowired
    private AppConfig appConfig;

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body instanceof HttpResult) {
            HttpResult httpResult = new HttpResult();
            HttpResult result = (HttpResult) body;
            //操作不为空，进行日志录入
            if (!StringUtils.isEmpty(result.getOperate())) {
                int level = result.getLevel();
                String operate=result.getOperate();
                if(result.getOperate().equalsIgnoreCase("用户登录")){
                    //将严重度调整为普通
                    level=0;
                }else if(result.getOperate().equalsIgnoreCase("密码确认")&&result.getResult().equalsIgnoreCase("成功")){
                    level=0;
                    operate="密码确认成功";
                }else if(result.getOperate().equalsIgnoreCase("密码确认")&&result.getResult().equalsIgnoreCase("失败")){
                    operate="密码确认失败";
                }
                try {
                    userLogDetailService.saveUserLog(result.getUserIndex(), result.getUserName(), operate, result.getResult(), appConfig.getSystemFlag(), result.getIp(), result.getLogType(), level);
                } catch (AccountException e) {
                    logger.error("保存用户操作日志记录异常，操作为："+result.getOperate()+",结果："+result.getResult()+",异常为："+e.toString());
                }
                //只返回前台需要的信息 code / message / body
                httpResult.setCode(result.getCode());
                httpResult.setMessage(result.getMessage());
                httpResult.setBody(result.getBody());
            }else{
                httpResult=result;
            }
            return httpResult;
        }
        return body;
    }
}
