OperationLogService.php 5.4 KB

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