WorkFlowService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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\WorkFlowSub;
  7. use App\Model\WorkFlowSubDetail;
  8. use App\Model\WorkFlowSubDetailCondition;
  9. use Illuminate\Support\Facades\DB;
  10. class WorkFlowService extends Service
  11. {
  12. public function orderList($data,$user){
  13. $model = WorkFlow::where('del_time',0)
  14. ->select('title','id','menu_id','crt_time','crt_id')
  15. ->orderby('id', 'desc');
  16. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  17. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  18. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  19. $model->where('crt_time','>=',$return[0]);
  20. $model->where('crt_time','<=',$return[1]);
  21. }
  22. $list = $this->limit($model,'',$data);
  23. $list = $this->fillData($list,$data);
  24. return [true, $list];
  25. }
  26. public function fillData($data,$ergs){
  27. if(empty($data['data'])) return $data;
  28. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  29. ->pluck('emp_name','id')
  30. ->toArray();
  31. $menu = SysMenu::whereIn('id',array_unique(array_column($data['data'],'menu_id')))
  32. ->pluck('title','id')
  33. ->toArray();
  34. foreach ($data['data'] as $key => $value){
  35. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  36. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  37. $data['data'][$key]['menu_title'] = $menu[$value['menu_id']] ?? '';
  38. }
  39. return $data;
  40. }
  41. public function detail($data, $user){
  42. if($this->isEmpty($data,'id')) return [false,'请选择工作流'];
  43. $workFlow = WorkFlow::where('del_time',0)
  44. ->where('id',$data['id'])
  45. ->first();
  46. if(empty($workFlow)) return [false,'工作流不存在或已被删除'];
  47. $workFlow = $workFlow->toArray();
  48. $emp_map = Employee::where('id',$workFlow['crt_id'])
  49. ->pluck('emp_name','id')
  50. ->toArray();
  51. $workFlow['crt_name'] = $emp_map[$workFlow['crt_id']] ?? '';
  52. $workFlow['crt_time'] = $workFlow['crt_time'] ? date("Y-m-d H:i:s", $workFlow['crt_time']): '';
  53. return [true, $workFlow];
  54. }
  55. public function del($data, $user){
  56. if($this->isEmpty($data,'id')) return [false,'请选择工作流'];
  57. $workFlow = WorkFlow::where('del_time',0)
  58. ->where('id',$data['id'])
  59. ->first();
  60. if(empty($workFlow)) return [false,'工作流不存在或已被删除'];
  61. try {
  62. DB::beginTransaction();
  63. $time = time();
  64. $workFlow->del_time = $time;
  65. $workFlow->save();
  66. $this->updateSonModel($data, $time);
  67. DB::commit();
  68. }catch (\Throwable $e){
  69. DB::rollBack();
  70. return [false, $e->getFile() . '|' . $e->getLine() . '|' . $e->getMessage()];
  71. }
  72. return [true, ''];
  73. }
  74. public function EditWorkFlowMenu($data, $user){
  75. list($status, $msg) = $this->EditWorkFlowMenuRule($data);
  76. if(! $status) return [false, $msg];
  77. try{
  78. DB::beginTransaction();
  79. //将前端给到的工作流数据组成多个一维数组
  80. $array = $this->flattenWorkflow($data['nodeConfig']);
  81. //踢掉没用的数据,为了组成树结构
  82. $clear_array = $this->organizationData($array);
  83. //树结构
  84. $tree = $this->makeTree(0, $clear_array);
  85. //通过节点id 找到当下流程
  86. // $aa = $this->createIdToNodeMap($tree);
  87. list($status, $msg) = $this->settleWorkFlowData($data,$array);
  88. if(! $status) return [false, $msg];
  89. list($detail, $detail_son, $array_detail_map) = $msg;
  90. $time = time();
  91. if(! empty($data['id'])){
  92. //更新
  93. $model = WorkFlow::where('id', $data['id'])->first();
  94. $model->title = $data['title'] ?? "";
  95. $model->menu_id = $data['tableId'] ?? 0;
  96. $model->vue_param = json_encode($data);
  97. $model->my_param = json_encode($tree);
  98. $model->save();
  99. //子表更新
  100. $this->updateSonModel($data, $time);
  101. }else{
  102. //添加
  103. $model = new WorkFlow();
  104. $model->title = $data['title'] ?? "";
  105. $model->menu_id = $data['tableId'] ?? 0;
  106. $model->vue_param = json_encode($data);
  107. $model->my_param = json_encode($tree);
  108. $model->crt_id = $user['id'];
  109. $model->save();
  110. $data['id'] = $model->id;
  111. }
  112. //子表写入
  113. $this->insertSonModel($data, $array, $detail, $detail_son, $array_detail_map,$time);
  114. DB::commit();
  115. }catch (\Throwable $exception){
  116. DB::rollBack();
  117. return [false, $exception->getLine() . ' | ' . $exception->getFile() . ' | ' . $exception->getMessage()];
  118. }
  119. return [true, ''];
  120. }
  121. public function insertSonModel($data, $array, $detail, $detail_son, $array_detail_map, $time){
  122. $sub = [];
  123. if(empty($array)) return;
  124. foreach ($array as $value){
  125. $sub[] = [
  126. 'workflow_id' => $data['id'],
  127. 'menu_id' => $data['tableId'],
  128. 'm_id' => $value['id'],
  129. 'crt_time' => $time
  130. ];
  131. }
  132. WorkFlowSub::insert($sub);
  133. //获取上一次插入订单的所有id
  134. $last_insert_id = WorkFlowSub::where('workflow_id', $data['id'])
  135. ->where('menu_id', $data['tableId'])
  136. ->where('crt_time', $time)
  137. ->where('del_time', 0)
  138. ->select('id')->get()->toArray();
  139. $last_insert_id = array_column($last_insert_id,'id');
  140. //组织 detail 数据 做写入
  141. $detail_array = [];
  142. foreach ($sub as $key => $value){
  143. $workflow_sub_id = $last_insert_id[$key];
  144. $keys = $array_detail_map[$key];
  145. if(empty($keys)) continue;
  146. foreach ($keys as $k){
  147. $detail_tmp = $detail[$k] ?? [];
  148. if(empty($detail_tmp)) continue;
  149. $detail_tmp['workflow_id'] = $value['workflow_id'];
  150. $detail_tmp['menu_id'] = $value['menu_id'];
  151. $detail_tmp['workflow_sub_id'] = $workflow_sub_id;
  152. $detail_tmp['crt_time'] = $time;
  153. $detail_array[] = $detail_tmp;
  154. }
  155. }
  156. if(! empty($detail_array)) WorkFlowSubDetail::insert($detail_array);
  157. if(empty($detail_son)) return;
  158. //获取上一次插入订单的所有id
  159. $last_insert_id = WorkFlowSubDetail::where('workflow_id', $data['id'])
  160. ->where('menu_id', $data['tableId'])
  161. ->where('crt_time', $time)
  162. ->where('del_time', 0)
  163. ->select('id')->get()->toArray();
  164. $last_insert_id = array_column($last_insert_id,'id');
  165. //组织 条件数据 做写入
  166. $detail_son_array = [];
  167. foreach ($detail_array as $key => $value){
  168. $workflow_sub_detail_id = $last_insert_id[$key];
  169. $detail_son_tmp = $detail_son[$key] ?? [];
  170. if(empty($detail_son_tmp)) continue;
  171. foreach ($detail_son_tmp as $val){
  172. $val['workflow_id'] = $value['workflow_id'];
  173. $val['menu_id'] = $value['menu_id'];
  174. $val['workflow_sub_id'] = $value['workflow_sub_id'];
  175. $val['workflow_sub_detail_id'] = $workflow_sub_detail_id;
  176. $val['crt_time'] = $value['crt_time'];
  177. $detail_son_array[] = $val;
  178. }
  179. }
  180. if(! empty($detail_son_array)) WorkFlowSubDetailCondition::insert($detail_son_array);
  181. }
  182. public function updateSonModel($data, $time){
  183. WorkFlowSub::where('del_time',0)
  184. ->where('workflow_id', $data['id'])
  185. ->update(['del_time' => $time]);
  186. WorkFlowSubDetail::where('del_time',0)
  187. ->where('workflow_id', $data['id'])
  188. ->update(['del_time' => $time]);
  189. WorkFlowSubDetailCondition::where('del_time',0)
  190. ->where('workflow_id', $data['id'])
  191. ->update(['del_time' => $time]);
  192. }
  193. public function EditWorkFlowMenuRule($data){
  194. // 最外层
  195. // tableId = 菜单id
  196. // flowPermission 发起人 空数组则为所有人 非空 则是指定人
  197. // directorMaxLevel 主管层级 (暂时没用到)
  198. // nodeConfig 节点数据
  199. if(empty($data['tableId'])) return [false, '菜单ID不能为空'];
  200. $menu = SysMenu::where('id', $data['tableId'])->first();
  201. if(empty($menu)) return [false, '菜单不存在或已被删除'];
  202. $menu = $menu->toArray();
  203. if($menu['del_time'] > 0) return [false, '菜单不存在或已被删除'];
  204. if(! isset($data['nodeConfig'])) return [false, '请传入节点参数'];
  205. $id = $data['id'] ?? 0;
  206. $bool = WorkFlow::where('del_time',0)
  207. ->where('menu_id',$data['tableId'])
  208. ->when(! empty($id),function ($query) use ($id){
  209. return $query->where('id', '<>', $id);
  210. })->exists();
  211. if($bool) return [false, '该菜单工作流已创建'];
  212. return [true, ''];
  213. }
  214. //给入节点id 获取节点下所有子集结构
  215. function createIdToNodeMap(array $tree, &$map = []) {
  216. foreach ($tree as $node) {
  217. // 如果节点有ID,则将其添加到映射中
  218. if (isset($node['id'])) {
  219. $map[$node['id']] = $node;
  220. }
  221. // 如果当前节点有子节点,则递归调用以处理子节点
  222. if (isset($node['children']) && is_array($node['children']) && ! empty($node['children'])) {
  223. $this->createIdToNodeMap($node['children'], $map);
  224. }
  225. }
  226. return $map;
  227. }
  228. function organizationData($array){
  229. $return = [];
  230. foreach ($array as $key => $value){
  231. $return[] = [
  232. 'id' => $value['id'],//节点id
  233. 'parent_id' => $value['parent_id'] ?? 0,//0代表无父级
  234. 'type' => $value['type'], // 0 发起人 1审批 2抄送 3条件 4路由
  235. 'priorityLevel' => $value['priorityLevel'] ?? 0,//0代表无优先级
  236. ];
  237. }
  238. return $return;
  239. }
  240. function flattenWorkflow($node, &$result = [], $parentId = null, &$idCounter = 0) {
  241. $idCounter++;
  242. $currentId = $idCounter;
  243. $nodeData = [
  244. 'id' => $currentId,
  245. 'parent_id' => $parentId,
  246. ];
  247. foreach ($node as $key => $value) {
  248. if (!in_array($key, ['childNode', 'conditionNodes'])) {
  249. $nodeData[$key] = $value;
  250. }
  251. }
  252. $result[] = $nodeData;
  253. // echo "Added node: " . $node['nodeName'] . "\n"; // Debugging line
  254. if ($node['type'] == 4 && isset($node['conditionNodes']) && is_array($node['conditionNodes']) && ! empty($node['conditionNodes'])) {
  255. foreach ($node['conditionNodes'] as $conditionNode) {
  256. $this->flattenWorkflow($conditionNode, $result, $currentId, $idCounter);
  257. }
  258. }
  259. if (isset($node['childNode']) && is_array($node['childNode']) && ! empty($node['childNode'])) {
  260. $this->flattenWorkflow($node['childNode'], $result, $currentId, $idCounter);
  261. }
  262. return $result;
  263. }
  264. function settleWorkFlowData($data, $array){
  265. $return = $return_detail = $map = [];
  266. if(empty($array)) return [true, [$return, $return_detail]];
  267. foreach ($array as $key => $value){
  268. $tmp = [
  269. 'm_id' => $value['id'],//节点id 后端生成
  270. 'type' => $value['type'],// 0 发起人 1审批 2抄送 3条件 4路由
  271. 'p_id' => 0,//人员id
  272. 'd_id' => 0,//部门id
  273. 'settype' => $value['settype'] ?? 0,// 审批人设置 1指定成员 2主管 4发起人自选 5发起人自己 7连续多级主管
  274. 'selectMode' => $value['selectMode'] ?? 0,//审批人数 1选一个人 2选多个人
  275. 'selectRange' => $value['selectRange'] ?? 0,//选择范围 1.全公司 2指定成员 2指定角色
  276. 'examineMode' => $value['examineMode'] ?? 0,//多人审批时采用的审批方式 1依次审批 2会签
  277. 'priorityLevel' => $value['priorityLevel'] ?? 0,//优先级
  278. ];
  279. $tmp_return = $return;
  280. if($value['type'] == 0){
  281. //发起人
  282. list($status, $msg) = $this->settleTypeZero($tmp, $data, $return);
  283. if(! $status) return [false, $msg];
  284. }elseif ($value['type'] == 1){
  285. //审批人
  286. list($status, $msg) = $this->settleTypeOne($tmp, $data, $value, $return);
  287. if(! $status) return [false, $msg];
  288. }elseif ($value['type'] == 2){
  289. //抄送人
  290. $this->settleTypeTwo($tmp, $value, $return);
  291. }elseif ($value['type'] == 3){
  292. //条件
  293. $this->settleTypeThree($tmp, $value, $return, $return_detail);
  294. }elseif ($value['type'] == 4){
  295. //路由
  296. $this->settleTypeFour($tmp, $value, $return);
  297. }else{
  298. return [false, '非法参数:节点TYPE'];
  299. }
  300. $differences = array_diff_key($return, $tmp_return);
  301. $keys = array_keys($differences);
  302. $map[$key] = $keys;
  303. }
  304. return [true, [$return, $return_detail,$map]];
  305. }
  306. //发起人
  307. function settleTypeZero($tmp, $data, &$return){
  308. if(empty($data['flowPermission'])) {
  309. //为空 则是 发起人为所有人
  310. $return[] = $tmp;
  311. }else{
  312. foreach ($data['flowPermission'] as $n){
  313. $t = $tmp;
  314. //发起人为指定的人或部门
  315. if($n['type'] == 1){
  316. $t['p_id'] = $n['targetId'];
  317. }elseif($n['type'] == 3){
  318. $t['d_id'] = $n['targetId'];
  319. }else{
  320. return [false, '非法参数:发起人'];
  321. }
  322. $return[] = $t;
  323. }
  324. }
  325. return [true, ''];
  326. }
  327. //审批人
  328. function settleTypeOne($tmp, $data, $value, &$return){
  329. //"settype": "",// 审批人设置 1指定成员 2主管 4发起人自选 5发起人自己 7连续多级主管
  330. //"selectMode": "", //审批人数 1选一个人 2选多个人
  331. //"selectRange": "", //选择范围 1.全公司 2指定成员 2指定角色
  332. //"directorLevel": "", //审批终点 最高层主管数
  333. //"examineMode": "", //多人审批时采用的审批方式 1依次审批 2会签
  334. //"noHanderAction": "",//审批人为空时 1自动审批通过/不允许发起 2转交给审核管理员
  335. //"examineEndDirectorLevel": "", //审批终点 第n层主管
  336. if($value['settype'] == 1){
  337. foreach ($value['nodeUserList'] as $n){
  338. $t = $tmp;
  339. //指定成员
  340. $t['p_id'] = $n['targetId'];
  341. $return[] = $t;
  342. }
  343. }elseif ($value['settype'] == 2){
  344. return [false, '暂不支持主管设置'];
  345. }elseif ($value['settype'] == 4){
  346. if($value['selectMode'] == 1){
  347. if($value['selectRange'] == 1){
  348. //全公司下的一个人
  349. $return[] = $tmp;
  350. }elseif ($value['selectRange'] == 2){
  351. foreach ($value['nodeUserList'] as $n){
  352. $t = $tmp;
  353. //指定人里的一个人
  354. $t['p_id'] = $n['targetId'];
  355. $return[] = $t;
  356. }
  357. }else{
  358. return [false, '非法参数:选择范围'];
  359. }
  360. }elseif ($value['selectMode'] == 2){
  361. if($value['selectRange'] == 1){
  362. //全公司里的所有人
  363. $return[] = $tmp;
  364. }elseif ($value['selectRange'] == 2){
  365. //指定人里的所有人
  366. foreach ($value['nodeUserList'] as $n){
  367. $t = $tmp;
  368. $t['p_id'] = $n['targetId'];
  369. $return[] = $t;
  370. }
  371. }else{
  372. return [false, '非法参数:选择范围'];
  373. }
  374. }
  375. }elseif ($value['settype'] == 5){
  376. //发起人则是触发工作流的那个人
  377. $tmp['selectMode'] = 1;
  378. $tmp['selectRange'] = 2;
  379. $return[] = $tmp;
  380. }elseif ($value['settype'] == 7){
  381. return [false, '暂不支持连续多级主管设置'];
  382. }else{
  383. return [false, '非法参数:审批人设置'];
  384. }
  385. return [true, ''];
  386. }
  387. //抄送人
  388. function settleTypeTwo($tmp, $value, &$return){
  389. foreach ($value['nodeUserList'] as $n){
  390. //抄送人为指定的一些人
  391. $tmp['p_id'] = $n['targetId'];
  392. $return[] = $tmp;
  393. }
  394. }
  395. //条件
  396. function settleTypeThree($tmp, $value, &$return, &$return_detail){
  397. $return[] = $tmp;
  398. $lastIndex = count($return) - 1;
  399. $tt = [];
  400. foreach ($value['conditionList'] as $val){
  401. $tt[] = [
  402. 'column' => $val['columnDbname'],
  403. 'opt1' => $val['opt1'],
  404. 'opt2' => $val['opt2'],
  405. 'opt_type' => $val['optType'],
  406. 'zdy1' => $val['zdy1'],
  407. 'zdy2' => $val['zdy2'],
  408. ];
  409. }
  410. $return_detail[$lastIndex] = $tt;
  411. }
  412. //路由
  413. function settleTypeFour($tmp, $value, &$return){
  414. $return[] = $tmp;
  415. }
  416. //触发工作流并生成
  417. public function create($data, $user){
  418. list($status, $msg) = $this->createRule($data, $user);
  419. if(! $status) return [false, $msg];
  420. try {
  421. //生成
  422. list($status, $msg) = $this->createWorkFlowDetail($data, $user);
  423. DB::commit();
  424. } catch (\Exception $e) {
  425. DB::rollBack();
  426. return [false, $e->getFile() . '|' . $e->getLine() . '|' . $e->getMessage()];
  427. }
  428. return [true, ''];
  429. }
  430. public function createRule($data, $user){
  431. if(empty($data['menu_id'])) return [false, '菜单ID不能为空'];
  432. if(empty($data['order_number'])) return [false, 'order_number不能为空'];
  433. if(empty($data['opt_case'])) return [false, 'opt_case不能为空'];
  434. $oa = config('oa');
  435. if(empty($oa)) return [false, '工作流配置未设置,请联系开发者'];
  436. $menu_id = array_column($oa,'menu_id');
  437. if(! in_array($data['menu_id'], $menu_id)) return [false, '该菜单暂不支持工作流'];
  438. }
  439. public function createWorkFlowDetail($data, $user){
  440. $workFlow = WorkFlow::where('del_time',0)
  441. ->where('menu_id', $data['menu_id'])
  442. ->first();
  443. if(empty($workFlow)) return [true, ''];
  444. }
  445. }