lrf402788946 3 years ago
parent
commit
3c447709df

+ 3 - 2
app/controller/patent/.purchase.js

@@ -9,7 +9,7 @@ module.exports = {
       "phone",
       "email",
       "user_id",
-      "status"
+      "status",
     ],
   },
   destroy: {
@@ -27,7 +27,7 @@ module.exports = {
       "phone",
       "email",
       "user_id",
-      "status"
+      "status",
     ],
   },
   show: {
@@ -39,6 +39,7 @@ module.exports = {
   index: {
     parameters: {
       query: {
+        code: "code",
         user_id: "user_id",
         status: "status",
         type: "type",

+ 1 - 0
app/controller/patent/.sell.js

@@ -43,6 +43,7 @@ module.exports = {
   index: {
     parameters: {
       query: {
+        code: "code",
         user_id: "user_id",
         status: "status",
         create_number: "create_number",

+ 31 - 0
app/service/patent/purchase.js

@@ -1,6 +1,7 @@
 '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');
 
@@ -9,6 +10,36 @@ 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) {
+    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;
   }
 }
 

+ 30 - 0
app/service/patent/sell.js

@@ -3,12 +3,42 @@ const { CrudService } = require('naf-framework-mongoose/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.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;
   }
 }