package com.iamberry.cms.handlers.machine;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

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.servlet.ModelAndView;

import com.iamberry.cms.core.entity.Machine;
import com.iamberry.cms.face.machine.MachineService;

@Controller
@RequestMapping("/admin/machine")
public class MachineHandler {
	
	@Autowired
	private MachineService machineService;

	/**
	 * 分页查询冲奶机列表信息
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectMachineList")
	public ModelAndView selectMachineList(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "10" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ModelAndView mv = new ModelAndView("admin/machine/machineList");
		Machine machine = new Machine();
		StringBuilder url = new StringBuilder("admin/machine/selectMachineList?&pageSize=" + pageSize);
		String status = request.getParameter("status");
		if (status != null && !"".equals(status)) {
			machine.setStatus(status);
			mv.addObject("status", status);
			url.append("&status=" + status);
		}
		String userName = request.getParameter("userName");
		if (userName != null && !"".equals(userName)) {
			machine.setUserName(userName);
			mv.addObject("userName", userName);
			url.append("&userName=" + userName);
		}
		String machineName = request.getParameter("machineName");
		if (machineName != null && !"".equals(machineName)) {
			machine.setMachineName(machineName);
			mv.addObject("machineName", machineName);
			url.append("&machineName=" + machineName);
		}
		String location = request.getParameter("location");
		if (location != null && !"".equals(location)) {
			machine.setLocation(location);
			mv.addObject("location", location);
			url.append("&location=" + location);
		}
		machine.getPage().setPageNumber(pageNO);
		machine.getPage().setPageSize(pageSize);
		machine.getPage().initRecordBegin();
		
		//分页查询冲奶机列表信息
		List<Machine> machineList = machineService.selectMachineList(machine);
		
		url.append("&pageSize=" + pageSize + "&pageNO=");
		mv.addObject("url", url.toString());
		mv.addObject("machineList", machineList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", machineService.selectMachineListCount(machine));
		return mv;
	}
}