|
@@ -4,10 +4,10 @@ import { ReturnModelType } from '@typegoose/typegoose';
|
|
|
import { BaseService, FrameworkErrorEnum, ServiceError } from 'free-midway-component';
|
|
|
import { Order } from '../../entity/dining/order.entity';
|
|
|
import { ConfigService } from '../system/config.service';
|
|
|
-import get = require('lodash/get');
|
|
|
import * as dayjs from 'dayjs';
|
|
|
import * as isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
|
|
|
import * as isBetween from 'dayjs/plugin/isBetween';
|
|
|
+import { pick, get } from 'lodash';
|
|
|
dayjs.extend(isSameOrBefore);
|
|
|
dayjs.extend(isBetween);
|
|
|
type modelType = ReturnModelType<typeof Order>;
|
|
@@ -19,6 +19,72 @@ export class OrderService extends BaseService<modelType> {
|
|
|
@Inject()
|
|
|
configService: ConfigService;
|
|
|
|
|
|
+ async computedMeal(date: string) {
|
|
|
+ const mealList = await this.model.find({ date }).lean();
|
|
|
+ // 将早餐,午餐,晚餐组成数据格式如下:
|
|
|
+ /**
|
|
|
+ *[
|
|
|
+ * { meal: 早餐, m_id: 菜品1数量, m_id 菜品2数量, .... },
|
|
|
+ * { meal: 午餐, m_id: 菜品1数量, m_id 菜品2数量, .... },
|
|
|
+ * { meal: 晚餐, m_id: 菜品1数量, m_id 菜品2数量, .... }
|
|
|
+ * ]
|
|
|
+ * */
|
|
|
+ const allBreakFastList = [];
|
|
|
+ const allLunchList = [];
|
|
|
+ const allDinnerList = [];
|
|
|
+ const menuAllList = [];
|
|
|
+ const merge = (data, type) => {
|
|
|
+ let list;
|
|
|
+ if (type === 'breakfast') list = allBreakFastList;
|
|
|
+ else if (type === 'lunch') list = allLunchList;
|
|
|
+ else if (type === 'dinner') list = allDinnerList;
|
|
|
+ else return;
|
|
|
+ for (const item of data) {
|
|
|
+ const m = pick(item, ['_id', 'name', 'num']);
|
|
|
+ if (!('_id' in m)) return;
|
|
|
+ // 菜品数量按早午晚合并
|
|
|
+ const am = list.find(f => f._id === m._id);
|
|
|
+ if (!am) list.push(m);
|
|
|
+ else {
|
|
|
+ am.num = am.num + m.num;
|
|
|
+ }
|
|
|
+ // 菜品合并
|
|
|
+ const i = menuAllList.findIndex(f => f._id === m._id);
|
|
|
+ if (i <= -1) menuAllList.push(pick(item, ['_id', 'name']));
|
|
|
+ }
|
|
|
+ };
|
|
|
+ for (const dm of mealList) {
|
|
|
+ merge(get(dm, 'breakfast.list', []), 'breakfast');
|
|
|
+ merge(get(dm, 'lunch.list', []), 'lunch');
|
|
|
+ merge(get(dm, 'dinner.list', []), 'dinner');
|
|
|
+ }
|
|
|
+ const assort = (list, type) => {
|
|
|
+ const obj: any = {};
|
|
|
+ let key = type.split('')[0];
|
|
|
+ if (type === 'breakfast') {
|
|
|
+ key = 'b';
|
|
|
+ obj.meal = '早餐';
|
|
|
+ } else if (type === 'lunch') {
|
|
|
+ key = 'l';
|
|
|
+ obj.meal = '午餐';
|
|
|
+ } else if (type === 'dinner') {
|
|
|
+ key = 'd';
|
|
|
+ obj.meal = '晚餐';
|
|
|
+ }
|
|
|
+ for (const i of list) {
|
|
|
+ const { _id, num } = i;
|
|
|
+ obj[`${key}_${_id}`] = num;
|
|
|
+ obj.key = key;
|
|
|
+ }
|
|
|
+ return obj;
|
|
|
+ };
|
|
|
+ const bobj = assort(allBreakFastList, 'breakfast');
|
|
|
+ const lobj = assort(allLunchList, 'lunch');
|
|
|
+ const dobj = assort(allDinnerList, 'dinner');
|
|
|
+ const data = [bobj, lobj, dobj];
|
|
|
+ return { menuAllList, data };
|
|
|
+ }
|
|
|
+
|
|
|
// 根据餐id 获取某餐
|
|
|
async getMeal(id: string) {
|
|
|
const query = {
|