package com.iamberry.cms.handlers.milk;

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.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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.cms.core.dto.MilkPowderInfoDto;
import com.iamberry.cms.core.entity.MilkPowderInfo;
import com.iamberry.cms.face.milk.MilkPowderService;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.banner.MessageDTO;
import com.iamberry.wechat.tools.ResultInfo;

/**
 * 奶粉信息 Controller
 * 2016年12月12日
 * 穆再兴
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/milk_powder")
public class MilkPowderHandler {
	
	@Autowired
	private MilkPowderService milkPowderService;
	
	/**
	 * 奶粉信息列表
	 * 2016年12月13日
	 * 穆再兴
	 * @param milkPowderDto
	 * @param pageSize
	 * @param pageNO
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView getMilkPowderList(MilkPowderInfoDto milkPowderDto,
			@RequestParam(value="pageSize",defaultValue= "6", required=false)Integer pageSize,
			@RequestParam(value="pageNO",defaultValue= "1", required=false)Integer pageNO){
		ModelAndView mv = new ModelAndView("milkPowder/milkPowderList");
		milkPowderDto.getPage().setPageSize(pageSize);
		milkPowderDto.getPage().setPageNumber(pageNO);
		List<MilkPowderInfo> mPowderList = milkPowderService.selectMilkPowderList(milkPowderDto);
		Integer count = milkPowderService.selectMilkPowderCount(milkPowderDto);
		
		mv.addObject("mPowderList", mPowderList);
		mv.addObject("mPowderDto", milkPowderDto);
		int pageCount =  count % milkPowderDto.getPage().getPageSize() == 0? count/milkPowderDto.getPage().getPageSize():(count/milkPowderDto.getPage().getPageSize())+1;
		//分页用
		StringBuffer url = new StringBuffer("/admin/milk_powder/list?pageSize=");
		if(milkPowderDto.getBrand() != null && !milkPowderDto.getBrand().equals("")){
			url.append("&brand="+milkPowderDto.getBrand());
		}
		if(milkPowderDto.getPowderNumber() != null && !milkPowderDto.getPowderNumber().equals("")){
			url.append("&powderNumber="+milkPowderDto.getPowderNumber());
		}
		if(milkPowderDto.getBarCode() != null && !milkPowderDto.getBarCode().equals("")){
			url.append("&barCode="+milkPowderDto.getBarCode());
		}
		if(milkPowderDto.getStatus() != null && !milkPowderDto.getStatus().equals("")){
			url.append("&status="+milkPowderDto.getStatus());
		}
		url.append("&pageNO=");
		mv.addObject("pageNO", pageNO);
		mv.addObject("url", url);
		mv.addObject("pageSize", pageCount);
		return mv;
	}
	
	/**
	 * 进入添加奶粉信息页面
	 * 2016年12月13日
	 * 穆再兴
	 * @return
	 */
	@RequestMapping("/to_add")
	public ModelAndView toAdd(){
		ModelAndView mv = new ModelAndView("milkPowder/addMilkPowder");
		return mv;
	}
	
	/**
	 * 添加一个奶粉信息
	 * 2016年12月13日
	 * 穆再兴
	 * @param req
	 * @param couponType
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView addMilkPowderInfo(HttpServletRequest req,MilkPowderInfo milkPowderInfo){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		String userTel = req.getParameter("userTel");
		if(milkPowderInfo == null){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
		}
		if("user_uploading".equals(milkPowderInfo.getStatus()) && StringUtils.isNotEmpty(userTel)){
			milkPowderInfo.setStatus(userTel);
		}
		Integer count = milkPowderService.insertMilkPowderInfo(milkPowderInfo);
		if(count != null && count > 0){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加成功！"));
		}else{
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
		}
		return mv;
	}
	
	/**
	 * 后台进入修改页面
	 * 2016年7月8日
	 * @param couponId
	 * @return
	 */
	@RequestMapping(value = "/to_update/{id}", method = RequestMethod.GET)
	public ModelAndView toUpdateMilkPowderInfo(@PathVariable("id")Integer id){
		ModelAndView mv = new ModelAndView("milkPowder/updateMilkPowder");
		if(id != null){
			MilkPowderInfo mPowder = milkPowderService.selectMilkPowderInfoById(id);
			if(mPowder != null){
				mv.addObject("mPowder", mPowder);
			}
		}
		return mv;
	}
	
	/**
	 * 修改一个奶粉信息
	 * @param mPowder
	 * @return
	 */
	@RequestMapping("/update")
	public ModelAndView updateMilkPowderInfo(HttpServletRequest req,MilkPowderInfo mPowder){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		String userTel = req.getParameter("userTel");
		if(mPowder == null){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "修改失败！"));
		}
		if("user_uploading".equals(mPowder.getStatus()) && StringUtils.isNotEmpty(userTel)){
			mPowder.setStatus(userTel);
		}
		
		Integer msg = milkPowderService.selectMilkPowderInfoByCode(mPowder);
		if(msg != null && msg.intValue() == 0){
			Integer i = milkPowderService.updateMilkPowderInfoById(mPowder);
			if(i != null && i.intValue() > 0){
				mv.addObject("msgObj", new MessageDTO(true, "操作结果", "修改成功！"));
			}else{
				mv.addObject("msgObj", new MessageDTO(false, "操作结果", "修改失败！"));
			}
		}else {
			mv.addObject("msgObj", new MessageDTO(false, "操作结果", "数据库已存在！"));
		}
		return mv;
	}
	
	/**
	 * 后台删除一个奶粉信息
	 * 2016年7月8日
	 * @return
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteMilkPowderInfo(@PathVariable("id")Integer id){
		ResultMsg rm = new ResultMsg();
		rm.setStatus(false);
		rm.setMessage(ResultInfo.ERRORCODE);
		if(id != null){
			Integer i = milkPowderService.deleteMilkPowderInfoById(id);
			if(i != null && i.intValue() > 0){
				rm.setStatus(true);
				rm.setMessage(ResultInfo.SUCCESSCODE);
			}
		}
		return rm;
	}
}
