订单消息监听模块图:
饭店管理端订单消息监听模块前端ajax代码:
setInterval(reloadNews, 5000);
function reloadNews(){
$.ajax({
url: "reloadnews",
type: "get",
success: function(data){
data = data.split("-----");
var orders = JSON.parse(data[0]);
var notes = JSON.parse(data[1]);
var ots = '<tr><th style="width: 60px;">订单编号:</th>'
+ '<th style="width: 60px;">桌号:</th>'
+ '<th style="width: 60px;">订单总价:</th>'
+ '<th style="width: 100px;">操作:</th></tr>';
$.each(orders, function(i, order){
ots += '<tr><td style="width: 60px;">'+order.oid+'</td>'
+'<td style="width: 60px;">'+order.tid+'</td>'
+'<td style="width: 60px;" class="text-danger">¥'+order.tprice+'</td>'
+'<td style="width: 100px;">'
+'<a class="btn btn-mini btn-danger" href="orderinfo?oid='+order.oid+'&tid='+order.tid+'">查看</a>'
+'<button class="btn btn-mini btn-danger o'+order.oid+'" type="button" οnclick="payConfirm('+order.oid+')">确认付款</button></td></tr>';
});
var nts = '<tr><th style="width: 100px;">消息编号:</th>'
+'<th style="width: 400px;">消息内容:</th>'
+'<th style="width: 100px;">操作:</th></tr>';
$.each(notes, function(i, note){
nts += '<tr><td style="width: 100px;">'+note.nid+'</td>'
+'<td style="width: 500px;">'+note.info+'</td>'
+'<td style="width: 100px;">'
+'<button class="btn btn-mini btn-danger n'+note.nid+'" type="button" οnclick="noteConfirm('+note.nid+')">确认处理</button></td></tr>';
});
$(".orders").html(ots);
$(".notes").html(nts);
}
});
}
饭店管理端订单消息监听模块后端代码:
servlet:
public class ReloadNewsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private IOrderService orderservice = new IOrderServiceImpl();
private INotesService notesservice = new INotesServiceImpl();
//后端获取消息页面需要的消息信息
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
List<Order> orders = orderservice.getNotPayOrder();
List<Notes> notes = notesservice.getNotes();
JSONArray ojsonArray = JSONArray.fromObject(orders);
JSONArray njsonArray = JSONArray.fromObject(notes);
String data = ojsonArray.toString()+"-----"+njsonArray.toString();
resp.getWriter().println(data);
}
}
service:
//把dao的到的数据库信息转化为JavaBean
public List<Order> getNotPayOrder() {
List<Map<String, Object>> os = orderdao.getNotPayOrder();
List<Order> orders = new ArrayList<Order>();
if(os.isEmpty()) return orders;
for(Map<String, Object> o : os) {
int oid = (int)o.get("oid");
int uid = (int)o.get("uid");
int tid = (int)o.get("tid");
int tprice = (int)o.get("totalprice");
Date orderdate = new Date(((java.sql.Date)o.get("orderdate")).getTime());
int orderstatus = (int)o.get("orderstatus");
Order order = new Order(oid,uid,tid,tprice,orderdate,orderstatus);
orders.add(order);
}
return orders;
}
//把dao的到的数据库信息转化为JavaBean
public List<Notes> getNotes() {
List<Map<String, Object>> ns = notesdao.getNotes();
List<Notes> notes = new ArrayList<Notes>();
for(Map<String, Object> n:ns) {
int nid = (int)n.get("nid");
String noteinfo = (String)n.get("noteinfo");
notes.add(new Notes(nid, noteinfo));
}
return notes;
}
dao:
//从数据库获取尚未支付的订单
public List<Map<String, Object>> getNotPayOrder() {
String sql = "select oid,uid,tid,totalprice,orderdate,orderstatus from orders where orderstatus=0";
List<Map<String, Object>> orders = DBHelper.doQuery(sql);
return orders;
}
//从数据库获取未处理的消息
public List<Map<String, Object>> getNotes() {
String sql = "select nid,noteinfo from notes where notestatus=0";
List<Map<String, Object>> notes = DBHelper.doQuery(sql);
return notes;
}