OperationLogService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Service;
  3. use App\Model\OperationLog;
  4. use App\Model\OperationLogDetail;
  5. use Illuminate\Support\Facades\DB;
  6. class OperationLogService extends Service
  7. {
  8. protected static $instance;
  9. public static function getInstance(): self
  10. {
  11. if (self::$instance == null) {
  12. self::$instance = new OperationLogService();
  13. }
  14. return self::$instance;
  15. }
  16. public function insertOperationLog($insert){
  17. try {
  18. DB::beginTransaction();
  19. $menu_id = $insert['menu_id'];
  20. $user_id = $insert['user_id'];
  21. $ip = $insert['ip'];
  22. $parent_id = $insert['parent_id'];
  23. $type = $insert['type']??2;
  24. $logModel = new OperationLog();
  25. $logModel->user_id = $user_id;
  26. $logModel->ip = $ip;
  27. $logModel->menu_id = $menu_id;
  28. $logModel->type = $type;
  29. $logModel->parent_id = $parent_id;
  30. $logModel->save();
  31. $LogParent_id = $logModel->id;
  32. if($type == 2){
  33. $sub_datas = [];
  34. $relationship_datas = [];
  35. $table_Data = $insert['table_data'];
  36. $sub_table_data = $insert['sub_table_data'] ?? [];
  37. $relationship_table_data = $insert['relationship_table_data'] ?? [];
  38. $table_Data = $this->findTableDatas($table_Data);
  39. if(!empty($sub_table_data)) {
  40. foreach ($sub_table_data as $v){
  41. $sub_data = $this->findTableData($v);
  42. $sub_datas = array_merge($sub_datas,$sub_data);
  43. }
  44. }
  45. if(!empty($relationship_table_data)) {
  46. foreach ($sub_table_data as $v){
  47. $relationship_data = $this->findTableData($relationship_table_data);
  48. $relationship_datas = array_merge($relationship_datas,$relationship_data);
  49. }
  50. }
  51. $insert_detail_data = array_merge($table_Data,$sub_datas,$relationship_datas);
  52. $table_Data = $this->dealOperationLogDetail($insert_detail_data,$LogParent_id);
  53. OperationLogDetail::insert($table_Data);
  54. }
  55. DB::commit();
  56. return [true,''];
  57. }catch (\Exception $e){
  58. DB::rollBack();
  59. return [false,$e->getLine().':'.$e->getMessage()];
  60. }
  61. }
  62. public function dealOperationLogDetail($data,$log_id){
  63. foreach ($data as $k=>$v){
  64. $data[$k]['log_id'] = $log_id;
  65. }
  66. return $data;
  67. }
  68. public function findTableData($data){
  69. $table = $data['table'];
  70. $param = $data['param'];
  71. $parent_id = $data['parent_id'];
  72. $parent_key = $data['parent_key']??'id';
  73. $model = DB::table($table)->where($parent_key,$parent_id);
  74. $select_list = [];
  75. $new_data_list = [];
  76. foreach ($param as $v){
  77. $select_list[] = $v['key'];
  78. $new_data_list[$v['key']] = [
  79. 'new_data' => $v['value'],
  80. 'parent_id' => $parent_id,
  81. ];
  82. }
  83. $detail = $model->select($select_list)->first()->toArray();
  84. foreach ($new_data_list as $k=>$v){
  85. $new_data_list[$k]['old_data'] = $detail[$k];
  86. }
  87. sort($new_data_list);
  88. return $new_data_list;
  89. }
  90. public function findTableDatas($data){
  91. $table = $data['table'];
  92. $parent_id = $data['parent_id'];
  93. $parent_key = $data['parent_key']??'id';
  94. $key = $data['key'];
  95. $value = $data['value'];
  96. $model = DB::table($table)->where($parent_key,$parent_id);
  97. $old_data = implode(',',$model->where($parent_key,$parent_id)->pluck($key)->toArray());
  98. return [['old_data'=>$old_data,'new_data'=>implode(',',$value),'parent_id'=>$parent_id]];
  99. }
  100. public function setParam()
  101. {
  102. //3种格式类型,1:单张表数据更新,只需要数组种有key,value;2.子表更新,也是单条数据更新只需要数组种有key,value;3.子表更新,但是是删除数据更新,则传old data 和 new data
  103. $insert = [
  104. 'menu_id'=>1,
  105. 'user_id'=>'1',
  106. 'ip'=>'1',
  107. 'parent_id'=>'1',
  108. 'table_data'=>[
  109. 'table'=>'table',
  110. 'parent_id'=>'1',
  111. 'param'=>[
  112. [
  113. 'key' => 'd',
  114. 'value' => 'd',
  115. ]
  116. ]
  117. ],
  118. 'sub_table_data'=>[
  119. [
  120. 'table'=>'table',
  121. 'parent_id'=>'1',
  122. 'parent_key'=>'1',
  123. 'param'=>[
  124. [
  125. 'key' => 'd',
  126. 'value' => 'd',
  127. ]
  128. ],
  129. ]
  130. ],
  131. 'relationship_table_data'=>[
  132. [
  133. 'table'=>'table',
  134. 'parent_id'=>'table',
  135. 'parent_key'=>'table',
  136. 'key'=>'title',
  137. 'value'=>[1,2,3,4,5],
  138. ]
  139. ]
  140. ];
  141. }
  142. }