shopCashOut.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. //
  8. class ShopCashOutService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'shopcashout');
  11. this.model = this.ctx.model.Shop.ShopCashOut;
  12. this.shopInBillModel = this.ctx.model.Shop.ShopInBill;
  13. }
  14. async beforeCreate(data) {
  15. const { shop } = data;
  16. if (!shop) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '缺少提款人');
  17. const num = await this.model.count({ shop, status: '0' });
  18. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '您有未处理提现申请,请联系管理员先处理之前的申请');
  19. return data;
  20. }
  21. async beforeUpdate(filter, update) {
  22. if (_.get(update, 'status') !== '0') {
  23. update.deal_person = _.get(this.ctx, 'admin._id');
  24. }
  25. return { filter, update };
  26. }
  27. async afterUpdate(filter, body, data) {
  28. const { status } = body;
  29. if (status === '1') {
  30. const { money: receipts, shop } = data;
  31. const obj = { shop, receipts, time: moment().format('YYYY-MM-DD HH:mm:ss'), status: '-1', source: '-1', source_id: data._id };
  32. const num = await this.shopInBillModel.count({ shop, status: '-1', source: '-1', source_id: data._id });
  33. // 说明该申请没有流水,添加流水;有流水就不加了
  34. if (num <= 0) await this.shopInBillModel.create(obj);
  35. }
  36. return data;
  37. }
  38. }
  39. module.exports = ShopCashOutService;