OperationLogService.php 8.7 KB

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