afterSale.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 { ObjectId } = require('mongoose').Types;
  7. const moment = require('moment');
  8. const Transaction = require('mongoose-transactions');
  9. //
  10. class AfterSaleService extends CrudService {
  11. constructor(ctx) {
  12. super(ctx, 'aftersale');
  13. this.model = this.ctx.model.Trade.AfterSale;
  14. this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
  15. this.orderModel = this.ctx.model.Trade.Order;
  16. this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
  17. this.configModel = this.ctx.model.System.Config;
  18. this.tran = new Transaction();
  19. }
  20. /**
  21. * 售后申请
  22. ** 售后申请有几种:取消订单; 拒收; 退款; 退货; 换货
  23. ** 取消订单:发货之前,付款之后: 买家申请, 卖家同意之后,直接处理退款即可(退回库存,券影响的所有商品全额退款,则退券)
  24. ** 拒收: 发货之后,收货之前;买家申请,卖家同意. 直到卖家 确认结束 售后,钱才退回给买家;(退回库存,券影响的所有商品全额退款,则退券)
  25. ** 退款:收货之后;买家申请,卖家同意.当即退钱,自动结束(退钱,券影响的所有商品全额退款,则退券)
  26. ** 退货:收货之后;买家申请,卖家同意.直到卖家 确认结束 售后,钱才退回给买家(退钱,退库存,券影响的所有商品全额退款,则退券)
  27. ** 换货:收货之后;买家申请,卖家同意.双方填写各自的单号. 买卖双方确认收货 后 售后结束(没什么变化?库存-x?)
  28. * @param {Object} body 售后信息
  29. */
  30. async create(body) {
  31. const { order_detail, goods: goods_id, type, ...others } = body;
  32. if (!order_detail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
  33. const orderDetail = await this.orderDetailModel.findById(order_detail);
  34. if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
  35. // 查看该商品是否已经申请售后
  36. let goods;
  37. const hasData = await this.model.count({ order_detail, 'goods._id': goods_id, status: { $nin: ['!1', '!2', '!3', '!4', '!5'] } });
  38. if (hasData > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该商品已有正在处理中的售后申请.请勿重复申请');
  39. if (type !== '4' && type !== '5') {
  40. const { goods: goodsList } = orderDetail;
  41. goods = goodsList.find((f) => {
  42. return ObjectId(f._id).equals(goods_id);
  43. });
  44. if (!goods) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未在当前订单中搜索到要售后的商品');
  45. } else {
  46. const realPay = await this.ctx.service.util.orderDetail.computedRealPay(orderDetail);
  47. others.money = realPay;
  48. others.desc = type === '4' ? '取消订单' : '拒收商品';
  49. }
  50. const { shop, customer } = orderDetail;
  51. const apply_time = moment().format('YYYY-MM-DD HH:mm:ss');
  52. const obj = { order_detail, customer, shop, goods, type, ...others, apply_time, status: '0' };
  53. await this.model.create(obj);
  54. }
  55. async update(filter, update, { projection } = {}) {
  56. assert(filter);
  57. assert(update);
  58. const { _id, id } = filter;
  59. if (_id || id) filter = { _id: ObjectId(_id || id) };
  60. // 检查数据是否存在
  61. const entity = await this.model.findOne(filter).exec();
  62. if (!entity) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  63. try {
  64. // 先处理数据,数据处理对了,需要退钱再退钱
  65. const { type } = entity;
  66. const { status } = update;
  67. // 没有修改状态,那就直接走修改返回
  68. if (!status) {
  69. entity.set(update);
  70. await entity.save();
  71. return;
  72. }
  73. // 退款信息,有内容就是要退款,没有内容(undefined)就是不需要退款
  74. let refundInfo;
  75. // 要修改成的状态
  76. const uStatus = _.get(update, 'status');
  77. // 根据类型不同,处理的函数不一样.
  78. if (type === '1') {
  79. // 仅退款,退优惠券,退款
  80. // 如果不是处理中,则不进行退款
  81. if (_.get(update, 'status') === '1') {
  82. // 1.检验并组织退款信息
  83. refundInfo = await this.toReturnMoney(entity, update, this.tran);
  84. // 2.检查是否退优惠券,该退就退
  85. await this.toReturnCoupons(entity, update, this.tran);
  86. // 3.修改数据, 直接修改成退完款的状态.如果后面退款失败了.直接回滚了
  87. update.status = '-1';
  88. update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
  89. } else if (_.get(update, 'status') === '!1') {
  90. update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
  91. }
  92. // 4.修改订单状态
  93. await this.refundOrder(entity, this.tran);
  94. } else if (type === '2') {
  95. // 退货(退库存),退款,退优惠券
  96. // 做记录,但是不是结束状态,都不退
  97. if (uStatus === '-2') {
  98. // 1.检验并组织退款信息
  99. refundInfo = await this.toReturnMoney(entity, update, this.tran);
  100. // 2.检查是否退优惠券
  101. await this.toReturnCoupons(entity, update, this.tran);
  102. // 3.修改订单状态
  103. await this.refundOrder(entity, this.tran);
  104. // 结束时间
  105. update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
  106. } else if (uStatus === '!2') {
  107. update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
  108. }
  109. } else if (type === '3') {
  110. // 换货,不需要退款
  111. // 但需要检查买卖双方的快递是否签收,都签收的话,需要将状态改为结束
  112. const uto = _.get(update, 'transport', {});
  113. const eto = _.get(entity, 'transport', {});
  114. // 有关快递的字段整合,将传来的和以前的放在一起.然后找是否签收
  115. const to = { ...eto, ...uto };
  116. const cr = _.get(to, 'customer_receive');
  117. const sr = _.get(to, 'shop_receive');
  118. if (cr && sr) {
  119. update.status = '-3';
  120. update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
  121. } else if (uStatus === '!3') {
  122. update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
  123. }
  124. } else if (type === '4') {
  125. // 取消订单: 退钱,退货(退库存)----不过不需要有快递信息,没发货,退优惠券
  126. // 没有商品,只有拆分的订单号,用这个去退
  127. if (uStatus === '4') {
  128. refundInfo = await this.returnOrder(entity, this.tran);
  129. update.status = '-4';
  130. // 检查是否是团购
  131. await this.exitGroup(entity, this.tran);
  132. }
  133. update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
  134. } else if (type === '5') {
  135. // 拒收: 退钱, 退货
  136. // 如果状态不是完成,那就不退,只是数据修改
  137. if (uStatus === '-5') {
  138. refundInfo = await this.returnOrder(entity, this.tran);
  139. update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
  140. } else if (uStatus === '!5') {
  141. update.end_time = moment().format('YYYY-MM-DD HH:mm:ss');
  142. }
  143. } else throw new BusinessError(ErrorCode.DATA_INVALID, '未知的售后类型,无法处理');
  144. // 售后处理人的添加
  145. if (update.status !== '0') {
  146. const admin = this.ctx.admin;
  147. update.deal_person = admin._id;
  148. }
  149. // 修改数据
  150. this.tran.update('AfterSale', entity._id, update);
  151. await this.tran.run();
  152. // 退钱
  153. if (!refundInfo) return;
  154. // console.log(refundInfo);
  155. const res = await this.ctx.service.trade.pay.refund(refundInfo);
  156. if (res.errcode && res.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, res.errmsg);
  157. } catch (error) {
  158. console.error(error);
  159. await this.tran.rollback();
  160. throw new BusinessError(ErrorCode.SERVICE_FAULT, '售后:修改失败');
  161. } finally {
  162. this.tran.clean();
  163. }
  164. }
  165. /**
  166. * 售后退钱
  167. **组织退钱的请求参数即可,没啥需要改的
  168. * @param {Object} data 售后修改前的数据
  169. * @param {Object} update 要修改的数据
  170. * @param {Transaction} tran 数据库事务
  171. */
  172. async toReturnMoney(data, update, tran) {
  173. const { order_detail, goods } = data;
  174. const orderDetail = await this.orderDetailModel.findById(order_detail);
  175. if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
  176. // 取出订单详情的每种商品规格的价格明细
  177. const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
  178. // 根据订单类型使用价格的key
  179. let priceKey;
  180. if (_.get(orderDetail, 'type', '0') === '1') priceKey = 'ggrp';
  181. else priceKey = 'grp';
  182. // 商品实际支付的金额
  183. const goodsRealPay = _.get(moneyDetail, `${goods._id}.${priceKey}`);
  184. // 需要退还的金额,如果传来的数据有金额,就使用传来的,没有的话就用原来的
  185. const returnMoney = _.get(update, 'money', _.get(data, 'money'));
  186. if (goodsRealPay < returnMoney) throw new BusinessError(ErrorCode.DATA_INVALID, '退款金额超出该商品支付的金额');
  187. // 组成退款单号
  188. const { order: order_id } = orderDetail;
  189. const order = await this.orderModel.findById(order_id);
  190. if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单信息');
  191. const order_no = _.get(order, 'pay.pay_no');
  192. if (!order_no) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到微信支付订单号');
  193. const str = this.ctx.service.util.trade.createNonceStr();
  194. const out_refund_no = `${order_no}-r-${str}`;
  195. const refundInfo = { reason: _.get(data, 'desc', '购物退款'), money: returnMoney, order_no, out_refund_no };
  196. // 退积分
  197. await this.ctx.service.user.point.refundOrderPoint(order_detail, tran);
  198. return refundInfo;
  199. }
  200. /**
  201. * 售后退优惠券
  202. **回溯至支付订单,检查每张优惠券影响的商品 及 当前售后的商品 是否都 全额退款
  203. **如果优惠券影响的商品全额退款,那就退还优惠券;
  204. **如果有一个没有退款 或者 全额退款,那优惠券就不退
  205. * @param {Object} data 售后修改前的数据
  206. * @param {Object} update 要修改的数据
  207. * @param {Transaction} tran 数据库事务
  208. */
  209. async toReturnCoupons(data, update, tran) {
  210. const orderDetail = await this.orderDetailModel.findById(data.order_detail);
  211. if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
  212. const { order: order_id } = orderDetail;
  213. const order = await this.orderModel.findById(order_id);
  214. if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单信息');
  215. const odList = await this.orderDetailModel.find({ order: order_id });
  216. // 已退款记录
  217. const goodsRefundList = [];
  218. // 在下面处理已退款记录中,应该把当前的这个售后项,作为已处理售后
  219. const tasq = { order_detail: data.order_detail, 'goods._id': _.get(data, 'goods._id'), status: ['-1', '-2'] };
  220. const asdd = JSON.parse(JSON.stringify(data));
  221. if (_.get(update, 'money')) asdd.money = _.get(update, 'money');
  222. const tr = await this.checkIsAllRefund(data);
  223. // 如果当前商品的售后都不满足全额退款条件.那就别往后查了
  224. if (tr) goodsRefundList.push(tasq);
  225. else return;
  226. for (const od of odList) {
  227. const { goods, _id: order_detail } = od;
  228. // 组合成查售后的条件
  229. // 然后查这些商品有没有退款审核成功的记录, 且只有退全款的商品才能退券
  230. const afterSaleQuerys = goods.map((i) => ({ order_detail, 'goods._id': i._id, status: ['-1', '-2'] }));
  231. for (const asq of afterSaleQuerys) {
  232. const asd = await this.model.findOne(asq);
  233. if (asd) {
  234. const r = await this.checkIsAllRefund(asd);
  235. if (r) goodsRefundList.push(asq);
  236. }
  237. }
  238. }
  239. // 获取支付单的优惠明细
  240. const dd = _.get(order, 'total_detail.discount_detail', {});
  241. for (const uc_id in dd) {
  242. // uc_id 用户领取优惠券的id
  243. // 该优惠券影响的商品id列表
  244. const goodsIds = Object.keys(_.get(dd, uc_id, {}));
  245. // 然后在已退款/退货记录中找,这个优惠券影响的商品是否都退款了.退全额
  246. const r = goodsIds.every((i) => goodsRefundList.find((f) => i === f['goods._id']));
  247. if (r) {
  248. // 说明这个优惠券影响的商品都退了,这个优惠券也就能退了
  249. tran.update('UserCoupon', uc_id, { status: '0' });
  250. }
  251. }
  252. }
  253. /**
  254. * 售后:修改拆分订单
  255. * @param {Object} data 售后修改前的数据
  256. * @param {Transaction} tran 数据库事务
  257. */
  258. async refundOrder(data, tran) {
  259. const { order_detail } = data;
  260. const orderDetail = await this.orderDetailModel.findById(order_detail);
  261. if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
  262. // 获取拆分订单的数据,并找到商品列表
  263. const goodsList = _.get(orderDetail, 'goods');
  264. // 依次找这些商品是否都售后完成,都售后完成,就改订单状态
  265. const asList = await this.model.find({ order_detail, status: { $nin: ['0', '!1', '!2', '!3', '!4', '!5'] } });
  266. let status;
  267. let fl = [];
  268. // 将当前数据添加进去
  269. fl.push({ goods: _.get(data, 'goods._id'), status: 'finish' });
  270. for (const gs of goodsList) {
  271. const r = asList.find((f) => ObjectId(_.get(f, 'goods._id')).equals(_.get(gs, '_id')));
  272. if (r) {
  273. const finishList = ['-1', '-2', '-3', '-4', '-5'];
  274. if (finishList.includes(r.status)) fl.push({ goods: gs._id, status: 'finish' });
  275. }
  276. }
  277. fl = _.uniqBy(fl, 'goods');
  278. // 说明所有的商品 都有 已处理的售后
  279. if (fl.length === goodsList.length) status = '-4';
  280. // 有状态码,则修改订单的状态
  281. if (status) {
  282. tran.update('OrderDetail', order_detail, { status });
  283. }
  284. }
  285. /**
  286. * 售后 取消订单 & 拒收处理完成
  287. * @param {Object} data 售后修改前的数据
  288. * @param {Transaction} tran 数据库事务
  289. */
  290. async returnOrder(data, tran) {
  291. // 1.退钱
  292. const { order_detail } = data;
  293. const orderDetail = await this.orderDetailModel.findById(order_detail);
  294. if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
  295. const order_id = _.get(orderDetail, 'order');
  296. const goodsList = _.get(orderDetail, 'goods');
  297. const order = await this.orderModel.findById(order_id);
  298. if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单信息');
  299. this.tran.update('OrderDetail', order_detail, { status: '-1' });
  300. // 实付的钱
  301. const realPay = await this.ctx.service.util.orderDetail.computedRealPay(orderDetail);
  302. // 组成退款单号
  303. const str = this.ctx.service.util.trade.createNonceStr();
  304. const order_no = _.get(order, 'pay.pay_no');
  305. const out_refund_no = `${order_no}-r-${str}`;
  306. const refundInfo = { reason: _.get(data, 'desc'), money: realPay, order_no, out_refund_no };
  307. // 2.退卷
  308. // 查找支付订单拆分的所有订单.不包含当前要退的单.默认当前要退的单是全都退了的,只检查其他拆分的就可以
  309. const odList = await this.orderDetailModel.find({ order: order_id, _id: { $ne: ObjectId(order_detail) } });
  310. const goodsRefundList = [];
  311. // 将这个单内的商品加入满足全额退款的商品列表中
  312. for (const g of goodsList) {
  313. const obj = { order_detail, 'goods._id': g._id };
  314. goodsRefundList.push(obj);
  315. }
  316. for (const od of odList) {
  317. const { goods, _id: order_detail } = od;
  318. // 组合成查售后的条件
  319. // 然后查这些商品有没有退款审核成功的记录, 且只有退全款的商品才能退券
  320. const afterSaleQuerys = goods.map((i) => ({ order_detail, 'goods._id': i._id, status: ['-1', '-2'] }));
  321. for (const asq of afterSaleQuerys) {
  322. const asd = await this.model.findOne(asq);
  323. if (asd) {
  324. const r = await this.checkIsAllRefund(asd);
  325. if (r) goodsRefundList.push(asq);
  326. }
  327. }
  328. }
  329. // 获取支付单的优惠明细
  330. const dd = _.get(order, 'total_detail.discount_detail', {});
  331. for (const uc_id in dd) {
  332. // uc_id 用户领取优惠券的id
  333. // 该优惠券影响的商品id列表
  334. const goodsIds = Object.keys(_.get(dd, uc_id, {}));
  335. // 然后在已退款/退货记录中找,这个优惠券影响的商品是否都退款了.退全额
  336. const r = goodsIds.every((i) => goodsRefundList.find((f) => i === f['goods._id']));
  337. if (r) {
  338. // 说明这个优惠券影响的商品都退了,这个优惠券也就能退了
  339. tran.update('UserCoupon', uc_id, { status: '0' });
  340. }
  341. }
  342. // 3.退库存
  343. for (const g of goodsList) {
  344. const { _id, buy_num } = g;
  345. const goodsSpec = await this.goodsSpecModel.findById(_id);
  346. if (!goodsSpec) continue;
  347. const { num } = goodsSpec;
  348. const newNum = this.ctx.plus(buy_num, num);
  349. this.tran.update('GoodsSpec', goodsSpec._id, { num: newNum });
  350. }
  351. return refundInfo;
  352. }
  353. /**
  354. * 售后 取消订单 & 拒收处理完成
  355. * @param {Object} data 售后修改前的数据
  356. * @param {Transaction} tran 数据库事务
  357. */
  358. async exitGroup(data, tran) {
  359. const { order_detail } = data;
  360. const orderDetail = await this.orderDetailModel.findById(order_detail);
  361. if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单数据');
  362. const { type, group, customer } = orderDetail;
  363. if (type !== '1' || !group) return;
  364. await this.ctx.service.group.group.refund({ group, customer }, tran);
  365. }
  366. /**
  367. * 判断是否全额退的商品
  368. * @param {Object} data 售后数据
  369. * @return {Boolean}
  370. */
  371. async checkIsAllRefund(data) {
  372. // 商品有退款审核通过的记录,查询每个商品是否退的是全款
  373. // money: 实际退款的金额
  374. const { money } = data;
  375. const od_id = _.get(data, 'order_detail');
  376. const goods_id = _.get(data, 'goods._id');
  377. const moneyDetail = await this.computedGoodsForRefund({ order_detail: od_id, goods_id });
  378. if (moneyDetail) {
  379. const { payTotal } = moneyDetail;
  380. if (this.ctx.minus(payTotal, money) === 0) {
  381. // 添加到已退款的列表中
  382. return true;
  383. }
  384. }
  385. }
  386. /**
  387. * 计算商品退货的金额最大值
  388. * @param {Object} body 参数体
  389. * @param body.order_detail 订单详情id
  390. * @param body.goods_id 商品id
  391. */
  392. async computedGoodsForRefund({ order_detail, goods_id }) {
  393. const orderDetail = await this.orderDetailModel.findById(order_detail);
  394. if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
  395. const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
  396. const gmd = _.get(moneyDetail, goods_id);
  397. const obj = {};
  398. if (_.get(orderDetail, 'type', '0') === '1') {
  399. obj.payTotal = _.get(gmd, 'ggrp');
  400. obj.goodsTotal = _.get(gmd, 'gst');
  401. } else {
  402. obj.payTotal = _.get(gmd, 'grp');
  403. obj.goodsTotal = _.get(gmd, 'st');
  404. }
  405. obj.freightTotal = _.get(gmd, 'ft');
  406. obj.discountTotal = _.get(gmd, 'dt');
  407. return obj;
  408. // const goodsTotal =
  409. // const goods = orderDetail.goods.find(f => f._id === goods_id);
  410. // // 货物支付金额, 数量*购买数量+ 数量*运费 - 优惠
  411. // const goodsTotal = this.ctx.multiply(goods.sell_money, goods.buy_num);
  412. // const freightTotal = this.ctx.multiply(goods.freight, goods.buy_num);
  413. // let discountTotal = 0;
  414. // const { total_detail = {} } = orderDetail;
  415. // const { discount_detail = {} } = total_detail;
  416. // for (const dd in discount_detail) {
  417. // const dm = _.get(discount_detail, `${dd}.${goods_id}`, 0);
  418. // discountTotal = this.ctx.plus(discountTotal, dm);
  419. // }
  420. // const payTotal = this.ctx.minus(this.ctx.plus(goodsTotal, freightTotal), discountTotal);
  421. // const obj = { payTotal, goodsTotal, freightTotal, discountTotal };
  422. // return obj;
  423. }
  424. /**
  425. * 退单
  426. * @param {Object} body 参数体
  427. * @param body.order_detail 订单详情id
  428. * @param body.desc 退单理由
  429. */
  430. async orderCancel({ order_detail, desc }) {
  431. // 查询要退的订单
  432. const orderDetail = await this.orderDetailModel.findById(order_detail);
  433. if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
  434. const { customer } = orderDetail;
  435. const basic = { order_detail, customer, type: '1', desc };
  436. const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
  437. let priceKey;
  438. if (_.get(orderDetail, 'type') === '1') priceKey = 'ggrp';
  439. else priceKey = 'grp';
  440. for (const goods_id in moneyDetail) {
  441. const d = _.get(moneyDetail, goods_id, {});
  442. const money = _.get(d, priceKey, 0);
  443. const obj = { ...basic, goods_id, money };
  444. await this.create(obj);
  445. }
  446. }
  447. async fetch(filter) {
  448. assert(filter);
  449. filter = await this.beforeFetch(filter);
  450. const { _id, id } = filter;
  451. if (_id || id) filter = { _id: ObjectId(_id || id) };
  452. const { populate } = this.getRefMods();
  453. let res = await this.model.findOne(filter).populate(populate).exec();
  454. res = await this.afterFetch(filter, res);
  455. return res;
  456. }
  457. /**
  458. * 查询售后快递
  459. * @param {Object} query 查询参数
  460. * @param {String} query.id 售后id
  461. */
  462. async getTransportInfo({ id }) {
  463. const data = await this.model.findById(id);
  464. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到售后数据');
  465. const { transport } = data;
  466. if (!transport) return;
  467. const { customer_transport_no, customer_transport_type, shop_transport_no, shop_transport_type } = transport;
  468. const result = {};
  469. if (customer_transport_no && customer_transport_type) {
  470. const q = { no: customer_transport_no, type: customer_transport_type };
  471. const customer = await this.ctx.service.util.kd100.search(q);
  472. result.customer = customer;
  473. }
  474. if (shop_transport_no && shop_transport_type) {
  475. const q = { no: shop_transport_no, type: shop_transport_type };
  476. const shop = await this.ctx.service.util.kd100.search(q);
  477. result.shop = shop;
  478. }
  479. return result;
  480. }
  481. async canRefund({ id }) {
  482. const od = await this.orderDetailModel.findById(id, { buy_time: 1 }).lean();
  483. if (!od) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单数据');
  484. const config = await this.configModel.findOne({}, { reward_day: 1 }).lean();
  485. const rd = _.get(config, 'reward_day', 14);
  486. const min = this.ctx.multiply(rd, this.ctx.multiply(24, 60));
  487. const buy_time = _.get(od, 'buy_time');
  488. const m = moment().diff(buy_time, 'm');
  489. const r = m < min;
  490. return r;
  491. }
  492. }
  493. module.exports = AfterSaleService;