12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- '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 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-DD HH:mm:ss'),
- source: '0',
- };
- console.log(pointData);
- tran.insert('Point', pointData);
- }
- }
- module.exports = PointService;
|