
package com.tofly.common.log.util;

import com.alibaba.fastjson.JSON;
import com.tofly.base.api.entity.Log;
import com.tofly.common.core.configs.MyParameterRequestWrapper;
import com.tofly.common.core.util.HttpURL;
import com.tofly.common.core.util.IpUtil;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * 系统日志工具类
 *
 */
@UtilityClass
@Slf4j
public class SysLogUtils {

	public Log getSysLog() {
		HttpServletRequest request = ((ServletRequestAttributes) Objects
			.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
		Log sysLog = new Log();
		sysLog.setCreateBy(Objects.requireNonNull(getUsername()));
		//sysLog.setType(CommonConstants.STATUS_NORMAL);
		sysLog.setRemoteAddr(IpUtil.getIpAddr(request));
		sysLog.setRequestUri(HttpURL.getPath(request.getRequestURI()));
		sysLog.setMethod(request.getMethod());
		sysLog.setParams(toParams(request));
		//sysLog.setServiceId(getClientId());
		//sysLog.setCreateTime(DateUtil.getCurrentTime());
		return sysLog;


	}


	 private String toParams(HttpServletRequest request){
		 StringBuilder sb=new StringBuilder();
		 Map m=request.getParameterMap();
		 if (request instanceof MyParameterRequestWrapper) {
			 // 签名处理过程 start....
			 MyParameterRequestWrapper myParameterRequestWrapper = (MyParameterRequestWrapper) request;
			m.putAll(myParameterRequestWrapper.getBodyParam());
			 // 签名处理过程 end....
		 }
		return JSON.toJSONString(m);
	 }

	 /**
	 * 获取客户端
	 *
	 * @return clientId
	 */
	private String getClientId() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication instanceof OAuth2Authentication) {
			OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
			return auth2Authentication.getOAuth2Request().getClientId();
		}
		return null;
	}

	/**
	 * 获取用户名称
	 *
	 * @return username
	 */
	private String getUsername() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null) {
			return null;
		}
		return authentication.getName();
	}



//	private String getBodyString(HttpServletRequest request) {
//		StringBuilder sb = new StringBuilder();
//		InputStream inputStream = null;
//		BufferedReader reader = null;
//		try {
//			inputStream = request.getInputStream();
//			reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
//			String line = "";
//			while ((line = reader.readLine()) != null) {
//				sb.append(line);
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			if (inputStream != null) {
//				try {
//					inputStream.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//			if (reader != null) {
//				try {
//					reader.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
//		return sb.toString().trim();
//	}

}
