food.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. 'use strict';
  2. import {Food as FoodModel, Menu as MenuModel} from '../../models/shopping/food'
  3. import ShopModel from '../../models/shopping/shop'
  4. import BaseComponent from '../../prototype/baseComponent'
  5. import formidable from 'formidable'
  6. class Food extends BaseComponent{
  7. constructor(){
  8. super();
  9. this.defaultData = [{
  10. name: '热销榜',
  11. description: '大家喜欢吃,才叫真好吃。',
  12. icon_url: "5da3872d782f707b4c82ce4607c73d1ajpeg",
  13. is_selected: true,
  14. type: 1,
  15. foods: [],
  16. }, {
  17. name: '优惠',
  18. description: '美味又实惠, 大家快来抢!',
  19. icon_url: "4735c4342691749b8e1a531149a46117jpeg",
  20. type: 1,
  21. foods: [],
  22. }]
  23. this.initData = this.initData.bind(this);
  24. this.addFood = this.addFood.bind(this);
  25. this.getCategory = this.getCategory.bind(this);
  26. this.addCategory = this.addCategory.bind(this);
  27. this.getSpecfoods = this.getSpecfoods.bind(this);
  28. this.updateFood = this.updateFood.bind(this);
  29. }
  30. async initData(restaurant_id){
  31. for (let i = 0; i < this.defaultData.length; i++) {
  32. let category_id;
  33. try{
  34. category_id = await this.getId('category_id');
  35. }catch(err){
  36. console.log('获取category_id失败');
  37. throw new Error(err);
  38. }
  39. const defaultData = this.defaultData[i];
  40. const Category = {...defaultData, id: category_id, restaurant_id};
  41. const newFood = new MenuModel(Category);
  42. try{
  43. await newFood.save();
  44. console.log('初始化食品数据成功');
  45. }catch(err){
  46. console.log('初始化食品数据失败');
  47. throw new Error(err);
  48. }
  49. }
  50. }
  51. async getCategory(req, res, next){
  52. const restaurant_id = req.params.restaurant_id;
  53. try{
  54. const category_list = await MenuModel.find({restaurant_id});
  55. res.send({
  56. status: 1,
  57. category_list,
  58. })
  59. }catch(err){
  60. console.log('获取餐馆食品种类失败');
  61. res.send({
  62. status: 0,
  63. type: 'ERROR_GET_DATA',
  64. message: '获取数据失败'
  65. })
  66. }
  67. }
  68. async addCategory(req, res, next){
  69. const form = new formidable.IncomingForm();
  70. form.parse(req, async (err, fields, files) => {
  71. try{
  72. if (!fields.name) {
  73. throw new Error('必须填写食品类型名称');
  74. }else if(!fields.restaurant_id){
  75. throw new Error('餐馆ID错误');
  76. }
  77. }catch(err){
  78. console.log(err.message, err);
  79. res.send({
  80. status: 0,
  81. type: 'ERROR_PARAMS',
  82. message: err.message
  83. })
  84. return
  85. }
  86. let category_id;
  87. try{
  88. category_id = await this.getId('category_id');
  89. }catch(err){
  90. console.log('获取category_id失败');
  91. res.send({
  92. type: 'ERROR_DATA',
  93. message: '获取数据失败'
  94. })
  95. return
  96. }
  97. const foodObj = {
  98. name: fields.name,
  99. description: fields.description,
  100. restaurant_id: fields.restaurant_id,
  101. id: category_id,
  102. foods: [],
  103. }
  104. const newFood = new MenuModel(foodObj);
  105. try{
  106. await newFood.save();
  107. res.send({
  108. status: 1,
  109. success: '添加食品种类成功',
  110. })
  111. }catch(err){
  112. console.log('保存数据失败');
  113. res.send({
  114. status: 0,
  115. type: 'ERROR_IN_SAVE_DATA',
  116. message: '保存数据失败',
  117. })
  118. }
  119. })
  120. }
  121. async addFood(req, res, next){
  122. const form = new formidable.IncomingForm();
  123. form.parse(req, async (err, fields, files) => {
  124. try{
  125. if (!fields.name) {
  126. throw new Error('必须填写食品名称');
  127. }else if(!fields.image_path){
  128. throw new Error('必须上传食品图片');
  129. }else if(!fields.specs.length){
  130. throw new Error('至少填写一种规格');
  131. }else if(!fields.category_id){
  132. throw new Error('食品类型ID错误');
  133. }else if(!fields.restaurant_id){
  134. throw new Error('餐馆ID错误');
  135. }
  136. }catch(err){
  137. console.log('前台参数错误', err.message);
  138. res.send({
  139. status: 0,
  140. type: 'ERROR_PARAMS',
  141. message: err.message
  142. })
  143. return
  144. }
  145. let category;
  146. let restaurant;
  147. try{
  148. category = await MenuModel.findOne({id: fields.category_id});
  149. restaurant = await ShopModel.findOne({id: fields.restaurant_id});
  150. }catch(err){
  151. console.log('获取食品类型和餐馆信息失败');
  152. res.send({
  153. status: 0,
  154. type: 'ERROR_DATA',
  155. message: '添加食品失败'
  156. })
  157. return
  158. }
  159. let item_id;
  160. try{
  161. item_id = await this.getId('item_id');
  162. }catch(err){
  163. console.log('获取item_id失败');
  164. res.send({
  165. status: 0,
  166. type: 'ERROR_DATA',
  167. message: '添加食品失败'
  168. })
  169. return
  170. }
  171. const rating_count = Math.ceil(Math.random()*1000);
  172. const month_sales = Math.ceil(Math.random()*1000);
  173. const tips = rating_count + "评价 月售" + month_sales + "份";
  174. const newFood = {
  175. name: fields.name,
  176. description: fields.description,
  177. image_path: fields.image_path,
  178. activity: null,
  179. attributes: [],
  180. restaurant_id: fields.restaurant_id,
  181. category_id: fields.category_id,
  182. satisfy_rate: Math.ceil(Math.random()*100),
  183. satisfy_count: Math.ceil(Math.random()*1000),
  184. item_id,
  185. rating: (4 + Math.random()).toFixed(1),
  186. rating_count,
  187. month_sales,
  188. tips,
  189. specfoods: [],
  190. specifications: [],
  191. }
  192. if (fields.activity) {
  193. newFood.activity = {
  194. image_text_color: 'f1884f',
  195. icon_color: 'f07373',
  196. image_text: fields.activity,
  197. }
  198. }
  199. if (fields.attributes.length) {
  200. fields.attributes.forEach(item => {
  201. let attr;
  202. switch(item){
  203. case '新':
  204. attr = {
  205. icon_color: '5ec452',
  206. icon_name: '新'
  207. }
  208. break;
  209. case '招牌':
  210. attr = {
  211. icon_color: 'f07373',
  212. icon_name: '招牌'
  213. }
  214. break;
  215. }
  216. newFood.attributes.push(attr);
  217. })
  218. }
  219. try{
  220. const [specfoods, specifications] = await this.getSpecfoods(fields, item_id);
  221. newFood.specfoods = specfoods;
  222. newFood.specifications = specifications;
  223. }catch(err){
  224. console.log('添加specs失败', err);
  225. res.send({
  226. status: 0,
  227. type: 'ERROR_DATA',
  228. message: '添加食品失败'
  229. })
  230. return
  231. }
  232. try{
  233. const foodEntity = await FoodModel.create(newFood);
  234. category.foods.push(foodEntity);
  235. category.markModified('foods');
  236. await category.save();
  237. res.send({
  238. status: 1,
  239. success: '添加食品成功',
  240. });
  241. }catch(err){
  242. console.log('保存食品到数据库失败', err);
  243. res.send({
  244. status: 0,
  245. type: 'ERROR_DATA',
  246. message: '添加食品失败'
  247. })
  248. }
  249. })
  250. }
  251. async getSpecfoods(fields, item_id){
  252. let specfoods = [], specifications = [];
  253. if (fields.specs.length < 2) {
  254. let food_id, sku_id;
  255. try{
  256. sku_id = await this.getId('sku_id');
  257. food_id = await this.getId('food_id');
  258. }catch(err){
  259. throw new Error('获取sku_id、food_id失败')
  260. }
  261. specfoods.push({
  262. packing_fee: fields.specs[0].packing_fee,
  263. price: fields.specs[0].price,
  264. specs: [],
  265. specs_name: fields.specs[0].specs,
  266. name: fields.name,
  267. item_id,
  268. sku_id,
  269. food_id,
  270. restaurant_id: fields.restaurant_id,
  271. recent_rating: (Math.random()*5).toFixed(1),
  272. recent_popularity: Math.ceil(Math.random()*1000),
  273. })
  274. }else{
  275. specifications.push({
  276. values: [],
  277. name: "规格"
  278. })
  279. for (let i = 0; i < fields.specs.length; i++) {
  280. let food_id, sku_id;
  281. try{
  282. sku_id = await this.getId('sku_id');
  283. food_id = await this.getId('food_id');
  284. }catch(err){
  285. throw new Error('获取sku_id、food_id失败')
  286. }
  287. specfoods.push({
  288. packing_fee: fields.specs[i].packing_fee,
  289. price: fields.specs[i].price,
  290. specs: [{
  291. name: "规格",
  292. value: fields.specs[i].specs
  293. }],
  294. specs_name: fields.specs[i].specs,
  295. name: fields.name,
  296. item_id,
  297. sku_id,
  298. food_id,
  299. restaurant_id: fields.restaurant_id,
  300. recent_rating: (Math.random()*5).toFixed(1),
  301. recent_popularity: Math.ceil(Math.random()*1000),
  302. })
  303. specifications[0].values.push(fields.specs[i].specs);
  304. }
  305. }
  306. return [specfoods, specifications]
  307. }
  308. async getMenu(req, res, next){
  309. const restaurant_id = req.query.restaurant_id;
  310. const allMenu = req.query.allMenu;
  311. if (!restaurant_id || !Number(restaurant_id)) {
  312. console.log('获取餐馆参数ID错误');
  313. res.send({
  314. status: 0,
  315. type: 'ERROR_PARAMS',
  316. message: '餐馆ID参数错误',
  317. })
  318. return
  319. }
  320. let filter;
  321. if (allMenu) {
  322. filter = {restaurant_id}
  323. }else{
  324. filter = {restaurant_id, $where: function(){return this.foods.length}};
  325. }
  326. try{
  327. const menu = await MenuModel.find(filter, '-_id');
  328. res.send(menu);
  329. }catch(err){
  330. console.log('获取食品数据失败', err);
  331. res.send({
  332. status: 0,
  333. type: 'GET_DATA_ERROR',
  334. message: '获取食品数据失败'
  335. })
  336. }
  337. }
  338. async getMenuDetail(req, res, next){
  339. const category_id = req.params.category_id;
  340. if (!category_id || !Number(category_id)) {
  341. console.log('获取Menu详情参数ID错误');
  342. res.send({
  343. status: 0,
  344. type: 'ERROR_PARAMS',
  345. message: 'Menu ID参数错误',
  346. })
  347. return
  348. }
  349. try{
  350. const menu = await MenuModel.findOne({id: category_id}, '-_id');
  351. res.send(menu)
  352. }catch(err){
  353. console.log('获取Menu详情失败', err);
  354. res.send({
  355. status: 0,
  356. type: 'GET_DATA_ERROR',
  357. message: '获取Menu详情失败'
  358. })
  359. }
  360. }
  361. async getFoods(req, res, next){
  362. const {restaurant_id, limit = 20, offset = 0} = req.query;
  363. try{
  364. let filter = {};
  365. if (restaurant_id && Number(restaurant_id)) {
  366. filter = {restaurant_id}
  367. }
  368. const foods = await FoodModel.find(filter, '-_id').sort({item_id: -1}).limit(Number(limit)).skip(Number(offset));
  369. res.send(foods);
  370. }catch(err){
  371. console.log('获取食品数据失败', err);
  372. res.send({
  373. status: 0,
  374. type: 'GET_DATA_ERROR',
  375. message: '获取食品数据失败'
  376. })
  377. }
  378. }
  379. async getFoodsCount(req, res, next){
  380. const restaurant_id = req.query.restaurant_id;
  381. try{
  382. let filter = {};
  383. if (restaurant_id && Number(restaurant_id)) {
  384. filter = {restaurant_id}
  385. }
  386. const count = await FoodModel.find(filter).count();
  387. res.send({
  388. status: 1,
  389. count,
  390. })
  391. }catch(err){
  392. console.log('获取食品数量失败', err);
  393. res.send({
  394. status: 0,
  395. type: 'ERROR_TO_GET_COUNT',
  396. message: '获取食品数量失败'
  397. })
  398. }
  399. }
  400. async updateFood(req, res, next){
  401. const form = new formidable.IncomingForm();
  402. form.parse(req, async (err, fields, files) => {
  403. if (err) {
  404. console.log('获取食品信息form出错', err);
  405. res.send({
  406. status: 0,
  407. type: 'ERROR_FORM',
  408. message: '表单信息错误',
  409. })
  410. return
  411. }
  412. const {name, item_id, description = "", image_path, category_id, new_category_id} = fields;
  413. try{
  414. if (!name) {
  415. throw new Error('食品名称错误');
  416. }else if(!item_id || !Number(item_id)){
  417. throw new Error('食品ID错误');
  418. }else if(!category_id || !Number(category_id)){
  419. throw new Error('食品分类ID错误');
  420. }else if(!image_path){
  421. throw new Error('食品图片地址错误');
  422. }
  423. const [specfoods, specifications] = await this.getSpecfoods(fields, item_id);
  424. let newData;
  425. if (new_category_id !== category_id) {
  426. newData = {name, description, image_path, category_id: new_category_id, specfoods, specifications};
  427. const food = await FoodModel.findOneAndUpdate({item_id}, {$set: newData});
  428. const menu = await MenuModel.findOne({id: category_id})
  429. const targetmenu = await MenuModel.findOne({id: new_category_id})
  430. let subFood = menu.foods.id(food._id);
  431. subFood.set(newData)
  432. targetmenu.foods.push(subFood)
  433. targetmenu.markModified('foods');
  434. await targetmenu.save()
  435. await subFood.remove()
  436. await menu.save()
  437. }else{
  438. newData = {name, description, image_path, specfoods, specifications};
  439. const food = await FoodModel.findOneAndUpdate({item_id}, {$set: newData});
  440. const menu = await MenuModel.findOne({id: category_id})
  441. let subFood = menu.foods.id(food._id);
  442. subFood.set(newData)
  443. await menu.save()
  444. }
  445. res.send({
  446. status: 1,
  447. success: '修改食品信息成功',
  448. })
  449. }catch(err){
  450. console.log(err.message, err);
  451. res.send({
  452. status: 0,
  453. type: 'ERROR_UPDATE_FOOD',
  454. message: '更新食品信息失败',
  455. })
  456. }
  457. })
  458. }
  459. async deleteFood(req, res, next){
  460. const food_id = req.params.food_id;
  461. if (!food_id || !Number(food_id)) {
  462. console.log('food_id参数错误');
  463. res.send({
  464. status: 0,
  465. type: 'ERROR_PARAMS',
  466. message: 'food_id参数错误',
  467. })
  468. return
  469. }
  470. try{
  471. const food = await FoodModel.findOne({item_id: food_id});
  472. const menu = await MenuModel.findOne({id: food.category_id})
  473. let subFood = menu.foods.id(food._id);
  474. await subFood.remove()
  475. await menu.save()
  476. await food.remove()
  477. res.send({
  478. status: 1,
  479. success: '删除食品成功',
  480. })
  481. }catch(err){
  482. console.log('删除食品失败', err);
  483. res.send({
  484. status: 0,
  485. type: 'DELETE_FOOD_FAILED',
  486. message: '删除食品失败',
  487. })
  488. }
  489. }
  490. }
  491. export default new Food()