package com.easy.front.servlet;

import javax.inject.Inject;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.thymeleaf.context.WebContext;

import com.easy.base.BaseServlet;
import com.easy.cms.service.DataService;
import com.easy.cms.service.SubjectService;
@WebServlet("/front/cafe")
public class CafeServlet extends BaseServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Inject
	DataService dataService;
	@Inject
	SubjectService subjectService;
	protected void service(HttpServletRequest request, HttpServletResponse response) {
		String action = request.getParameter("action");
		try {
			WebContext ctx = new WebContext(request, response, getServletContext());
			if (StringUtils.isEmpty(action)) {
				index(ctx);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//this.write(new Ajax(false, "出现错误！"), response); 
	}
	
	public void index(WebContext ctx) throws Exception {
		ctx.setVariable("helloword","CafeHome");
		//ctx.getResponse().setCharacterEncoding("utf-8");
		engine.process("cafe/index.html",ctx, ctx.getResponse().getWriter());
	}
/*	
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		this.common(request, response);
		
		String subjectId= request.getParameter("subjectId");
		Subject subject = subjectService.get(Integer.parseInt(subjectId));
		request.setAttribute("subject", subject);
		//查询对应品类下的产品
		List<Data> list= dataService.findLimit(Integer.parseInt(subjectId), 100);
		request.setAttribute("list", list);
		request.getRequestDispatcher(prefix + "front/list.jsp").forward(request, response);
	}
	public void detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//主题市场   菜单
		List<Subject> subjects = subjectService.getByParentId(2);
		for (Subject subject : subjects) {
			List<Subject> childs = subjectService.getByParentId(subject.getId());
			subject.setChilds(childs);
		}
		request.setAttribute("subjects", subjects);
		//end
		String subjectId= request.getParameter("subjectId");
		Subject subject = subjectService.get(Integer.parseInt(subjectId));
		request.setAttribute("subject", subject);
		String id =  request.getParameter("id");
		Data data = new Data();
		data.setTableName(subject.getTableName());
		data.setSubjectId(subject.getId());
		data.setId(Integer.parseInt(id));
		data = dataService.get(data);
		request.setAttribute("data", data);
		request.getRequestDispatcher(prefix + "front/detail.jsp").forward(request, response);
	}
	public void common(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//主题市场   菜单
		List<Subject> subjects = subjectService.getByParentId(2);
		for (Subject subject : subjects) {
			List<Subject> childs = subjectService.getByParentId(subject.getId());
			subject.setChilds(childs);
		}
		request.setAttribute("subjects", subjects);
		DateTime dateTime = new DateTime();  
		request.setAttribute("date", dateTime.toString("yyyy/MM/dd HH:mm:ss.SSSa"));
	}*/
}
