OperationLogService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. $menu_id = $data['menu_id'];
  38. $param = isset($data['order_number']) ? ['order_number'=>$data['order_number']]:['id'=>$data['id']];
  39. $request = request();
  40. foreach ($param as $k=>$v){
  41. $request->$k = $v;
  42. }
  43. $all = $request->all();
  44. if(!isset($all['order_number'])) $request->merge($param);
  45. $detail = $this->oaGetData($menu_id,$request);
  46. $bind_data = [
  47. 'user_id' => $user['id'],
  48. 'menu_id' => $menu_id,
  49. 'type' => $type,
  50. ];
  51. $bind_data_detail = [];
  52. //以下是对比逻辑
  53. foreach ($data as $v){
  54. foreach ($detail as $vv){
  55. }
  56. }
  57. }
  58. public function oaGetData($menu_id,$request)
  59. {
  60. $api = SysMenu::where('id',$menu_id)->value('api');
  61. $path = $this->getMenu();
  62. $control = '\\'.$path[$api]["controller"];
  63. $act = $path[$api]["act"];
  64. $new = new $control();
  65. $detail = $new->$act($request);
  66. // if(!isset($detail['data']['data'][0])) $detail['data']['data'][0] = $detail['data'];
  67. return $detail['data']['data'][0];
  68. }
  69. public function getMenu(){
  70. $app = App();
  71. $routes = $app->routes->getRoutes();
  72. $path = [];
  73. foreach ($routes as $k => $value) {
  74. if(!isset($value->action['controller'])) continue;
  75. $act = explode('@',$value->action['controller']);
  76. if(!isset($act[1])) continue;
  77. $path[$value->uri]['act'] = $act[1];
  78. $path[$value->uri]['controller'] = $act[0];
  79. }
  80. return $path;
  81. }
  82. public function insertOperationLog($insert){
  83. try {
  84. DB::beginTransaction();
  85. $menu_id = $insert['menu_id'];
  86. $user_id = $insert['user_id'];
  87. $ip = $insert['ip'];
  88. $parent_id = $insert['parent_id'];
  89. $type = $insert['type']??2;
  90. $logModel = new OperationLog();
  91. $logModel->user_id = $user_id;
  92. $logModel->ip = $ip;
  93. $logModel->menu_id = $menu_id;
  94. $logModel->type = $type;
  95. $logModel->parent_id = $parent_id;
  96. $logModel->save();
  97. $LogParent_id = $logModel->id;
  98. if($type == 2){
  99. $sub_datas = [];
  100. $relationship_datas = [];
  101. $table_Data = $insert['table_data'];
  102. $sub_table_data = $insert['sub_table_data'] ?? [];
  103. $relationship_table_data = $insert['relationship_table_data'] ?? [];
  104. $table_Data = $this->findTableDatas($table_Data);
  105. if(!empty($sub_table_data)) {
  106. foreach ($sub_table_data as $v){
  107. $sub_data = $this->findTableData($v);
  108. $sub_datas = array_merge($sub_datas,$sub_data);
  109. }
  110. }
  111. if(!empty($relationship_table_data)) {
  112. foreach ($sub_table_data as $v){
  113. $relationship_data = $this->findTableData($relationship_table_data);
  114. $relationship_datas = array_merge($relationship_datas,$relationship_data);
  115. }
  116. }
  117. $insert_detail_data = array_merge($table_Data,$sub_datas,$relationship_datas);
  118. $table_Data = $this->dealOperationLogDetail($insert_detail_data,$LogParent_id);
  119. OperationLogDetail::insert($table_Data);
  120. }
  121. DB::commit();
  122. return [true,''];
  123. }catch (\Exception $e){
  124. DB::rollBack();
  125. return [false,$e->getLine().':'.$e->getMessage()];
  126. }
  127. }
  128. public function dealOperationLogDetail($data,$log_id){
  129. foreach ($data as $k=>$v){
  130. $data[$k]['log_id'] = $log_id;
  131. }
  132. return $data;
  133. }
  134. public function findTableData($data){
  135. $table = $data['table'];
  136. $param = $data['param'];
  137. $parent_id = $data['parent_id'];
  138. $parent_key = $data['parent_key']??'id';
  139. $model = DB::table($table)->where($parent_key,$parent_id);
  140. $select_list = [];
  141. $new_data_list = [];
  142. foreach ($param as $v){
  143. $select_list[] = $v['key'];
  144. $new_data_list[$v['key']] = [
  145. 'new_data' => $v['value'],
  146. 'parent_id' => $parent_id,
  147. ];
  148. }
  149. $detail = $model->select($select_list)->first()->toArray();
  150. foreach ($new_data_list as $k=>$v){
  151. $new_data_list[$k]['old_data'] = $detail[$k];
  152. }
  153. sort($new_data_list);
  154. return $new_data_list;
  155. }
  156. public function findTableDatas($data){
  157. $table = $data['table'];
  158. $parent_id = $data['parent_id'];
  159. $parent_key = $data['parent_key']??'id';
  160. $key = $data['key'];
  161. $value = $data['value'];
  162. $model = DB::table($table)->where($parent_key,$parent_id);
  163. $old_data = implode(',',$model->where($parent_key,$parent_id)->pluck($key)->toArray());
  164. return [['old_data'=>$old_data,'new_data'=>implode(',',$value),'parent_id'=>$parent_id]];
  165. }
  166. public function setParam()
  167. {
  168. //3种格式类型,1:单张表数据更新,只需要数组种有key,value;2.子表更新,也是单条数据更新只需要数组种有key,value;3.子表更新,但是是删除数据更新,则传old data 和 new data
  169. $insert = [
  170. 'menu_id'=>1,
  171. 'user_id'=>'1',
  172. 'ip'=>'1',
  173. 'parent_id'=>'1',
  174. 'table_data'=>[
  175. 'table'=>'table',
  176. 'parent_id'=>'1',
  177. 'param'=>[
  178. [
  179. 'key' => 'd',
  180. 'value' => 'd',
  181. ]
  182. ]
  183. ],
  184. 'sub_table_data'=>[
  185. [
  186. 'table'=>'table',
  187. 'parent_id'=>'1',
  188. 'parent_key'=>'1',
  189. 'param'=>[
  190. [
  191. 'key' => 'd',
  192. 'value' => 'd',
  193. ]
  194. ],
  195. ]
  196. ],
  197. 'relationship_table_data'=>[
  198. [
  199. 'table'=>'table',
  200. 'parent_id'=>'table',
  201. 'parent_key'=>'table',
  202. 'key'=>'title',
  203. 'value'=>[1,2,3,4,5],
  204. ]
  205. ]
  206. ];
  207. }
  208. }