123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const { ObjectId } = require('mongoose').Types;
- // 超市
- class TicketService extends CrudService {
- constructor(ctx) {
- super(ctx, 'ticket');
- this.model = this.ctx.model.Ticket;
- this.record = this.ctx.model.TicketRecord;
- }
- async query({ skip = 0, limit = 0, ...query } = {}) {
- const nquery = _.omit(query, [ 'name' ]);
- if (nquery.user_id) nquery.user_id = ObjectId(nquery.user_id);
- if (nquery.mechanism_id) nquery.mechanism_id = ObjectId(nquery.mechanism_id);
- const arr = [
- { $match: nquery },
- {
- $lookup: {
- from: 'organization',
- localField: 'user_id',
- foreignField: '_id',
- as: 'company',
- },
- },
- { $unwind: '$company' },
- { $addFields: { name: '$company.name' } },
- { $project: { _id: 1, name: 1, meta: 1, status: 1 } },
- ];
- if (_.get(query, 'name')) {
- arr.push({ $match: { name: new RegExp(_.get(query, 'name')) } });
- }
- let res = await this.model.aggregate(arr);
- const total = res.length;
- if (limit && limit !== '0') {
- res = _.slice(res, parseInt(skip), parseInt(limit));
- }
- return { data: res, total };
- }
- async fetch({ id }) {
- let res = await this.model.findById(id);
- if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定数据');
- res = JSON.parse(JSON.stringify(res));
- const { user_id } = res;
- if (user_id) {
- const company = await this.ctx.model.Organization.findById(user_id);
- if (company) res.name = company.name;
- }
- return res;
- }
- async status({ id }, body) {
- const { status } = body;
- await this.model.updateOne({ _id: id }, { status });
- // TODO,添加记录
- body.ticket_id = id;
- await this.record.create(body);
- }
- }
- module.exports = TicketService;
|