WorkFlowService.php 17 KB

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