|
@@ -4,6 +4,7 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
const _ = require('lodash');
|
|
|
const assert = require('assert');
|
|
|
const moment = require('moment');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
|
|
|
class OrderService extends CrudService {
|
|
|
constructor(ctx) {
|
|
@@ -11,25 +12,143 @@ class OrderService extends CrudService {
|
|
|
this.model = this.ctx.model.Dining.Order;
|
|
|
}
|
|
|
async create(data) {
|
|
|
+ const { date } = data;
|
|
|
+ if (!moment().isBefore(date)) throw new BusinessError(ErrorCode.BUSINESS, '只能预订明天及之后的用餐!');
|
|
|
const res = await this.model.create(data);
|
|
|
|
|
|
this.ctx.service.dining.menu.addOrder(data);
|
|
|
return res;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ * 根据openid查某天的订单
|
|
|
+ * @param {Object} query ctx.query
|
|
|
+ * @property {String} openid
|
|
|
+ * @property {String} date 日期
|
|
|
+ * @return {Any} Object:该用户点的单;Null:没点单
|
|
|
+ */
|
|
|
+ async getByOpenid({ openid, date }) {
|
|
|
+ const res = await this.model.findOne({ openid, date });
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ * 首页餐卡内容获取
|
|
|
+ * @param {Object} query ctx.query
|
|
|
+ * @property {String} openid 就是openid,微信的,没啥解释
|
|
|
+ * @return {Any} Object:返回哪一餐的内容/null
|
|
|
+ */
|
|
|
+ async mealCard({ openid }) {
|
|
|
+ const _tenant = this.ctx.tenant;
|
|
|
+ const site = await this.ctx.model.System.Tenant.findOne({ _tenant });
|
|
|
+ if (!site) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到分站信息!'); }
|
|
|
+ const { params } = site;
|
|
|
+ if (!params) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到分站参数设置'); }
|
|
|
+ const be = params.find(f => f.key === 'breakfast_end');
|
|
|
+ if (!be) {
|
|
|
+ throw new BusinessError(
|
|
|
+ ErrorCode.DATA_NOT_EXIST,
|
|
|
+ '未找到分站早餐结束时间设置'
|
|
|
+ );
|
|
|
+ }
|
|
|
+ const le = params.find(f => f.key === 'lunch_end');
|
|
|
+ if (!le) {
|
|
|
+ throw new BusinessError(
|
|
|
+ ErrorCode.DATA_NOT_EXIST,
|
|
|
+ '未找到分站午餐结束时间设置'
|
|
|
+ );
|
|
|
+ }
|
|
|
+ const de = params.find(f => f.key === 'dinner_end');
|
|
|
+ if (!de) {
|
|
|
+ throw new BusinessError(
|
|
|
+ ErrorCode.DATA_NOT_EXIST,
|
|
|
+ '未找到分站晚餐结束时间设置'
|
|
|
+ );
|
|
|
+ }
|
|
|
+ const date = moment().format('YYYY-MM-DD');
|
|
|
+ const arr = [
|
|
|
+ { key: 'breakfast', time: `${date} ${be.value}` },
|
|
|
+ { key: 'lunch', time: `${date} ${le.value}` },
|
|
|
+ { key: 'dinner', time: `${date} ${de.value}` },
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
+ let res;
|
|
|
+ for (const i of arr) {
|
|
|
+ const { key, time } = i;
|
|
|
+ const r = this.checkMealType(time, key);
|
|
|
+ if (r !== false) {
|
|
|
+ res = r;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const query = { openid };
|
|
|
+ if (res) {
|
|
|
+
|
|
|
+ query.date = date;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ query.date = moment().add(1, 'days').format('YYYY-MM-DD');
|
|
|
+ res = 'breakfast';
|
|
|
+ }
|
|
|
+ const order = await this.model.findOne(query);
|
|
|
+ if (!order) return null;
|
|
|
+ const meal = _.get(order, res);
|
|
|
+ return { data: meal, type: this.ctx.service.util.util.getZh(res) };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 判断是哪一餐
|
|
|
+ * @param {String} time 时间字符串格式YYYY-MM-DD HH:mm:ss
|
|
|
+ * @param {String} type 三餐类型
|
|
|
+ * @return {Any} String,确定是该类型的餐;Boolean,不是该类型的餐
|
|
|
+ */
|
|
|
+ checkMealType(time, type) {
|
|
|
+ const r = moment().isSameOrBefore(time);
|
|
|
+ if (r) return type;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* 扫码领餐
|
|
|
* @param {Object} query 参数
|
|
|
* @property id 早/中/晚餐的数据id,子文档id
|
|
|
*/
|
|
|
async useMeal({ id }) {
|
|
|
- console.log(id);
|
|
|
+ id = ObjectId(id);
|
|
|
+ console.log(this.ctx.tenant);
|
|
|
+ const query = {
|
|
|
+ $or: [
|
|
|
+ { 'breakfast._id': id },
|
|
|
+ { 'lunch._id': id },
|
|
|
+ { 'dinner._id': id },
|
|
|
+ ],
|
|
|
+ };
|
|
|
+ let order = await this.model.findOne(query);
|
|
|
+ if (order) {
|
|
|
+ order = this.checkOrder(id, order, 'breakfast');
|
|
|
+ order = this.checkOrder(id, order, 'lunch');
|
|
|
+ order = this.checkOrder(id, order, 'dinner');
|
|
|
+ return await order.save();
|
|
|
+ }
|
|
|
+ throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该餐的数据!');
|
|
|
+ }
|
|
|
+
|
|
|
+ checkOrder(id, order, type) {
|
|
|
+ if (ObjectId(id).equals(order[type]._id)) {
|
|
|
+ if (order[type].is_use === '1' || order[type].is_use === '2') throw new BusinessError(ErrorCode.DATA_INVALID, '该餐状已经使用');
|
|
|
+ else if (order[type].is_use === '3') throw new BusinessError(ErrorCode.DATA_INVALID, '该餐状已失效');
|
|
|
+ else order[type].is_use = '2';
|
|
|
+ }
|
|
|
+ return order;
|
|
|
}
|
|
|
|
|
|
|
|
|
* 检查所有的票是否过期
|
|
|
*/
|
|
|
async checkTimeOut() {
|
|
|
- const tenantList = await this.ctx.model.System.Tenant.find({ _tenant: { $ne: 'master' } });
|
|
|
+ const tenantList = await this.ctx.model.System.Tenant.find({
|
|
|
+ _tenant: { $ne: 'master' },
|
|
|
+ });
|
|
|
for (const site of tenantList) {
|
|
|
const { _tenant, params } = site;
|
|
|
const pbe = params.find(f => f.key === 'breakfast_end');
|
|
@@ -48,13 +167,15 @@ class OrderService extends CrudService {
|
|
|
const breakfast_time = `${date} ${pbe.value}`;
|
|
|
const lunch_time = `${date} ${ple.value}`;
|
|
|
const dinner_time = `${date} ${pde.value}`;
|
|
|
- if (order.breakfast.list.length > 0 && moment().isSameOrAfter(breakfast_time)) order.breakfast.is_use = '3';
|
|
|
- if (order.lunch.list.length > 0 && moment().isSameOrAfter(lunch_time)) order.lunch.is_use = '3';
|
|
|
- if (order.dinner.list.length > 0 && moment().isSameOrAfter(dinner_time)) order.dinner.is_use = '3';
|
|
|
+ if (
|
|
|
+ order.breakfast.list.length > 0 &&
|
|
|
+ moment().isSameOrAfter(breakfast_time)
|
|
|
+ ) { order.breakfast.is_use = '3'; }
|
|
|
+ if (order.lunch.list.length > 0 && moment().isSameOrAfter(lunch_time)) { order.lunch.is_use = '3'; }
|
|
|
+ if (order.dinner.list.length > 0 && moment().isSameOrAfter(dinner_time)) { order.dinner.is_use = '3'; }
|
|
|
await order.save();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
|