OperationLogService.php 5.9 KB

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