package com.iamberry.wechat.handles.porduct;

import java.util.Date;
import java.util.List;

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

import org.apache.commons.lang.StringUtils;
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.wechat.core.entity.banner.MessageDTO;
import com.iamberry.wechat.core.entity.product.Product;
import com.iamberry.wechat.core.entity.product.ProductType;
import com.iamberry.wechat.face.porduct.ProductTypeService;

@Controller
@RequestMapping("/admin/productType")
public class ProductTypeHandler {
	@Autowired
	private ProductTypeService productTypeService;
	
	/**
	 * 查询所有产品类型
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView getAllType(){
		ModelAndView mv = new ModelAndView("/admin/product/productTypeList");
		List<ProductType> list = productTypeService.getAll();
		mv.addObject("list", list);
		return mv ;
	}
	
	/**
	 * 进入添加产品类型页面
	 * @return
	 */
	@RequestMapping("/toAdd")
	public ModelAndView toAdd(){
		ModelAndView mv = new ModelAndView("/admin/product/addProductType");
		return mv ;
	}
	
	/**
	 * 添加一个产品类型
	 * @param productType
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView addProductType(ProductType productType){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		if(productType != null){
			if(StringUtils.isNotEmpty(productType.getTypeName())
					&& StringUtils.isNotEmpty(productType.getTypeDetilas()) 
					&& productType.getTypeState() != null ){
				Date date = new Date();
				productType.setTypeCreateDate(date);
				productType.setTypeUpdateDate(date);
				Integer i = productTypeService.insertProductType(productType);
				if(i > 0 ){
					mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加成功！"));
					return mv ;
				}else{
					mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
					return mv ;
				}
			}
		}
		mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
		return mv ;
	}
	
	/**
	 * 进入产品类型修改页面
	 * @return
	 */
	@RequestMapping("/toUpdate")
	public ModelAndView toUpdateProductType(
			@RequestParam(value= "typeId",defaultValue= "" ,required=false)String typeId){
		ModelAndView mv = new ModelAndView("/admin/product/updateProductType");
		if(StringUtils.isNotEmpty(typeId)){
			ProductType type = productTypeService.selectOneById(Integer.parseInt(typeId));
			mv.addObject("type", type);
		}
		return mv ;
	}
	
	/**
	 * 修改产品类型
	 * @return
	 */
	@RequestMapping("/update")
	public ModelAndView updateProductType(ProductType productType){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		if(productType != null && productType.getTypeId() != null){
			Integer i = productTypeService.updateProductTypeById(productType);
			if(i > 0){
				mv.addObject("msgObj", new MessageDTO(true, "操作结果", "修改成功！"));
				return mv ;
			}else{
				mv.addObject("msgObj", new MessageDTO(false, "操作结果", "修改失败！"));
				return mv ;
			}
		}
		mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
		return mv ;
	}
	
	/**
	 * 修改商品类型状态
	 * 2016年4月22日
	 * @param request
	 * @param response
	 * @author 穆再兴
	 */
	@RequestMapping("/updateStatus")
	@ResponseBody
	public String updateProductStatus(HttpServletRequest request,
			HttpServletResponse response,ProductType productType) throws Exception {
		String typeId = request.getParameter("typeId");
		String typeState = request.getParameter("typeState");
		if(StringUtils.isEmpty("typeId") || StringUtils.isEmpty("typeState")){
			return "{\"status\":\"修改失败！\"}";
		}
		productType.setTypeId(Integer.parseInt(typeId));
		productType.setTypeState(Integer.parseInt(typeState));
		
		Integer msg = productTypeService.updateProductTypeStatusById(productType);
		if(msg == 0){
			return "{\"status\":\"修改失败！\"}";
		}
		return "{\"status\":\"修改成功！\"}";
	}
}
