ticket.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. // 超市
  7. class TicketService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'ticket');
  10. this.model = this.ctx.model.Ticket;
  11. this.record = this.ctx.model.TicketRecord;
  12. }
  13. async query({ skip = 0, limit = 0, ...query } = {}) {
  14. const nquery = _.omit(query, [ 'name' ]);
  15. const arr = [
  16. { $match: nquery },
  17. {
  18. $lookup: {
  19. from: 'organization',
  20. localField: 'user_id',
  21. foreignField: '_id',
  22. as: 'company',
  23. },
  24. },
  25. { $unwind: '$company' },
  26. { $addFields: { name: '$company.name' } },
  27. { $project: { _id: 1, name: 1, meta: 1, status: 1 } },
  28. ];
  29. if (_.get(query, 'name')) {
  30. arr.push({ $match: { name: new RegExp(_.get(query, 'name')) } });
  31. }
  32. let res = await this.model.aggregate(arr);
  33. const total = res.length;
  34. if (limit && limit !== '0') {
  35. res = _.slice(res, parseInt(skip), parseInt(limit));
  36. }
  37. return { data: res, total };
  38. }
  39. async fetch({ id }) {
  40. let res = await this.model.findById(id);
  41. if (!res) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定数据');
  42. res = JSON.parse(JSON.stringify(res));
  43. const { user_id } = res;
  44. if (user_id) {
  45. const company = await this.ctx.model.Organization.findById(user_id);
  46. if (company) res.name = company.name;
  47. }
  48. return res;
  49. }
  50. async status({ id }, body) {
  51. const { status } = body;
  52. await this.model.updateOne({ _id: id }, { status });
  53. // TODO,添加记录
  54. body.ticket_id = id;
  55. await this.record.create(body);
  56. }
  57. }
  58. module.exports = TicketService;