root il y a 2 ans
Parent
commit
31a0f2bd9e
2 fichiers modifiés avec 104 ajouts et 89 suppressions
  1. 17 15
      app/Service/Box/BoxHookService.php
  2. 87 74
      app/Service/Box/BoxService.php

+ 17 - 15
app/Service/Box/BoxHookService.php

@@ -45,6 +45,7 @@ class BoxHookService extends Service
         $data['order_no'] = $this->setOrderNo();
         if(!isset($data['out_order_no'])) return [false,'out_order_no is not exist'];
         if(!isset($data['top_id'])) return [false,'top_id is not exist'];
+        if(!isset($data['num'])) return [false,'num is not exist'];
         list($status,$box) = $this->dealBox($box,$data);
         if(!$status) return [false,$box];
         $box->save();
@@ -66,6 +67,7 @@ class BoxHookService extends Service
         $box->order_no = $data['order_no'];
         $box->out_order_no = $data['out_order_no'];
         $box->top_id = $data['top_id'];
+        $box->num = $data['num'];
         $box->ext_1 = isset($data['ext_1']) ? $data['ext_1'] : '';
         $box->ext_2 = isset($data['ext_2']) ? $data['ext_2'] : '';
         $box->ext_3 = isset($data['ext_3']) ? $data['ext_3'] : '';
@@ -88,7 +90,7 @@ class BoxHookService extends Service
         $out_order_no = $data['out_order_no'];
         $box_detail = new BoxDetail(['channel'=>$order_no]);
 
-        if(!isset($data['detail'])) return [false,'detail is not exist'];
+        if(!isset($data['detail'])||empty($data['detail'])) return [true,''];
 
         $insert = $data['detail'];
 
@@ -146,7 +148,7 @@ class BoxHookService extends Service
      * @param $data
      * @return array
      */
