package com.iamberry.wechat.handles.porduct;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.wechat.core.entity.banner.MessageDTO;
import com.iamberry.wechat.core.entity.product.Product;
import com.iamberry.wechat.core.entity.product.ProductColor;
import com.iamberry.wechat.face.porduct.ProductColorService;
import com.iamberry.wechat.face.porduct.ProductService;

/**
 * 产品颜色	handler
 * 2017年2月5日
 * @author muzx
 */
@Controller
@RequestMapping("/admin/product_color")
public class ProductColorHandler {
	@Autowired
	private ProductColorService productColorService;
	
	@Autowired
	private ProductService productservice;
	
	/**
	 * 产品颜色列表
	 * 2017年2月6日
	 * @author muzx
	 * @param pageSize
	 * @param pageNO
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView getColorList(
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO){
		ModelAndView mv = new ModelAndView("/admin/product/productColorList");
		ProductColor pc = new ProductColor();
		pc.getPage().setPageSize(pageSize);
		pc.getPage().setPageNumber(pageNO);
		List<ProductColor> list = productColorService.selectProductColorList(pc);
		int count = productColorService.selectProductColorCount(pc);
		int pageCount =  count % pc.getPage().getPageSize() == 0? count/pc.getPage().getPageSize():(count/pc.getPage().getPageSize())+1;
		mv.addObject("list", list);
		mv.addObject("pageSize", pageSize);
		mv.addObject("url", "/admin/product_color/list?pageSize=" + pageSize + "&pageNO=");
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", pageCount);
		return mv;
	}
	
	/**
	 * 进入添加页面
	 * 2017年2月6日
	 * @author muzx
	 * @return
	 */
	@RequestMapping("/to_add")
	public ModelAndView toAdd(){
		ModelAndView mv = new ModelAndView("admin/product/addProductColor");
		//产品下拉框
		List<Product> pList = productservice.selectProductIdAndName();
		mv.addObject("pList", pList);
		return mv;
	}
	
	/**
	 * 添加一个产品颜色
	 * 2017年2月6日
	 * @author muzx
	 * @param productColor
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(ProductColor productColor){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
		Integer i = productColorService.insertProductColor(productColor);
		if(i > 0){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加成功！"));
		}
		return mv;
	}
	
	/**
	 * 进入修改页面
	 * 2017年2月6日
	 * @author muzx
	 * @param colorId
	 * @return
	 */
	@RequestMapping(value = "/to_update/{colorId}", method = RequestMethod.GET)
	public ModelAndView toUpdate(
			@PathVariable("colorId") String colorId){
		ModelAndView mv = new ModelAndView("admin/product/updateProductColor");
		if(StringUtils.isNotEmpty(colorId)){
			ProductColor productColor = productColorService.selectProductColorById(Integer.parseInt(colorId));
			mv.addObject("productColor", productColor);
			
			//产品下拉框
			List<Product> pList = productservice.selectProductIdAndName();
			mv.addObject("pList", pList);
		}
		return mv;
	}
	
	/**
	 * 修改一个产品颜色
	 * 2017年2月6日
	 * @author muzx
	 * @return
	 */
	@RequestMapping("/update")
	public ModelAndView update(ProductColor productColor){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		mv.addObject("msgObj", new MessageDTO(true, "操作结果", "修改失败！"));
		Integer i = productColorService.updateProductColorById(productColor);
		if(i > 0){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "修改成功！"));
		}
		return mv;
	}
}
