package lingtao.net.controller;

import java.util.List;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.LoginIp;
import lingtao.net.bean.Msg;
import lingtao.net.service.LoginIpService;

@Controller
public class LoginIpController {

	@Autowired
	private LoginIpService loginIpService;

	// IP跳转页面
	@RequestMapping("/loginIp/index")
	public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.sendRedirect(request.getContextPath() + "/views/system/loginIp/loginIp.jsp");
	}

	/**
	 * 查询所有IP
	 */
	@RequestMapping("/getLoginIpList")
	@ResponseBody
	private Msg IPList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, LoginIp loginIp) {
		PageHelper.startPage(page, limit);
		List<LoginIp> list = loginIpService.getLoginIpList(loginIp);
		PageInfo<LoginIp> pageInfo = new PageInfo<LoginIp>(list);
		return Msg.success().add("ipList", pageInfo);
	}

	/**
	 * 添加IP
	 */
	@ResponseBody
	@RequestMapping("/addIp")
	public Msg addIp(LoginIp loginIp) {
		loginIpService.addIp(loginIp);
		return Msg.success();
	}
	
	/**
	 * 修改IP
	 */
	@ResponseBody
	@RequestMapping("/updateIp")
	public Msg updateIp(LoginIp loginIp) {
		loginIpService.updateIp(loginIp);
		return Msg.success();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/deleteIp")
	@ResponseBody
	public Msg deleteIp(@RequestParam("id") Integer id) {
		loginIpService.deleteIpById(id);
		return Msg.success();
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 */
	@ResponseBody
	@RequestMapping("/deleteIps")
	public Msg deleteBatch(@RequestParam(value = "ids") String ids) {
		String[] arrIds = ids.split(",");
		// String数组转为Integer数组
		int[] ints = new int[arrIds.length];
		for (int i = 0; i < arrIds.length; i++) {
			ints[i] = Integer.parseInt(arrIds[i]);
		}
		return loginIpService.deleteBatch(ints);
	}

	/**
	 * 检查授权IP是否存在
	 */
	@ResponseBody
	@RequestMapping("/checkIP")
	public Msg checkIP(@RequestParam(value = "agreeIp") String agreeIp) {
		return loginIpService.checkIP(agreeIp);
	}
}
