package com.fs.core.exception;



import com.fs.common.constant.HttpStatus;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.exception.BaseException;
import com.fs.common.exception.CustomException;
import com.fs.common.exception.DemoModeException;
import com.fs.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.sql.SQLSyntaxErrorException;


/**
 * 异常处理器
 */
@RestControllerAdvice
public class FSExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(FSException.class)
	public R handleRRException(FSException e){
		R r = new R();
		r.put("code", e.getCode());
		r.put("msg", e.getMessage());

		return r;
	}

	@ExceptionHandler(NoHandlerFoundException.class)
	public R handlerNoFoundException(Exception e) {
		logger.error(e.getMessage(), e);
		return R.error(404, "路径不存在，请检查路径是否正确");
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public R handleDuplicateKeyException(DuplicateKeyException e){
		logger.error(e.getMessage(), e);
		return R.error("数据库中已存在该记录");
	}

	@ExceptionHandler(SQLSyntaxErrorException.class)
	public R handleSQLSyntaxErrorException(SQLSyntaxErrorException e){
		logger.error(e.getMessage(), e);
		return R.error("SQL语法错误，请联系管理员!");
	}

	@ExceptionHandler(Exception.class)
	public R handleException(Exception e){
		logger.error(e.getMessage(), e);
		return R.error(e.getMessage());
	}


	/**
	 * 基础异常
	 */
	@ExceptionHandler(BaseException.class)
	public AjaxResult baseException(BaseException e)
	{
		return AjaxResult.error(e.getMessage());
	}

	/**
	 * 业务异常
	 */
	@ExceptionHandler(CustomException.class)
	public AjaxResult businessException(CustomException e)
	{
		if (StringUtils.isNull(e.getCode()))
		{
			return AjaxResult.error(e.getMessage());
		}
		return AjaxResult.error(e.getCode(), e.getMessage());
	}



	@ExceptionHandler(AccessDeniedException.class)
	public AjaxResult handleAuthorizationException(AccessDeniedException e)
	{
		logger.error(e.getMessage());
		return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限，请联系管理员授权");
	}

	@ExceptionHandler(AccountExpiredException.class)
	public AjaxResult handleAccountExpiredException(AccountExpiredException e)
	{
		logger.error(e.getMessage(), e);
		return AjaxResult.error(e.getMessage());
	}

	@ExceptionHandler(UsernameNotFoundException.class)
	public AjaxResult handleUsernameNotFoundException(UsernameNotFoundException e)
	{
		logger.error(e.getMessage(), e);
		return AjaxResult.error(e.getMessage());
	}



	/**
	 * 自定义验证异常
	 */
	@ExceptionHandler(BindException.class)
	public AjaxResult validatedBindException(BindException e)
	{
		logger.error(e.getMessage(), e);
		String message = e.getAllErrors().get(0).getDefaultMessage();
		return AjaxResult.error(message);
	}

	/**
	 * 自定义验证异常
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public Object validExceptionHandler(MethodArgumentNotValidException e)
	{
		logger.error(e.getMessage(), e);
		String message = e.getBindingResult().getFieldError().getDefaultMessage();
		return AjaxResult.error(message);
	}

	@ExceptionHandler(CompanyException.class)
	public R handleCompanyException(CompanyException e){
		R r = new R();
		r.put("code",500);
		r.put("msg", e.getMessage());
		return r;
	}

	/**
	 * 演示模式异常
	 */
	@ExceptionHandler(DemoModeException.class)
	public AjaxResult demoModeException(DemoModeException e)
	{
		return AjaxResult.error("演示模式，不允许操作");
	}
}
