package com.iamberry.rent.handlers.admin;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.rent.face.machine.MachineService;
import com.iamberry.wechat.core.entity.Page;
import com.iamberry.wechat.core.entity.rent.MachineInfo;
@RequestMapping("/admin/rent")
@Controller
public class AdminRentHandler {
	@Autowired
	private MachineService machineService;
	
	/**
	 * 机器信息列表（后台）
	 * @param pageNO
	 * @param pageSize
	 * @param agentMachineStatus
	 * @param request
	 * @return
	 * @author LJK
	 * @Time   2017年3月15日14:19:42
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/machineInfoList")
	public ModelAndView agentMachineList(
			@RequestParam(value = "pageNO", defaultValue = "1", required = false) int pageNO,
			@RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
			HttpServletRequest request,MachineInfo machineInfo) throws Exception {
		ModelAndView mv = new ModelAndView();
		Page page = new Page();
		StringBuilder url = new StringBuilder("/admin/reserve/machineInfoList?pageSize=" + pageSize);
		String stopDate = request.getParameter("stopDate");//剩余时间
		String initState = request.getParameter("initState");//剩余水量
		
		GregorianCalendar gc=new GregorianCalendar(); 
		gc.setTime(new Date()); 
		if(stopDate != null && !stopDate.equals("")){
			switch (Integer.valueOf(stopDate)) {
			case 1:
				gc.add(5, +10);
				machineInfo.setEndDate(gc.getTime());
				break;
			case 2:
				gc.add(5, +10);
				machineInfo.setBeginDate(gc.getTime());
				gc.setTime(new Date()); 
				gc.add(5, +30);
				machineInfo.setEndDate(gc.getTime());
				break;
			case 3:
				gc.add(Calendar.MONTH, +1);
				machineInfo.setBeginDate(gc.getTime());
				gc.setTime(new Date()); 
				gc.add(Calendar.MONTH, +3);
				machineInfo.setEndDate(gc.getTime());
				break;
			case 4:
				gc.add(Calendar.MONTH, +3);
				machineInfo.setBeginDate(gc.getTime());
				gc.setTime(new Date()); 
				gc.add(Calendar.MONTH, +6);
				machineInfo.setEndDate(gc.getTime());
				break;
			case 5:
				gc.add(Calendar.MONTH, +6);
				machineInfo.setBeginDate(gc.getTime());
				gc.setTime(new Date()); 
				gc.add(Calendar.MONTH, +12);
				machineInfo.setEndDate(gc.getTime());
				break;
			default:
				break;
			}
		}
		if(initState != null && !initState.equals("")){
			switch (Integer.valueOf(initState)) {
			case 1:
				machineInfo.setEndWater(500);
				break;
			case 2:
				machineInfo.setBeginWater(500);
				machineInfo.setEndWater(1000);
				break;
			case 3:
				machineInfo.setBeginWater(100);
				machineInfo.setEndWater(1500);
				break;
			case 4:
				machineInfo.setBeginWater(1500);
				machineInfo.setEndWater(2000);
				break;
			case 5:
				machineInfo.setBeginWater(2000);
				machineInfo.setEndWater(2500);
				break;
			default:
				break;
			}
		}
		page.setPageNo((pageNO - 1) * pageSize);
		int count = machineService.selectEquipmentCount(machineInfo);
		int pageCount = count % pageSize == 0 ? count / pageSize : count
				/ pageSize + 1;
		page.setPageSize(pageSize);
		machineInfo.setPage(page);
		List<MachineInfo> machineInfoList = machineService.selectEquipmentList(machineInfo);
		url.append("&pageNO=");
		if(machineInfo.getMachineMac() != null){
			mv.addObject("machineMac", machineInfo.getMachineMac());
		}
		if(machineInfo.getMachineDeviceId() != null){
			mv.addObject("machineDeviceId", machineInfo.getMachineDeviceId());
		}
		if(machineInfo.getMachineInitState() != null){
			mv.addObject("machineInitState", machineInfo.getMachineInitState());
		}
		mv.addObject("machineInfoList", machineInfoList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", pageCount);
		mv.addObject("stopDate", stopDate);
		mv.addObject("initState", initState);
		mv.addObject("url", url);
		mv.setViewName("/admin/reserve/machineInfoList");
		return mv;
	}
}
