12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const { trimData } = require('naf-core').Util;
- // 交易
- class SellService extends CrudService {
- constructor(ctx) {
- super(ctx, 'sell');
- this.model = this.ctx.model.Patent.Sell;
- this.personalModel = this.ctx.model.User.Personal;
- this.organizationModel = this.ctx.model.User.Organization;
- }
- async query(query, { skip = 0, limit = 0 }) {
- query = await this.resetCode(query);
- const res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
- .sort({ 'meta.createdAt': -1 });
- return res;
- }
- async count(query) {
- query = await this.resetCode(query);
- const res = await this.model.countDocuments(trimData(query)).exec();
- return res;
- }
- async resetCode(query) {
- query = this.ctx.service.util.util.dealQuery(query);
- const { code } = query;
- let ids = [];
- if (code) {
- const plist = await this.personalModel.find({ code });
- ids = plist.map(i => i._id);
- const olist = await this.organizationModel.find({ code });
- ids = [ ...ids, ...olist.map(i => i._id) ];
- }
- if (ids.length > 0) {
- query.user_id = { $in: ids };
- delete query.code;
- }
- return query;
- }
- }
- module.exports = SellService;
|