OperationLogService.php 8.3 KB

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