lrf 2 年之前
父节点
当前提交
0fffba362f
共有 2 个文件被更改,包括 21 次插入3 次删除
  1. 1 1
      app/model/zrOrder.js
  2. 20 2
      app/service/zrOrder.js

+ 1 - 1
app/model/zrOrder.js

@@ -10,7 +10,7 @@ const zrOrder = {
   buy_num: { type: Number, required: false, zh: '购买数量' }, //
   buy_time: { type: String, required: false, zh: '购买时间' }, //
   no: { type: String, required: false, zh: '订单号' }, //
-  address: { type: Object, required: false, zh: '地址', ref: 'Base.Address' }, // 快照
+  address: { type: Object, required: false, zh: '地址' }, // 快照
   transport: { type: Object, required: false, zh: '快递信息' }, //
   remarks: { type: String, required: false, zh: '备注' }, //
   status: { type: String, required: false, default: '0', zh: '订单状态' }, //

+ 20 - 2
app/service/zrOrder.js

@@ -5,6 +5,7 @@ const _ = require('lodash');
 const assert = require('assert');
 const moment = require('moment');
 const Transaction = require('mongoose-transactions');
+const { ObjectId } = require('mongoose').Types;
 //
 class ZrOrderService extends CrudService {
   constructor(ctx) {
@@ -23,6 +24,23 @@ class ZrOrderService extends CrudService {
   // 下单,走同样的函数判断.
   // 直接生成订单,扣除积分
 
+  async fetch(filter, { sort, desc, projection } = {}) {
+    assert(filter);
+    filter = await this.beforeFetch(filter);
+    const { _id, id } = filter;
+    if (_id || id) filter = { _id: ObjectId(_id || id) };
+
+    // 处理排序
+    if (sort && _.isString(sort)) {
+      sort = { [sort]: desc ? -1 : 1 };
+    } else if (sort && _.isArray(sort)) {
+      sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
+    }
+    const { populate } = this.getRefMods();
+    let res = await this.model.findOne(filter, projection).populate(populate).exec();
+    res = await this.afterFetch(filter, res);
+    return res;
+  }
   async create(data) {
     const { shop, goods, buy_num, address, remarks } = data;
     const res = await this.checkCanBuy({ shop, goods, num: buy_num }, false);
@@ -32,6 +50,7 @@ class ZrOrderService extends CrudService {
     const goodsInfo = await this.goodsModel.findById(goods);
     const buy_time = moment().format('YYYY-MM-DD HH:mm:ss');
     const no = `${moment().format('YYYYMMDDHHmmss')}-${this.createNonceStr()}`;
+    const addressInfo = await this.addressModel.findById(address);
     try {
       const orderData = {
         customer,
@@ -40,7 +59,7 @@ class ZrOrderService extends CrudService {
         buy_num,
         buy_time,
         no,
-        address,
+        address: addressInfo,
         status: '1',
         remarks,
       };
@@ -62,7 +81,6 @@ class ZrOrderService extends CrudService {
     } finally {
       this.tran.clean();
     }
-
   }
 
   async toMakeOrder({ key }) {