sell.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const { trimData } = require('naf-core').Util;
  7. // 交易
  8. class SellService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'sell');
  11. this.model = this.ctx.model.Patent.Sell;
  12. this.personalModel = this.ctx.model.User.Personal;
  13. this.organizationModel = this.ctx.model.User.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 = SellService;