WorkFlowService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\SysMenu;
  5. use App\Model\WorkFlow;
  6. use App\Model\WorkFlowGenerated;
  7. use App\Model\WorkFlowGeneratedSub;
  8. use App\Model\WorkFlowGeneratedSubDetail;
  9. use App\Model\WorkFlowGeneratedSubDetailCondition;
  10. use App\Model\WorkFlowSub;
  11. use App\Model\WorkFlowSubDetail;
  12. use App\Model\WorkFlowSubDetailCondition;
  13. use Illuminate\Support\Facades\DB;
  14. class WorkFlowService extends Service
  15. {
  16. public function orderList($data,$user){
  17. $model = WorkFlow::where('del_time',0)
  18. ->select('title','id','menu_id','crt_time','crt_id')
  19. ->orderby('id', 'desc');
  20. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  21. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  22. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  23. $model->where('crt_time','>=',$return[0]);
  24. $model->where('crt_time','<=',$return[1]);
  25. }
  26. $list = $this->limit($model,'',$data);
  27. $list = $this->fillData($list,$data);
  28. return [true, $list];
  29. }
  30. public function fillData($data,$ergs){
  31. if(empty($data['data'])) return $data;
  32. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  33. ->pluck('emp_name','id')
  34. ->toArray();
  35. $menu = SysMenu::whereIn('id',array_unique(array_column($data['data'],'menu_id')))
  36. ->pluck('title','id')
  37. ->toArray();
  38. foreach ($data['data'] as $key => $value){
  39. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  40. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  41. $data['data'][$key]['menu_title'] = $menu[$value['menu_id']] ?? '';
  42. }
  43. return $data;
  44. }
  45. public function detail($data, $user){
  46. if($this->isEmpty($data,'id')) return [false,'请选择工作流'];
  47. $workFlow = WorkFlow::where('del_time',0)
  48. ->where('id',$data['id'])
  49. ->first();
  50. if(empty($workFlow)) return [false,'工作流不存在或已被删除'];
  51. $workFlow = $workFlow->toArray();
  52. $emp_map = Employee::where('id',$workFlow['crt_id'])
  53. ->pluck('emp_name','id')
  54. ->toArray();
  55. $workFlow['crt_name'] = $emp_map[$workFlow['crt_id']] ?? '';
  56. $workFlow['crt_time'] = $workFlow['crt_time'] ? date("Y-m-d H:i:s", $workFlow['crt_time']): '';
  57. return [true, $workFlow];
  58. }
  59. public function del($data, $user){
  60. if($this->isEmpty($data,'id')) return [false,'请选择工作流'];
  61. $workFlow = WorkFlow::where('del_time',0)
  62. ->where('id',$data['id'])
  63. ->first();
  64. if(empty($workFlow)) return [false,'工作流不存在或已被删除'];
  65. try {
  66. DB::beginTransaction();
  67. $time = time();
  68. $workFlow->del_time = $time;
  69. $workFlow->save();
  70. $this->updateSonModel($data, $time);
  71. DB::commit();
  72. }catch (\Throwable $e){
  73. DB::rollBack();
  74. return [false, $e->getFile() . '|' . $e->getLine() . '|' . $e->getMessage()];
  75. }
  76. return [true, ''];
  77. }
  78. public function EditWorkFlowMenu($data, $user){
  79. list($status, $msg) = $this->EditWorkFlowMenuRule($data);
  80. if(! $status) return [false, $msg];
  81. try{
  82. DB::beginTransaction();
  83. //将前端给到的工作流数据组成多个一维数组
  84. $array = $this->flattenWorkflow($data['nodeConfig']);
  85. //踢掉没用的数据,为了组成树结构
  86. $clear_array = $this->organizationData($array);
  87. //树结构
  88. $tree = $this->makeTree(0, $clear_array);
  89. list($status, $msg) = $this->settleWorkFlowData($data,$array);
  90. if(! $status) return [false, $msg];
  91. list($detail, $detail_son, $array_detail_map) = $msg;
  92. $time = time();
  93. if(! empty($data['id'])){
  94. //更新
  95. $model = WorkFlow::where('id', $data['id'])->first();
  96. $model->title = $data['title'] ?? "";
  97. $model->menu_id = $data['tableId'] ?? 0;
  98. $model->vue_param = json_encode($data);
  99. $model->my_param = json_encode($tree);
  100. $model->save();
  101. //子表更新
  102. $this->updateSonModel($data, $time);
  103. }else{
  104. //添加
  105. $model = new WorkFlow();
  106. $model->title = $data['title'] ?? "";
  107. $model->menu_id = $data['tableId'] ?? 0;
  108. $model->vue_param = json_encode($data);
  109. $model->my_param = json_encode($tree);
  110. $model->crt_id = $user['id'];
  111. $model->save();
  112. $data['id'] = $model->id;
  113. }
  114. //子表写入
  115. $this->insertSonModel($data, $array, $detail, $detail_son, $array_detail_map,$time);
  116. DB::commit();
  117. }catch (\Throwable $exception){
  118. DB::rollBack();
  119. return [false, $exception->getLine() . ' | ' . $exception->getFile() . ' | ' . $exception->getMessage()];
  120. }
  121. return [true, ''];
  122. }
  123. public function insertSonModel($data, $array, $detail, $detail_son, $array_detail_map, $time){
  124. $sub = [];
  125. if(empty($array)) return;
  126. foreach ($array as $value){
  127. $sub[] = [
  128. 'workflow_id' => $data['id'],
  129. 'menu_id' => $data['tableId'],
  130. 'm_id' => $value['id'],
  131. 'crt_time' => $time
  132. ];
  133. }
  134. WorkFlowSub::insert($sub);
  135. //获取上一次插入订单的所有id
  136. $last_insert_id = WorkFlowSub::where('workflow_id', $data['id'])
  137. ->where('menu_id', $data['tableId'])
  138. ->where('crt_time', $time)
  139. ->where('del_time', 0)
  140. ->select('id')->get()->toArray();
  141. $last_insert_id = array_column($last_insert_id,'id');
  142. //组织 detail 数据 做写入
  143. $detail_array = [];
  144. foreach ($sub as $key => $value){
  145. $workflow_sub_id = $last_insert_id[$key];
  146. $keys = $array_detail_map[$key];
  147. if(empty($keys)) continue;
  148. foreach ($keys as $k){
  149. $detail_tmp = $detail[$k] ?? [];
  150. if(empty($detail_tmp)) continue;
  151. $detail_tmp['workflow_id'] = $value['workflow_id'];
  152. $detail_tmp['menu_id'] = $value['menu_id'];
  153. $detail_tmp['workflow_sub_id'] = $workflow_sub_id;
  154. $detail_tmp['crt_time'] = $time;
  155. $detail_array[] = $detail_tmp;
  156. }
  157. }
  158. if(! empty($detail_array)) WorkFlowSubDetail::insert($detail_array);
  159. if(empty($detail_son)) return;
  160. //获取上一次插入订单的所有id
  161. $last_insert_id = WorkFlowSubDetail::where('workflow_id', $data['id'])
  162. ->where('menu_id', $data['tableId'])
  163. ->where('crt_time', $time)
  164. ->where('del_time', 0)
  165. ->select('id')->get()->toArray();
  166. $last_insert_id = array_column($last_insert_id,'id');
  167. //组织 条件数据 做写入
  168. $detail_son_array = [];
  169. foreach ($detail_array as $key => $value){
  170. $workflow_sub_detail_id = $last_insert_id[$key];
  171. $detail_son_tmp = $detail_son[$key] ?? [];
  172. if(empty($detail_son_tmp)) continue;
  173. foreach ($detail_son_tmp as $val){
  174. $val['workflow_id'] = $value['workflow_id'];
  175. $val['menu_id'] = $value['menu_id'];
  176. $val['workflow_sub_id'] = $value['workflow_sub_id'];
  177. $val['workflow_sub_detail_id'] = $workflow_sub_detail_id;
  178. $val['crt_time'] = $value['crt_time'];
  179. $detail_son_array[] = $val;
  180. }
  181. }
  182. if(! empty($detail_son_array)) WorkFlowSubDetailCondition::insert($detail_son_array);
  183. }
  184. public function updateSonModel($data, $time){
  185. WorkFlowSub::where('del_time',0)
  186. ->where('workflow_id', $data['id'])
  187. ->update(['del_time' => $time]);
  188. WorkFlowSubDetail::where('del_time',0)
  189. ->where('workflow_id', $data['id'])
  190. ->update(['del_time' => $time]);
  191. WorkFlowSubDetailCondition::where('del_time',0)
  192. ->where('workflow_id', $data['id'])
  193. ->update(['del_time' => $time]);
  194. }
  195. public function EditWorkFlowMenuRule($data){
  196. // 最外层
  197. // tableId = 菜单id
  198. // flowPermission 发起人 空数组则为所有人 非空 则是指定人
  199. // directorMaxLevel 主管层级 (暂时没用到)
  200. // nodeConfig 节点数据
  201. if(empty($data['tableId'])) return [false, '菜单ID不能为空'];
  202. $menu = SysMenu::where('id', $data['tableId'])->first();
  203. if(empty($menu)) return [false, '菜单不存在或已被删除'];
  204. $menu = $menu->toArray();
  205. if($menu['del_time'] > 0) return [false, '菜单不存在或已被删除'];
  206. if(! isset($data['nodeConfig'])) return [false, '请传入节点参数'];
  207. $id = $data['id'] ?? 0;
  208. $bool = WorkFlow::where('del_time',0)
  209. ->where('menu_id',$data['tableId'])
  210. ->when(! empty($id),function ($query) use ($id){
  211. return $query->where('id', '<>', $id);
  212. })->exists();
  213. if($bool) return [false, '该菜单工作流已创建'];
  214. return [true, ''];
  215. }
  216. //给入节点id 获取节点下所有子集结构
  217. function createIdToNodeMap(array $tree, &$map = []) {
  218. foreach ($tree as $node) {
  219. // 如果节点有ID,则将其添加到映射中
  220. if (isset($node['id'])) {
  221. $map[$node['id']] = $node;
  222. }
  223. // 如果当前节点有子节点,则递归调用以处理子节点
  224. if (isset($node['children']) && is_array($node['children']) && ! empty($node['children'])) {
  225. $this->createIdToNodeMap($node['children'], $map);
  226. }
  227. }
  228. return $map;
  229. }
  230. function organizationData($array){
  231. $return = [];
  232. foreach ($array as $key => $value){
  233. $return[] = [
  234. 'id' => $value['id'],//节点id
  235. 'parent_id' => $value['parent_id'] ?? 0,//0代表无父级
  236. 'type' => $value['type'], // 0 发起人 1审批 2抄送 3条件 4路由
  237. 'priorityLevel' => $value['priorityLevel'] ?? 0,//0代表无优先级
  238. ];
  239. }
  240. return $return;
  241. }
  242. function flattenWorkflow($node, &$result = [], $parentId = null, &$idCounter = 0) {
  243. $idCounter++;
  244. $currentId = $idCounter;
  245. $nodeData = [
  246. 'id' => $currentId,
  247. 'parent_id' => $parentId,
  248. ];
  249. foreach ($node as $key => $value) {
  250. if (!in_array($key, ['childNode', 'conditionNodes'])) {
  251. $nodeData[$key] = $value;
  252. }
  253. }
  254. $result[] = $nodeData;
  255. // echo "Added node: " . $node['nodeName'] . "\n"; // Debugging line
  256. if ($node['type'] == 4 && isset($node['conditionNodes']) && is_array($node['conditionNodes']) && ! empty($node['conditionNodes'])) {
  257. foreach ($node['conditionNodes'] as $conditionNode) {
  258. $this->flattenWorkflow($conditionNode, $result, $currentId, $idCounter);
  259. }
  260. }
  261. if (isset($node['childNode']) && is_array($node['childNode']) && ! empty($node['childNode'])) {
  262. $this->flattenWorkflow($node['childNode'], $result, $currentId, $idCounter);
  263. }
  264. return $result;
  265. }
  266. function settleWorkFlowData($data, $array){
  267. $return = $return_detail = $map = [];
  268. if(empty($array)) return [true, [$return, $return_detail]];
  269. foreach ($array as $key => $value){
  270. $tmp = [
  271. 'm_id' => $value['id'],//节点id 后端生成
  272. 'type' => $value['type'],// 0 发起人 1审批 2抄送 3条件 4路由
  273. 'p_id' => 0,//人员id
  274. 'd_id' => 0,//部门id
  275. 'settype' => $value['settype'] ?? 0,// 审批人设置 1指定成员 2主管 4发起人自选 5发起人自己 7连续多级主管
  276. 'selectMode' => $value['selectMode'] ?? 0,//审批人数 1选一个人 2选多个人
  277. 'selectRange' => $value['selectRange'] ?? 0,//选择范围 1.全公司 2指定成员 2指定角色
  278. 'examineMode' => $value['examineMode'] ?? 0,//多人审批时采用的审批方式 1依次审批 2会签
  279. 'priorityLevel' => $value['priorityLevel'] ?? 0,//优先级
  280. ];
  281. $tmp_return = $return;
  282. if($value['type'] == 0){
  283. //发起人
  284. list($status, $msg) = $this->settleTypeZero($tmp, $data, $return);
  285. if(! $status) return [false, $msg];
  286. }elseif ($value['type'] == 1){
  287. //审批人
  288. list($status, $msg) = $this->settleTypeOne($tmp, $data, $value, $return);
  289. if(! $status) return [false, $msg];
  290. }elseif ($value['type'] == 2){
  291. //抄送人
  292. $this->settleTypeTwo($tmp, $value, $return);
  293. }elseif ($value['type'] == 3){
  294. //条件
  295. $this->settleTypeThree($tmp, $value, $return, $return_detail);
  296. }elseif ($value['type'] == 4){
  297. //路由
  298. $this->settleTypeFour($tmp, $value, $return);
  299. }else{
  300. return [false, '非法参数:节点TYPE'];
  301. }
  302. $differences = array_diff_key($return, $tmp_return);
  303. $keys = array_keys($differences);
  304. $map[$key] = $keys;
  305. }
  306. return [true, [$return, $return_detail,$map]];
  307. }
  308. //发起人
  309. function settleTypeZero($tmp, $data, &$return){
  310. if(empty($data['flowPermission'])) {
  311. //为空 则是 发起人为所有人
  312. $return[] = $tmp;
  313. }else{
  314. foreach ($data['flowPermission'] as $n){
  315. $t = $tmp;
  316. //发起人为指定的人或部门
  317. if($n['type'] == 1){
  318. $t['p_id'] = $n['targetId'];
  319. }elseif($n['type'] == 3){
  320. $t['d_id'] = $n['targetId'];
  321. }else{
  322. return [false, '非法参数:发起人'];
  323. }
  324. $return[] = $t;
  325. }
  326. }
  327. return [true, ''];
  328. }
  329. //审批人
  330. function settleTypeOne($tmp, $data, $value, &$return){
  331. //"settype": "",// 审批人设置 1指定成员 2主管 4发起人自选 5发起人自己 7连续多级主管
  332. //"selectMode": "", //审批人数 1选一个人 2选多个人
  333. //"selectRange": "", //选择范围 1.全公司 2指定成员 2指定角色
  334. //"directorLevel": "", //审批终点 最高层主管数
  335. //"examineMode": "", //多人审批时采用的审批方式 1依次审批 2会签
  336. //"noHanderAction": "",//审批人为空时 1自动审批通过/不允许发起 2转交给审核管理员
  337. //"examineEndDirectorLevel": "", //审批终点 第n层主管
  338. if($value['settype'] == 1){
  339. foreach ($value['nodeUserList'] as $n){
  340. $t = $tmp;
  341. //指定成员
  342. $t['p_id'] = $n['targetId'];
  343. $return[] = $t;
  344. }
  345. }elseif ($value['settype'] == 2){
  346. return [false, '暂不支持主管设置'];
  347. }elseif ($value['settype'] == 4){
  348. if($value['selectMode'] == 1){
  349. if($value['selectRange'] == 1){
  350. //全公司下的一个人
  351. $return[] = $tmp;
  352. }elseif ($value['selectRange'] == 2){
  353. foreach ($value['nodeUserList'] as $n){
  354. $t = $tmp;
  355. //指定人里的一个人
  356. $t['p_id'] = $n['targetId'];
  357. $return[] = $t;
  358. }
  359. }else{
  360. return [false, '非法参数:选择范围'];
  361. }
  362. }elseif ($value['selectMode'] == 2){
  363. if($value['selectRange'] == 1){
  364. //全公司里的所有人
  365. $return[] = $tmp;
  366. }elseif ($value['selectRange'] == 2){
  367. //指定人里的所有人
  368. foreach ($value['nodeUserList'] as $n){
  369. $t = $tmp;
  370. $t['p_id'] = $n['targetId'];
  371. $return[] = $t;
  372. }
  373. }else{
  374. return [false, '非法参数:选择范围'];
  375. }
  376. }
  377. }elseif ($value['settype'] == 5){
  378. //发起人则是触发工作流的那个人
  379. $tmp['selectMode'] = 1;
  380. $tmp['selectRange'] = 2;
  381. $return[] = $tmp;
  382. }elseif ($value['settype'] == 7){
  383. return [false, '暂不支持连续多级主管设置'];
  384. }else{
  385. return [false, '非法参数:审批人设置'];
  386. }
  387. return [true, ''];
  388. }
  389. //抄送人
  390. function settleTypeTwo($tmp, $value, &$return){
  391. foreach ($value['nodeUserList'] as $n){
  392. //抄送人为指定的一些人
  393. $tmp['p_id'] = $n['targetId'];
  394. $return[] = $tmp;
  395. }
  396. }
  397. //条件
  398. function settleTypeThree($tmp, $value, &$return, &$return_detail){
  399. $return[] = $tmp;
  400. $lastIndex = count($return) - 1;
  401. $tt = [];
  402. foreach ($value['conditionList'] as $val){
  403. $tt[] = [
  404. 'column' => $val['columnDbname'],
  405. 'opt1' => $val['opt1'],
  406. 'opt2' => $val['opt2'],
  407. 'opt_type' => $val['optType'],
  408. 'zdy1' => $val['zdy1'],
  409. 'zdy2' => $val['zdy2'],
  410. ];
  411. }
  412. $return_detail[$lastIndex] = $tt;
  413. }
  414. //路由
  415. function settleTypeFour($tmp, $value, &$return){
  416. $return[] = $tmp;
  417. }
  418. //触发工作流并生成
  419. public function create($data, $user){$this->runWorkFlow($data['order_number'], 23);dd(1);
  420. list($status, $msg) = $this->createRule($data, $user);
  421. if(! $status) return [false, $msg];
  422. try {
  423. //生成
  424. list($status, $msg) = $this->createWorkFlowDetail($data, $user);
  425. if(! $status){
  426. DB::rollBack();
  427. return [false, $msg];
  428. }
  429. DB::commit();
  430. } catch (\Exception $e) {
  431. DB::rollBack();
  432. return [false, $e->getFile() . '|' . $e->getLine() . '|' . $e->getMessage()];
  433. }
  434. return [true, ''];
  435. }
  436. public function createRule($data, $user){
  437. if(empty($data['menu_id'])) return [false, '菜单ID不能为空'];
  438. if(empty($data['order_number'])) return [false, 'order_number不能为空'];
  439. if(empty($data['opt_case'])) return [false, 'opt_case不能为空'];
  440. $oa = config('oa');
  441. if(empty($oa)) return [false, '工作流配置未设置,请联系开发者'];
  442. $menu_id = array_column($oa,'menu_id');
  443. if(! in_array($data['menu_id'], $menu_id)) return [false, '该菜单暂不支持工作流'];
  444. return [true, ''];
  445. }
  446. public function createWorkFlowDetail($data, $user){
  447. list($status, $msg) = $this->createWorkFlowDetailRule($data, $user);
  448. if(! $status) {
  449. if(isset($data['is_check'])){
  450. //因为工作流不存在,或者点击的人不触发工作流,所以直接成功返回
  451. list($status, $msg) = $this->callCheck($data, CheckService::TYPE_ONE, $user);
  452. return [$status, $msg];
  453. }
  454. return [false, $msg];
  455. }
  456. //创建审批流根据工作流
  457. list($status, $msg) = $this->createWorkFlow($data, $user, $msg);
  458. if(! $status) return [false, $msg];
  459. return [true, ''];
  460. }
  461. public function createWorkFlowDetailRule($data, $user){
  462. $workFlow = WorkFlow::where('del_time',0)
  463. ->where('menu_id', $data['menu_id'])
  464. ->first();
  465. if(empty($workFlow)) return [false, [-1, '菜单工作流不存在、已被删除或未新建']];
  466. $workFlow = $workFlow->toArray();
  467. //判断是否创建工作流
  468. list($status, $msg) = $this->getStartWorkFlow($workFlow['id'], $user);
  469. if(! $status) return [false, $msg];
  470. return [true, $workFlow];
  471. }
  472. //获取工作流的开始 判断是否激活审批流
  473. public function getStartWorkFlow($workFlowId = 0, $user){
  474. //用户的部门
  475. $depart_user = $user['depart_range'] ?? [];
  476. //工作流开始节点数据
  477. $list = WorkFlowSubDetail::where('del_time',0)
  478. ->where('workflow_id',$workFlowId)
  479. ->where('type', WorkFlowSubDetail::type_zero)
  480. ->get()->toArray();
  481. //工作流的部门 人
  482. $workflow_depart = $workflow_man = [];
  483. foreach ($list as $value){
  484. if($value['p_id'] > 0 && ! in_array($value['p_id'], $workflow_man)) $workflow_man[] = $value['p_id'];
  485. if($value['d_id'] > 0 && ! in_array($value['d_id'], $workflow_depart)) $workflow_depart[] = $value['d_id'];
  486. }
  487. //所有人都能触发审批流
  488. if(empty($workflow_depart) && empty($workflow_man)) return [true, ''];
  489. if(! empty($workflow_depart)){
  490. $d = array_intersect($depart_user, $workflow_depart);
  491. if(! empty($d)) return [true, ''];
  492. }
  493. if(! empty($workflow_man)){
  494. $p = array_intersect([$user['id']], $workflow_man);
  495. if(! empty($p)) return [true, ''];
  496. }
  497. return [false, [-1, '触发人不在工作流发起者内']];
  498. }
  499. //创建审批流根据工作流
  500. public function createWorkFlow($data, $user, $workFlow){
  501. try {
  502. DB::beginTransaction();
  503. $time = time();
  504. $model = new WorkFlowGenerated();
  505. $model->title = $workFlow['title'];
  506. $model->menu_id = $workFlow['menu_id'];
  507. $model->vue_param = $workFlow['vue_param'];
  508. $model->my_param = $workFlow['my_param'];
  509. $model->order_number = $data['order_number'];
  510. $model->crt_id = $user['id'];
  511. $model->save();
  512. $id = $model->id;
  513. $this->insertSonModel2($workFlow['id'], $id, $time);
  514. DB::commit();
  515. }catch (\Throwable $e){
  516. DB::rollBack();
  517. if (str_contains($e->getMessage(), '1062') || str_contains($e->getMessage(), 'Duplicate entry')) {
  518. return [false, '审批流程已存在'];
  519. }
  520. return [false, $e->getFile() . '|' . $e->getLine() . '|' . $e->getMessage()];
  521. }
  522. return [true, $id];
  523. }
  524. public function insertSonModel2($workFlowId = 0, $workFlowGeneratedId = 0, $time){
  525. $insert = [];
  526. $workFlowSub = WorkFlowSub::where('del_time',0)
  527. ->where('workflow_id', $workFlowId)
  528. ->select('id','menu_id','m_id')
  529. ->get()->toArray();
  530. $keys = [];
  531. foreach ($workFlowSub as $value){
  532. $insert[] = [
  533. 'menu_id' => $value['menu_id'],
  534. 'm_id' => $value['m_id'],
  535. 'workflow_generated_id' => $workFlowGeneratedId,
  536. 'crt_time' => $time,
  537. ];
  538. $keys[] = $value['id'];
  539. }
  540. WorkFlowGeneratedSub::insert($insert);
  541. //获取上一次插入订单的所有id
  542. $last_insert_id = WorkFlowGeneratedSub::where('workflow_generated_id', $workFlowGeneratedId)
  543. ->where('crt_time', $time)
  544. ->where('del_time', 0)
  545. ->select('id')->get()->toArray();
  546. $last_insert_id = array_column($last_insert_id,'id');
  547. $map = array_combine($keys, $last_insert_id);
  548. $workFlowSubDetail = WorkFlowSubDetail::where('del_time',0)
  549. ->where('workflow_id', $workFlowId)
  550. ->select('id','menu_id','m_id','workflow_sub_id','type','p_id','d_id','settype','selectMode','selectRange','examineMode','priorityLevel')
  551. ->get()->toArray();
  552. $insert2 = [];
  553. $keys = [];
  554. foreach ($workFlowSubDetail as $value){
  555. $keys[] = $value['id'];
  556. $workflow_generated_sub_id = $map[$value['workflow_sub_id']] ?? 0;
  557. unset($value['id']);unset($value['workflow_sub_id']);
  558. $value['workflow_generated_id'] = $workFlowGeneratedId;
  559. $value['workflow_generated_sub_id'] = $workflow_generated_sub_id;
  560. $value['crt_time'] = $time;
  561. $insert2[] = $value;
  562. }
  563. WorkFlowGeneratedSubDetail::insert($insert2);
  564. //获取上一次插入订单的所有id
  565. $last_insert_id = WorkFlowGeneratedSubDetail::where('workflow_generated_id', $workFlowGeneratedId)
  566. ->where('crt_time', $time)
  567. ->where('del_time', 0)
  568. ->get()->toArray();
  569. $last_map = array_column($last_insert_id,null,'id');
  570. $last_insert_id = array_column($last_insert_id,'id');
  571. $map = array_combine($keys, $last_insert_id);
  572. $insert3 = [];
  573. $workFlowSubDetailCondition = WorkFlowSubDetailCondition::where('del_time',0)
  574. ->where('workflow_id', $workFlowId)
  575. ->select('menu_id','workflow_sub_detail_id','column','opt1','opt2','opt_type','zdy1','zdy2')
  576. ->get()->toArray();
  577. if(! empty($workFlowSubDetailCondition)){
  578. foreach ($workFlowSubDetailCondition as $value){
  579. $workflow_generated_sub_detail_id = $map[$value['workflow_sub_detail_id']] ?? 0;
  580. unset($value['workflow_sub_detail_id']);
  581. $tmp = $last_map[$workflow_generated_sub_detail_id] ?? [];
  582. $value['workflow_generated_id'] = $tmp['workflow_generated_id'];
  583. $value['workflow_generated_sub_id'] = $tmp['workflow_generated_sub_id'];
  584. $value['workflow_generated_sub_detail_id'] = $workflow_generated_sub_detail_id;
  585. $value['crt_time'] = $time;
  586. $insert3[] = $value;
  587. }
  588. WorkFlowGeneratedSubDetailCondition::insert($insert3);
  589. }
  590. }
  591. //运行工作流 0 发起人 1审批 2抄送 3条件 4路由
  592. public function runWorkFlow($order_number = "", $workFlowGeneratedId = 0){
  593. $workFlowGenerated = WorkFlowGenerated::where('del_time', 0)
  594. ->when(! empty($order_number), function ($query) use ($order_number) {
  595. return $query->where('order_number', $order_number);
  596. })
  597. ->when(! empty($workFlowGeneratedId), function ($query) use ($workFlowGeneratedId) {
  598. return $query->where('id', $workFlowGeneratedId);
  599. })
  600. ->first();
  601. if(empty($workFlowGenerated)) return [false, '工作流不存在或已被删除'];
  602. $workFlowGenerated = $workFlowGenerated->toArray();
  603. //当前所处的节点ID
  604. $node_id = $workFlowGenerated['node_id'];
  605. //节点关系树状图
  606. $workFlowTree = json_decode($workFlowGenerated['my_param'], true);
  607. //获取剩余节点的树状图
  608. if($node_id > 1) $workFlowTree = $this->getDescendants($workFlowTree, $node_id);
  609. //赋值临时变量
  610. $tmp = $workFlowTree;
  611. //路由、审批、抄送节点
  612. $ly = $sp = $cs = [];
  613. //下个节点
  614. $this->traverseTree($tmp, $ly, $sp, $cs);
  615. }
  616. //获取某个节点下所有子集
  617. //$son = $this->getDescendants($workFlowTree, $node_id);
  618. function getDescendants(array $tree, int $targetId): array {
  619. $descendants = [];
  620. // Helper function to perform a depth-first search in the tree.
  621. $dfs = function ($node) use (&$dfs, &$descendants, $targetId) {
  622. if ($node['id'] == $targetId) {
  623. // If this is the target node, start collecting its children.
  624. foreach ($node['children'] ?? [] as $childId => $child) {
  625. $descendants[$childId] = $child;
  626. $dfs($child); // Recursively collect all descendants of this child.
  627. }
  628. } else {
  629. // Otherwise, continue searching in the children.
  630. foreach ($node['children'] ?? [] as $child) {
  631. $dfs($child);
  632. }
  633. }
  634. };
  635. // Start the search from the root node.
  636. foreach ($tree as $node) {
  637. $dfs($node);
  638. if (!empty($descendants)) {
  639. break; // Stop searching once we've found the target and collected its descendants.
  640. }
  641. }
  642. return $descendants;
  643. }
  644. function traverseTree($data, &$ly = [], &$sp = [], &$cs = []) {
  645. // Helper function to perform the actual traversal.
  646. $traverseHelper = function($node) use (&$ly, &$sp, &$cs, &$traverseHelper) {
  647. if ($node['type'] == 4) { //路由 路由下会带条件
  648. $ly[] = $node;
  649. return false; // Stop further recursion when type is 4.
  650. }
  651. if ($node['type'] == 2) {
  652. $cs[] = $node['id']; // 抄送节点id
  653. }
  654. if ($node['type'] == 1) {
  655. $sp[] = $node['id']; // 审批人节点id
  656. }
  657. if (!empty($node['children'])) {
  658. foreach ($node['children'] as $childNode) {
  659. if ($traverseHelper($childNode) === false) {
  660. break; // Break if a child node has type 4.
  661. }
  662. }
  663. }
  664. return $node['type'] != 4;
  665. };
  666. // Find the root node and start traversing from there.
  667. foreach ($data as $nodeId => $node) {
  668. $traverseHelper($node);
  669. break; // Only one root node should exist.
  670. }
  671. return [$sp, $cs, $ly];
  672. }
  673. //调用审批
  674. public function callCheck($data, $type, $user)
  675. {
  676. $service = new CheckService();
  677. list($bool, $msg) = $service->createRecordAndInventory([
  678. 'order_number' => $data['order_number'],
  679. 'type' => $type,
  680. 'opt_case' => $data['opt_case'],
  681. 'user_data' => $user,
  682. ]);
  683. return [$bool, $msg];
  684. }
  685. }