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.entities.Orders;
import com.lgx8.gateway.json.JSONObject;
import com.lgx8.management.dao.IAccountDetailDao;
import com.lgx8.management.dao.ICustmerXFDao;
import com.lgx8.management.entities.AccountDetail;
import com.lgx8.management.entities.Custmerxf;
import com.lgx8.right.entities.RunTimeUser;
/**
* Servlet implementation class QueryAccountDetailServlet
*/
public class QueryAccountDetailServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public QueryAccountDetailServlet() {
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 sql1 = " select id 'id',confirmtime 'time', score 'score',status 'status','order' as 'type','' as 'description' from orders where userid="+currentUser.getId()
+" and status in("+Orders.STATUS_PAID+","+Orders.STATUS_CONFIRM+","+Orders.STATUS_SENDED+","+ Orders.STATUS_RETURN_OK+")";
String sql2 = " select ID,XFSJ,HDJFS,0,'custmerxf','' from custmerxf where HYBH="+currentUser.getId();
String sql3 = "select id,recordDate,score,0,'note',note from memberrecord where recorderid = " + currentUser.getId() ;
String sql = sql1+" union "+sql2 + " union " + sql3;
String condition = "";
//得到当前页数
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("".equals(condition)) condition += " where ";
else condition += " and ";
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]:"";
condition += q + " >= str_to_date('"+d1+"','%Y-%m-%d %H:%i:%s')";
condition += "".equals(d2)?"":" and " + q + " <= str_to_date('"+d2+"','%Y-%m-%d %H:%i:%s')";
}else if(!"".equals(map.get(q)))
{
condition += q + " like '%"+map.get(q)+"%'";
}
}
}
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IAccountDetailDao accountDetailDao = (IAccountDetailDao) ac.getBean("accountDetailDao");
PageList pageList = accountDetailDao.queryAccountDetails(sql,condition, 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<AccountDetail> 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>();
String description = "";
if("custmerxf".equals(list.get(i).getType()))
{
description = "商家消费,获取积分";
}else if("note".equals(list.get(i).getType()))
{
description = list.get(i).getDescription();
}else
{
if(list.get(i).getStatus()==Orders.STATUS_RETURN_OK)
{
description = "商城消费退单,相关订单:"+list.get(i).getId();
}else{
description = "商城消费,相关订单:"+list.get(i).getId();
}
}
cellMap.put("id", list.get(i).getId());
cellMap.put("cell", new Object[]{
list.get(i).getId(),
((num-1)*size+(i+1)),
format.format(list.get(i).getTime()),
list.get(i).getScore()==null?"0.00":list.get(i).getScore(),
description
});
mapList.add(cellMap);
}
pageInfo.put("rows", mapList);
JSONObject object = new JSONObject(pageInfo);
jsonStr = object.toString();
return jsonStr;
}
}