package org.ccpit.base.controller;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.ccpit.base.modol.BaseEntity;
import org.ccpit.base.user.User;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2015/8/13.
 */
public class BaseController {

    public static final String USER_IN_SESSION="user_in_session";
    public static final String SUCCESS= "{success:true}";

    public void setUserSession(HttpServletRequest request,BaseEntity entity){
        request.getSession().setAttribute(USER_IN_SESSION,entity);
    }
    public User getUserSession(HttpServletRequest request){
        return (User) request.getSession().getAttribute(USER_IN_SESSION);
    }


    /**
     * 方法描述：封装网格查询字段排序和分页信息
     *
     * @param request
     * @return PageRequest
     */
    public PageRequest getPage(HttpServletRequest request) {
        int page = 1;
        int rows = 10;
        // 从请求中拿page 判断是否为空
        String requestPage = request.getParameter("page");
        if (!StringUtils.isEmpty(requestPage) && StringUtils.isNumeric(requestPage)) {
            page = Integer.parseInt(requestPage);
        }

        // 从请求中拿rows 判断是否为空
        String requestRow = request.getParameter("rows");
        if (!StringUtils.isEmpty(requestRow) && StringUtils.isNumeric(requestRow)) {
            rows = Integer.parseInt(requestRow);
        }

        String sortName = "";
        String sortOrder = "";
        // 从请求中拿sortName 判断是否为空
        String requestSort = request.getParameter("sort");
        if (!StringUtils.isEmpty(requestSort)) {
            sortName = StringEscapeUtils.escapeSql(requestSort);
        }
        // 从请求中拿sortOrder 判断是否为空
        String requestOrder = request.getParameter("order");
        if (!StringUtils.isEmpty(requestOrder)) {
            sortOrder = StringEscapeUtils.escapeSql(requestOrder);
        }

        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageSize(rows);
        pageRequest.setPageNo(page);
        /**
         * 设置排序字段, 多个排序字段时用','分隔.
         */
        pageRequest.setOrderBy(sortName + " " + sortOrder);
        return pageRequest;
    }
    /**
     * getRequestIp:(获取客户端访问在Ip). 如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP值，那么真正的用户端的真实IP则是取X-Forwarded-For中第一个非unknown的有效IP字符串。<br/>
     * @author dingwei
     * @param request
     * @return
     * @since JDK 1.6
     */
    public final static String getRequestIp(HttpServletRequest request){
    	String ip = request.getHeader("x-forwarded-for");
    	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}
