|
@@ -49,11 +49,11 @@ class AfterSaleService extends CrudService {
|
|
update = beforeUpdateResult.update;
|
|
update = beforeUpdateResult.update;
|
|
const { _id, id } = filter;
|
|
const { _id, id } = filter;
|
|
if (_id || id) filter = { _id: ObjectId(_id || id) };
|
|
if (_id || id) filter = { _id: ObjectId(_id || id) };
|
|
- // TODO:检查数据是否存在
|
|
|
|
|
|
+ // 检查数据是否存在
|
|
const entity = await this.model.findOne(filter).exec();
|
|
const entity = await this.model.findOne(filter).exec();
|
|
if (!entity) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
|
|
if (!entity) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
|
|
|
|
|
|
- // TODO: 修改数据
|
|
|
|
|
|
+ // 修改数据
|
|
try {
|
|
try {
|
|
this.tran.update('AfterSale', entity._id, update);
|
|
this.tran.update('AfterSale', entity._id, update);
|
|
await this.tran.run();
|
|
await this.tran.run();
|
|
@@ -87,21 +87,56 @@ class AfterSaleService extends CrudService {
|
|
const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
|
|
const { populate } = this.ctx.service.trade.orderDetail.getRefMods();
|
|
const orderDetail = await this.orderDetailModel.findById(data.order_detail).populate(populate);
|
|
const orderDetail = await this.orderDetailModel.findById(data.order_detail).populate(populate);
|
|
if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到售后信息的订单');
|
|
if (!orderDetail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到售后信息的订单');
|
|
- const money = _.get(data, 'goods.sell_money');
|
|
|
|
|
|
+ // 计算商品原价
|
|
|
|
+ let money = this.ctx.multiply(_.get(data, 'goods.sell_money'), _.get(data, 'goods.buy_num'));
|
|
const reason = _.get(data, 'desc');
|
|
const reason = _.get(data, 'desc');
|
|
const order_no = _.get(orderDetail, 'order.pay.pay_no');
|
|
const order_no = _.get(orderDetail, 'order.pay.pay_no');
|
|
if (!order_no) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单号');
|
|
if (!order_no) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付订单号');
|
|
|
|
+ // 查查优惠中,是否有这个商品:将这个商品的每一个优惠券的优惠部分都加一起,然后用原价-优惠价=实付价
|
|
|
|
+ const discount_detail = _.get(orderDetail, 'total_detail.discount_detail');
|
|
|
|
+ if (discount_detail) {
|
|
|
|
+ let discountMoney = 0;
|
|
|
|
+ for (const uc_id in discount_detail) {
|
|
|
|
+ const detail = discount_detail[uc_id];
|
|
|
|
+ const gd = detail[goods_id];
|
|
|
|
+ if (gd && !_.get(gd, 'refund', false)) {
|
|
|
|
+ discountMoney = this.ctx.plus(discountMoney, gd.discountMoney);
|
|
|
|
+ gd.refund = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ money = this.ctx.minus(money, discountMoney);
|
|
|
|
+ }
|
|
|
|
+
|
|
// 找下当前这个订单有多少次售后记录
|
|
// 找下当前这个订单有多少次售后记录
|
|
let num = await this.model.count({ order_detail: data.order_detail });
|
|
let num = await this.model.count({ order_detail: data.order_detail });
|
|
num += 1;
|
|
num += 1;
|
|
// 组成退款单号
|
|
// 组成退款单号
|
|
const out_refund_no = `${order_no}-r${num}`;
|
|
const out_refund_no = `${order_no}-r${num}`;
|
|
const obj = { reason, money, order_no, out_refund_no };
|
|
const obj = { reason, money, order_no, out_refund_no };
|
|
- // TODO优惠部分
|
|
|
|
-
|
|
|
|
// 退款请求
|
|
// 退款请求
|
|
- const refundRes = await this.ctx.service.trade.pay.refund(obj);
|
|
|
|
- // console.log(refundRes);
|
|
|
|
|
|
+ await this.ctx.service.trade.pay.refund(obj);
|
|
|
|
+
|
|
|
|
+ // 检查优惠券是否都退了
|
|
|
|
+ let allRefund = true;
|
|
|
|
+ for (const dd of discount_detail) {
|
|
|
|
+ for (const d in dd) {
|
|
|
|
+ if (!_.get(d, 'refund')) {
|
|
|
|
+ allRefund = false;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (allRefund) {
|
|
|
|
+ // 优惠券部分全都退了,那就把优惠券退了
|
|
|
|
+ const couponIds = Object.keys(discount_detail);
|
|
|
|
+ for (const id of couponIds) {
|
|
|
|
+ tran.update('UserCoupon', id, { status: '0' });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 修改订单详情的优惠券标记
|
|
|
|
+ tran.update('OrderDetail', orderDetail._id, { 'total_detail.discount_detail': discount_detail });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|