'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); // class OrderDetailService extends CrudService { constructor(ctx) { super(ctx, 'orderdetail'); } /** * 计算订单详情实际支付金额 * @param {Object} data 订单详情数据 * @return {Number} 实际支付金额 */ async computedRealPay(data) { const { goods = [], total_detail = {} } = data; let total = goods.reduce((p, n) => { const price = this.ctx.plus(n.sell_money, n.freight); const goodsTotal = this.ctx.multiply(price, n.buy_num); return this.ctx.plus(p, goodsTotal); }, 0); let dmt = 0; const discount_detail = _.get(total_detail, 'discount_detail', {}); for (const uc_id in discount_detail) { const detail = _.get(discount_detail, uc_id, {}); const values = Object.values(detail); const dm = values.reduce((p, n) => this.ctx.plus(p, n), 0); dmt = this.ctx.plus(dmt, dm); } total = this.ctx.minus(total, dmt); if (total <= 0) total = 0; return total; } } module.exports = OrderDetailService;