OperationLogService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace App\Service;
  3. use App\Model\OperationLog;
  4. use App\Model\OperationLogDetail;
  5. use App\Model\SysMenu;
  6. use Illuminate\Support\Facades\DB;
  7. class OperationLogService extends Service
  8. {
  9. protected static $instance;
  10. public static function getInstance(): self
  11. {
  12. if (self::$instance == null) {
  13. self::$instance = new OperationLogService();
  14. }
  15. return self::$instance;
  16. }
  17. public function getOperationList($data){
  18. $data = [
  19. [
  20. 'type' => 1,
  21. 'user_id' => 1,
  22. 'user_name' => 1,
  23. 'crt_time' => 123456,
  24. 'data' => [
  25. [
  26. 'key' => '金额',
  27. 'old_data' => '100',
  28. 'new_data' => '200',
  29. ]
  30. ]
  31. ]
  32. ];
  33. return [true,$data];
  34. }
  35. // public function
  36. public function setOperationList($data,$user,$type=1,$menu_id=18){
  37. file_put_contents('log.txt',json_encode($data).PHP_EOL,8);
  38. //获取oa参数
  39. $key = 'menu_id'; // 要匹配的键
  40. $value = $menu_id; // 要匹配的值
  41. $result = array_filter( config('oa'), function($array) use ($key, $value) {
  42. return $array[$key] == $value;
  43. });
  44. if(!isset($result['children'])) return [true,''];
  45. $key_data = [];
  46. foreach ($result['children'] as $v){
  47. $key_data[$v['key']] = $v['title'];
  48. }
  49. $menu_id = $data['menu_id'];
  50. $param = isset($data['order_number']) ? ['order_number'=>$data['order_number']]:['id'=>$data['id']];
  51. $request = request();
  52. foreach ($param as $k=>$v){
  53. $request->$k = $v;
  54. }
  55. $all = $request->all();
  56. if(!isset($all['order_number'])) $request->merge($param);
  57. $detail = $this->oaGetData($menu_id,$request);
  58. try {
  59. DB::beginTransaction();
  60. $log = new OperationLog();
  61. $log->user_id = $user['id'];
  62. $log->menu_id = $menu_id;
  63. $log->order_no = $data['order_number'];
  64. $log->type = $type;
  65. $log->save();
  66. $id = $log->id;
  67. $bind_data_detail = [];
  68. //以下是对比逻辑
  69. foreach ($data as $k=>$v){
  70. if(isset($detail[$k])&&isset($key_data[$k])&&$detail[$k] != $v){
  71. $bind_data_detail[] = [
  72. 'log_id' => $id,
  73. 'parent_id' => 0,
  74. 'old_data' => $v,
  75. 'new_data' => $detail[$k],
  76. 'title' => $key_data[$k],
  77. ];
  78. }
  79. }
  80. OperationLogDetail::insert($bind_data_detail);
  81. DB::commit();
  82. die('ok');
  83. return [true,''];
  84. }catch (\Exception $e){
  85. DB::rollBack();
  86. var_dump($e->getLine().$e->getMessage());die;
  87. file_put_contents('log.txt',$e->getLine().$e->getMessage().PHP_EOL,8);
  88. return [false,$e->getLine().$e->getMessage()];
  89. }
  90. }
  91. public function oaGetData($menu_id,$request)
  92. {
  93. $api = SysMenu::where('id',$menu_id)->value('api');
  94. $path = $this->getMenu();
  95. $control = '\\'.$path[$api]["controller"];
  96. $act = $path[$api]["act"];
  97. $new = new $control();
  98. $detail = $new->$act($request);
  99. // if(!isset($detail['data']['data'][0])) $detail['data']['data'][0] = $detail['data'];
  100. return $detail['data']['data'][0];
  101. }
  102. public function getMenu(){
  103. $app = App();
  104. $routes = $app->routes->getRoutes();
  105. $path = [];
  106. foreach ($routes as $k => $value) {
  107. if(!isset($value->action['controller'])) continue;
  108. $act = explode('@',$value->action['controller']);
  109. if(!isset($act[1])) continue;
  110. $path[$value->uri]['act'] = $act[1];
  111. $path[$value->uri]['controller'] = $act[0];
  112. }
  113. return $path;
  114. }
  115. public function insertOperationLog($insert){
  116. try {
  117. DB::beginTransaction();
  118. $menu_id = $insert['menu_id'];
  119. $user_id = $insert['user_id'];
  120. $ip = $insert['ip'];
  121. $parent_id = $insert['parent_id'];
  122. $type = $insert['type']??2;
  123. $logModel = new OperationLog();
  124. $logModel->user_id = $user_id;
  125. $logModel->ip = $ip;
  126. $logModel->menu_id = $menu_id;
  127. $logModel->type = $type;
  128. $logModel->parent_id = $parent_id;
  129. $logModel->save();
  130. $LogParent_id = $logModel->id;
  131. if($type == 2){
  132. $sub_datas = [];
  133. $relationship_datas = [];
  134. $table_Data = $insert['table_data'];
  135. $sub_table_data = $insert['sub_table_data'] ?? [];
  136. $relationship_table_data = $insert['relationship_table_data'] ?? [];
  137. $table_Data = $this->findTableDatas($table_Data);
  138. if(!empty($sub_table_data)) {
  139. foreach ($sub_table_data as $v){
  140. $sub_data = $this->findTableData($v);
  141. $sub_datas = array_merge($sub_datas,$sub_data);
  142. }
  143. }
  144. if(!empty($relationship_table_data)) {
  145. foreach ($sub_table_data as $v){
  146. $relationship_data = $this->findTableData($relationship_table_data);
  147. $relationship_datas = array_merge($relationship_datas,$relationship_data);
  148. }
  149. }
  150. $insert_detail_data = array_merge($table_Data,$sub_datas,$relationship_datas);
  151. $table_Data = $this->dealOperationLogDetail($insert_detail_data,$LogParent_id);
  152. OperationLogDetail::insert($table_Data);
  153. }
  154. DB::commit();
  155. return [true,''];
  156. }catch (\Exception $e){
  157. DB::rollBack();
  158. return [false,$e->getLine().':'.$e->getMessage()];
  159. }
  160. }
  161. public function dealOperationLogDetail($data,$log_id){
  162. foreach ($data as $k=>$v){
  163. $data[$k]['log_id'] = $log_id;
  164. }
  165. return $data;
  166. }
  167. public function findTableData($data){
  168. $table = $data['table'];
  169. $param = $data['param'];
  170. $parent_id = $data['parent_id'];
  171. $parent_key = $data['parent_key']??'id';
  172. $model = DB::table($table)->where($parent_key,$parent_id);
  173. $select_list = [];
  174. $new_data_list = [];
  175. foreach ($param as $v){
  176. $select_list[] = $v['key'];
  177. $new_data_list[$v['key']] = [
  178. 'new_data' => $v['value'],
  179. 'parent_id' => $parent_id,
  180. ];
  181. }
  182. $detail = $model->select($select_list)->first()->toArray();
  183. foreach ($new_data_list as $k=>$v){
  184. $new_data_list[$k]['old_data'] = $detail[$k];
  185. }
  186. sort($new_data_list);
  187. return $new_data_list;
  188. }
  189. public function findTableDatas($data){
  190. $table = $data['table'];
  191. $parent_id = $data['parent_id'];
  192. $parent_key = $data['parent_key']??'id';
  193. $key = $data['key'];
  194. $value = $data['value'];
  195. $model = DB::table($table)->where($parent_key,$parent_id);
  196. $old_data = implode(',',$model->where($parent_key,$parent_id)->pluck($key)->toArray());
  197. return [['old_data'=>$old_data,'new_data'=>implode(',',$value),'parent_id'=>$parent_id]];
  198. }
  199. public function setParam()
  200. {
  201. //3种格式类型,1:单张表数据更新,只需要数组种有key,value;2.子表更新,也是单条数据更新只需要数组种有key,value;3.子表更新,但是是删除数据更新,则传old data 和 new data
  202. $insert = [
  203. 'menu_id'=>1,
  204. 'user_id'=>'1',
  205. 'ip'=>'1',
  206. 'parent_id'=>'1',
  207. 'table_data'=>[
  208. 'table'=>'table',
  209. 'parent_id'=>'1',
  210. 'param'=>[
  211. [
  212. 'key' => 'd',
  213. 'value' => 'd',
  214. ]
  215. ]
  216. ],
  217. 'sub_table_data'=>[
  218. [
  219. 'table'=>'table',
  220. 'parent_id'=>'1',
  221. 'parent_key'=>'1',
  222. 'param'=>[
  223. [
  224. 'key' => 'd',
  225. 'value' => 'd',
  226. ]
  227. ],
  228. ]
  229. ],
  230. 'relationship_table_data'=>[
  231. [
  232. 'table'=>'table',
  233. 'parent_id'=>'table',
  234. 'parent_key'=>'table',
  235. 'key'=>'title',
  236. 'value'=>[1,2,3,4,5],
  237. ]
  238. ]
  239. ];
  240. }
  241. }