package com.iamberry.wechat.handles.questionnaire;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

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

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.servlet.ModelAndView;

import com.iamberry.wechat.core.entity.questionnaire.QuestionnaireAnswer;
import com.iamberry.wechat.core.entity.questionnaire.QuestionnaireAnswerDetailed;
import com.iamberry.wechat.face.questionnaire.QuestionnaireAnswerDetailedService;
import com.iamberry.wechat.face.questionnaire.QuestionnaireAnswerService;

@Controller
@RequestMapping("/admin/answerData")
public class AnswerDataHander {
	@Autowired
	private QuestionnaireAnswerService questionnaireAnswerService;
	@Autowired
	private QuestionnaireAnswerDetailedService questionnaireAnswerDetailedService;
	
	/**
	 * 分页查询题库所有用户信息
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getAllAnswer")
	public ModelAndView getAllAnswer(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ModelAndView mv = new ModelAndView("/admin/questionnaire/questionnaireAnswerList");
		String answerBabyAge = request.getParameter("answerBabyAge");//宝宝月份
		String answerBabyName = request.getParameter("answerBabyName");//宝宝昵称
		
		QuestionnaireAnswer questionnaireAnswer = new QuestionnaireAnswer();
		questionnaireAnswer.getPage().setPageNumber(pageNO);
		questionnaireAnswer.getPage().setPageSize(pageSize);
		questionnaireAnswer.getPage().initRecordBegin();
		StringBuffer url = new StringBuffer("/admin/answerData/getAllAnswer?&pageSize=" + pageSize);
		if (null != answerBabyAge && !"".equals(answerBabyAge)) {
				questionnaireAnswer.setAnswerBabyAge(Integer.valueOf(answerBabyAge));
		}
		if (null != answerBabyName && !"".equals(answerBabyName)) {
			questionnaireAnswer.setAnswerBabyName(answerBabyName);
		}
		List<QuestionnaireAnswer> answerList = questionnaireAnswerService.getAllAnswer(questionnaireAnswer);
		
		url.append("&pageNO=");
		mv.addObject("answerBabyAge", answerBabyAge);
		mv.addObject("answerBabyName", answerBabyName);
		mv.addObject("answerList", answerList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("url", url.toString());
		mv.addObject("pageSize", questionnaireAnswerService.getAllAnswerCount(questionnaireAnswer));
		return mv;
	}
	
	/**
	 * 查询用户回答详情
	 * @param request
	 * @param response
	 * @param questionnaireAnswerDetailedList
	 * @return
	 */
	@RequestMapping("/getAnswerDetailed")
	public ModelAndView getAnswerDetailed(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv = new ModelAndView("/admin/questionnaire/answerDetailed");
		List<QuestionnaireAnswerDetailed> questionnaireAnswerDetailedList = null;
		String detailedAnswerId = request.getParameter("answerId");
		if (null != detailedAnswerId && !"".equals(detailedAnswerId)) {
			questionnaireAnswerDetailedList =	questionnaireAnswerDetailedService.getAnswerDetailed(Integer.valueOf(detailedAnswerId));
		}
		mv.addObject("questionnaireAnswerDetailedList", questionnaireAnswerDetailedList);
		return mv;
	}
}
