<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,java.lang.*,org.springframework.context.ApplicationContext"%>  
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>  
<%@ page import="com.iamberry.wechat.core.entity.WechatUtils"%>
<%@ page import="com.iamberry.wechat.face.activity.answer.*"%>
<%@ page import="com.iamberry.wechat.core.entity.activity.answer.*"%>
<%@ page import="com.iamberry.wechat.core.entity.member.*"%>  

<% 
	ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application); 
	WechatUtils wechatUtils = (WechatUtils)context.getBean("wechatUtils");
	AnswerPeopleDao answerPeopleDao = (AnswerPeopleDao)context.getBean("answerPeopleDaoImpl");
	AnswerCountDao answerCountDao = (AnswerCountDao)context.getBean("answerCountDaoImpl");
	//String openid = "o3dG7wViuhImN5I0w6N0upIUVw_0";
	String openid = "";
	try {
		Member member = wechatUtils.getUserBySession(request);
		openid = member.getUserOpenid();
	} catch (Exception e) {
		e.printStackTrace();
	}
	String answer = request.getParameter("answer");
	String result = "";
	if(answer == null || "".equals(answer)){
		result = "{\"status\":\"failed\",\"msg\":\"参数不能为空!\"}";
	}else{
		AnswerPeople people = new AnswerPeople();
		people.setpOpenid(openid);
		Integer i = answerPeopleDao.insertPeople(people);
		//Integer i = 0;
		if(i > 0){	//添加答题人成功
			//AnswerCount answerCount = answerCountDao.selectAnswerCountByAnswer(answer);
			//改答案已录入 则修改个数加1
			String[] answerStr = answer.split(",");
			for(int j = 0;j < answerStr.length;j++){
				Integer m = answerCountDao.updateAnswerCountByAnswer(answerStr[j].trim());	
			}
			result = "{\"status\":\"success\",\"msg\":\"答题成功!\"}";	
		}else{
			result = "{\"status\":\"failed\",\"msg\":\"您已答题!\"}";
		}
		
	}
	response.getWriter().print(result);
%>
