select('title','id','menu_id','crt_time') ->orderby('id', 'desc'); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['crt_time']); $model->where('crt_time','>=',$return[0]); $model->where('crt_time','<=',$return[1]); } $list = $this->limit($model,'',$data); $list = $this->fillData($list,$data); return [true, $list]; } public function fillData($data,$ergs){ if(empty($data['data'])) return $data; $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id'))) ->pluck('emp_name','id') ->toArray(); foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; } return $data; } public function detail($data, $user){ if($this->isEmpty($data,'id')) return [false,'请选择工作流']; $workFlow = WorkFlow::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($workFlow)) return [false,'工作流不存在或已被删除']; $workFlow = $workFlow->toArray(); $emp_map = Employee::where('id',$workFlow['crt_id']) ->pluck('emp_name','id') ->toArray(); $workFlow['crt_name'] = $emp_map[$workFlow['crt_id']] ?? ''; $workFlow['crt_time'] = $workFlow['crt_time'] ? date("Y-m-d H:i:s", $workFlow['crt_time']): ''; return [true, $workFlow]; } public function del($data, $user){ if($this->isEmpty($data,'id')) return [false,'请选择工作流']; $workFlow = WorkFlow::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($workFlow)) return [false,'工作流不存在或已被删除']; $workFlow->del_time = time(); $workFlow->save(); return [true, '']; } public function EditWorkFlowMenu($data, $user){ list($status, $msg) = $this->EditWorkFlowMenuRule($data); if(! $status) return [false, $msg]; try{ DB::beginTransaction(); //将前端给到的工作流数据组成多个一维数组 $array = $this->flattenWorkflow($data['nodeConfig']); //踢掉没用的数据,为了组成树结构 $clear_array = $this->organizationData($array); //树结构 $tree = $this->makeTree(0, $clear_array); //通过节点id 找到当下流程 // $aa = $this->createIdToNodeMap($tree); list($status, $msg) = $this->settleWorkFlowData($data,$array); if(! $status) return [false, $msg]; list($detail, $detail_son, $array_detail_map) = $msg; $time = time(); if(! empty($data['id'])){ //更新 $model = WorkFlow::where('id', $data['id'])->first(); $model->title = $data['title'] ?? ""; $model->menu_id = $data['tableId'] ?? 0; $model->vue_param = json_encode($data); $model->my_param = json_encode($tree); $model->save(); //子表更新 $this->updateSonModel($data, $time); }else{ //添加 $model = new WorkFlow(); $model->title = $data['title'] ?? ""; $model->menu_id = $data['tableId'] ?? 0; $model->vue_param = json_encode($data); $model->my_param = json_encode($tree); $model->crt_id = $user['id']; $model->save(); $data['id'] = $model->id; } //子表写入 $this->insertSonModel($data, $array, $detail, $detail_son, $array_detail_map,$time); DB::commit(); }catch (\Throwable $exception){ DB::rollBack(); return [false, $exception->getLine() . ' | ' . $exception->getFile() . ' | ' . $exception->getMessage()]; } return [true, '']; } public function insertSonModel($data, $array, $detail, $detail_son, $array_detail_map, $time){ $sub = []; if(empty($array)) return; foreach ($array as $value){ $sub[] = [ 'workflow_id' => $data['id'], 'menu_id' => $data['tableId'], 'm_id' => $value['id'], 'crt_time' => $time ]; } WorkFlowSub::insert($sub); //获取上一次插入订单的所有id $last_insert_id = WorkFlowSub::where('workflow_id', $data['id']) ->where('menu_id', $data['tableId']) ->where('crt_time', $time) ->where('del_time', 0) ->select('id')->get()->toArray(); $last_insert_id = array_column($last_insert_id,'id'); //组织 detail 数据 做写入 $detail_array = []; foreach ($sub as $key => $value){ $workflow_sub_id = $last_insert_id[$key]; $keys = $array_detail_map[$key]; if(empty($keys)) continue; foreach ($keys as $k){ $detail_tmp = $detail[$k] ?? []; if(empty($detail_tmp)) continue; $detail_tmp['workflow_id'] = $value['workflow_id']; $detail_tmp['menu_id'] = $value['menu_id']; $detail_tmp['workflow_sub_id'] = $workflow_sub_id; $detail_tmp['crt_time'] = $time; $detail_array[] = $detail_tmp; } } if(! empty($detail_array)) WorkFlowSubDetail::insert($detail_array); if(empty($detail_son)) return; //获取上一次插入订单的所有id $last_insert_id = WorkFlowSubDetail::where('workflow_id', $data['id']) ->where('menu_id', $data['tableId']) ->where('crt_time', $time) ->where('del_time', 0) ->select('id')->get()->toArray(); $last_insert_id = array_column($last_insert_id,'id'); //组织 条件数据 做写入 $detail_son_array = []; foreach ($detail_array as $key => $value){ $workflow_sub_detail_id = $last_insert_id[$key]; $detail_son_tmp = $detail_son[$key] ?? []; if(empty($detail_son_tmp)) continue; foreach ($detail_son_tmp as $val){ $val['workflow_id'] = $value['workflow_id']; $val['menu_id'] = $value['menu_id']; $val['workflow_sub_id'] = $value['workflow_sub_id']; $val['workflow_sub_detail_id'] = $workflow_sub_detail_id; $val['crt_time'] = $value['crt_time']; $detail_son_array[] = $val; } } if(! empty($detail_son_array)) WorkFlowSubDetailCondition::insert($detail_son_array); } public function updateSonModel($data, $time){ WorkFlowSub::where('del_time',0) ->where('workflow_id', $data['id']) ->update(['del_time' => $time]); WorkFlowSubDetail::where('del_time',0) ->where('workflow_id', $data['id']) ->update(['del_time' => $time]); WorkFlowSubDetailCondition::where('del_time',0) ->where('workflow_id', $data['id']) ->update(['del_time' => $time]); } public function EditWorkFlowMenuRule($data){ // 最外层 // tableId = 菜单id // flowPermission 发起人 空数组则为所有人 非空 则是指定人 // directorMaxLevel 主管层级 (暂时没用到) // nodeConfig 节点数据 if(empty($data['tableId'])) return [false, '菜单ID不能为空']; $menu = SysMenu::where('id', $data['tableId'])->first(); if(empty($menu)) return [false, '菜单不存在或已被删除']; $menu = $menu->toArray(); if($menu['del_time'] > 0) return [false, '菜单不存在或已被删除']; if(! isset($data['nodeConfig'])) return [false, '请传入节点参数']; $id = $data['id'] ?? 0; $bool = WorkFlow::where('del_time',0) ->where('menu_id',$data['tableId']) ->when(! empty($id),function ($query) use ($id){ return $query->where('id', '<>', $id); })->exists(); if($bool) return [false, '该菜单工作流已创建']; return [true, '']; } //给入节点id 获取节点下所有子集结构 function createIdToNodeMap(array $tree, &$map = []) { foreach ($tree as $node) { // 如果节点有ID,则将其添加到映射中 if (isset($node['id'])) { $map[$node['id']] = $node; } // 如果当前节点有子节点,则递归调用以处理子节点 if (isset($node['children']) && is_array($node['children']) && ! empty($node['children'])) { $this->createIdToNodeMap($node['children'], $map); } } return $map; } function organizationData($array){ $return = []; foreach ($array as $key => $value){ $return[] = [ 'id' => $value['id'],//节点id 'parent_id' => $value['parent_id'] ?? 0,//0代表无父级 'type' => $value['type'], // 0 发起人 1审批 2抄送 3条件 4路由 'priorityLevel' => $value['priorityLevel'] ?? 0,//0代表无优先级 ]; } return $return; } function flattenWorkflow($node, &$result = [], $parentId = null, &$idCounter = 0) { $idCounter++; $currentId = $idCounter; $nodeData = [ 'id' => $currentId, 'parent_id' => $parentId, ]; foreach ($node as $key => $value) { if (!in_array($key, ['childNode', 'conditionNodes'])) { $nodeData[$key] = $value; } } $result[] = $nodeData; // echo "Added node: " . $node['nodeName'] . "\n"; // Debugging line if ($node['type'] == 4 && isset($node['conditionNodes']) && is_array($node['conditionNodes']) && ! empty($node['conditionNodes'])) { foreach ($node['conditionNodes'] as $conditionNode) { $this->flattenWorkflow($conditionNode, $result, $currentId, $idCounter); } } if (isset($node['childNode']) && is_array($node['childNode']) && ! empty($node['childNode'])) { $this->flattenWorkflow($node['childNode'], $result, $currentId, $idCounter); } return $result; } // 0 发起人 1审批 2抄送 3条件 4路由 function settleWorkFlowData($data, $array){ $return = $return_detail = $map = []; if(empty($array)) return [true, [$return, $return_detail]]; foreach ($array as $key => $value){ $tmp = [ 'm_id' => $value['id'], 'type' => $value['type'], 'data_id' => 0, 'settype' => $value['settype'] ?? 0, 'selectMode' => $value['selectMode'] ?? 0, 'selectRange' => $value['selectRange'] ?? 0, 'examineMode' => $value['examineMode'] ?? 0, 'priorityLevel' => $value['priorityLevel'] ?? 0, ]; $tmp_return = $return; if($value['type'] == 0){ //发起人 $this->settleTypeZero($tmp, $data, $return); }elseif ($value['type'] == 1){ //审批人 list($status, $msg) = $this->settleTypeOne($tmp, $data, $value, $return); if(! $status) return [false, $msg]; }elseif ($value['type'] == 2){ //抄送人 $this->settleTypeTwo($tmp, $value, $return); }elseif ($value['type'] == 3){ //条件 $this->settleTypeThree($tmp, $value, $return, $return_detail); }elseif ($value['type'] == 4){ //路由 $this->settleTypeFour($tmp, $value, $return); }else{ return [false, '不存在的TYPE类型']; } $differences = array_diff_key($return, $tmp_return); $keys = array_keys($differences); $map[$key] = $keys; } return [true, [$return, $return_detail,$map]]; } //发起人 function settleTypeZero($tmp, $data, &$return){ if(empty($data['flowPermission'])) { //为空 则是 发起人为所有人 $return[] = $tmp; }else{ foreach ($data['flowPermission'] as $n){ //发起人为指定的一些人 $tmp['data_id'] = $n['targetId']; $return[] = $tmp; } } } //审批人 function settleTypeOne($tmp, $data, $value, &$return){ //"settype": "",// 审批人设置 1指定成员 2主管 4发起人自选 5发起人自己 7连续多级主管 //"selectMode": "", //审批人数 1选一个人 2选多个人 //"selectRange": "", //选择范围 1.全公司 2指定成员 2指定角色 //"directorLevel": "", //审批终点 最高层主管数 //"examineMode": "", //多人审批时采用的审批方式 1依次审批 2会签 //"noHanderAction": "",//审批人为空时 1自动审批通过/不允许发起 2转交给审核管理员 //"examineEndDirectorLevel": "", //审批终点 第n层主管 if($value['settype'] == 1){ foreach ($value['nodeUserList'] as $n){ //发起人为指定的一些人 $tmp['data_id'] = $n['targetId']; $return[] = $tmp; } }elseif ($value['settype'] == 2){ return [false, '暂不支持主管设置']; }elseif ($value['settype'] == 4){ if($value['selectMode'] == 1){ if($value['selectRange'] == 1){ //全公司下的一个人 $return[] = $tmp; }elseif ($value['selectRange'] == 2){ foreach ($value['nodeUserList'] as $n){ //指定人里的一个人 $tmp['data_id'] = $n['targetId']; $return[] = $tmp; } }else{ return [false, '非法参数:选择范围']; } }elseif ($value['selectMode'] == 2){ if($value['selectRange'] == 1){ //全公司里的所有人 $return[] = $tmp; }elseif ($value['selectRange'] == 2){ //指定人里的所有人 foreach ($value['nodeUserList'] as $n){ $tmp['data_id'] = $n['targetId']; $return[] = $tmp; } }else{ return [false, '非法参数:选择范围']; } } }elseif ($value['settype'] == 5){ if(empty($data['flowPermission'])) { //为空 则是 审核人为 公司里的随意一个人 $tmp['selectMode'] = 1; $tmp['selectRange'] = 1; $return[] = $tmp; }else{ foreach ($data['flowPermission'] as $n){ //审核人 发起人为指定的一些人 $tmp['data_id'] = $n['targetId']; $return[] = $tmp; } } }elseif ($value['settype'] == 7){ return [false, '暂不支持连续多级主管设置']; }else{ return [false, '非法参数:审批人设置']; } return [true, '']; } //抄送人 function settleTypeTwo($tmp, $value, &$return){ foreach ($value['nodeUserList'] as $n){ //抄送人为指定的一些人 $tmp['data_id'] = $n['targetId']; $return[] = $tmp; } } //条件 function settleTypeThree($tmp, $value, &$return, &$return_detail){ $return[] = $tmp; $lastIndex = count($return) - 1; $tt = []; foreach ($value['conditionList'] as $val){ $tt[] = [ 'column' => $val['columnDbname'], 'opt1' => $val['opt1'], 'opt2' => $val['opt2'], 'opt_type' => $val['optType'], 'zdy1' => $val['zdy1'], 'zdy2' => $val['zdy2'], ]; } $return_detail[$lastIndex] = $tt; } //路由 function settleTypeFour($tmp, $value, &$return){ $return[] = $tmp; } }