/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-02-08
 * 描述            ：岗位操作IP限制
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Lengthwise.Performance.impl;
import javax.servlet.http.HttpServletRequest;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Lengthwise.Performance.mapper.MesOprIpPermissionsMapper;
import market.Mes.Lengthwise.Performance.service.MesOprIpPermissionsService;
import market.Mes.Lengthwise.Performance.vo.MesOprIpPermissions;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(path="MesOprIpPermissionsService")
public class MesOprIpPermissionsServiceImpl extends BaseServiceImpl<MesOprIpPermissions,Object> implements MesOprIpPermissionsService {
	/**
	 * 过程时间处理
	 * @param sum
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<MesOprIpPermissions> sum){
		ResultModel rm=new ResultModel();
		//获取ip地址，若存在交换机的情况则获取的会出现错误
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  
		String ip=request.getHeader("X-Real-IP");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			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 || "unknow".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String[] ips=ip.split(",");
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割  
        /*if(ip!=null && ip.length()>15){ //"***.***.***.***".length() = 15  
            if(ip.indexOf(",")>0){  
            	ip = ip.substring(0,ip.indexOf(","));  
            }  
        }  */
		
		if(sum.getObject()==null){
			rm.setState("500");
			rm.setMsgInfo("请传入参数");
			return rm;
		}
		try{
			for(String ipc:ips){
				MesOprIpPermissions moip=new MesOprIpPermissions();
				moip.setIp_address_01(ipc);
				moip.setMachine_code(sum.getObject().getMachine_code());
				moip.setPurpose_code("USE_01");
				int icount=this.getMapper(MesOprIpPermissionsMapper.class).count(moip);
				if(icount>0){
					rm.setData(true);
					break;
				}else{
					rm.setData(false);
				}
			}
			rm.setState("200");
			rm.setMsgInfo(ip);
		}catch(Exception ex){
			rm.setState("500");
			rm.setData(ips);
			rm.setMsgInfo("查询异常！");
			rm.setErrorInfo(ex.getMessage());
		}
		
		return rm;
	}
	
	/**
	 * 过程时间处理
	 * @param sum
	 * @return
	 */
	public ResultModel getIp(){
		ResultModel rm=new ResultModel();
		//获取ip地址，若存在交换机的情况则获取的会出现错误
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  
		String ip=request.getHeader("X-Real-IP");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			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 || "unknow".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String[] ips=ip.split(",");
        rm.setMsgInfo(ip);
        rm.setData(ips);
        rm.setState("200");
		return rm;
	}
	
	
}
