purchase.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { trimData } = require('naf-core').Util;
  5. const _ = require('lodash');
  6. const assert = require('assert');
  7. // 求购
  8. class PurchaseService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'purchase');
  11. this.model = this.ctx.model.Patent.Purchase;
  12. this.personalModel = this.ctx.model.Personal;
  13. this.organizationModel = this.ctx.model.Organization;
  14. }
  15. async query(query, { skip = 0, limit = 0 }) {
  16. query = await this.resetCode(query);
  17. const res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
  18. .sort({ 'meta.createdAt': -1 });
  19. return res;
  20. }
  21. async count(query) {
  22. query = await this.resetCode(query);
  23. const res = await this.model.countDocuments(trimData(query)).exec();
  24. return res;
  25. }
  26. async resetCode(query) {
  27. query = this.ctx.service.util.util.dealQuery(query);
  28. const { code } = query;
  29. let ids = [];
  30. if (code) {
  31. const plist = await this.personalModel.find({ code });
  32. ids = plist.map(i => i._id);
  33. const olist = await this.organizationModel.find({ code });
  34. ids = [ ...ids, ...olist.map(i => i._id) ];
  35. }
  36. if (ids.length > 0) {
  37. query.user_id = { $in: ids };
  38. delete query.code;
  39. }
  40. return query;
  41. }
  42. }
  43. module.exports = PurchaseService;