shopInBill.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 ShopInBillService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'shopinbill');
  11. this.model = this.ctx.model.Shop.ShopInBill;
  12. this.shopModel = this.ctx.model.Shop.Shop;
  13. this.orderUtil = this.ctx.service.util.orderDetail;
  14. this.configModel = this.ctx.model.System.Config;
  15. }
  16. /**
  17. * 正常购物创建店铺流水
  18. * @param {object} orderDetail 订单详情数据
  19. * @param {Transaction} tran 数据库事务
  20. */
  21. async createByOrder(orderDetail, tran) {
  22. const obj = { shop: _.get(orderDetail, 'shop') };
  23. const shopData = await this.shopModel.findById(_.get(orderDetail, 'shop'), { cut: 1 });
  24. if (!shopData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到店铺数据');
  25. obj.cut = _.get(shopData, 'cut', 0);
  26. const real_pay = this.orderUtil.computedRealPay(orderDetail);
  27. obj.total = real_pay;
  28. obj.receipts = this.ctx.multiply(this.ctx.minus(1, this.ctx.divide(obj.cut, 100)), real_pay);
  29. obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
  30. obj.source = '3';
  31. obj.source_id = _.get(orderDetail, '_id');
  32. const num = await this.model.count({ source: obj.source, source_id: obj.source_id });
  33. if (num <= 0) tran.insert('ShopInBill', obj);
  34. }
  35. /**
  36. * 售后创建流水
  37. * @param {object} afterSale 订单详情数据
  38. * @param {number} returnMoney 退款金额
  39. * @param {Transaction} tran 数据库事务
  40. */
  41. async createByAfterSale(afterSale, returnMoney, tran) {
  42. const query = { source_id: _.get(afterSale, 'order_detail'), source: '3' };
  43. const inBill = await this.model.findOne(query);
  44. // 该订单没有入账,就不需要退
  45. if (!inBill) return;
  46. const q2 = { source: '-3', source_id: _.get(afterSale, '_id') };
  47. const n2 = await this.model.count(q2);
  48. // 有过售后退钱记录,不需要再生成
  49. if (n2 > 0) return;
  50. const obj = { shop: _.get(afterSale, 'shop') };
  51. obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
  52. obj.source = '-3';
  53. obj.source_id = _.get(afterSale, 'order_detail');
  54. const limit = _.get(inBill, 'receipts');
  55. if (this.ctx.minus(limit, returnMoney) >= 0) obj.receipts = returnMoney;
  56. else obj.receipts = limit;
  57. tran.insert('ShopInBill', obj);
  58. }
  59. async computedTotal({ shop }) {
  60. const list = await this.model.find({ shop }).lean();
  61. const config = await this.configModel.findOne({}, { reward_day: 1 }).lean();
  62. const rd = _.get(config, 'reward_day', 14);
  63. const min = this.ctx.multiply(rd, this.ctx.multiply(24, 60));
  64. let total = 0;
  65. for (const i of list) {
  66. const { source, receipts: money } = i;
  67. const sn = parseInt(source);
  68. if (sn >= 0) total = this.ctx.plus(total, money);
  69. else total = this.ctx.minus(total, money);
  70. }
  71. let canGet = 0;
  72. const inBill = list.filter(f => parseInt(f.source) >= 0);
  73. const outBill = list.filter(f => parseInt(f.source) < 0);
  74. const getOutBill = list.filter(f => f.source === '-1');
  75. const outBillMoney = getOutBill.reduce((p, n) => this.ctx.plus(p, n.receipts), 0);
  76. const canGetIds = [];
  77. const shouldOutIds = [];
  78. for (const i of inBill) {
  79. const cAt = _.get(i, 'time');
  80. const m = moment().diff(cAt, 'm');
  81. if (m >= min) {
  82. // 已经过了提款时间,那就再找下,是否有该记录相应的退款
  83. const { receipts: money, source_id } = i;
  84. canGetIds.push(source_id);
  85. let cg = money;
  86. const os = outBill.filter(f => f.source_id === source_id);
  87. console.log(`${source_id} 退款数: ${os.length}`);
  88. for (const o of os) {
  89. cg = this.ctx.minus(cg, o.receipts);
  90. shouldOutIds.push(o.source_id);
  91. }
  92. canGet = this.ctx.plus(canGet, cg);
  93. }
  94. }
  95. canGet = this.ctx.minus(canGet, outBillMoney);
  96. return { total, canGet };
  97. }
  98. async afterQuery(filter, data) {
  99. for (const i of data) {
  100. const { source } = i;
  101. if (parseInt(source) >= 0) i.receipts = `+${i.receipts}`;
  102. else i.receipts = `-${i.receipts}`;
  103. }
  104. return data;
  105. }
  106. }
  107. module.exports = ShopInBillService;