| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 | 
							- <?php
 
- namespace App\Service;
 
- use App\Model\Employee;
 
- use App\Model\OperationLog;
 
- use App\Model\OperationLogDetail;
 
- use App\Model\SysMenu;
 
- use Illuminate\Support\Facades\DB;
 
- class OperationLogService extends Service
 
- {
 
-     protected static $instance;
 
-     public static function getInstance(): self
 
-     {
 
-         if (self::$instance == null) {
 
-             self::$instance = new OperationLogService();
 
-         }
 
-         return self::$instance;
 
-     }
 
-     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)
 
-     {
 
-         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) {
 
-             return $array[$key] == $value;
 
-         });
 
-         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_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]) ) {
 
-                     $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();
 
-             return [true, ''];
 
-         } catch (\Exception $e) {
 
-             DB::rollBack();
 
-             file_put_contents('log.txt', $e->getLine() . $e->getMessage() . PHP_EOL, 8);
 
-             return [false, $e->getLine() . $e->getMessage()];
 
-         }
 
-     }
 
-     public function oaGetData($menu_id, $request)
 
-     {
 
-         $api = SysMenu::where('id', $menu_id)->value('api');
 
-         $path = $this->getMenu();
 
-         $control = '\\' . $path[$api]["controller"];
 
-         $act = $path[$api]["act"];
 
-         $new = new $control();
 
-         $detail = $new->$act($request);
 
- //        if(!isset($detail['data']['data'][0])) $detail['data']['data'][0] = $detail['data'];
 
-         return $detail['data']['data'][0];
 
-     }
 
-     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;
 
-             $path[$value->uri]['act'] = $act[1];
 
-             $path[$value->uri]['controller'] = $act[0];
 
-         }
 
-         return $path;
 
-     }
 
-     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;
 
-             $logModel = new OperationLog();
 
-             $logModel->user_id = $user_id;
 
-             $logModel->ip = $ip;
 
-             $logModel->menu_id = $menu_id;
 
-             $logModel->type = $type;
 
-             $logModel->parent_id = $parent_id;
 
-             $logModel->save();
 
-             $LogParent_id = $logModel->id;
 
-             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)) {
 
-                     foreach ($sub_table_data as $v) {
 
-                         $sub_data = $this->findTableData($v);
 
-                         $sub_datas = array_merge($sub_datas, $sub_data);
 
-                     }
 
-                 }
 
-                 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);
 
-                     }
 
-                 }
 
-                 $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) {
 
-             DB::rollBack();
 
-             return [false, $e->getLine() . ':' . $e->getMessage()];
 
-         }
 
-     }
 
-     public function dealOperationLogDetail($data, $log_id)
 
-     {
 
-         foreach ($data as $k => $v) {
 
-             $data[$k]['log_id'] = $log_id;
 
-         }
 
-         return $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);
 
-         $select_list = [];
 
-         $new_data_list = [];
 
-         foreach ($param as $v) {
 
-             $select_list[] = $v['key'];
 
-             $new_data_list[$v['key']] = [
 
-                 'new_data' => $v['value'],
 
-                 'parent_id' => $parent_id,
 
-             ];
 
-         }
 
-         $detail = $model->select($select_list)->first()->toArray();
 
-         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)
 
-     {
 
-         $table = $data['table'];
 
-         $parent_id = $data['parent_id'];
 
-         $parent_key = $data['parent_key'] ?? 'id';
 
-         $key = $data['key'];
 
-         $value = $data['value'];
 
-         $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]];
 
-     }
 
-     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' => [
 
-                     [
 
-                         '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],
 
-                 ]
 
-             ]
 
-         ];
 
-     }
 
- }
 
 
  |