gogs 1 year ago
parent
commit
db56bb55ef
2 changed files with 206 additions and 126 deletions
  1. 29 3
      app/Http/Controllers/Api/TestController.php
  2. 177 123
      app/Service/OperationLogService.php

+ 29 - 3
app/Http/Controllers/Api/TestController.php

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api;
 
 use App\Model\BoxDetail;
 use App\Service\Box\BoxHookService;
+use App\Service\EmployeeService;
 use App\Service\MeasureService;
 use App\Service\OperationLogService;
 use Illuminate\Http\Request;
@@ -24,10 +25,35 @@ class TestController extends BaseController
 
     public function aa(){
         $a = new OperationLogService();
+//        $a = $a->getOperationList(['order_number'=>'T9XS.202402281513167014']);
+//        var_dump($a);die;
+        $checkResult = EmployeeService::checkUser(1);
+        list($state, $data) = $checkResult;
         $user['id'] = 1;
-        $data = json_decode('{"model_type":1,"sales_order_type":1,"crt_name":"\u8d85\u7ea7\u7ba1\u7406\u5458","order_number":"T9XS.202402281513167014","order_type":168,"construction_time":1706716800,"handover_time":1706630400,"deal_type":177,"customer_id":17,"sign_time":1709049600,"expire_time":1709049600,"contract_state":178,"employee_one":[52],"employee_one_title":"\u5185\u6d4b\u9ad8\u5065","employee_two":[47],"employee_two_title":"\u5185\u6d4b\u5510\u73ca","employee_three":[43],"employee_three_title":"\u738b\u7965","rate":"100.00","product_total":"2220.00","contract_fee":"2220.00","other_fee":"0","discount_fee":"0","contract_type":182,"pay_way":183,"send_state":188,"logistics_company":191,"product":[{"product_id":49,"code":"999","number":"10","bar_code":"11","title":"999","size":"1","depart_price":"222.00","sports_bag_id":0,"_X_ROW_KEY":"row_196","cost":"444.00","basic_type_id":0,"retail_price":"222.00","final_amount":"2220.00","price":"222.00"}],"menu_id":"37","file":[],"depart_id":2,"top_depart_id":2}
-',true);
-        $a->setOperationList($data,$user,1,37);
+        $data['role'] = EmployeeService::getPersonRole(1);
+        $return = EmployeeService::getLoginDepart(1);
+        //所属部门
+        $data['rule_depart'] = $return[0] ?? [];
+        //顶级公司
+        $data['depart_top'] = $return[1] ?? [];
+        //部门对应的顶级公司
+        $data['depart_map'] = $return[2] ?? [];
+        //权限范围内的部门以及公司
+        $data['depart_range'] = $return[3] ?? [];
+        //是否有所有的部门权限
+        $data['is_all_depart'] = $return[4] ?? 0;
+        //总公司
+        $data['head'] = $return[5] ?? [];
+        //是否是总公司下的人
+        $data['is_behind_main'] = $return[6] ?? 0;
+        //是否库存校验
+        $data['is_check_stock'] = true;
+
+        //写入user信息
+        $user = $data;
+
+        $data = json_decode('{"id":94,"sales_order_type":1,"model_type":1,"order_number":"T9XS.202402281811376059","title":null,"selling_price":"0.00","vin_no":null,"order_type":168,"deal_type":176,"customer_id":17,"sign_time":1708473600,"contract_state":178,"product_total":"40959.00","rate":"100.00","crt_id":1,"del_time":0,"crt_time":"2024-02-28 18:12:08","upd_time":"1709115128","mark":null,"construction_time":1708963200,"handover_time":1707421600,"expire_time":1708493600,"other_fee":0,"discount_fee":0,"contract_fee":"40959.00","contract_type":null,"pay_way":null,"send_state":null,"logistics_company":null,"logistics_number":null,"car_type":null,"year":null,"mileage":null,"color":null,"original_set":null,"processing":null,"state":0,"dispatch_time_first":null,"dispatch_time_second":null,"customer_title":"\u5185\u6d4b2","sales_order_type_title":"\u5b89\u88c5\u4ef6","dispatch_depart_two":[],"dispatch_employee":[],"dispatch_depart_one":[],"product":[{"product_id":48,"code":"333","number":"123","bar_code":"333","title":"333","size":"333","depart_price":"333.00","sports_bag_id":0,"_X_ROW_KEY":"row_803","cost":"333.00","basic_type_id":0,"retail_price":"333.00","final_amount":"40959.00","price":"333.00"}],"employee_three":[50],"employee_two":[52],"employee_one":[52],"file":[],"activity_product":[],"order_type_title":"\u603b\u90e8\u65bd\u5de5","deal_type_title":"\u5f53\u65e5\u6210\u4ea4","contract_state_title":"\u7b7e\u7ea6","contract_type_title":null,"car_type_title":null,"pay_way_title":null,"send_state_title":null,"logistics_company_title":null,"crt_name":"\u8d85\u7ea7\u7ba1\u7406\u5458","return_exchange":[],"importance":null,"employee_one_title":"\u5185\u6d4b\u9ad8\u5065","employee_two_title":"\u5185\u6d4b\u9ad8\u5065","employee_three_title":"\u5185\u6d4b\u590f\u793c\u68ee","menu_id":"37"}',true);
+        $a->setOperationList($data,$user,2,37);
     }
 
 

+ 177 - 123
app/Service/OperationLogService.php

@@ -3,7 +3,7 @@
 namespace App\Service;
 
 
-
+use App\Model\Employee;
 use App\Model\OperationLog;
 use App\Model\OperationLogDetail;
 use App\Model\SysMenu;
@@ -12,6 +12,7 @@ use Illuminate\Support\Facades\DB;
 class OperationLogService extends Service
 {
     protected static $instance;
+
     public static function getInstance(): self
     {
         if (self::$instance == null) {
@@ -21,100 +22,150 @@ class OperationLogService extends Service
     }
 
 
-    public function getOperationList($data){
-
-        $data = [
-            [
-                'type' => 1,
-                'user_id' => 1,
-                'user_name' => 1,
-                'crt_time' => 123456,
-                'data' => [
-                    [
-                        'key' => '金额',
-                        'old_data' => '100',
-                        'new_data' => '200',
-                    ]
-
-                ]
-            ]
-        ];
-        return [true,$data];
+    public function getOperationList($data)
+    {
+//        if(!isset($data['order_number'])) return [false,'订单号不存在!'];
+//        $data = [
+//            [
+//                'type' => 1,
+//                'user_id' => 1,
+//                'user_name' => 1,
+//                'crt_time' => 123456,
+//                'data' => [
+//                    [
+//                        'key' => '金额',
+//                        'old_data' => '100',
+//                        'new_data' => '200',
+//                    ]
+//
+//                ]
+//            ]
+//        ];
+        $list = OperationLog::where('order_number', $data['order_number'])->select('*')->get()->toArray();
+        $employee_key_list = Employee::pluck('emp_name', 'id')->toArray();
+        $log_ids = [];
+        $log_list = [];
+        foreach ($list as $v) {
+            $log_ids[] = $v['id'];
+            $log_list[] = [
+                'type' => $v['type'],
+                'id' => $v['id'],
+                'user_id' => $v['user_id'],
+                'user_name' => $employee_key_list[$v['user_id']],
+                'crt_time' => date('Y-m-d H:i:s', $v['crt_time']),
+            ];
+        }
+        $detail_list = OperationLogDetail::wherein('log_id', $log_ids)->select('*')->get()->toArray();
+        $detail_data_list = [];
+        foreach ($detail_list as $v) {
+            if(is_numeric($v['old_data'])&&strlen($v['old_data']) === 10) $v['old_data'] = date('Y-m-d H:i:s',$v['old_data']);
+            if(is_numeric($v['new_data'])&&strlen($v['new_data']) === 10) $v['new_data'] = date('Y-m-d H:i:s',$v['new_data']);
+            $detail_data_list[$v['log_id']][] = [
+                'key' => $v['title'],
+                'old_data' => $v['old_data'],
+                'new_data' => $v['new_data'],
+            ];
+        }
+        foreach ($log_list as &$v) {
+            $data = $detail_data_list[$v['id']] ?? [];
+            $v['data'] = $data;
+        }
+        return [true, $log_list];
     }
 
 //    public function
 
 
-    public function setOperationList($data,$user,$type=1,$menu_id=18){
+    public function setOperationList($data, $user, $type = 1, $menu_id = 18)
+    {
 
-        file_put_contents('log.txt',json_encode($data).PHP_EOL,8);
+        file_put_contents('log.txt', json_encode($data) . PHP_EOL, 8);
 
         //获取oa参数
         $key = 'menu_id'; // 要匹配的键
         $value = $menu_id; // 要匹配的值
 
-        $result = array_filter( config('oa'), function($array) use ($key, $value) {
+        $result = array_filter(config('oa'), function ($array) use ($key, $value) {
             return $array[$key] == $value;
         });
-        if(!isset($result['children']))   return [true,''];
-        $key_data = [];
-        foreach ($result['children'] as $v){
-            $key_data[$v['key']] = $v['title'];
-        }
-        $menu_id = $data['menu_id'];
-        $param  = isset($data['order_number']) ?  ['order_number'=>$data['order_number']]:['id'=>$data['id']];
-        $request =  request();
-        foreach ($param as $k=>$v){
-            $request->$k = $v;
-        }
-        $all = $request->all();
-        if(!isset($all['order_number']))  $request->merge($param);
-        $detail = $this->oaGetData($menu_id,$request);
-
+        if (!isset($result[0]['children'])) return [true, ''];
+        $result = $result[0];
         try {
             DB::beginTransaction();
+            if ($type == 1) {
+                $log = new OperationLog();
+                $log->user_id = $user['id'];
+                $log->menu_id = $menu_id;
+                $log->crt_time = time();
+                $log->order_number = $data['order_number'];
+                $log->type = $type;
+                $log->save();
+                DB::commit();
+                return [true, ''];
+            }
+            $key_data = [];
+            foreach ($result['children'] as $v) {
+                $key_data[$v['key']] = $v['title'];
+            }
+            $menu_id = $data['menu_id'];
+            $param = isset($data['order_number']) ? ['order_number' => $data['order_number']] : ['id' => $data['id']];
+            $request = request();
+            foreach ($param as $k => $v) {
+                $request->$k = $v;
+            }
+            $request->userData = $user;
+            $all = $request->all();
+            if (!isset($all['order_number'])) $request->merge($param);
+            $detail = $this->oaGetData($menu_id, $request);
             $log = new OperationLog();
             $log->user_id = $user['id'];
             $log->menu_id = $menu_id;
-            $log->order_no = $data['order_number'];
+            $log->order_number = $data['order_number'];
+            $log->crt_time = time();
             $log->type = $type;
             $log->save();
             $id = $log->id;
             $bind_data_detail = [];
-
             //以下是对比逻辑
-            foreach ($data as $k=>$v){
-                if(isset($detail[$k])&&isset($key_data[$k])&&$detail[$k] != $v){
-                    $bind_data_detail[] = [
-                        'log_id' => $id,
-                        'parent_id' => 0,
-                        'old_data' => $v,
-                        'new_data' => $detail[$k],
-                        'title' => $key_data[$k],
-                    ];
+            foreach ($data as $k => $v) {
+
+                if (isset($detail[$k]) && isset($key_data[$k]) ) {
+                    $pattern = '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/';
+                    if (preg_match($pattern, $detail[$k])) {
+                        $detail[$k] = strtotime($detail[$k]);
+                    }
+                    if (preg_match($pattern, $v)) {
+                        $v = strtotime($v);
+                    }
+                    if($v != $detail[$k]){
+
+                        $bind_data_detail[] = [
+                            'log_id' => $id,
+                            'parent_id' => 0,
+                            'new_data' => $v,
+                            'old_data' => $detail[$k],
+                            'title' => $key_data[$k],
+                        ];
+                    }
                 }
             }
             OperationLogDetail::insert($bind_data_detail);
             DB::commit();
-            die('ok');
-            return [true,''];
-        }catch (\Exception $e){
+            return [true, ''];
+        } catch (\Exception $e) {
             DB::rollBack();
-            var_dump($e->getLine().$e->getMessage());die;
-            file_put_contents('log.txt',$e->getLine().$e->getMessage().PHP_EOL,8);
-            return [false,$e->getLine().$e->getMessage()];
+            file_put_contents('log.txt', $e->getLine() . $e->getMessage() . PHP_EOL, 8);
+            return [false, $e->getLine() . $e->getMessage()];
         }
 
 
-
-
     }
 
-    public function oaGetData($menu_id,$request)
+    public function oaGetData($menu_id, $request)
     {
-        $api = SysMenu::where('id',$menu_id)->value('api');
+        $api = SysMenu::where('id', $menu_id)->value('api');
         $path = $this->getMenu();
-        $control = '\\'.$path[$api]["controller"];
+        $control = '\\' . $path[$api]["controller"];
         $act = $path[$api]["act"];
         $new = new $control();
         $detail = $new->$act($request);
@@ -122,14 +173,15 @@ class OperationLogService extends Service
         return $detail['data']['data'][0];
     }
 
-    public function getMenu(){
+    public function getMenu()
+    {
         $app = App();
         $routes = $app->routes->getRoutes();
         $path = [];
         foreach ($routes as $k => $value) {
-            if(!isset($value->action['controller'])) continue;
-            $act = explode('@',$value->action['controller']);
-            if(!isset($act[1])) continue;
+            if (!isset($value->action['controller'])) continue;
+            $act = explode('@', $value->action['controller']);
+            if (!isset($act[1])) continue;
             $path[$value->uri]['act'] = $act[1];
             $path[$value->uri]['controller'] = $act[0];
         }
@@ -137,14 +189,15 @@ class OperationLogService extends Service
         return $path;
     }
 
-    public function insertOperationLog($insert){
+    public function insertOperationLog($insert)
+    {
         try {
             DB::beginTransaction();
             $menu_id = $insert['menu_id'];
             $user_id = $insert['user_id'];
             $ip = $insert['ip'];
             $parent_id = $insert['parent_id'];
-            $type = $insert['type']??2;
+            $type = $insert['type'] ?? 2;
             $logModel = new OperationLog();
             $logModel->user_id = $user_id;
             $logModel->ip = $ip;
@@ -153,62 +206,62 @@ class OperationLogService extends Service
             $logModel->parent_id = $parent_id;
             $logModel->save();
             $LogParent_id = $logModel->id;
-            if($type == 2){
+            if ($type == 2) {
                 $sub_datas = [];
                 $relationship_datas = [];
                 $table_Data = $insert['table_data'];
                 $sub_table_data = $insert['sub_table_data'] ?? [];
                 $relationship_table_data = $insert['relationship_table_data'] ?? [];
                 $table_Data = $this->findTableDatas($table_Data);
-                if(!empty($sub_table_data)) {
+                if (!empty($sub_table_data)) {
 
-                    foreach ($sub_table_data as $v){
+                    foreach ($sub_table_data as $v) {
                         $sub_data = $this->findTableData($v);
-                        $sub_datas = array_merge($sub_datas,$sub_data);
+                        $sub_datas = array_merge($sub_datas, $sub_data);
                     }
 
                 }
-                if(!empty($relationship_table_data)) {
-                    foreach ($sub_table_data as $v){
+                if (!empty($relationship_table_data)) {
+                    foreach ($sub_table_data as $v) {
                         $relationship_data = $this->findTableData($relationship_table_data);
-                        $relationship_datas = array_merge($relationship_datas,$relationship_data);
+                        $relationship_datas = array_merge($relationship_datas, $relationship_data);
                     }
 
                 }
-                $insert_detail_data =  array_merge($table_Data,$sub_datas,$relationship_datas);
-                $table_Data = $this->dealOperationLogDetail($insert_detail_data,$LogParent_id);
+                $insert_detail_data = array_merge($table_Data, $sub_datas, $relationship_datas);
+                $table_Data = $this->dealOperationLogDetail($insert_detail_data, $LogParent_id);
                 OperationLogDetail::insert($table_Data);
             }
 
 
-
-
             DB::commit();
-            return [true,''];
-        }catch (\Exception $e){
+            return [true, ''];
+        } catch (\Exception $e) {
             DB::rollBack();
-            return [false,$e->getLine().':'.$e->getMessage()];
+            return [false, $e->getLine() . ':' . $e->getMessage()];
         }
 
     }
 
-    public function dealOperationLogDetail($data,$log_id){
-        foreach ($data as $k=>$v){
+    public function dealOperationLogDetail($data, $log_id)
+    {
+        foreach ($data as $k => $v) {
             $data[$k]['log_id'] = $log_id;
         }
 
         return $data;
     }
 
-    public function findTableData($data){
+    public function findTableData($data)
+    {
         $table = $data['table'];
         $param = $data['param'];
         $parent_id = $data['parent_id'];
-        $parent_key = $data['parent_key']??'id';
-        $model = DB::table($table)->where($parent_key,$parent_id);
+        $parent_key = $data['parent_key'] ?? 'id';
+        $model = DB::table($table)->where($parent_key, $parent_id);
         $select_list = [];
         $new_data_list = [];
-        foreach ($param as $v){
+        foreach ($param as $v) {
             $select_list[] = $v['key'];
             $new_data_list[$v['key']] = [
                 'new_data' => $v['value'],
@@ -216,65 +269,66 @@ class OperationLogService extends Service
             ];
         }
         $detail = $model->select($select_list)->first()->toArray();
-        foreach ($new_data_list as $k=>$v){
+        foreach ($new_data_list as $k => $v) {
             $new_data_list[$k]['old_data'] = $detail[$k];
         }
         sort($new_data_list);
         return $new_data_list;
     }
 
-    public function findTableDatas($data){
+    public function findTableDatas($data)
+    {
         $table = $data['table'];
         $parent_id = $data['parent_id'];
-        $parent_key = $data['parent_key']??'id';
+        $parent_key = $data['parent_key'] ?? 'id';
         $key = $data['key'];
         $value = $data['value'];
-        $model = DB::table($table)->where($parent_key,$parent_id);
+        $model = DB::table($table)->where($parent_key, $parent_id);
 
-        $old_data = implode(',',$model->where($parent_key,$parent_id)->pluck($key)->toArray());
-        return [['old_data'=>$old_data,'new_data'=>implode(',',$value),'parent_id'=>$parent_id]];
+        $old_data = implode(',', $model->where($parent_key, $parent_id)->pluck($key)->toArray());
+        return [['old_data' => $old_data, 'new_data' => implode(',', $value), 'parent_id' => $parent_id]];
     }
 
     public function setParam()
     {
         //3种格式类型,1:单张表数据更新,只需要数组种有key,value;2.子表更新,也是单条数据更新只需要数组种有key,value;3.子表更新,但是是删除数据更新,则传old data 和 new data
         $insert = [
-                'menu_id'=>1,
-                'user_id'=>'1',
-                'ip'=>'1',
-                'parent_id'=>'1',
-                'table_data'=>[
-                    'table'=>'table',
-                    'parent_id'=>'1',
-                    'param'=>[
+            'menu_id' => 1,
+            'user_id' => '1',
+            'ip' => '1',
+            'parent_id' => '1',
+            'table_data' => [
+                'table' => 'table',
+                'parent_id' => '1',
+                'param' => [
+                    [
+                        'key' => 'd',
+                        'value' => 'd',
+                    ]
+                ]
+            ],
+            'sub_table_data' => [
+                [
+                    'table' => 'table',
+                    'parent_id' => '1',
+                    'parent_key' => '1',
+                    'param' => [
                         [
                             'key' => 'd',
                             'value' => 'd',
                         ]
-                    ]
-                ],
-                'sub_table_data'=>[
-                    [
-                        'table'=>'table',
-                        'parent_id'=>'1',
-                        'parent_key'=>'1',
-                        'param'=>[
-                            [
-                                'key' => 'd',
-                                'value' => 'd',
-                            ]
-                        ],
-                    ]
-                ],
-                'relationship_table_data'=>[
-                    [
-                        'table'=>'table',
-                        'parent_id'=>'table',
-                        'parent_key'=>'table',
-                        'key'=>'title',
-                        'value'=>[1,2,3,4,5],
-                    ]
+                    ],
                 ]
+            ],
+            'relationship_table_data' => [
+                [
+                    'table' => 'table',
+                    'parent_id' => 'table',
+                    'parent_key' => 'table',
+                    'key' => 'title',
+                    'value' => [1, 2, 3, 4, 5],
+                ]
+            ]
         ];
     }
 }