cqp 14 小時之前
父節點
當前提交
31d374d08f

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

@@ -97,6 +97,16 @@ class TestController extends BaseController
         }
     }
 
+    public function getInventoryStock(Request $request){
+        list($bool, $data) = (new TestService())->getInventoryStock($request->all());
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
     // 仓库档案 http://localhost:8060/api/WareHouse/Get  ?CodeorName=0501 为空返回所有数据
     // 部门档案 http://localhost:8060/api/Department/Get ?CodeorName=0501 为空返回所有数据
     // 人员档案 http://localhost:8060/api/Person/Get     ?CodeorName=00043&bPsnPerson=1(是否业务员)  为空返回所有数据

+ 11 - 0
app/Service/TestService.php

@@ -478,6 +478,17 @@ class TestService extends Service
         ]);
     }
 
+    public function getInventoryStock($data){
+        if(empty($data['site'])) return [false,'站点不能为空'];
+        $config = config("u");
+        if(! isset($config[$data['site']])) return [false,'站点不存在'];
+
+        $service = new U8ThirtyPartyDatabaseServerService($config[$data['site']]);
+        list($status, $msg) = $service->getStockCountByCategory();
+
+        return [$service, $msg];
+    }
+
     //-----------------------------------朗峰u8-----
     public function post_helper($url, $data, $header = [], $timeout = 20, $title = ""){
         Log::channel('apiLog')->info($title . 'POST', ["api" => $url , "param" => json_decode($data,true) ,"header" => $header]);

+ 71 - 0
app/Service/U8ThirtyPartyDatabaseServerService.php

@@ -0,0 +1,71 @@
+<?php
+namespace App\Service;
+
+use Illuminate\Support\Facades\Config;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+
+class U8ThirtyPartyDatabaseServerService extends Service
+{
+    protected $connectionName = '';
+
+    public function __construct($data)
+    {
+        $this->createConnection($data);
+    }
+
+    private function createConnection($data)
+    {
+        $mainConnName = $data['connect_name'];
+        $this->connectionName = $mainConnName;
+
+        // 创建连接
+        $mainConfig = [
+            'driver'   => 'sqlsrv',
+            'host'     => $data['api_host'],
+            'port'     => $data['database_port'],
+            'database' => 'UFDATA_001_2025',
+            'username' => $data['username'],
+            'password' => $data['password'],
+            'options'  => [
+                \PDO::SQLSRV_ATTR_QUERY_TIMEOUT => 15,  // 减少超时
+                \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
+            ],
+        ];
+
+        // 配置注入
+        Config::set("database.connections.{$mainConnName}", $mainConfig);
+    }
+
+    public function getStockCountByCategory()
+    {
+        try {
+            $result = DB::connection($this->connectionName)
+                ->table('Inventory as i')
+                ->lock('WITH(NOLOCK)')
+                ->join('InventoryClass as ic', 'i.cInvCCode', '=', 'ic.cInvCCode')
+                ->join('CurrentStock as s', 'i.cInvCode', '=', 's.cInvCode')
+                ->select([
+                    'ic.cInvCCode as category_code',
+                    'ic.cInvCName as category_name',
+                    DB::raw('SUM(s.iQuantity) as total_quantity')
+                ])
+                ->groupBy('ic.cInvCCode', 'ic.cInvCName')
+                ->orderBy('ic.cInvCCode', 'ASC')
+                ->get()
+                ->map(function($item) {
+                    return [
+                        'category_code'  => trim($item->category_code),
+                        'category_name'  => trim($item->category_name),
+                        'total_quantity' => (float)$item->total_quantity,
+                    ];
+                })
+                ->toArray();
+
+            return [true, $result];
+
+        } catch (\Throwable $e) {
+            return [false, $e->getMessage()];
+        }
+    }
+}

+ 12 - 0
config/u.php

@@ -4,13 +4,25 @@ return [
     "LFMY" => [
         'api_host' => 'samata2924.imwork.net',
         'api_port' => '47438',
+        'database_port' => '55362',
+        'connect_name' => 'lfmy',
+        'username' => 'sa',
+        'password' => 'Aa1',
     ],
     "HCLT" => [
         'api_host' => '1ag17727201hq.vicp.fun',
         'api_port' => '46227',
+        'database_port' => '54788',
+        'connect_name' => 'hclt',
+        'username' => 'sa',
+        'password' => 'Aa1',
     ],
     "JLWM" => [
         'api_host' => '1wz1796225me1.vicp.fun',
         'api_port' => '42860',
+        'database_port' => '41402',
+        'connect_name' => 'jlwm',
+        'username' => 'sa',
+        'password' => 'Aa1',
     ],
 ];

+ 1 - 0
routes/api.php

@@ -65,6 +65,7 @@ Route::any('production_status','Api\ReportFormsController@production_status');
 
 //朗峰
 Route::any('recordList', 'Api\TestController@recordList');
+Route::any('getInventoryStock', 'Api\TestController@getInventoryStock');
 Route::group(['middleware'=> ['CheckU8']],function ($route){
     $route->any('salesOrderGet', 'Api\TestController@salesOrderGet');
     $route->any('materialAddU8', 'Api\TestController@materialAddU8')->middleware('U8Deal');