package cn.superdesk.uniorder.controller.base;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.constants.BaseSysConf;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @Description: 计划弃用 或 改造
 * @Author: linjinyu
 * @CreateDate: 2021/10/27 下午3:30
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/27 下午3:30
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
public class BaseController {


	@Autowired
	protected HttpServletRequest request;

	@ResponseBody
	@ExceptionHandler
	public R exceptionHandler(HttpServletRequest request, Exception exception) {

		exception.printStackTrace();

		HttpServletResponse response = getResponse();

		R responseResult = new R();

		// 让浏览器用 utf8 来解析返回的数据
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		// 告诉servlet用 UTF-8 转码,而不是用默认的ISO8859
		response.setCharacterEncoding("UTF-8");

		Date             now = DateUtil.date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		log.error("time:" + sdf.format(now) + " err info:" + exception.toString());

		if (exception instanceof UnifiedOrderRuntimeException) {
			UnifiedOrderRuntimeException myException = (UnifiedOrderRuntimeException) exception;
			responseResult.setCode(myException.getCode());
			responseResult.setMessage(myException.getMessage());
			responseResult.setSuccess(false);
			return responseResult;
		}

		responseResult.setCode("-1");
		responseResult.setSuccess(false);
		responseResult.setData(exception.toString());
		responseResult.setMessage("请求失败 !");

		return responseResult;
	}

	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	public HttpServletResponse getResponse() {
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
		return response;
	}

	public HttpSession getSession() {
		HttpSession session = getRequest().getSession();
		return session;
	}

	public Long getLong(String key) {

		String value = request.getParameter(key);

		if (StringUtils.isNotBlank(value)) {
			return Long.parseLong(value.trim());
		}

		return null;
	}

	public BigDecimal getBigDecimal(String key) {

		String value = request.getParameter(key);

		if (StringUtils.isNotBlank(value)) {
			return new BigDecimal(value);
		}

		return null;
	}

	public Byte getBtye(String key) {

		String value = request.getParameter(key);

		if (StringUtils.isNotBlank(value)) {
			return Byte.valueOf(value);
		}

		return null;
	}

	// ///////////////////////getHttpRequest()方法扩展//////////////////////////

	/**
	 * 根据参数名从HttpRequest中获取Double类型的参数值,无值则返回null .
	 *
	 * @param key .
	 * @return DoubleValue or null .
	 */
	public Double getDouble(String key) {

		String value = request.getParameter(key);

		if (StringUtils.isNotBlank(value)) {
			return Double.parseDouble(value.trim());
		}

		return null;
	}

	public Integer getInteger(String key) {

		String value = request.getParameter(key);

		if (StringUtils.isNotBlank(value)) {
			return Integer.parseInt(value.trim());
		}

		return null;
	}

	public String getString(String key) {

		String param = request.getParameter(key);

		if (StringUtils.isEmpty(param)) {
			return param;
		} else {
			return param.trim();
		}

	}

	/**
	 * 当前页数 TODO 写死 pageNumber
	 *
	 * @return
	 */
	protected int getPageNum() {

		String pageNumStr = request.getParameter("pageNumber");

		int pageNum = 1;

		if (StringUtils.isNotBlank(pageNumStr)) {
			pageNum = Integer.valueOf(pageNumStr);
		}

		return pageNum;
	}


	/**
	 * TODO 写死 pageSize
	 *
	 * @return
	 */
	protected int getPageSize() {

		String numPerPageStr = request.getParameter("pageSize");
		int    numPerPage    = 20;
		if (StringUtils.isNotBlank(numPerPageStr)) {
			numPerPage = Integer.parseInt(numPerPageStr);
		}
		return numPerPage;
	}

	protected String getToken() {

		String token = request.getHeader(BaseSysConf.TOKEN);

		if (StringUtils.isEmpty(token)) {
			token = request.getParameter("token");
		}

		return token;
	}
}
