package com.iamberry.cms.handlers.milk;

import java.io.UnsupportedEncodingException;
import java.sql.Date;
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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.cms.core.entity.MilkMakingRecord;
import com.iamberry.cms.face.milk.MilkMakingRecordService;
import com.iamberry.wechat.core.entity.Page;
import com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.banner.ActionBanner;
import com.iamberry.wechat.core.entity.banner.JSONDTO;
/**
 * 
 * description: 奶粉信息
 * 	    author： LJK
 * 	createDate： 2016年12月12日16:40:16
 */
@RequestMapping("/admin/milkMakingRecord")
@Controller
public class MilkMakingRecordHandle {
	@Autowired
	private MilkMakingRecordService milkMakingRecordService;
	
	@RequestMapping("/milkMakingRecordList")
	public ModelAndView milkMakingRecordList(
			@RequestParam(value= "pageSize",defaultValue= "10" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1") Integer pageNO,
			HttpServletRequest request,String beginDate, String endDate,
			String username,String controller,String comments) throws UnsupportedEncodingException {
		ModelAndView mv = new ModelAndView("cms/milkMakingRecordList");
		MilkMakingRecord milkMakingRecord = new MilkMakingRecord();
		String name = username;
		/*String controller = request.getParameter("controller");
		String comments = request.getParameter("comments");
		String beginDate = request.getParameter("beginDate");
		String endDate = request.getParameter("endDate");*/
		Page page = new Page();
		if(name != null && !name.equals("")){
			page.setName("%"+name+"%");
			
		}
		if(controller != null && !controller.equals("")){
			milkMakingRecord.setController(controller);
		
		}
		if(comments != null && !comments.equals("")){
			milkMakingRecord.setComments(comments);
			
		}
		if(beginDate != null && !beginDate.equals("")){
			page.setBeginDate(Date.valueOf(beginDate));
			
		}
		if(endDate != null && !endDate.equals("")){
			page.setEndDate(Date.valueOf(endDate));
			
		}
		page.setPageNo(pageNO);
		page.setPageSize(pageSize);
		milkMakingRecord.setPage(page);
		// 搜索
		List<MilkMakingRecord> milkMakingRecordList = milkMakingRecordService.selectAllMilk(milkMakingRecord);
		Integer pageCount = milkMakingRecordService.selectAllMilkNumber(milkMakingRecord);
		page.setPageSize(pageSize);//获取记录总条数
		page.setTotalRecord(pageCount);
		mv.addObject("milkMakingRecordList", milkMakingRecordList);
		mv.addObject("pageSize",page.getTotalRecord() % pageSize == 0 ? page.getTotalRecord() / pageSize : page.getTotalRecord()
				/ pageSize + 1);
		mv.addObject("url", "/admin/milkMakingRecord/milkMakingRecordList?pageSize=" +
		pageSize +"&username="+(name == null? "" : name)+"&endDate="+(endDate == null? "" : endDate)+"&beginDate="+(beginDate == null? "" : beginDate)+
				"&comments="+(comments == null? "" : comments)+"&controller="+(controller == null? "" : controller)+ "&pageNO=");
		mv.addObject("pageNO", pageNO);
		mv.addObject("controller",controller);
		mv.addObject("comments",comments);
		mv.addObject("beginDate",beginDate);
		mv.addObject("endDate",endDate );
		mv.addObject("username",name);
		return mv;
	}
	
	/**
	 * 删除奶粉记录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteMilkMakingRecord")
	public JSONDTO deleteMilkMakingRecord(
			HttpServletRequest request
			) {
		JSONDTO dto = new JSONDTO();
		// 解析imgid
		String id = request.getParameter("id");
		Integer imgId = null;
		if (id == null || id.isEmpty()) {
			dto.setInfo("操作有误，请稍后重试!");
			return dto;
		} else {
			try {
				imgId = Integer.parseInt(id);
			} catch (Exception e) {
			}
		}
		if (imgId == null) {
			dto.setInfo("操作有误，请稍后重试!");
			return dto;
		}
		Integer msg = milkMakingRecordService.deleteMilk(id);
		if(msg == 1){
			dto.setInfo("删除成功!");
			return dto;
		}else{
			dto.setInfo("删除失败!");
			return dto;
		}
	}
}
