package com.lgx8.gateway.member.servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
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.gateway.json.JSONObject;
import com.lgx8.management.dao.ICustmerXFDao;
import com.lgx8.management.entities.Custmerxf;
import com.lgx8.right.entities.RunTimeUser;
/**
* Servlet implementation class QueryCustmerxfServlet
*/
public class QueryCustmerxfServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public QueryCustmerxfServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<Object,Object> pageInfo = new HashMap<Object,Object>();
RunTimeUser currentUser = RunTimeUser.getRunTimeUser(request);
if(null==currentUser) {
response.setContentType("html/txt");
response.setCharacterEncoding("utf-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache, must-revalidate");
response.setHeader("Pragma", "no-cache");
try {
response.getWriter().write("");
response.getWriter().flush();
response.getWriter().close();
} catch (IOException e) {
e.printStackTrace();
}
}
String hql = " from Custmerxf c where c.xfhy.id = " + currentUser.getId();
//得到当前页数
String page = request.getParameter("page");
//得到每页显示行数
String maxResult = request.getParameter("rp");
//获得查询的key
String query = request.getParameter("query");
//获得查询的条件
String qtype = request.getParameter("qtype");
//计算查询开始数据下标
if(query!=null && !("").equals(query))
{
String[] q1 = qtype.split(",");
String[] q2 = query.split(",");
Map<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<q2.length;i++)
{
String[] q22 = q2[i].split("#");
map.put(q22[0], q22.length > 1?q22[1]:"");
}
for(int i=0;i<q1.length;i++)
{
String q = q1[i].split(":")[0];
String d = q1[i].split(":").length>1?q1[i].split(":")[1]:"";
if("date".equals(d) && !"".equals(d))
{
String d1 = map.get(q).toString().split("&")[0];
String d2 = map.get(q).toString().split("&").length >1?map.get(q).toString().split("&")[1]:"";
hql += " and " + q + " >= str_to_date('"+d1+"','%Y-%m-%d %H:%i:%s')";
hql += "".equals(d2)?"":" and " + q + " <= str_to_date('"+d2+"','%Y-%m-%d %H:%i:%s')";
}else if(!"".equals(map.get(q)))
{
hql += " and " + q + " like '%"+map.get(q)+"%'";
}
}
}
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
ICustmerXFDao custmerXFDao = (ICustmerXFDao) ac.getBean("custmerXFDao");
PageList pageList = custmerXFDao.listCustmerXFByConditions(hql, new Object[]{}, Integer.parseInt(page), Integer.parseInt(maxResult));
//当前页数设置
pageInfo.put("page", page);
//当前页数设置
pageInfo.put("size", maxResult);
//总搜索数据量设置
pageInfo.put("total", pageList.getTotalRecordNum());
String jsonStr = creMessageJSON(pageList.getDataList(), pageInfo);
//response相关处理
response.setContentType("html/txt");
response.setCharacterEncoding("utf-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache, must-revalidate");
response.setHeader("Pragma", "no-cache");
try {
response.getWriter().write(jsonStr);
response.getWriter().flush();
response.getWriter().close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String creMessageJSON(List<Custmerxf> list,Map<Object,Object> pageInfo){
String jsonStr = "";
int num = Integer.parseInt(pageInfo.get("page").toString());
int size = Integer.parseInt(pageInfo.get("size").toString());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Map<Object,Object>> mapList = new ArrayList<Map<Object,Object>>();
for(int i=0;i<list.size();i++){
Map<Object,Object> cellMap = new HashMap<Object,Object>();
cellMap.put("id", list.get(i).getID());
cellMap.put("cell", new Object[]{
list.get(i).getID(),
((num-1)*size+(i+1)),
list.get(i).getXfhy().getCard().getId(),
list.get(i).getXfhy().getUsername(),
list.get(i).getMerchant().getSJMC(),
format.format(list.get(i).getXFSJ()),
list.get(i).getXFJE(),
list.get(i).getHDJFS()
});
mapList.add(cellMap);
}
pageInfo.put("rows", mapList);
JSONObject object = new JSONObject(pageInfo);
jsonStr = object.toString();
return jsonStr;
}
}