package com.iamberry.wechat.handles.rewards;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

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.ResultMsg;
import com.iamberry.wechat.core.entity.banner.MessageDTO;
import com.iamberry.wechat.core.entity.product.ProductType;
import com.iamberry.wechat.core.entity.rewards.RewardsRank;
import com.iamberry.wechat.face.porduct.ProductTypeService;
import com.iamberry.wechat.face.rewards.RewardsService;
import com.iamberry.wechat.tools.ResultInfo;

/**
 * 会员等级 会员分销奖励记录 
 * 2016年9月20日
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/rewards")
public class RewardsHandler {
	@Autowired
	private RewardsService rewardsService;
	
	@Autowired
	private ProductTypeService productTypeService;
	/**
	 * 获取所有分销奖励规则
	 * 2016年9月20日
	 * 穆再兴
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView getAll(){
		ModelAndView mv = new ModelAndView("/admin/rewards/rewardsRankeList");
		List<RewardsRank> list = rewardsService.selectRewardsList();
		if(list != null && list.size() > 0){
			mv.addObject("list", list);
		}
		return mv;
	}
	
	/**
	 * 进入添加页面
	 * 2016年9月20日
	 * @return
	 */
	@RequestMapping("/toAdd")
	public ModelAndView toAdd(){
		ModelAndView mv = new ModelAndView("/admin/rewards/addRewardsRank");
		//查出所有产品类型
		List<ProductType> typelist = productTypeService.getAll();
		mv.addObject("typeList", typelist);
		return mv;
	}
	
	/**
	 * 添加一个分销奖励规则
	 * 2016年9月20日
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(HttpServletRequest req,RewardsRank rewardsRank ){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		if(rewardsRank == null || StringUtils.isEmpty(rewardsRank.getRankName())
				|| StringUtils.isEmpty(rewardsRank.getRankDesc())){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
			return mv ;
		}
		//设置产品类型
		String typeId = req.getParameter("typeId");
		if(StringUtils.isNotEmpty(typeId)){
			ProductType type = new ProductType();
			type.setTypeId(Integer.parseInt(typeId));
			rewardsRank.setProductType(type);
		}
		Integer i = rewardsService.insertRewardsRank(rewardsRank);
		if(i > 0 ){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加成功！"));
			return mv ;
		}else{
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
			return mv ;
		}
	}
	
	/**
	 * 进入修改页面
	 * 2016年9月20日
	 * @return
	 */
	@RequestMapping("/toUpdate")
	public ModelAndView toUpdate(
			@RequestParam(value= "rankId",defaultValue= "" ,required=false)String rankId){
		ModelAndView mv = new ModelAndView("/admin/rewards/updateRewardsRank");
		if(StringUtils.isNotEmpty(rankId)){
			RewardsRank rewardsRank = rewardsService.selectRewardsRankById(Integer.parseInt(rankId));
			mv.addObject("rewardsRank", rewardsRank);
			//查出所有产品类型
			List<ProductType> typelist = productTypeService.getAll();
			mv.addObject("typeList", typelist);
		}
		return mv;
	}
	
	/**
	 * 修改一个分销奖励规则
	 * @return
	 */
	@RequestMapping("/update")
	public ModelAndView update(HttpServletRequest req,RewardsRank rewardsRank){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		if(rewardsRank != null && rewardsRank.getRankId() != null){
			//设置产品类型
			String typeId = req.getParameter("typeId");
			if(StringUtils.isNotEmpty(typeId)){
				ProductType type = new ProductType();
				type.setTypeId(Integer.parseInt(typeId));
				rewardsRank.setProductType(type);
			}
			Integer i = rewardsService.updateRewardsRank(rewardsRank);
			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年7月8日
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public ResultMsg deleteCouponType(HttpServletRequest req){
		ResultMsg rm = new ResultMsg();
		String rankId = req.getParameter("rankId");
		if(StringUtils.isNotEmpty(rankId)){
			RewardsRank rank = new RewardsRank();
			rank.setRankId(Integer.parseInt(rankId));
			Integer i = rewardsService.deleteRewardsRankById(rank);
			if(i != null && i.intValue() > 0){
				rm.setStatus(true);
				rm.setMessage(ResultInfo.SUCCESSINFO);
			}else{
				rm.setStatus(false);
				rm.setMessage(ResultInfo.ERRORINFO);
			}
		}else{
			rm.setStatus(false);
			rm.setMessage(ResultInfo.ERRORINFO);
		}
		return rm;
	}
}
