WIN-2308041133\Administrator 1 month ago
parent
commit
55a92ce430

+ 3 - 1
app/models/store/StoreCart.php

@@ -90,17 +90,19 @@ class StoreCart extends BaseModel
             $product = StoreProduct::get($product_id);
             if ($product['store_type'] == 2 && !$is_new) return self::setErrorInfo('虚拟商品不可加入购物车');
         }
+        $mer_id = StoreProduct::where('id', $product_id)->value('mer_id');
         if ($cart = self::where('type', $type)->where('uid', $uid)->where('product_id', $product_id)->where('product_attr_unique', $product_attr_unique)->where('is_new', $is_new)->where('is_pay', 0)->where('is_del', 0)->where('combination_id', $combination_id)->where('bargain_id', $bargain_id)->where('seckill_id', $seckill_id)->find()) {
             if ($is_new)
                 $cart->cart_num = $cart_num;
             else
                 $cart->cart_num = bcadd($cart_num, $cart->cart_num, 0);
             $cart->add_time = time();
+            $cart->mer_id = $mer_id;
             $cart->save();
             return $cart;
         } else {
             $add_time = time();
-            return self::create(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'combination_id', 'add_time', 'bargain_id', 'seckill_id'));
+            return self::create(compact('uid', 'mer_id','product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'combination_id', 'add_time', 'bargain_id', 'seckill_id'));
         }
     }
 

+ 73 - 1
app/models/store/StoreOrder.php

@@ -9,6 +9,7 @@ namespace app\models\store;
 
 use app\admin\model\system\ShippingTemplatesFree;
 use app\admin\model\system\ShippingTemplatesRegion;
+use app\models\system\SystemUserLevel;
 use crmeb\basic\BaseModel;
 use think\db\exception\DataNotFoundException;
 use think\db\exception\DbException;
@@ -314,11 +315,13 @@ class StoreOrder extends BaseModel
             }
 
             $cartIds = [];
+            $mer_id = [];
             $totalNum = 0;
             $gainIntegral = 0;
             foreach ($cartInfo as $cart) {
                 $cartIds[] = $cart['id'];
                 $totalNum += $cart['cart_num'];
+                $mer_id[] = $cart['mer_id'];
                 if (!$seckill_id) $seckill_id = $cart['seckill_id'];
                 if (!$bargain_id) $bargain_id = $cart['bargain_id'];
                 if (!$combinationId) $combinationId = $cart['combination_id'];
@@ -421,10 +424,11 @@ class StoreOrder extends BaseModel
                     'SurplusIntegral' => $SurplusIntegral,
                 ];
             }
+            $mer_id = array_unique($mer_id);
             $orderInfo = [
                 'uid' => $uid,
                 'order_id' => $test ? 0 : self::getNewOrderId(),
-//                'mer_id' =>
+                'mer_id' => $mer_id,
                 'real_name' => $addressInfo['real_name'],
                 'user_phone' => $addressInfo['phone'],
                 'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
@@ -459,6 +463,8 @@ class StoreOrder extends BaseModel
             }
             $order = self::create($orderInfo);
             if (!$order) return self::setErrorInfo('订单生成失败!', true);
+            //生成门店订单信息
+            self::createOrderPart($orderInfo); //拆单创建门店订单表
             $res5 = true;
             foreach ($cartInfo as $cart) {
                 //减库存加销量
@@ -487,7 +493,59 @@ class StoreOrder extends BaseModel
         }
     }
 
+    public static function createOrderPart($orderInfo){
+        $user_info = User::where('uid', $orderInfo['uid'])->find();
+        $cart_ids = explode(',', $orderInfo['cart_id']);
+
+        $cartInfo = StoreCart::where('id', 'in', $cart_ids)
+            ->field('mer_id, GROUP_CONCAT(id) as ids') // 按mer_id分组,并用逗号拼接id
+            ->group('mer_id') // 按mer_id分组
+            ->select()
+            ->each(function ($item) {
+                // 将拼接的id字符串转为数组
+                $item->ids = explode(',', $item->ids);
+                return $item;
+            })
+            ->toArray();
+        $grouped = array_column($cartInfo, 'ids', 'mer_id');
+        foreach ($cartInfo as $k => $v){
+            $total_num=0;
+            $total_price = 0;
+            $deduction_price = 0;
+            $cartInfo = StoreCart::where('id','in',$v)->select();
+            foreach ($cartInfo as $cart){
+                $price = StoreProduct::where('id', $cart['product_id'])->value('price');
+                $product_price = bcmul($price, $cart['cart_num'], 2);
+                $total_price = bcadd($total_price, $product_price, 2); //商品总价
+                $total_num = bcadd($total_num, $cart['cart_num'], 2);
+            }
+            if ($user_info['level']>0){ //会员折扣
+                $discount = SystemUserLevel::where('id', $user_info['level'])->value('discount');
+                $moto_price = $total_price;
+                $total_price = bcmul($total_price, bcsub(1, $discount, 2), 2);
+                $deduction_price = bcsub($moto_price, $total_price, 2); //抵扣金额
+            }
+
+            $orderInfo =[
+                'mer_id' => $k,
+                'cart_id' => $v,
+                'order_id' => $orderInfo['order_id'],
+                'part_order_id' => self::getNewpartOrderId(), // 子订单号
+                'uid' => $orderInfo['uid'],
+                'real_name' => $orderInfo['real_name'],
+                'user_phone' => $orderInfo['user_phone'],
+                'user_address' => $orderInfo['user_address'],
+                'total_num' => $total_num,
+                'total_price' => $total_price,
+                'deduction_price' => $deduction_price,
+                'paid' => 0,
+                'mark' => $orderInfo['mark'],
+                'add_time' => time(),
+            ];
+            $order = StoreOrderPart::create($orderInfo);
+        }
 
+    }
     /**
      * 回退积分
      * @param $order 订单信息
@@ -588,6 +646,20 @@ class StoreOrder extends BaseModel
         } while (self::be(['order_id' => $orderId]));// $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
         return $orderId;
     }
+    /**
+     * 生成订单唯一id
+     * @param $uid 用户uid
+     * @return string
+     */
+    public static function getNewpartOrderId()
+    {
+        do {
+            list($msec, $sec) = explode(' ', microtime());
+            $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
+            $orderId = 'pa' . $msectime . mt_rand(10000, 99999);
+        } while (self::be(['order_id' => $orderId]));// $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
+        return $orderId;
+    }
 
     /**
      * 修改订单号

+ 34 - 0
app/models/store/StoreOrderPart.php

@@ -0,0 +1,34 @@
+<?php
+/**
+ *
+ * @author: xaboy<365615158@qq.com>
+ * @day: 2018/01/22
+ */
+
+namespace app\models\store;
+
+
+use crmeb\basic\BaseModel;
+use crmeb\traits\ModelTrait;
+
+/**
+ * TODO 优惠券Model
+ * Class StoreCoupon
+ * @package app\models\store
+ */
+class StoreOrderPart extends BaseModel
+{
+    /**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'store_order_part';
+
+    use ModelTrait;
+}