|
@@ -15,6 +15,7 @@ class AfterSaleService extends CrudService {
|
|
|
this.orderDetailModel = this.ctx.model.Trade.OrderDetail;
|
|
|
this.orderModel = this.ctx.model.Trade.Order;
|
|
|
this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
|
|
|
+ this.configModel = this.ctx.model.System.Config;
|
|
|
this.tran = new Transaction();
|
|
|
}
|
|
|
/**
|
|
@@ -34,11 +35,11 @@ class AfterSaleService extends CrudService {
|
|
|
if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
|
|
|
// 查看该商品是否已经申请售后
|
|
|
let goods;
|
|
|
- const hasData = await this.model.count({ order_detail, 'goods._id': goods_id, status: { $nin: [ '!1', '!2', '!3', '!4', '!5' ] } });
|
|
|
+ const hasData = await this.model.count({ order_detail, 'goods._id': goods_id, status: { $nin: ['!1', '!2', '!3', '!4', '!5'] } });
|
|
|
if (hasData > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该商品已有正在处理中的售后申请.请勿重复申请');
|
|
|
if (type !== '4' && type !== '5') {
|
|
|
const { goods: goodsList } = orderDetail;
|
|
|
- goods = goodsList.find(f => {
|
|
|
+ goods = goodsList.find((f) => {
|
|
|
return ObjectId(f._id).equals(goods_id);
|
|
|
});
|
|
|
if (!goods) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未在当前订单中搜索到要售后的商品');
|
|
@@ -218,7 +219,7 @@ class AfterSaleService extends CrudService {
|
|
|
// 已退款记录
|
|
|
const goodsRefundList = [];
|
|
|
// 在下面处理已退款记录中,应该把当前的这个售后项,作为已处理售后
|
|
|
- const tasq = { order_detail: data.order_detail, 'goods._id': _.get(data, 'goods._id'), status: [ '-1', '-2' ] };
|
|
|
+ const tasq = { order_detail: data.order_detail, 'goods._id': _.get(data, 'goods._id'), status: ['-1', '-2'] };
|
|
|
const asdd = JSON.parse(JSON.stringify(data));
|
|
|
if (_.get(update, 'money')) asdd.money = _.get(update, 'money');
|
|
|
const tr = await this.checkIsAllRefund(data);
|
|
@@ -229,7 +230,7 @@ class AfterSaleService extends CrudService {
|
|
|
const { goods, _id: order_detail } = od;
|
|
|
// 组合成查售后的条件
|
|
|
// 然后查这些商品有没有退款审核成功的记录, 且只有退全款的商品才能退券
|
|
|
- const afterSaleQuerys = goods.map(i => ({ order_detail, 'goods._id': i._id, status: [ '-1', '-2' ] }));
|
|
|
+ const afterSaleQuerys = goods.map((i) => ({ order_detail, 'goods._id': i._id, status: ['-1', '-2'] }));
|
|
|
for (const asq of afterSaleQuerys) {
|
|
|
const asd = await this.model.findOne(asq);
|
|
|
if (asd) {
|
|
@@ -245,7 +246,7 @@ class AfterSaleService extends CrudService {
|
|
|
// 该优惠券影响的商品id列表
|
|
|
const goodsIds = Object.keys(_.get(dd, uc_id, {}));
|
|
|
// 然后在已退款/退货记录中找,这个优惠券影响的商品是否都退款了.退全额
|
|
|
- const r = goodsIds.every(i => goodsRefundList.find(f => i === f['goods._id']));
|
|
|
+ const r = goodsIds.every((i) => goodsRefundList.find((f) => i === f['goods._id']));
|
|
|
if (r) {
|
|
|
// 说明这个优惠券影响的商品都退了,这个优惠券也就能退了
|
|
|
tran.update('UserCoupon', uc_id, { status: '0' });
|
|
@@ -264,15 +265,15 @@ class AfterSaleService extends CrudService {
|
|
|
// 获取拆分订单的数据,并找到商品列表
|
|
|
const goodsList = _.get(orderDetail, 'goods');
|
|
|
// 依次找这些商品是否都售后完成,都售后完成,就改订单状态
|
|
|
- const asList = await this.model.find({ order_detail, status: { $nin: [ '0', '!1', '!2', '!3', '!4', '!5' ] } });
|
|
|
+ const asList = await this.model.find({ order_detail, status: { $nin: ['0', '!1', '!2', '!3', '!4', '!5'] } });
|
|
|
let status;
|
|
|
let fl = [];
|
|
|
// 将当前数据添加进去
|
|
|
fl.push({ goods: _.get(data, 'goods._id'), status: 'finish' });
|
|
|
for (const gs of goodsList) {
|
|
|
- const r = asList.find(f => ObjectId(_.get(f, 'goods._id')).equals(_.get(gs, '_id')));
|
|
|
+ const r = asList.find((f) => ObjectId(_.get(f, 'goods._id')).equals(_.get(gs, '_id')));
|
|
|
if (r) {
|
|
|
- const finishList = [ '-1', '-2', '-3', '-4', '-5' ];
|
|
|
+ const finishList = ['-1', '-2', '-3', '-4', '-5'];
|
|
|
if (finishList.includes(r.status)) fl.push({ goods: gs._id, status: 'finish' });
|
|
|
}
|
|
|
}
|
|
@@ -320,7 +321,7 @@ class AfterSaleService extends CrudService {
|
|
|
const { goods, _id: order_detail } = od;
|
|
|
// 组合成查售后的条件
|
|
|
// 然后查这些商品有没有退款审核成功的记录, 且只有退全款的商品才能退券
|
|
|
- const afterSaleQuerys = goods.map(i => ({ order_detail, 'goods._id': i._id, status: [ '-1', '-2' ] }));
|
|
|
+ const afterSaleQuerys = goods.map((i) => ({ order_detail, 'goods._id': i._id, status: ['-1', '-2'] }));
|
|
|
for (const asq of afterSaleQuerys) {
|
|
|
const asd = await this.model.findOne(asq);
|
|
|
if (asd) {
|
|
@@ -336,7 +337,7 @@ class AfterSaleService extends CrudService {
|
|
|
// 该优惠券影响的商品id列表
|
|
|
const goodsIds = Object.keys(_.get(dd, uc_id, {}));
|
|
|
// 然后在已退款/退货记录中找,这个优惠券影响的商品是否都退款了.退全额
|
|
|
- const r = goodsIds.every(i => goodsRefundList.find(f => i === f['goods._id']));
|
|
|
+ const r = goodsIds.every((i) => goodsRefundList.find((f) => i === f['goods._id']));
|
|
|
if (r) {
|
|
|
// 说明这个优惠券影响的商品都退了,这个优惠券也就能退了
|
|
|
tran.update('UserCoupon', uc_id, { status: '0' });
|
|
@@ -487,6 +488,18 @@ class AfterSaleService extends CrudService {
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ async canRefund({ id }) {
|
|
|
+ const od = await this.orderDetailModel.findById(id, { buy_time: 1 }).lean();
|
|
|
+ if (!od) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单数据');
|
|
|
+ const config = await this.configModel.findOne({}, { reward_day: 1 }).lean();
|
|
|
+ const rd = _.get(config, 'reward_day', 14);
|
|
|
+ const min = this.ctx.multiply(rd, this.ctx.multiply(24, 60));
|
|
|
+ const buy_time = _.get(od, 'buy_time');
|
|
|
+ const m = moment().diff(buy_time, 'm');
|
|
|
+ const r = m < min;
|
|
|
+ return r;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = AfterSaleService;
|