12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- '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 ShopCashOutService extends CrudService {
- constructor(ctx) {
- super(ctx, 'shopcashout');
- this.model = this.ctx.model.Shop.ShopCashOut;
- this.shopInBillModel = this.ctx.model.Shop.ShopInBill;
- }
- async beforeCreate(data) {
- const { shop } = data;
- if (!shop) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '缺少提款人');
- const num = await this.model.count({ shop, status: '0' });
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '您有未处理提现申请,请联系管理员先处理之前的申请');
- return data;
- }
- async beforeUpdate(filter, update) {
- if (_.get(update, 'status') !== '0') {
- update.deal_person = _.get(this.ctx, 'admin._id');
- }
- return { filter, update };
- }
- async afterUpdate(filter, body, data) {
- const { status } = body;
- if (status === '1') {
- const { money: receipts, shop } = data;
- const obj = { shop, receipts, time: moment().format('YYYY-MM-DD HH:mm:ss'), status: '-1', source: '-1', source_id: data._id };
- const num = await this.shopInBillModel.count({ shop, status: '-1', source: '-1', source_id: data._id });
- // 说明该申请没有流水,添加流水;有流水就不加了
- if (num <= 0) await this.shopInBillModel.create(obj);
- }
- return data;
- }
- }
- module.exports = ShopCashOutService;
|