lrf 2 年 前
コミット
3e52306ffc

+ 2 - 0
app/controller/trade/config/.afterSale.js

@@ -5,6 +5,7 @@ module.exports = {
       'order_detail',
       'customer',
       'shop',
+      'set_id',
       'goods',
       'type',
       'reason',
@@ -29,6 +30,7 @@ module.exports = {
       'order_detail',
       'customer',
       'shop',
+      'set_id',
       'goods',
       'type',
       'reason',

+ 2 - 1
app/model/trade/afterSale.js

@@ -15,7 +15,8 @@ const afterSale = {
   order_detail: { type: String, required: false, zh: '订单详情', ref: 'Trade.OrderDetail' }, //
   customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
   shop: { type: String, required: false, zh: '店铺', ref: 'Shop.Shop' }, //
-  goods: { type: Object, required: false, zh: '需要售后的商品快照清单' }, // 一条记录值售后1个商品
+  set_id: { type: String, required: false, zh: '套装id' }, //
+  goods: { type: Object, required: false, zh: '需要售后的商品快照清单' }, // 一条记录值售后1个商品, goods._id:规格id
   type: { type: String, required: false, zh: '售后类型' }, // 字典:afterSale_type
   reason: { type: String, required: false, zh: '申请理由' }, // 字典:afterSale_reason
   desc: { type: String, required: false, zh: '申请售后描述' }, //

+ 23 - 16
app/service/trade/afterSale.js

@@ -16,6 +16,7 @@ class AfterSaleService extends CrudService {
     this.orderModel = this.ctx.model.Trade.Order;
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
     this.configModel = this.ctx.model.System.Config;
+    this.orderDetailService = this.ctx.service.util.orderDetail;
     this.tran = new Transaction();
   }
   /**
@@ -29,19 +30,31 @@ class AfterSaleService extends CrudService {
    * @param {Object} body 售后信息
    */
   async create(body) {
-    const { order_detail, goods: goods_id, type, ...others } = body;
+    const { order_detail, goods: goods_id, set_id, type, ...others } = body;
     if (!order_detail) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
     const orderDetail = await this.orderDetailModel.findById(order_detail);
     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' ] } });
-    if (hasData > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该商品已有正在处理中的售后申请.请勿重复申请');
+    const aData = await this.model.findOne({ order_detail, set_id, 'goods._id': goods_id, status: { $nin: [ '!1', '!2', '!3', '!4', '!5' ] } });
+    if (aData) throw new BusinessError(ErrorCode.DATA_EXISTED, '该商品已有正在处理中的售后申请.请勿重复申请');
     if (type !== '4' && type !== '5') {
       const { goods: goodsList } = orderDetail;
       goods = goodsList.find(f => {
-        return ObjectId(f._id).equals(goods_id);
+        const { is_set = '1' } = f;
+        if (!set_id && is_set === '1') return ObjectId(f._id).equals(goods_id);
+        return set_id === f.set_id;
       });
+      // 套装的数据处理
+      if (set_id && _.get(goods, 'set_id') === set_id) {
+        const setData = goods.goods.find(f => _.get(f, 'spec._id') === goods_id);
+        if (setData) {
+          const { spec, goods: sg } = setData;
+          const newGoods = { ...spec, goods: sg };
+          goods = newGoods;
+        }
+
+      }
       if (!goods) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未在当前订单中搜索到要售后的商品');
     } else {
       const realPay = await this.ctx.service.util.orderDetail.computedRealPay(orderDetail);
@@ -50,7 +63,8 @@ class AfterSaleService extends CrudService {
     }
     const { shop, customer } = orderDetail;
     const apply_time = moment().format('YYYY-MM-DD HH:mm:ss');
-    const obj = { order_detail, customer, shop, goods, type, ...others, apply_time, status: '0' };
+    this.orderDetailService.turn$numberDecimalToNumber(goods);
+    const obj = { order_detail, customer, shop, goods, type, ...others, apply_time, status: '0', set_id };
     const res = await this.model.create(obj);
     const msgData = { source_id: res._id, type: '0' };
     try {
@@ -184,9 +198,7 @@ class AfterSaleService extends CrudService {
     // 取出订单详情的每种商品规格的价格明细
     const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
     // 根据订单类型使用价格的key
-    let priceKey;
-    if (_.get(orderDetail, 'type', '0') === '1') priceKey = 'ggrp';
-    else priceKey = 'grp';
+    const priceKey = 'grp';
     // 商品实际支付的金额
     const goodsRealPay = _.get(moneyDetail, `${goods._id}.${priceKey}`);
     // 需要退还的金额,如果传来的数据有金额,就使用传来的,没有的话就用原来的
@@ -387,7 +399,7 @@ class AfterSaleService extends CrudService {
    * 计算商品退货的金额最大值
    * @param {Object} body 参数体
    * @param body.order_detail 订单详情id
-   * @param body.goods_id 商品id
+   * @param body.goods_id 商品id/套装id
    */
   async computedGoodsForRefund({ order_detail, goods_id }) {
     const orderDetail = await this.orderDetailModel.findById(order_detail);
@@ -395,13 +407,8 @@ class AfterSaleService extends CrudService {
     const moneyDetail = this.ctx.service.util.orderDetail.moneyDetail(orderDetail);
     const gmd = _.get(moneyDetail, goods_id);
     const obj = {};
-    if (_.get(orderDetail, 'type', '0') === '1') {
-      obj.payTotal = _.get(gmd, 'ggrp');
-      obj.goodsTotal = _.get(gmd, 'gst');
-    } else {
-      obj.payTotal = _.get(gmd, 'grp');
-      obj.goodsTotal = _.get(gmd, 'st');
-    }
+    obj.payTotal = _.get(gmd, 'grp');
+    obj.goodsTotal = _.get(gmd, 'st');
     obj.freightTotal = _.get(gmd, 'ft');
     obj.discountTotal = _.get(gmd, 'dt');
     return obj;

+ 18 - 0
app/service/util/orderDetail.js

@@ -79,6 +79,24 @@ class OrderDetailService extends CrudService {
     result.at = at;
     return result;
   }
+  /**
+   * $numberDecimal 转换成数字
+   * @param {Any} data
+   */
+  turn$numberDecimalToNumber(data) {
+    if (_.isArray(data)) {
+      for (const i of data) {
+        if (_.isArray(data)) this.turn$numberDecimalToNumber(i);
+        else if (_.isObject(data)) this.turn$numberDecimalToNumber(i);
+      }
+    } else if (_.isObject(data)) {
+      for (const key in data) {
+        const value = data[key];
+        if (_.isArray(value)) this.turn$numberDecimalToNumber(value);
+        else if (_.isObject(value) && _.has(value, '$numberDecimal')) data[key] = this.ctx.toNumber(value);
+      }
+    }
+  }
 }
 
 module.exports = OrderDetailService;