WorkFlowService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. //通过节点id 找到当下流程
  90. // $aa = $this->createIdToNodeMap($tree);
  91. list($status, $msg) = $this->settleWorkFlowData($data,$array);
  92. if(! $status) return [false, $msg];
  93. list($detail, $detail_son, $array_detail_map) = $msg;
  94. $time = time();
  95. if(! empty($data['id'])){
  96. //更新
  97. $model = WorkFlow::where('id', $data['id'])->first();
  98. $model->title = $data['title'] ?? "";
  99. $model->menu_id = $data['tableId'] ?? 0;
  100. $model->vue_param = json_encode($data);
  101. $model->my_param = json_encode($tree);
  102. $model->save();
  103. //子表更新
  104. $this->updateSonModel($data, $time);
  105. }else{
  106. //添加
  107. $model = new WorkFlow();
  108. $model->title = $data['title'] ?? "";
  109. $model->menu_id = $data['tableId'] ?? 0;
  110. $model->vue_param = json_encode($data);
  111. $model->my_param = json_encode($tree);
  112. $model->crt_id = $user['id'];
  113. $model->save();
  114. $data['id'] = $model->id;
  115. }
  116. //子表写入
  117. $this->insertSonModel($data, $array, $detail, $detail_son, $array_detail_map,$time);
  118. DB::commit();
  119. }catch (\Throwable $exception){
  120. DB::rollBack();
  121. return [false, $exception->getLine() . ' | ' . $exception->getFile() . ' | ' . $exception->getMessage()];
  122. }
  123. return [true, ''];
  124. }
  125. public function insertSonModel($data, $array, $detail, $detail_son, $array_detail_map, $time){
  126. $sub = [];
  127. if(empty($array)) return;
  128. foreach ($array as $value){
  129. $sub[] = [
  130. 'workflow_id' => $data['id'],
  131. 'menu_id' => $data['tableId'],
  132. 'm_id' => $value['id'],
  133. 'crt_time' => $time
  134. ];
  135. }
  136. WorkFlowSub::insert($sub);
  137. //获取上一次插入订单的所有id
  138. $last_insert_id = WorkFlowSub::where('workflow_id', $data['id'])
  139. ->where('menu_id', $data['tableId'])
  140. ->where('crt_time', $time)
  141. ->where('del_time', 0)
  142. ->select('id')->get()->toArray();
  143. $last_insert_id = array_column($last_insert_id,'id');
  144. //组织 detail 数据 做写入
  145. $detail_array = [];
  146. foreach ($sub as $key => $value){
  147. $workflow_sub_id = $last_insert_id[$key];
  148. $keys = $array_detail_map[$key];
  149. if(empty($keys)) continue;
  150. foreach ($keys as $k){
  151. $detail_tmp = $detail[$k] ?? [];
  152. if(empty($detail_tmp)) continue;
  153. $detail_tmp['workflow_id'] = $value['workflow_id'];
  154. $detail_tmp['menu_id'] = $value['menu_id'];
  155. $detail_tmp['workflow_sub_id'] = $workflow_sub_id;
  156. $detail_tmp['crt_time'] = $time;
  157. $detail_array[] = $detail_tmp;
  158. }
  159. }
  160. if(! empty($detail_array)) WorkFlowSubDetail::insert($detail_array);
  161. if(empty($detail_son)) return;
  162. //获取上一次插入订单的所有id
  163. $last_insert_id = WorkFlowSubDetail::where('workflow_id', $data['id'])
  164. ->where('menu_id', $data['tableId'])
  165. ->where('crt_time', $time)
  166. ->where('del_time', 0)
  167. ->select('id')->get()->toArray();
  168. $last_insert_id = array_column($last_insert_id,'id');
  169. //组织 条件数据 做写入
  170. $detail_son_array = [];
  171. foreach ($detail_array as $key => $value){
  172. $workflow_sub_detail_id = $last_insert_id[$key];
  173. $detail_son_tmp = $detail_son[$key] ?? [];
  174. if(empty($detail_son_tmp)) continue;
  175. foreach ($detail_son_tmp as $val){
  176. $val['workflow_id'] = $value['workflow_id'];
  177. $val['menu_id'] = $value['menu_id'];
  178. $val['workflow_sub_id'] = $value['workflow_sub_id'];
  179. $val['workflow_sub_detail_id'] = $workflow_sub_detail_id;
  180. $val['crt_time'] = $value['crt_time'];
  181. $detail_son_array[] = $val;
  182. }
  183. }
  184. if(! empty($detail_son_array)) WorkFlowSubDetailCondition::insert($detail_son_array);
  185. }
  186. public function updateSonModel($data, $time){
  187. WorkFlowSub::where('del_time',0)
  188. ->where('workflow_id', $data['id'])
  189. ->update(['del_time' => $time]);
  190. WorkFlowSubDetail::where('del_time',0)
  191. ->where('workflow_id', $data['id'])
  192. ->update(['del_time' => $time]);
  193. WorkFlowSubDetailCondition::where('del_time',0)
  194. ->where('workflow_id', $data['id'])
  195. ->update(['del_time' => $time]);
  196. }
  197. public function EditWorkFlowMenuRule($data){
  198. // 最外层
  199. // tableId = 菜单id
  200. // flowPermission 发起人 空数组则为所有人 非空 则是指定人
  201. // directorMaxLevel 主管层级 (暂时没用到)
  202. // nodeConfig 节点数据
  203. if(empty($data['tableId'])) return [false, '菜单ID不能为空'];
  204. $menu = SysMenu::where('id', $data['tableId'])->first();
  205. if(empty($menu)) return [false, '菜单不存在或已被删除'];
  206. $menu = $menu->toArray();
  207. if($menu['del_time'] > 0) return [false, '菜单不存在或已被删除'];
  208. if(! isset($data['nodeConfig'])) return [false, '请传入节点参数'];
  209. $id = $data['id'] ?? 0;
  210. $bool = WorkFlow::where('del_time',0)
  211. ->where('menu_id',$data['tableId'])
  212. ->when(! empty($id),function ($query) use ($id){
  213. return $query->where('id', '<>', $id);
  214. })->exists();
  215. if($bool) return [false, '该菜单工作流已创建'];
  216. return [true, ''];
  217. }
  218. //给入节点id 获取节点下所有子集结构
  219. function createIdToNodeMap(array $tree, &$map = []) {
  220. foreach ($tree as $node) {
  221. // 如果节点有ID,则将其添加到映射中
  222. if (isset($node['id'])) {
  223. $map[$node['id']] = $node;
  224. }
  225. // 如果当前节点有子节点,则递归调用以处理子节点
  226. if (isset($node['children']) && is_array($node['children']) && ! empty($node['children'])) {
  227. $this->createIdToNodeMap($node['children'], $map);
  228. }
  229. }
  230. return $map;
  231. }
  232. function organizationData($array){
  233. $return = [];
  234. foreach ($array as $key => $value){
  235. $return[] = [
  236. 'id' => $value['id'],//节点id
  237. 'parent_id' => $value['parent_id'] ?? 0,//0代表无父级
  238. 'type' => $value['type'], // 0 发起人 1审批 2抄送 3条件 4路由
  239. 'priorityLevel' => $value['priorityLevel'] ?? 0,//0代表无优先级
  240. ];
  241. }
  242. return $return;
  243. }
  244. function flattenWorkflow($node, &$result = [], $parentId = null, &$idCounter = 0) {
  245. $idCounter++;
  246. $currentId = $idCounter;
  247. $nodeData = [
  248. 'id' => $currentId,
  249. 'parent_id' => $parentId,
  250. ];
  251. foreach ($node as $key => $value) {
  252. if (!in_array($key, ['childNode', 'conditionNodes'])) {
  253. $nodeData[$key] = $value;
  254. }
  255. }
  256. $result[] = $nodeData;
  257. // echo "Added node: " . $node['nodeName'] . "\n"; // Debugging line
  258. if ($node['type'] == 4 && isset($node['conditionNodes']) && is_array($node['conditionNodes']) && ! empty($node['conditionNodes'])) {
  259. foreach ($node['conditionNodes'] as $conditionNode) {
  260. $this->flattenWorkflow($conditionNode, $result, $currentId, $idCounter);
  261. }
  262. }
  263. if (isset($node['childNode']) && is_array($node['childNode']) && ! empty($node['childNode'])) {
  264. $this->flattenWorkflow($node['childNode'], $result, $currentId, $idCounter);
  265. }
  266. return $result;
  267. }
  268. function settleWorkFlowData($data, $array){
  269. $return = $return_detail = $map = [];
  270. if(empty($array)) return [true, [$return, $return_detail]];
  271. foreach ($array as $key => $value){
  272. $tmp = [
  273. 'm_id' => $value['id'],//节点id 后端生成
  274. 'type' => $value['type'],// 0 发起人 1审批 2抄送 3条件 4路由
  275. 'p_id' => 0,//人员id
  276. 'd_id' => 0,//部门id
  277. 'settype' => $value['settype'] ?? 0,// 审批人设置 1指定成员 2主管 4发起人自选 5发起人自己 7连续多级主管
  278. 'selectMode' => $value['selectMode'] ?? 0,//审批人数 1选一个人 2选多个人
  279. 'selectRange' => $value['selectRange'] ?? 0,//选择范围 1.全公司 2指定成员 2指定角色
  280. 'examineMode' => $value['examineMode'] ?? 0,//多人审批时采用的审批方式 1依次审批 2会签
  281. 'priorityLevel' => $value['priorityLevel'] ?? 0,//优先级
  282. ];
  283. $tmp_return = $return;
  284. if($value['type'] == 0){
  285. //发起人
  286. list($status, $msg) = $this->settleTypeZero($tmp, $data, $return);
  287. if(! $status) return [false, $msg];
  288. }elseif ($value['type'] == 1){
  289. //审批人
  290. list($status, $msg) = $this->settleTypeOne($tmp, $data, $value, $return);
  291. if(! $status) return [false, $msg];
  292. }elseif ($value['type'] == 2){
  293. //抄送人
  294. $this->settleTypeTwo($tmp, $value, $return);
  295. }elseif ($value['type'] == 3){
  296. //条件
  297. $this->settleTypeThree($tmp, $value, $return, $return_detail);
  298. }elseif ($value['type'] == 4){
  299. //路由
  300. $this->settleTypeFour($tmp, $value, $return);
  301. }else{
  302. return [false, '非法参数:节点TYPE'];
  303. }
  304. $differences = array_diff_key($return, $tmp_return);
  305. $keys = array_keys($differences);
  306. $map[$key] = $keys;
  307. }
  308. return [true, [$return, $return_detail,$map]];
  309. }
  310. //发起人
  311. function settleTypeZero($tmp, $data, &$return){
  312. if(empty($data['flowPermission'])) {
  313. //为空 则是 发起人为所有人
  314. $return[] = $tmp;
  315. }else{
  316. foreach ($data['flowPermission'] as $n){
  317. $t = $tmp;
  318. //发起人为指定的人或部门
  319. if($n['type'] == 1){
  320. $t['p_id'] = $n['targetId'];
  321. }elseif($n['type'] == 3){
  322. $t['d_id'] = $n['targetId'];
  323. }else{
  324. return [false, '非法参数:发起人'];
  325. }
  326. $return[] = $t;
  327. }
  328. }
  329. return [true, ''];
  330. }
  331. //审批人
  332. function settleTypeOne($tmp, $data, $value, &$return){
  333. //"settype": "",// 审批人设置 1指定成员 2主管 4发起人自选 5发起人自己 7连续多级主管
  334. //"selectMode": "", //审批人数 1选一个人 2选多个人
  335. //"selectRange": "", //选择范围 1.全公司 2指定成员 2指定角色
  336. //"directorLevel": "", //审批终点 最高层主管数
  337. //"examineMode": "", //多人审批时采用的审批方式 1依次审批 2会签
  338. //"noHanderAction": "",//审批人为空时 1自动审批通过/不允许发起 2转交给审核管理员
  339. //"examineEndDirectorLevel": "", //审批终点 第n层主管
  340. if($value['settype'] == 1){
  341. foreach ($value['nodeUserList'] as $n){
  342. $t = $tmp;
  343. //指定成员
  344. $t['p_id'] = $n['targetId'];
  345. $return[] = $t;
  346. }
  347. }elseif ($value['settype'] == 2){
  348. return [false, '暂不支持主管设置'];
  349. }elseif ($value['settype'] == 4){
  350. if($value['selectMode'] == 1){
  351. if($value['selectRange'] == 1){
  352. //全公司下的一个人
  353. $return[] = $tmp;
  354. }elseif ($value['selectRange'] == 2){
  355. foreach ($value['nodeUserList'] as $n){
  356. $t = $tmp;
  357. //指定人里的一个人
  358. $t['p_id'] = $n['targetId'];
  359. $return[] = $t;
  360. }
  361. }else{
  362. return [false, '非法参数:选择范围'];
  363. }
  364. }elseif ($value['selectMode'] == 2){
  365. if($value['selectRange'] == 1){
  366. //全公司里的所有人
  367. $return[] = $tmp;
  368. }elseif ($value['selectRange'] == 2){
  369. //指定人里的所有人
  370. foreach ($value['nodeUserList'] as $n){
  371. $t = $tmp;
  372. $t['p_id'] = $n['targetId'];
  373. $return[] = $t;
  374. }
  375. }else{
  376. return [false, '非法参数:选择范围'];
  377. }
  378. }
  379. }elseif ($value['settype'] == 5){
  380. //发起人则是触发工作流的那个人
  381. $tmp['selectMode'] = 1;
  382. $tmp['selectRange'] = 2;
  383. $return[] = $tmp;
  384. }elseif ($value['settype'] == 7){
  385. return [false, '暂不支持连续多级主管设置'];
  386. }else{
  387. return [false, '非法参数:审批人设置'];
  388. }
  389. return [true, ''];
  390. }
  391. //抄送人
  392. function settleTypeTwo($tmp, $value, &$return){
  393. foreach ($value['nodeUserList'] as $n){
  394. //抄送人为指定的一些人
  395. $tmp['p_id'] = $n['targetId'];
  396. $return[] = $tmp;
  397. }
  398. }
  399. //条件
  400. function settleTypeThree($tmp, $value, &$return, &$return_detail){
  401. $return[] = $tmp;
  402. $lastIndex = count($return) - 1;
  403. $tt = [];
  404. foreach ($value['conditionList'] as $val){
  405. $tt[] = [
  406. 'column' => $val['columnDbname'],
  407. 'opt1' => $val['opt1'],
  408. 'opt2' => $val['opt2'],
  409. 'opt_type' => $val['optType'],
  410. 'zdy1' => $val['zdy1'],
  411. 'zdy2' => $val['zdy2'],
  412. ];
  413. }
  414. $return_detail[$lastIndex] = $tt;
  415. }
  416. //路由
  417. function settleTypeFour($tmp, $value, &$return){
  418. $return[] = $tmp;
  419. }
  420. //触发工作流并生成
  421. public function create($data, $user){
  422. list($status, $msg) = $this->createRule($data, $user);
  423. if(! $status) return [false, $msg];
  424. try {
  425. //生成
  426. list($status, $msg) = $this->createWorkFlowDetail($data, $user);
  427. if(! $status){
  428. DB::rollBack();
  429. return [false, $msg];
  430. }
  431. DB::commit();
  432. } catch (\Exception $e) {
  433. DB::rollBack();
  434. return [false, $e->getFile() . '|' . $e->getLine() . '|' . $e->getMessage()];
  435. }
  436. return [true, ''];
  437. }
  438. public function createRule($data, $user){
  439. if(empty($data['menu_id'])) return [false, '菜单ID不能为空'];
  440. if(empty($data['order_number'])) return [false, 'order_number不能为空'];
  441. if(empty($data['opt_case'])) return [false, 'opt_case不能为空'];
  442. $oa = config('oa');
  443. if(empty($oa)) return [false, '工作流配置未设置,请联系开发者'];
  444. $menu_id = array_column($oa,'menu_id');
  445. if(! in_array($data['menu_id'], $menu_id)) return [false, '该菜单暂不支持工作流'];
  446. return [true, ''];
  447. }
  448. public function createWorkFlowDetail($data, $user){
  449. list($status, $msg) = $this->createWorkFlowDetailRule($data, $user);
  450. if(! $status) {
  451. if(isset($data['is_check'])){
  452. //因为工作流不存在,或者点击的人不触发工作流,所以直接成功返回
  453. list($status, $msg) = $this->callCheck($data, CheckService::TYPE_ONE, $user);
  454. return [$status, $msg];
  455. }
  456. return [false, $msg];
  457. }
  458. //创建审批流根据工作流
  459. list($status, $msg) = $this->createWorkFlow($data, $user, $msg);
  460. if(! $status) return [false, $msg];
  461. return [true, ''];
  462. }
  463. public function createWorkFlowDetailRule($data, $user){
  464. $workFlow = WorkFlow::where('del_time',0)
  465. ->where('menu_id', $data['menu_id'])
  466. ->first();
  467. if(empty($workFlow)) return [false, [-1, '菜单工作流不存在、已被删除或未新建']];
  468. $workFlow = $workFlow->toArray();
  469. //判断是否创建工作流
  470. list($status, $msg) = $this->getStartWorkFlow($workFlow['id'], $user);
  471. if(! $status) return [false, $msg];
  472. return [true, $workFlow];
  473. }
  474. //获取工作流的开始 判断是否激活审批流
  475. public function getStartWorkFlow($workFlowId = 0, $user){
  476. //用户的部门
  477. $depart_user = $user['depart_range'] ?? [];
  478. //工作流开始节点数据
  479. $list = WorkFlowSubDetail::where('del_time',0)
  480. ->where('workflow_id',$workFlowId)
  481. ->where('type', WorkFlowSubDetail::type_zero)
  482. ->get()->toArray();
  483. //工作流的部门 人
  484. $workflow_depart = $workflow_man = [];
  485. foreach ($list as $value){
  486. if($value['p_id'] > 0 && ! in_array($value['p_id'], $workflow_man)) $workflow_man[] = $value['p_id'];
  487. if($value['d_id'] > 0 && ! in_array($value['d_id'], $workflow_depart)) $workflow_depart[] = $value['d_id'];
  488. }
  489. //所有人都能触发审批流
  490. if(empty($workflow_depart) && empty($workflow_man)) return [true, ''];
  491. if(! empty($workflow_depart)){
  492. $d = array_intersect($depart_user, $workflow_depart);
  493. if(! empty($d)) return [true, ''];
  494. }
  495. if(! empty($workflow_man)){
  496. $p = array_intersect([$user['id']], $workflow_man);
  497. if(! empty($p)) return [true, ''];
  498. }
  499. return [false, [-1, '触发人不在工作流发起者内']];
  500. }
  501. //创建审批流根据工作流
  502. public function createWorkFlow($data, $user, $workFlow){
  503. try {
  504. DB::beginTransaction();
  505. $time = time();
  506. $model = new WorkFlowGenerated();
  507. $model->title = $workFlow['title'];
  508. $model->menu_id = $workFlow['menu_id'];
  509. $model->vue_param = $workFlow['vue_param'];
  510. $model->my_param = $workFlow['my_param'];
  511. $model->order_number = $data['order_number'];
  512. $model->crt_id = $user['id'];
  513. $model->save();
  514. $id = $model->id;
  515. $this->insertSonModel2($workFlow['id'], $id, $time);
  516. DB::commit();
  517. }catch (\Throwable $e){
  518. DB::rollBack();
  519. if (str_contains($e->getMessage(), '1062') || str_contains($e->getMessage(), 'Duplicate entry')) {
  520. return [false, '审批流程已存在'];
  521. }
  522. return [false, $e->getFile() . '|' . $e->getLine() . '|' . $e->getMessage()];
  523. }
  524. return [true, $id];
  525. }
  526. public function insertSonModel2($workFlowId = 0, $workFlowGeneratedId = 0, $time){
  527. $insert = [];
  528. $workFlowSub = WorkFlowSub::where('del_time',0)
  529. ->where('workflow_id', $workFlowId)
  530. ->select('id','menu_id','m_id')
  531. ->get()->toArray();
  532. $keys = [];
  533. foreach ($workFlowSub as $value){
  534. $insert[] = [
  535. 'menu_id' => $value['menu_id'],
  536. 'm_id' => $value['m_id'],
  537. 'workflow_generated_id' => $workFlowGeneratedId,
  538. 'crt_time' => $time,
  539. ];
  540. $keys[] = $value['id'];
  541. }
  542. WorkFlowGeneratedSub::insert($insert);
  543. //获取上一次插入订单的所有id
  544. $last_insert_id = WorkFlowGeneratedSub::where('workflow_generated_id', $workFlowGeneratedId)
  545. ->where('crt_time', $time)
  546. ->where('del_time', 0)
  547. ->select('id')->get()->toArray();
  548. $last_insert_id = array_column($last_insert_id,'id');
  549. $map = array_combine($keys, $last_insert_id);
  550. $workFlowSubDetail = WorkFlowSubDetail::where('del_time',0)
  551. ->where('workflow_id', $workFlowId)
  552. ->select('id','menu_id','m_id','workflow_sub_id','type','p_id','d_id','settype','selectMode','selectRange','examineMode','priorityLevel')
  553. ->get()->toArray();
  554. $insert2 = [];
  555. $keys = [];
  556. foreach ($workFlowSubDetail as $value){
  557. $keys[] = $value['id'];
  558. $workflow_generated_sub_id = $map[$value['workflow_sub_id']] ?? 0;
  559. unset($value['id']);unset($value['workflow_sub_id']);
  560. $value['workflow_generated_id'] = $workFlowGeneratedId;
  561. $value['workflow_generated_sub_id'] = $workflow_generated_sub_id;
  562. $value['crt_time'] = $time;
  563. $insert2[] = $value;
  564. }
  565. WorkFlowGeneratedSubDetail::insert($insert2);
  566. //获取上一次插入订单的所有id
  567. $last_insert_id = WorkFlowGeneratedSubDetail::where('workflow_generated_id', $workFlowGeneratedId)
  568. ->where('crt_time', $time)
  569. ->where('del_time', 0)
  570. ->get()->toArray();
  571. $last_map = array_column($last_insert_id,null,'id');
  572. $last_insert_id = array_column($last_insert_id,'id');
  573. $map = array_combine($keys, $last_insert_id);
  574. $insert3 = [];
  575. $workFlowSubDetailCondition = WorkFlowSubDetailCondition::where('del_time',0)
  576. ->where('workflow_id', $workFlowId)
  577. ->select('menu_id','workflow_sub_detail_id','column','opt1','opt2','opt_type','zdy1','zdy2')
  578. ->get()->toArray();
  579. if(! empty($workFlowSubDetailCondition)){
  580. foreach ($workFlowSubDetailCondition as $value){
  581. $workflow_generated_sub_detail_id = $map[$value['workflow_sub_detail_id']] ?? 0;
  582. unset($value['workflow_sub_detail_id']);
  583. $tmp = $last_map[$workflow_generated_sub_detail_id] ?? [];
  584. $value['workflow_generated_id'] = $tmp['workflow_generated_id'];
  585. $value['workflow_generated_sub_id'] = $tmp['workflow_generated_sub_id'];
  586. $value['workflow_generated_sub_detail_id'] = $workflow_generated_sub_detail_id;
  587. $value['crt_time'] = $time;
  588. $insert3[] = $value;
  589. }
  590. WorkFlowGeneratedSubDetailCondition::insert($insert3);
  591. }
  592. }
  593. //运行工作流
  594. public function runWorkFlow($order_number = "", $workFlowGeneratedId = 0){
  595. $workFlowGenerated = WorkFlowGenerated::where('del_time', 0)
  596. ->when(! empty($order_number), function ($query) use ($order_number) {
  597. return $query->where('order_number', $order_number);
  598. })
  599. ->when(! empty($workFlowGeneratedId), function ($query) use ($workFlowGeneratedId) {
  600. return $query->where('id', $workFlowGeneratedId);
  601. })
  602. ->first();
  603. if(empty($workFlowGenerated)) return [false, '工作流不存在或已被删除'];
  604. $workFlowGenerated = $workFlowGenerated->toArray();
  605. // if($workFlowGenerated['node_id'])
  606. }
  607. //调用审批
  608. public function callCheck($data, $type, $user)
  609. {
  610. $service = new CheckService();
  611. list($bool, $msg) = $service->createRecordAndInventory([
  612. 'order_number' => $data['order_number'],
  613. 'type' => $type,
  614. 'opt_case' => $data['opt_case'],
  615. 'user_data' => $user,
  616. ]);
  617. return [$bool, $msg];
  618. }
  619. }