|
@@ -1,6 +1,7 @@
|
|
'use strict';
|
|
'use strict';
|
|
const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
|
+const { trimData } = require('naf-core').Util;
|
|
const _ = require('lodash');
|
|
const _ = require('lodash');
|
|
const assert = require('assert');
|
|
const assert = require('assert');
|
|
|
|
|
|
@@ -9,6 +10,36 @@ class PurchaseService extends CrudService {
|
|
constructor(ctx) {
|
|
constructor(ctx) {
|
|
super(ctx, 'purchase');
|
|
super(ctx, 'purchase');
|
|
this.model = this.ctx.model.Patent.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) {
|
|
|
|
+ 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|