'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'); const moment = require('moment'); // class CashBackService extends CrudService { constructor(ctx) { super(ctx, 'cashback'); this.model = this.ctx.model.User.CashBack; this.configModel = this.ctx.model.System.Config; this.orderDetailModel = this.ctx.model.Trade.OrderDetail; this.userModel = this.ctx.model.User.User; } async computedTotal({ customer }) { assert(customer, '缺少用户信息'); // status(状态) 和 source(来源) 均表示钱是入账还是出账,判断一个就行,主要还是依靠来源吧 const res = await this.model.find({ inviter: customer }).lean(); const config = await this.configModel.findOne({}, { reward_day: 1 }).lean(); const rd = _.get(config, 'reward_day', 14); const min = this.ctx.multiply(rd, this.ctx.multiply(24, 60)); let total = 0; for (const i of res) { const { status, money } = i; const sn = parseInt(status); if (sn >= 0) total = this.ctx.plus(total, money); else total = this.ctx.minus(total, money); } // 计算提现金额 // 上面总价包含了提现金额 // 下面计算的金额是可以提现的金额 提现总金额 - 提现金额 let canGet = 0; const inBill = res.filter(f => parseInt(f.status) >= 0); const outBill = res.filter(f => parseInt(f.status) < 0); // const getOutBill = res.filter(f => f.status === '-1'); // const outBillMoney = getOutBill.reduce((p, n) => this.ctx.plus(p, n.money), 0); for (const i of inBill) { const cAt = _.get(i, 'time'); const m = moment().diff(cAt, 'm'); if (m >= min) { // 已经过了提款时间,那就再找下,是否有该记录相应的退款 const { money, source_id } = i; let cg = money; const os = outBill.filter(f => f.source_id === source_id); for (const o of os) { cg = this.ctx.minus(cg, o.money); } canGet = this.ctx.plus(canGet, cg); } } // canGet = this.ctx.minus(canGet, outBillMoney); return { total, canGet }; } /** * 添加检查返现 * @param {String} orderDetail_id 订单详情id * @param {Transaction} tran 数据库事务 */ async create(orderDetail_id, tran) { const { populate } = this.ctx.service.trade.orderDetail.getRefMods(); const orderDetail = await this.orderDetailModel.findById(orderDetail_id).populate(populate); if (!orderDetail) return; // 找到商品的返现类型 let rmoney = 0; const { goods: goodsSpec, type } = orderDetail; // 根据订单类型判断应该取哪个价钱,为下面的百分比计算取值用 let priceKey; if (type === '1') priceKey = 'ggrp'; else priceKey = 'grp'; const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail); for (const gs of goodsSpec) { const goods = _.get(gs, 'goods'); if (!goods) continue; const { is_cashBack, cb_config } = goods; // 0:返现(使用) if (is_cashBack !== '0') continue; const type = _.get(cb_config, 'back_type'); if (!type) continue; const money = _.get(cb_config, 'money'); // 返现/比例小于等于 0则不需要进行 if (this.ctx.minus(money, 0) <= 0) continue; if (type === 'fixed') rmoney = this.ctx.plus(rmoney, money); else if (type === 'percent') { const specId = _.get(gs, '_id'); const realPay = _.get(moneyDetail, `${specId}.${priceKey}`); const grm = this.ctx.multiply(realPay, this.ctx.divide(money, 100)); rmoney = this.ctx.plus(rmoney, grm); } } const { inviter, _id: order_detail } = orderDetail; if (!inviter) return; // 查询用户是否是会员,若不是会员不返现 const inviter_id = _.get(inviter, "_id"); const leaderInfo = await this.userModel.findById(inviter_id, { is_leader }); if(leaderInfo.is_leader !=='0') return; const obj = { inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0', money: rmoney, time: moment().format('YYYY-MM-DD HH:mm:ss') }; // 检查是否已经返利 const num = await this.model.count({ inviter: _.get(inviter, '_id'), source_id: order_detail, source: '0' }); if (num > 0) return; tran.insert('CashBack', obj); } /** * 退货退流水 * @param {Object} afterSale 修改前的售后数据 * @param {Transaction} tran 数据库事务 */ async refund(afterSale, tran) { const source_id = _.get(afterSale, 'order_detail'); const inBill = await this.model.findOne({ source_id }).lean(); if (!inBill) return; const outBill = _.pick(inBill, [ 'inviter', 'money', 'source', 'source_id' ]); outBill.time = moment().format('YYYY-MM-DD HH:mm:ss'); outBill.status = '-1'; tran.insert('CashBack', outBill); } } module.exports = CashBackService;