package com.bonus.gs.sub.evaluate.manager.controller;


import com.bonus.gs.sub.evaluate.manager.utils.AjaxRes;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.regex.Pattern;

public class BaseController<T> {


    protected Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * 得到ModelAndView
     */
    public ModelAndView getModelAndView() {
        return new ModelAndView();
    }

    public AjaxRes getAjaxRes() {
        return new AjaxRes();
    }

    /**
     * 得到request对象
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        return request;
    }

    // 判断字符串是否为数字
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }
    public String toChangeString(String ids) {
        String idsss = "";
        String[] idss  = ids.split(",");
        for(String id: idss) {
            boolean isInt = isInteger(id);
            if(isInt) {
                idsss += id+",";
            }
        }
        return idsss;
    }

    //获取本地IP地址
    public String getIpAddress() {
        InetAddress addr = null;
        String ipAddress = "";
        try {
            addr = InetAddress.getLocalHost();
            System.out.println("Local HostAddress:"+addr.getHostAddress());
            //String hostname = addr.getHostName();
            ipAddress = addr.getHostAddress();
            //System.out.println("Local host name: "+hostname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ipAddress;
    }

}
