OperationLogService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\OperationLog;
  5. use App\Model\OperationLogDetail;
  6. use App\Model\SysMenu;
  7. use Illuminate\Support\Facades\DB;
  8. class OperationLogService extends Service
  9. {
  10. protected static $instance;
  11. public static function getInstance(): self
  12. {
  13. if (self::$instance == null) {
  14. self::$instance = new OperationLogService();
  15. }
  16. return self::$instance;
  17. }
  18. public function getOperationList($data)
  19. {
  20. // if(!isset($data['order_number'])) return [false,'订单号不存在!'];
  21. // $data = [
  22. // [
  23. // 'type' => 1,
  24. // 'user_id' => 1,
  25. // 'user_name' => 1,
  26. // 'crt_time' => 123456,
  27. // 'data' => [
  28. // [
  29. // 'key' => '金额',
  30. // 'old_data' => '100',
  31. // 'new_data' => '200',
  32. // ]
  33. //
  34. // ]
  35. // ]
  36. // ];
  37. $list = OperationLog::where('order_number', $data['order_number'])->select('*')->get()->toArray();
  38. $employee_key_list = Employee::pluck('emp_name', 'id')->toArray();
  39. $log_ids = [];
  40. $log_list = [];
  41. foreach ($list as $v) {
  42. $log_ids[] = $v['id'];
  43. $log_list[] = [
  44. 'type' => $v['type'],
  45. 'id' => $v['id'],
  46. 'user_id' => $v['user_id'],
  47. 'user_name' => $employee_key_list[$v['user_id']],
  48. 'crt_time' => date('Y-m-d H:i:s', $v['crt_time']),
  49. ];
  50. }
  51. $detail_list = OperationLogDetail::wherein('log_id', $log_ids)->select('*')->get()->toArray();
  52. $detail_data_list = [];
  53. foreach ($detail_list as $v) {
  54. if(is_numeric($v['old_data'])&&strlen($v['old_data']) === 10) $v['old_data'] = date('Y-m-d H:i:s',$v['old_data']);
  55. if(is_numeric($v['new_data'])&&strlen($v['new_data']) === 10) $v['new_data'] = date('Y-m-d H:i:s',$v['new_data']);
  56. $detail_data_list[$v['log_id']][] = [
  57. 'key' => $v['title'],
  58. 'old_data' => $v['old_data'],
  59. 'new_data' => $v['new_data'],
  60. ];
  61. }
  62. foreach ($log_list as &$v) {
  63. $data = $detail_data_list[$v['id']] ?? [];
  64. $v['data'] = $data;
  65. }
  66. return [true, $log_list];
  67. }
  68. // public function
  69. public function setOperationList($data, $user, $type = 1, $menu_id = 18)
  70. {
  71. file_put_contents('log.txt', json_encode($data) . PHP_EOL, 8);
  72. //获取oa参数
  73. $key = 'menu_id'; // 要匹配的键
  74. $value = $menu_id; // 要匹配的值
  75. $result = array_filter(config('oa'), function ($array) use ($key, $value) {
  76. return $array[$key] == $value;
  77. });
  78. if (!isset($result[0]['children'])) return [true, ''];
  79. $result = $result[0];
  80. try {
  81. DB::beginTransaction();
  82. if ($type == 1) {
  83. $log = new OperationLog();
  84. $log->user_id = $user['id'];
  85. $log->menu_id = $menu_id;
  86. $log->crt_time = time();
  87. $log->order_number = $data['order_number'];
  88. $log->type = $type;
  89. $log->save();
  90. DB::commit();
  91. return [true, ''];
  92. }
  93. $key_data = [];
  94. foreach ($result['children'] as $v) {
  95. $key_data[$v['key']] = $v['title'];
  96. }
  97. $menu_id = $data['menu_id'];
  98. $param = isset($data['order_number']) ? ['order_number' => $data['order_number']] : ['id' => $data['id']];
  99. $request = request();
  100. foreach ($param as $k => $v) {
  101. $request->$k = $v;
  102. }
  103. $request->userData = $user;
  104. $all = $request->all();
  105. if (!isset($all['order_number'])) $request->merge($param);
  106. $detail = $this->oaGetData($menu_id, $request);
  107. $log = new OperationLog();
  108. $log->user_id = $user['id'];
  109. $log->menu_id = $menu_id;
  110. $log->order_number = $data['order_number'];
  111. $log->crt_time = time();
  112. $log->type = $type;
  113. $log->save();
  114. $id = $log->id;
  115. $bind_data_detail = [];
  116. //以下是对比逻辑
  117. foreach ($data as $k => $v) {
  118. if (isset($detail[$k]) && isset($key_data[$k]) ) {
  119. $pattern = '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/';
  120. if (preg_match($pattern, $detail[$k])) {
  121. $detail[$k] = strtotime($detail[$k]);
  122. }
  123. if (preg_match($pattern, $v)) {
  124. $v = strtotime($v);
  125. }
  126. if($v != $detail[$k]){
  127. $bind_data_detail[] = [
  128. 'log_id' => $id,
  129. 'parent_id' => 0,
  130. 'new_data' => $v,
  131. 'old_data' => $detail[$k],
  132. 'title' => $key_data[$k],
  133. ];
  134. }
  135. }
  136. }
  137. OperationLogDetail::insert($bind_data_detail);
  138. DB::commit();
  139. return [true, ''];
  140. } catch (\Exception $e) {
  141. DB::rollBack();
  142. file_put_contents('log.txt', $e->getLine() . $e->getMessage() . PHP_EOL, 8);
  143. return [false, $e->getLine() . $e->getMessage()];
  144. }
  145. }
  146. public function oaGetData($menu_id, $request)
  147. {
  148. $api = SysMenu::where('id', $menu_id)->value('api');
  149. $path = $this->getMenu();
  150. $control = '\\' . $path[$api]["controller"];
  151. $act = $path[$api]["act"];
  152. $new = new $control();
  153. $detail = $new->$act($request);
  154. // if(!isset($detail['data']['data'][0])) $detail['data']['data'][0] = $detail['data'];
  155. return $detail['data']['data'][0];
  156. }
  157. public function getMenu()
  158. {
  159. $app = App();
  160. $routes = $app->routes->getRoutes();
  161. $path = [];
  162. foreach ($routes as $k => $value) {
  163. if (!isset($value->action['controller'])) continue;
  164. $act = explode('@', $value->action['controller']);
  165. if (!isset($act[1])) continue;
  166. $path[$value->uri]['act'] = $act[1];
  167. $path[$value->uri]['controller'] = $act[0];
  168. }
  169. return $path;
  170. }
  171. public function insertOperationLog($insert)
  172. {
  173. try {
  174. DB::beginTransaction();
  175. $menu_id = $insert['menu_id'];
  176. $user_id = $insert['user_id'];
  177. $ip = $insert['ip'];
  178. $parent_id = $insert['parent_id'];
  179. $type = $insert['type'] ?? 2;
  180. $logModel = new OperationLog();
  181. $logModel->user_id = $user_id;
  182. $logModel->ip = $ip;
  183. $logModel->menu_id = $menu_id;
  184. $logModel->type = $type;
  185. $logModel->parent_id = $parent_id;
  186. $logModel->save();
  187. $LogParent_id = $logModel->id;
  188. if ($type == 2) {
  189. $sub_datas = [];
  190. $relationship_datas = [];
  191. $table_Data = $insert['table_data'];
  192. $sub_table_data = $insert['sub_table_data'] ?? [];
  193. $relationship_table_data = $insert['relationship_table_data'] ?? [];
  194. $table_Data = $this->findTableDatas($table_Data);
  195. if (!empty($sub_table_data)) {
  196. foreach ($sub_table_data as $v) {
  197. $sub_data = $this->findTableData($v);
  198. $sub_datas = array_merge($sub_datas, $sub_data);
  199. }
  200. }
  201. if (!empty($relationship_table_data)) {
  202. foreach ($sub_table_data as $v) {
  203. $relationship_data = $this->findTableData($relationship_table_data);
  204. $relationship_datas = array_merge($relationship_datas, $relationship_data);
  205. }
  206. }
  207. $insert_detail_data = array_merge($table_Data, $sub_datas, $relationship_datas);
  208. $table_Data = $this->dealOperationLogDetail($insert_detail_data, $LogParent_id);
  209. OperationLogDetail::insert($table_Data);
  210. }
  211. DB::commit();
  212. return [true, ''];
  213. } catch (\Exception $e) {
  214. DB::rollBack();
  215. return [false, $e->getLine() . ':' . $e->getMessage()];
  216. }
  217. }
  218. public function dealOperationLogDetail($data, $log_id)
  219. {
  220. foreach ($data as $k => $v) {
  221. $data[$k]['log_id'] = $log_id;
  222. }
  223. return $data;
  224. }
  225. public function findTableData($data)
  226. {
  227. $table = $data['table'];
  228. $param = $data['param'];
  229. $parent_id = $data['parent_id'];
  230. $parent_key = $data['parent_key'] ?? 'id';
  231. $model = DB::table($table)->where($parent_key, $parent_id);
  232. $select_list = [];
  233. $new_data_list = [];
  234. foreach ($param as $v) {
  235. $select_list[] = $v['key'];
  236. $new_data_list[$v['key']] = [
  237. 'new_data' => $v['value'],
  238. 'parent_id' => $parent_id,
  239. ];
  240. }
  241. $detail = $model->select($select_list)->first()->toArray();
  242. foreach ($new_data_list as $k => $v) {
  243. $new_data_list[$k]['old_data'] = $detail[$k];
  244. }
  245. sort($new_data_list);
  246. return $new_data_list;
  247. }
  248. public function findTableDatas($data)
  249. {
  250. $table = $data['table'];
  251. $parent_id = $data['parent_id'];
  252. $parent_key = $data['parent_key'] ?? 'id';
  253. $key = $data['key'];
  254. $value = $data['value'];
  255. $model = DB::table($table)->where($parent_key, $parent_id);
  256. $old_data = implode(',', $model->where($parent_key, $parent_id)->pluck($key)->toArray());
  257. return [['old_data' => $old_data, 'new_data' => implode(',', $value), 'parent_id' => $parent_id]];
  258. }
  259. public function setParam()
  260. {
  261. //3种格式类型,1:单张表数据更新,只需要数组种有key,value;2.子表更新,也是单条数据更新只需要数组种有key,value;3.子表更新,但是是删除数据更新,则传old data 和 new data
  262. $insert = [
  263. 'menu_id' => 1,
  264. 'user_id' => '1',
  265. 'ip' => '1',
  266. 'parent_id' => '1',
  267. 'table_data' => [
  268. 'table' => 'table',
  269. 'parent_id' => '1',
  270. 'param' => [
  271. [
  272. 'key' => 'd',
  273. 'value' => 'd',
  274. ]
  275. ]
  276. ],
  277. 'sub_table_data' => [
  278. [
  279. 'table' => 'table',
  280. 'parent_id' => '1',
  281. 'parent_key' => '1',
  282. 'param' => [
  283. [
  284. 'key' => 'd',
  285. 'value' => 'd',
  286. ]
  287. ],
  288. ]
  289. ],
  290. 'relationship_table_data' => [
  291. [
  292. 'table' => 'table',
  293. 'parent_id' => 'table',
  294. 'parent_key' => 'table',
  295. 'key' => 'title',
  296. 'value' => [1, 2, 3, 4, 5],
  297. ]
  298. ]
  299. ];
  300. }
  301. }