Sfoglia il codice sorgente

计算当日菜品总量.通过openid绑定用户与管理员

lrf 1 anno fa
parent
commit
627bb094e2

+ 7 - 0
src/controller/dining/order.controller.ts

@@ -10,9 +10,16 @@ export class OrderController extends BaseController {
   @Inject()
   service: OrderService;
 
+  @Get('/computedMeal/:date')
+  async computedMeal(@Param('date') date:string){
+    const result = await this.service.computedMeal(date)
+    return result;
+  }
+
   @Get('/useMeal/:id')
   async useMeal(@Param('id') id: string) {
     await this.service.useMeal(id)
+    return 'ok';
   }
 
   @Post('/mealCard/:openid')

+ 2 - 0
src/entity/system/admin.entity.ts

@@ -27,6 +27,8 @@ export class Admin extends BaseModel {
   is_super: string;
   @prop({ required: false, index: false, zh: '角色' })
   role: string;
+  @prop({ required: false, index: false, zh: 'openid' })
+  openid: string;
   @prop({ required: false, index: false, zh: '是否启用', default: '0' })
   is_use: string;
 }

+ 1 - 1
src/interface/dining/order.interface.ts

@@ -31,7 +31,7 @@ export class FVO_order {
 export class QDTO_order extends SearchBase {
   constructor() {
     const like_prop = [];
-    const props = ['date@start','date@end'];
+    const props = ['date@start','date@end', 'date'];
     const mapping = [];
     super({ like_prop, props, mapping });
   }

+ 5 - 0
src/interface/system/admin.interface.ts

@@ -24,6 +24,8 @@ export class FVO_admin {
   'is_super': string = undefined;
   @ApiProperty({ description: '角色' })
   'role': string = undefined;
+  @ApiProperty({ description: 'openid' })
+  'openid': string = undefined;
   @ApiProperty({ description: '是否启用' })
   'is_use': string = undefined;
 }
@@ -62,6 +64,9 @@ export class CDTO_admin {
   @ApiProperty({ description: '角色' })
   @Rule(RuleType['string']().empty(''))
   'role': string = undefined;
+  @ApiProperty({ description: 'openid' })
+  @Rule(RuleType['string']().empty(''))
+  'openid': string = undefined;
   @ApiProperty({ description: '是否启用' })
   @Rule(RuleType['string']().empty(''))
   'is_use': string = undefined;

+ 67 - 1
src/service/dining/order.service.ts

@@ -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 = {

+ 5 - 0
src/service/weixin.service.ts

@@ -5,6 +5,7 @@ import { FrameworkErrorEnum, ServiceError } from 'free-midway-component';
 import { Context } from '@midwayjs/koa';
 import * as CryptoJs from 'crypto-js';
 import { UserService } from './system/user.service';
+import { AdminService } from './system/admin.service';
 
 @Provide()
 export class WeixinService {
@@ -14,6 +15,8 @@ export class WeixinService {
   ctx: Context;
   @Inject()
   userService: UserService;
+  @Inject()
+  adminService: AdminService;
 
   async appAuth() {
     const query = this.ctx.query;
@@ -30,6 +33,8 @@ export class WeixinService {
     if (!openid) throw new ServiceError('未获取到openid', FrameworkErrorEnum.REQUEST_FAULT);
     let user = await this.userService.findOne({ openid });
     if (!user) user = await this.userService.create({ openid });
+    const admin = await this.adminService.findOne({ openid });
+    if (admin) Object.assign({ is_admin: true }, user);
     return user;
   }