'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const { trimData } = require('naf-core').Util; const _ = require('lodash'); const assert = require('assert'); // 求购 class PurchaseService extends CrudService { constructor(ctx) { super(ctx, 'purchase'); this.model = this.ctx.model.Patent.Purchase; this.personalModel = this.ctx.model.Personal; this.organizationModel = this.ctx.model.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 = PurchaseService;