package com.lgx8.gateway.search.servlet;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.lgx8.common.PageList;
import com.lgx8.common.util.Constants;
import com.lgx8.gateway.entities.Product;
import com.lgx8.gateway.service.IGatewayService;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
* Servlet implementation class SearchResultServlet
*/
public class SearchResultServlet extends SearchBaseServlet {
private static final long serialVersionUID = 1L;
/**
* @see SearchBaseServlet#SearchBaseServlet()
*/
public SearchResultServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String categoryid = request.getParameter("categoryid");
String areacategoryid = request.getParameter("areacategoryid");
String brandid = request.getParameter("brandid");
String keyword = request.getParameter("keyword");
String pageno = request.getParameter("pageno");
//1商品,2品牌
String type = request.getParameter("type");
String ordertype = request.getParameter("ordertype");
//处理分区
if(areacategoryid==null) {
areacategoryid = "all";
}
//处理分类
if(categoryid==null) {
categoryid = "all";
}
//处理排序
String orderStr = "";
if(ordertype!=null&&!"".equals(ordertype)&&ordertype.length()==4) {
//时间升序
if("1".equals(ordertype.charAt(0)+"")&&"1".equals(ordertype.charAt(1)+"")) {
orderStr = " order by createdate asc ";
//时间降序
} else if("1".equals(ordertype.charAt(0)+"")&&"2".equals(ordertype.charAt(1)+"")) {
orderStr = " order by createdate desc ";
//销量升序
} else if("2".equals(ordertype.charAt(0)+"")&&"1".equals(ordertype.charAt(2)+"")) {
orderStr = " order by (amount-leaves) asc ";
//销量降序
} else if("2".equals(ordertype.charAt(0)+"")&&"2".equals(ordertype.charAt(2)+"")) {
orderStr = " order by (amount-leaves) desc ";
//价格升序
} else if("3".equals(ordertype.charAt(0)+"")&&"1".equals(ordertype.charAt(3)+"")) {
orderStr = " order by price asc ";
//价格降序
} else if("3".equals(ordertype.charAt(0)+"")&&"2".equals(ordertype.charAt(3)+"")) {
orderStr = " order by price desc ";
} else {
orderStr = "";
}
} else {
ordertype = null;
orderStr = " order by createdate desc ";
}
Map<Object, Object> root = new HashMap<Object, Object>();
root.putAll(base);
root.put("categoryid", categoryid);
root.put("areacategoryid", areacategoryid);
root.put("brandid", brandid);
root.put("keyword", keyword);
root.put("type", type);
root.put("ordertype", ordertype);
//处理关键字
String[] keys = null;
if(keyword!=null) {
keyword = keyword.trim();
}
if(keyword==null||"".equals(keyword)) {
keys = null;
} else {
if(keyword.indexOf(" ")>0) {
String[] keywords = keyword.split(" ");
keys = new String[keywords.length+1];
keys[0] = keyword;
for(int i=1;i<keys.length;i++) {
keys[i] = keywords[i-1];
}
} else {
keys = new String[]{keyword };
}
}
//处理当前页
if(pageno==null) {
pageno = "1";
}
int pagenum = 1;
try {
pagenum = Integer.parseInt(pageno);
} catch (Exception e1) {
pagenum = 1;
}
//处理商品、品牌类型
if(type!=null&&"2".equals(type)) {
type = "2";
} else {
type = "1";
}
long lbrandid = -1;
try {
lbrandid = Long.parseLong(brandid);
} catch (Exception e) {
lbrandid = -1;
}
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IGatewayService gatewayService = (IGatewayService) ac.getBean("gatewayService");
PageList page = null;
if("all".equals(areacategoryid)&&"all".equals(categoryid)&&"all".equals(brandid)) {
} else if("all".equals(areacategoryid)&&"all".equals(categoryid)) {
//查商品
if("1".equals(type)) {
page = gatewayService.findProduct(keys, pagenum, Constants.SEARCH_RESULT_PAGE_SIZE, orderStr);
//查品牌
} else {
if(brandid!=null&&!"".equals(brandid)&&lbrandid!=-1) {
page = gatewayService.findProductByBrand(lbrandid, pagenum, Constants.SEARCH_RESULT_PAGE_SIZE, orderStr);
} else {
page = gatewayService.findProductByBrand(keys, pagenum, Constants.SEARCH_RESULT_PAGE_SIZE, orderStr);
}
}
//查询指定商品分类下面所有分区商品
} else if("all".equals(areacategoryid)&&categoryid!=null) {
page = gatewayService.findProduct(new Long(categoryid), keys, pagenum, Constants.SEARCH_RESULT_PAGE_SIZE, orderStr);
//查询指定分区下面所有商品分类
} else if("all".equals(categoryid)&&areacategoryid!=null) {
page = gatewayService.findProductAllCategory(new Long(areacategoryid), keys, pagenum, Constants.SEARCH_RESULT_PAGE_SIZE, orderStr);
} else if(categoryid!=null&&areacategoryid!=null) {
page = gatewayService.findProduct(new Long(areacategoryid), new Long(categoryid), keys, pagenum, Constants.SEARCH_RESULT_PAGE_SIZE, orderStr);
}
Template temp = null;
//TODO 假数据
List<Product> lastWeekHotProduct = gatewayService.findLastWeekHotProduct(Constants.LAST_WEEK_HOT_PRODUCT_SIZE);
if(lastWeekHotProduct!=null) {
root.put("lastWeekHotProduct", lastWeekHotProduct);
}
//TODO 猜你喜欢的商品,先做假数据
List<Product> yourLoveProducts = lastWeekHotProduct;
if(yourLoveProducts!=null) {
root.put("yourLoveProducts", yourLoveProducts);
}
//查询有结果
if(page!=null&&page.getDataList()!=null&&page.getDataList().size()>0) {
root.put("page", page);
temp = getConfiguration().getTemplate("search/result.ftl");
//查询无相关记录
} else {
//TODO 收藏的商品,先做假数据
List<Product> yourCollectProducts = lastWeekHotProduct;
if(yourLoveProducts!=null) {
root.put("yourCollectProducts", yourCollectProducts);
}
temp = getConfiguration().getTemplate("search/noresult.ftl");
}
/* 将模板和数据模型合并 */
Writer out = response.getWriter();
try {
temp.process(root, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.flush();
}
}