root 1 năm trước cách đây
mục cha
commit
1001e598df

+ 26 - 0
app/Http/Controllers/Api/TestController.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+
+use App\Model\BoxDetail;
+use App\Service\MeasureService;
+use Illuminate\Http\Request;
+
+
+class TestController extends BaseController
+{
+    public function aa(Request $request)
+    {
+        $model = new BoxDetail(['channel'=>date('YmdHis')]);
+        var_dump($model);
+
+    }
+
+
+
+
+
+
+
+}

+ 50 - 0
app/Model/BoxDetail.php

@@ -3,6 +3,8 @@
 namespace App\Model;
 
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Schema;
 
 /**
  *
@@ -15,5 +17,53 @@ class BoxDetail extends Model
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
+    protected $guarded = [];
 
+    protected $month = [
+        1 => '01_03',
+        2 => '01_03',
+        3 => '01_03',
+        4 => '04_06',
+        5 => '04_06',
+        6 => '04_06',
+        7 => '07_09',
+        8 => '07_09',
+        9 => '07_09',
+        10 => '10_12',
+        11 => '10_12',
+        12 => '10_12',
+    ];
+
+    public function __construct(array $attributes = [])
+    {
+        parent::__construct($attributes);
+        if (isset($attributes['channel']) && $attributes['channel'] > 0) {
+            $channel = $attributes['channel'];
+            $month = (int)substr($channel,4,2);
+            $channel = substr($channel,0,4).$this->month[$month];
+            $this->setTableById($channel);
+        }
+    }
+
+
+    /**
+     * @param $channel
+     */
+    // 动态指定数据表
+    public function setTableById($channel)
+    {
+        if ($channel > 0) {
+            $tb = $this->table.'_' . (string)$channel;
+            $this->createTable($tb);
+            $this->setTable($tb);
+        }
+
+    }
+
+    public function createTable($channel){
+//        $table_name = 'box_detail_' . (string)$channel;
+        if(!Schema::hasTable($channel)){
+            DB::update('create table '.$channel.' like box_detail');
+        }
+    }
 }

+ 4 - 3
app/Service/Box/BoxHookService.php

@@ -81,13 +81,14 @@ class BoxHookService extends Service
      * @return array
      */
     public function boxDetailInsert($data){
-        $box_detail = new BoxDetail();
+        $order_no = $data['order_no'];
+        $out_order_no = $data['out_order_no'];
+        $box_detail = new BoxDetail(['channel'=>$order_no]);
 
         if(!isset($data['detail'])) return [false,'detail is not exist'];
 
         $insert = $data['detail'];
-        $order_no = $data['order_no'];
-        $out_order_no = $data['out_order_no'];
+
         list($status,$insert) = $this->dealBoxDetail($insert,$order_no,$out_order_no);
         if(!$status) return [false,$insert];
         $box_detail->insert($insert);

+ 1 - 0
routes/api.php

@@ -18,6 +18,7 @@ Route::middleware('auth:api')->get('/user', function (Request $request) {
 });
 
 Route::any('login', 'Api\LoginController@login');
+Route::any('test', 'Api\TestController@aa');
 Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('menuAdd', 'Api\SysMenuController@add');
     $route->any('menuEdit', 'Api\SysMenuController@edit');