WorkFlowService.php 18 KB

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