-    public function boxList($data){
+    public function boxDetail($data){
         $box = new Box();
         if(!isset($data['id'])) return [false,'id not found'];
         $list = $box->where('top_id',$data['id'])->get()->toArray();
@@ -155,19 +157,19 @@ class BoxHookService extends Service
 
     }
 
-    /**
-     * @param $data
-     * @return array
-     */
-    public function boxDetail($data){
-        $order_no = $data['order_no'];
-
-        $box = new BoxDetail(['channel'=>$order_no]);
-
-        $list = $this->limit($box,'*',$data);
-
-        return [true,$list];
-    }
+//    /**
+//     * @param $data
+//     * @return array
+//     */
+//    public function boxDetail($data){
+//        $order_no = $data['order_no'];
+//
+//        $box = new BoxDetail(['channel'=>$order_no]);
+//
+//        $list = $this->limit($box,'*',$data);
+//
+//        return [true,$list];
+//    }
 
 
 

+ 87 - 74
app/Service/Box/BoxService.php

@@ -3,12 +3,12 @@
 namespace App\Service\Box;
 
 
-
 use App\Model\Box;
 use App\Model\BoxDetail;
 use App\Model\Header_ext;
 use App\Model\SaleOrdersProduct;
 use App\Service\Service;
+use Illuminate\Support\Facades\DB;
 
 /**
  * 包装相关
@@ -21,48 +21,51 @@ class BoxService extends Service
     protected static $box_header;
     protected static $box_detail_header;
     protected static $box_hook;
+    public $lock_key = 'box';
 
-    public function __construct(){
+    public function __construct()
+    {
 
-        self::$box_header = Header_ext::where('type','box')->pluck('value','key')->toArray();
-        self::$box_detail_header = Header_ext::where('type','box_detail')->pluck('value','key')->toArray();
+        self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();
+        self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();
         self::$box_hook = BoxHookService::getInstance();
 
     }
 
 
-
     /**
      * 包装
      * @param $data
      * @return array
      */
-    public function boxIn($data){
-
-//        $param = $data['param'];
-        $param = [
-            [
-                'id'=>716,
-                'param'=>[
-                    '1',
-                    '1'
-                ],
-            ],[
-                'id'=>723,
-                'param'=>[
-                    '40',
-                    '40'
-                ],
-            ],
-        ];
+    public function boxIn($data)
+    {
+
+        if(!isset($data['param'])) return [false,'not found param'];
+        $param = $data['param'];
+//        $param = [
+//            [
+//                'id' => 716,
+//                'param' => [
+//                    '1',
+//                    '1'
+//                ],
+//            ], [
+//                'id' => 723,
+//                'param' => [
+//                    '40',
+//                    '40'
+//                ],
+//            ],
+//        ];
 
         $ids = [];
         $key_list = [];
-        foreach ($param as $v){
+        foreach ($param as $v) {
             $ids[] = $v['id'];
             $total = 0;
 
-            foreach ($v['param'] as $vv){
+            foreach ($v['param'] as $vv) {
                 $total += $vv;
             }
             $key_list[$v['id']] = [
@@ -71,47 +74,62 @@ class BoxService extends Service
             ];
         }
 
-        $product_list = SaleOrdersProduct::wherein('id',$ids)->get()->toArray();
-        foreach ($product_list as $v){
-            $num_list = $key_list[$v['id']];
-            $total = $num_list['total'];
-            $detail = $num_list['detail'];
-            $un_box_num = $v['order_quantity'] - $v['box_num'];
-            if($num_list['total'] > $un_box_num) return [false,$v['product_title'].'数量不足'];
+        try{
+            DB::beginTransaction();
+            $product_list = SaleOrdersProduct::wherein('id', $ids)->get()->toArray();
+            foreach ($product_list as $v) {
+                $key = $this->lock_key.'_'.$v['id'];
+                $lock_status = $this->isLock($key);
+                if(!$lock_status) return [false,'操作过于频繁'];
+                $num_list = $key_list[$v['id']];
+                $total = $num_list['total'];
+                $detail = $num_list['detail'];
+                $un_box_num = $v['order_quantity'] - $v['box_num'];
+                if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
+
+                $ext_1 = $v['customer_no'];
+                $ext_2 = $v['customer_name'];
+                $ext_3 = $v['product_no'];
+                $ext_4 = $v['product_title'];
+                $ext_5 = $v['product_size'];
+                $out_order_no = $v['out_order_no'];
+                $top_id = $v['id'];
+                foreach ($detail as $vv){
+                    $box_insert = [
+                        'out_order_no' => $out_order_no,
+                        'top_id' => $top_id,
+                        'ext_1' => $ext_1,
+                        'ext_2' => $ext_2,
+                        'ext_3' => $ext_3,
+                        'ext_4' => $ext_4,
+                        'ext_5' => $ext_5,
+                        'num' => $vv,
+
+                    ];
+                    list($status,$msg) = self::$box_hook->boxInsert($box_insert);
+
+                    if(!$status) {
+                        $this->delLock($key);
+                        DB::rollBack();
+                        return [false,$msg];
+                    }
+
+                }
+                $this->delLock($key);
+                SaleOrdersProduct::where('id',$top_id)->update([
+                    'box_num' => DB::raw('box_num + '.$total),
+                ]);
 
-        }
 
-        //$data = [
-        //            'out_order_no' => 'test123',
-        //            'top_id' => '1',
-        //            'ext_1' => '1',
-        //            'ext_2' => '2',
-        //            'ext_3' => '3',
-        //            'ext_4' => '4',
-        //            'ext_5' => '5',
-        //            'detail' => [
-        //                [
-        //                    'top_id' => '1',
-        //                    'code' => '001',
-        //                    'title' => '产品名称',
-        //                    'ext_1' => '1',
-        //                    'ext_2' => '2',
-        //                    'ext_3' => '3',
-        //                    'ext_4' => '4',
-        //                    'ext_5' => '5',
-        //                ],[
-        //                    'top_id' => '2',
-        //                    'code' => '002',
-        //                    'title' => '产品名称1',
-        //                    'ext_1' => '11',
-        //                    'ext_2' => '22',
-        //                    'ext_3' => '33',
-        //                    'ext_4' => '44',
-        //                    'ext_5' => '55',
-        //                ],
-        //            ],
-        //        ];
-        return [true,''];
+            }
+
+            DB::commit();
+            return [true,''];
+        }catch (\Exception $e){
+
+            DB::rollBack();
+            return [false,$e->getLine().':'.$e->getMessage()];
+        }
     }
 
 
@@ -120,17 +138,12 @@ class BoxService extends Service
      * @param $data
      * @return array
      */
-    public function boxDetail($data){
-        list($status,$data) = self::$box_hook->boxList($data);
-        if(!$status) return [false,$data];
-        return [true,$data];
+    public function boxDetail($data)
+    {
+        list($status, $data) = self::$box_hook->boxDetail($data);
+        if (!$status) return [false, $data];
+        return [true, $data];
     }
 
 
-
-
-
-
-
-
 }