-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.js
87 lines (82 loc) · 2.6 KB
/
menu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//查找菜单
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('test.db');
var Promise = require('bluebird');
var async=require('async');
exports.find=(foodname)=>{
return new Promise(
(resolve,reject)=>{
db.all("select * from food where name like ? order by hot desc limit 0,10",'%'+foodname+'%',function(err,res){
if(!err)
{
resolve(res);
}
else
reject(err);
});
});
};
exports.findMoreFood=(foodlist)=>{
return new Promise(
(resolve,reject)=>{
async.map(
foodlist,
function(item,callback)
{
db.all("select * from food where id = ?",item['id'],function(err,res){
if(!err)
{
Object.assign(item,res[0]);
callback(null,item);
}
else
{
callback(err,null);
}
});
},
function(err,res)
{
if(err) reject(err);
else
{
res.sort(function(a,b){
return b.score-a.score});
resolve(res);
}
}
);
});
}
exports.findToList=(keywords)=>{
return new Promise(
(resolve,reject)=>{
var menus=[];
var maxlength=keywords.length;
var i=0;
function findMenu(foodname)
{
db.all("select * from food where name like ? order by hot desc limit 0,10",'%'+foodname+'%',function(err,res)
{
if(!err)
{
menus=menus.concat(res);
i++;
if(menus.length<10 && i<maxlength)
{
findMenu(keywords[i]);
}
else
{
resolve(menus);
}
}
else
{
reject(err);
}
});
}
findMenu(keywords[i]);
});
};