lrf 2 年之前
父節點
當前提交
a28627ceac

+ 7 - 4
app/controller/trade/config/.orderDetail.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['discount', 'total', 'order', 'shop', 'customer', 'address', 'goods', 'freight', 'buy_time', 'pay_time', 'status'],
+    requestBody: ['order_id'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['discount', 'total', 'order', 'shop', 'customer', 'address', 'goods', 'freight', 'buy_time', 'pay_time', 'status'],
+    requestBody: ['order', 'shop', 'customer', 'address', 'no', 'transport', 'goods', 'total_detail', 'buy_time', 'pay_time', 'discount', 'status'],
   },
   show: {
     parameters: {
@@ -25,8 +25,11 @@ module.exports = {
         shop: 'shop',
         customer: 'customer',
         address: 'address',
-        buy_time: 'buy_time',
-        pay_time: 'pay_time',
+        no: 'no',
+        'pay_time@start': 'pay_time@start',
+        'pay_time@end': 'pay_time@end',
+        'buy_time@start': 'buy_time@start',
+        'buy_time@end': 'buy_time@end',
         status: 'status',
       },
       // options: {

+ 7 - 7
app/model/trade/orderDetail.js

@@ -2,19 +2,19 @@
 const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 
-const MoneyPlugin = require('naf-framework-mongoose-free/lib/model/type-money-plugin');
-
 // 订单详情
 const orderDetail = {
   order: { type: String, required: false, zh: '总订单', ref: 'Trade.order' }, //
   shop: { type: String, required: false, zh: '店铺', ref: 'Shop.Shop' }, //
-  customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
-  address: { type: String, required: false, zh: '邮寄地址', ref: 'User.address' }, //
+  customer: { type: String, required: false, zh: '顾客', ref: 'User.Customer' }, //
+  address: { type: Object, required: false, zh: '邮寄地址' }, //
+  no: { type: String, required: false, zh: '订单号' }, //
+  transport: { type: Object, required: false, zh: '快递' }, //
   goods: { type: Array, required: false, zh: '商品快照清单' }, // 下单时,商品的属性设置
-  freight: { type: String, required: false, zh: '运费' }, //
+  total_detail: { type: Object, required: false, zh: '金额明细' }, //
   buy_time: { type: String, required: false, zh: '下单时间' }, //
   pay_time: { type: String, required: false, zh: '支付时间' }, //
-  discount: { type: Array, required: false, zh: '优惠' }, // 优惠情况汇总:e.g.:多个可重叠的优惠券
+  discount: { type: Array, required: false, zh: '优惠' }, //
   status: { type: String, required: false, zh: '订单状态' }, // 字典:order_process
 };
 const schema = new Schema(orderDetail, { toJSON: { getters: true, virtuals: true } });
@@ -24,12 +24,12 @@ schema.index({ order: 1 });
 schema.index({ shop: 1 });
 schema.index({ customer: 1 });
 schema.index({ address: 1 });
+schema.index({ no: 1 });
 schema.index({ buy_time: 1 });
 schema.index({ pay_time: 1 });
 schema.index({ status: 1 });
 
 schema.plugin(metaPlugin);
-schema.plugin(MoneyPlugin({ zh: '总金额', required: false, key: 'total' }));
 
 module.exports = app => {
   const { mongoose } = app;

+ 37 - 4
app/service/trade/orderDetail.js

@@ -13,16 +13,49 @@ class OrderDetailService extends CrudService {
   }
 
   /**
-   * 创建用户支付完成后的订单
+   * 创建:用户支付完成后的订单
    * @param {Object} body 请求参数体
    * @param body.order_id 下单的id
+   * @param {Transaction} tran 数据库事务实例
    */
-  async create({ order_id }) {
+  async create({ order_id }, tran) {
+    assert(order_id, '缺少支付订单信息');
+    // if (!tran) throw new BusinessError(ErrorCode.DATA_INVALID, '支付成功添加积分:缺少事务参数');
     const order = await this.orderModel.findById(order_id);
     if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单的数据');
-    const { customer, address, goods, no, status, buy_time } = order;
+    const { customer, address, goods, no, status, buy_time, pay } = order;
     if (status === '0') throw new BusinessError(ErrorCode.DATA_INVALID, '订单未支付');
-    const orderDetailData = { customer, address, order: order_id };
+    const orderDetailData = { customer, address, order: order_id, buy_time, pay_time: _.get(pay, 'pay_time', status) };
+    // 补全 shop, goods, total_detail TODO: 补全 discount;
+    // const arr = [];
+    let noTimes = 1;
+    for (const s of goods) {
+      const shop = _.get(s, 'shop');
+      const goodsList = _.get(s, 'goods', []);
+      const detailNo = `${no}-${noTimes}`;
+      const total_detail = this.getTotalDetail(goodsList);
+      noTimes++;
+      const obj = { ...orderDetailData, shop, goods: goodsList, no: detailNo, total_detail };
+      tran.insert(obj);
+      // arr.push(obj);
+    }
+    // await this.model.insertMany(arr);
+  }
+  /**
+   * 计算某店铺的金额总计
+   * TODO: 缺少优惠计算部分
+   * @param {Array} goodsList 商品规格列表
+   */
+  getTotalDetail(goodsList) {
+    const goods_total = _.floor(
+      goodsList.reduce((p, n) => p + (parseFloat(n.sell_money) || 0), 0),
+      2
+    );
+    const freight_total = _.floor(
+      goodsList.reduce((p, n) => p + (parseFloat(n.freight) || 0), 0),
+      2
+    );
+    return { goods_total, freight_total };
   }
 }
 

+ 17 - 21
app/service/trade/pay.js

@@ -14,8 +14,6 @@ class PayService extends CrudService {
     this.orderModel = this.ctx.model.Trade.Order;
     this.dictDataModel = this.ctx.model.Dev.DictData;
     this.payOrderReturnUrl = this.app.config.payReturn.order;
-    this.userModel = this.ctx.model.User.User;
-    this.pointModel = this.ctx.model.User.Point;
     this.wxDomain = _.get(this.app, 'config.httpPrefix.wechat');
     this.tran = new Transaction();
   }
@@ -108,27 +106,25 @@ class PayService extends CrudService {
     const payData = _.get(orderData, 'pay', {});
     // 支付结果全都存起来
     payData.result = result;
+    // 支付时间
+    payData.pay_time = moment().format('YYYY-MM-DD HH:mm:ss');
     // 修改状态
-    await this.orderModel.updateOne(query, { pay: payData, status: '1' });
-
-    // 10块钱给1积分,不到10块不给
-    const pay_money = _.get(orderData, 'pay.pay_money');
-    if (!_.isNumber(pay_money) || pay_money < 10) return;
-    const point = _.floor(pay_money / 10);
-    // 添加积分
-    const user = await this.userModel.findOne({ openid });
-    if (!user) {
-      console.error('未找到用户信息----添加积分');
-      return;
+    try {
+      this.tran.update('Order', orderData._id, { pay: payData, status: '1' });
+      // 加积分
+      await this.ctx.service.user.point.afterPayOrder({ order_id: orderData._id, openid }, this.tran);
+      // 拆订单
+      await this.tran.run();
+    } catch (error) {
+      console.error(error);
+      await this.tran.rollback();
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, '支付回调:修改失败');
+    } finally {
+      // 清空事务
+      this.tran.clean();
     }
-    const customer = _.get(user, '_id');
-    const pointData = {
-      customer,
-      point,
-      time: moment().format('YYYY-MM-D HH:mm:ss'),
-      source: '0',
-    };
-    await this.pointModel.create(pointData);
+
+
   }
 
   /**

+ 35 - 2
app/service/user/point.js

@@ -3,12 +3,45 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
-
-// 
+const moment = require('moment');
+//
 class PointService extends CrudService {
   constructor(ctx) {
     super(ctx, 'point');
     this.model = this.ctx.model.User.Point;
+    this.orderModel = this.ctx.model.Trade.Order;
+    this.userModel = this.ctx.model.User.User;
+  }
+  /**
+   * 添加积分;将处理添加至事务之中
+   * @param {Object} params 参数
+   * @param params.order_id 订单id
+   * @param params.openid 支付人的openid
+   * @param {Transaction} tran 数据库事务实例
+   * @return
+   */
+  async afterPayOrder({ order_id, openid }, tran) {
+    if (!tran) throw new BusinessError(ErrorCode.DATA_INVALID, '支付成功添加积分:缺少事务参数');
+    const orderData = await this.orderModel.findById(order_id);
+    if (!orderData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '支付成功添加积分:未找到支付订单信息');
+    // 10块钱给1积分,不到10块不给
+    const pay_money = _.get(orderData, 'pay.pay_money');
+    if (!_.isNumber(pay_money) || pay_money < 10) return;
+    const point = _.floor(pay_money / 10);
+    // 添加积分
+    const user = await this.userModel.findOne({ openid });
+    if (!user) {
+      console.error('未找到用户信息----添加积分');
+      return;
+    }
+    const customer = _.get(user, '_id');
+    const pointData = {
+      customer,
+      point,
+      time: moment().format('YYYY-MM-D HH:mm:ss'),
+      source: '0',
+    };
+    tran.insert(point, pointData);
   }
 }