package com.iamberry.rent.handlers.admin;

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.rent.face.order.ServiceCouponService;
import com.iamberry.wechat.core.entity.banner.MessageDTO;
import com.iamberry.wechat.core.entity.rent.ServiceCouponInfo;

@Controller
@RequestMapping("/admin/serviceCoupon")
public class AdminServiceCouponHandler {
	
	@Autowired
	private ServiceCouponService serviceCouponService;

	@RequestMapping("/selectServiceCouponList")
	public ModelAndView selectServiceCouponList(HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		ServiceCouponInfo info = new ServiceCouponInfo();
		List<ServiceCouponInfo> couponList = serviceCouponService.list(info);
		mv.addObject("couponList", couponList);
		return mv;
	}
	
	@RequestMapping("/saveServiceCoupon")
	public ModelAndView saveServiceCoupon(HttpServletRequest request, ServiceCouponInfo info) throws Exception {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		int count = serviceCouponService.saveServiceCoupon(info);
		if(count > 0){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加成功！"));
		}else{
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
		}
		return mv;
	}
	
	public ModelAndView goUpdateView(HttpServletRequest request,
			@RequestParam(value="serviceId",defaultValue= "", required=false)Integer serviceId) throws Exception {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		if (serviceId == null) {
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "进入修改页面失败！"));
			return mv;
		}
		ServiceCouponInfo info = new ServiceCouponInfo();
		info.setServiceId(serviceId);
		List<ServiceCouponInfo> couponList = serviceCouponService.list(info);
		if (couponList == null || couponList.size() == 0) {
			mv.setViewName("admin/msg/msg");
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "进入修改页面失败！"));
			return mv;
		}
		ServiceCouponInfo serviceCouponInfo = couponList.get(0);
		mv.addObject("serviceCouponInfo", serviceCouponInfo);
		return mv;
	}
	
	@RequestMapping("/updateServiceCoupon")
	public ModelAndView updateServiceCoupon(HttpServletRequest request,
			@RequestParam(value="serviceId",defaultValue= "", required=false)Integer serviceId) throws Exception {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		if (serviceId == null) {
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
			return mv;
		}
		ServiceCouponInfo info = new ServiceCouponInfo();
		info.setServiceId(serviceId);
		int count = serviceCouponService.saveServiceCoupon(info);
		if(count > 0){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加成功！"));
		}else{
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
		}
		return mv;
	}
}
