| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- <?php
- namespace App\Service;
- use App\Model\Draft;
- use App\Model\Employee;
- use App\Model\SysMenu;
- use App\Model\WorkFlowInstances;
- use App\Model\WorkFlowInstancesNodes;
- use App\Model\WorkFlowTemplates;
- use Illuminate\Support\Facades\DB;
- class WorkFlowService extends Service
- {
- public function workFlowEdit($data,$user){
- list($status,$msg) = $this->workFlowRule($data, $user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = WorkFlowTemplates::where('id', $data['id'])->first();
- $model->code = $data['code'] ?? '';
- $model->title = $data['title'] ?? '';
- $model->content = $data['content'] ?? '';
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function workFlowAdd($data,$user){
- list($status,$msg) = $this->workFlowRule($data, $user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new WorkFlowTemplates();
- $model->code = $data['code'] ?? '';
- $model->title = $data['title'] ?? '';
- $model->content = $data['content'] ?? '';
- $model->crt_id = $user['id'];
- $model->top_depart_id = $user['top_depart_id'];
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function workFlowDetail($data, $user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = WorkFlowTemplates::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'审批流不存在或已被删除'];
- $customer = $customer->toArray();
- $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
- $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
- return [true, $customer];
- }
- public function workFlowDel($data, $user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = WorkFlowTemplates::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'审批流不存在或已被删除'];
- $customer->del_time = time();
- $customer->save();
- return [true, ''];
- }
- public function workFlowCommon($data,$user, $field = []){
- if(empty($field)) $field = WorkFlowTemplates::$field;
- $model = WorkFlowTemplates::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->select($field)
- ->orderby('id', 'desc');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['id'])) $model->whereIn('id', $data['id']);
- 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]);
- }
- return $model;
- }
- public function workFlowList($data,$user){
- $model = $this->workFlowCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true, $list];
- }
- public function workFlowRule(&$data, $user, $is_add = true) {
- if(empty($data['title'])) return [false, '审批流名称不能为空'];
- if(empty($data['code'])) return [false, '审批流编码不能为空'];
- if(empty($data['content'])) return [false, '审批流内容不能为空'];
- // 1. JSON 格式校验与转换
- $content = $data['content'];
- if (is_string($content)) {
- $content = json_decode($content, true);
- if (json_last_error() !== JSON_ERROR_NONE) {
- return [false, '审批流内容格式错误,不是有效的JSON'];
- }
- }
- // 2. 结构完整性校验
- if (!isset($content['nodes']) || !is_array($content['nodes'])) {
- return [false, '审批流配置缺少节点(nodes)信息'];
- }
- if (!isset($content['edges']) || !is_array($content['edges'])) {
- return [false, '审批流配置缺少连线(edges)信息'];
- }
- $data['content'] = $content;
- // 4. 业务唯一性校验
- $query = WorkFlowTemplates::where('code', $data['code'])
- ->where('top_depart_id', $user['top_depart_id'])
- ->where('del_time', 0);
- if (!$is_add) {
- if (empty($data['id'])) return [false, 'ID不能为空'];
- $query->where('id', '<>', $data['id']);
- }
- if ($query->exists()) return [false, '审批流编码已存在'];
- return [true, ''];
- }
- public function fillData($data){
- if(empty($data['data'])) return $data;
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_merge_recursive(array_column($data['data'],'charge_id'), array_column($data['data'],'crt_id'))));
- 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;
- }
- /**
- * 触发审批流实例
- * * @param string $templateCode 审批流唯一编码
- * @param int $documentId 业务单据ID
- * @param string $documentType 业务模型类名
- * @param array $user 当前操作用户信息
- */
- public function triggerWorkflow($templateID, $documentId, $documentType, $user)
- {
- if($user['select_tree_type'] == SysMenu::tree_type_zero) return [true, ''];
- // 1. 获取对应的审批流模板
- $template = WorkFlowTemplates::where('id', $templateID)
- ->where('top_depart_id', $user['top_depart_id'])
- ->where('del_time', 0)
- ->first();
- if (!$template) return [false, '审批模板不存在'];
- // 2. 创建流程主实例
- $instance = WorkFlowInstances::create([
- 'template_id' => $template->id,
- 'document_id' => $documentId,
- 'document_type' => $documentType,
- 'status' => WorkFlowInstances::status_two, // 激活
- 'crt_id' => $user['id'],
- 'crt_time' => time(),
- 'upd_time' => time(),
- 'top_depart_id' => $user['top_depart_id']
- ]);
- // 获取前端原始 JSON 中的节点和连线 [cite: 1]
- $nodes = $template->content['nodes'];
- $edges = $template->content['edges'];
- // 3. 准备批量插入的节点数据
- $insertNodes = [];
- foreach ($nodes as $node) {
- $nodeKey = $node['id'];
- // 从 edges 中寻找当前节点的前置 (source 为谁) 和 后置 (target 为谁) [cite: 1]
- // 这里的 source 和 target 是 edges 顶层的字符串 ID
- $prev = collect($edges)->where('target', $nodeKey)->pluck('source')->first();
- $next = collect($edges)->where('source', $nodeKey)->pluck('target')->first();
- $tempAssignees = $node['data']['assignees'] ?? [];
- foreach($tempAssignees as $k => $person) {
- $tempAssignees[$k]['is_pass'] = 0; // 0-未审,1-通过,2-驳回
- $tempAssignees[$k]['mark'] = '';
- $tempAssignees[$k]['pass_time'] = 0;
- }
- $insertNodes[] = [
- 'instance_id' => $instance->id,
- 'node_key' => $nodeKey,
- 'label' => $node['label'] ?? '未命名节点',
- 'prev_node_key' => $prev, // 上级节点 ID
- 'next_node_key' => $next, // 下级节点 ID
- // 审批人快照存储 [cite: 1]
- 'assignees' => json_encode($tempAssignees),
- // 审批类型:1 会签,2 或签 [cite: 1]
- 'approval_type' => $node['data']['type'] ?? 2,
- // 初始状态逻辑:如果没有前置节点,说明是首节点,直接进入“审批中”状态
- 'status' => $prev ? 0 : 1,
- 'handled_time' => 0,
- 'crt_time' => time(),
- 'upd_time' => time(),
- 'crt_id' => $user['id'],
- 'top_depart_id' => $user['top_depart_id']
- ];
- }
- // 4. 写入节点实例表
- WorkFlowInstancesNodes::insert($insertNodes);
- return [true, $instance->id];
- }
- /**
- * 获取当前用户的待办审核列表
- */
- public function getMyPendingApprovals($data, $user)
- {
- $userId = $user['id'];
- $topDepartId = $user['top_depart_id'];
- // 1. 构造查询对象
- $model = WorkFlowInstancesNodes::where('del_time',0)
- ->where('top_depart_id', $topDepartId)
- ->where('status', 1) // 1 = 审批中
- ->whereRaw('JSON_CONTAINS(assignees, JSON_OBJECT("id", ?))', [(int)$userId])
- // 如果我是“或签”,只要我还没审,节点状态就是1,我能看到;
- // 如果我是“会签”,我审过了但别人没审,节点状态还是1,但通过这个排除条件,我看不到了。
- ->whereRaw('NOT JSON_CONTAINS(assignees, JSON_OBJECT("id", ?, "is_pass", 1))', [$userId])
- ->orderBy('id', 'desc');
- $pageData = $this->limit($model, '', $data);
- if (empty($pageData['data'])) return [true, $pageData];
- // 3. 提取当前页的所有 instance_id
- $instanceIds = array_unique(array_column($pageData['data'], 'instance_id'));
- // 4. 获取关联信息
- $instances = WorkFlowInstances::with(['template'])
- ->whereIn('id', $instanceIds)
- ->get()
- ->keyBy('id');
- // 5. 组装返回数据 (遍历当前页的数据进行填充)
- foreach ($pageData['data'] as $key => $node) {
- $instance = $instances[$node['instance_id']] ?? null;
- $pageData['data'][$key] = [
- 'node_instance_id' => $node['id'],
- 'instance_id' => $node['instance_id'],
- 'node_label' => $node['label'],
- 'document_id' => $instance->document_id ?? 0, //业务单据id
- 'document_type' => $instance->document_type ?? '', //业务单据类型
- 'flow_title' => $instance->template->title ?? '未知流程',
- 'apply_time_fmt' => isset($instance->crt_time->timestamp) ? date('Y-m-d H:i:s', $instance->crt_time->timestamp) : '',
- 'approval_type' => $node['approval_type'],
- ];
- }
- return [true, $pageData];
- }
- public function approval($data, $user){
- list($status, $msg, $result) = $this->approve($data, $user);
- if(! $status) return [false, $msg];
- if($result == 1 || $result == 2){
- $instance_id = $msg;
- $this->businessSettle($result, $instance_id);
- }
- return [true, ''];
- }
- public function businessSettle($result, $instance_id){
- $w = WorkFlowInstances::where('id', $instance_id)->first();
- if(empty($w)) return;
- $w = $w->toArray();
- if($result == 1){
- $draft = Draft::where('del_time',0)
- ->where('document_type', $w['document_type'])
- ->where('document_id', $w['document_id'])
- ->where('top_depart_id', $w['top_depart_id'])
- ->latest()
- ->first();
- if(! empty($draft)){
- $draft = $draft->toArray();
- if($draft['opt_type'] == 1){
- //调用编辑接口 覆盖数据
- if($draft['document_type'] == 'item'){
- (new ItemService())->itemEditSave($draft['content'], $draft['user']);
- }
- }else{
- //单纯更新某个字段
- }
- }
- }else{
- //审核状态标记改回去
- DB::table($w['document_type'])->where('id', $w['document_id'])->update(['approval_state' => 0]);
- }
- }
- /**
- * @param int $nodeInstanceId 节点实例ID
- * @param int $status 结果:2-通过,3-驳回
- * @param string $remark 意见
- * @param array $user 当前操作人
- *
- * 业务结果说明:0-审批中(继续等待), 1-最终通过(业务单据可改为已完成), 2-已驳回(业务单据可改为已拒绝)
- */
- public function approve($data, $user)
- {
- $nodeInstanceId = $data['node_instance_id'] ?? 0;
- $status = $data['status'] ?? 2;
- $remark = $data['mark'] ?? '';
- $currentNode = WorkFlowInstancesNodes::where('del_time',0)
- ->where('id', $nodeInstanceId)
- ->where('top_depart_id', $user['top_depart_id'])
- ->first();
- if (!$currentNode || $currentNode->status != 1) return [false, '环节无效或已处理', 0];
- $assignees = is_string($currentNode->assignees) ? json_decode($currentNode->assignees, true) : $currentNode->assignees;
- $myIndex = collect($assignees)->search(fn($item) => $item['id'] == $user['id']);
- if ($myIndex === false) return [false, '您不在审批人名单中', 0];
- try {
- DB::beginTransaction();
- $time = time();
- $assignees[$myIndex]['mark'] = $remark;
- $assignees[$myIndex]['pass_time'] = $time;
- // --- 情况 A:驳回 ---
- if ($status == 3) {
- $currentNode->status = 3;
- $currentNode->handled_time = $time;
- $assignees[$myIndex]['is_pass'] = 2;
- $currentNode->assignees = $assignees;
- $currentNode->save();
- WorkFlowInstances::where('id', $currentNode->instance_id)->update(['status' => WorkFlowInstances::status_four]);
- DB::commit();
- return [true, $currentNode->instance_id, 2];
- }
- // --- 情况 B:通过 ---
- $assignees[$myIndex]['is_pass'] = 1;
- $currentNode->assignees = $assignees;
- $shouldMoveToNext = false;
- if ($currentNode->approval_type == 2) {
- $shouldMoveToNext = true;
- } else {
- $unPassedCount = collect($assignees)->where('is_pass', '!=', 1)->count();
- if ($unPassedCount == 0) {
- $shouldMoveToNext = true;
- }
- }
- $finalResult = 0; // 默认:还在审批中
- if ($shouldMoveToNext) {
- $currentNode->status = 2;
- $currentNode->handled_time = $time;
- $currentNode->save();
- if (!empty($currentNode->next_node_key)) {
- $hasActiveNext = $this->activateNextNode($currentNode->instance_id, $currentNode->next_node_key);
- if (!$hasActiveNext) {
- // 如果逻辑上虽然有key但没找到下个节点,也视为终结
- WorkFlowInstances::where('id', $currentNode->instance_id)->update(['status' => WorkFlowInstances::status_three]);
- $finalResult = 1;
- }
- } else {
- // 没有下个节点,流程最终终结
- WorkFlowInstances::where('id', $currentNode->instance_id)->update(['status' => WorkFlowInstances::status_three]);
- $finalResult = 1; // 返回 1,表示业务层面可以执行“生效”动作
- }
- } else {
- // 会签中,还有人没审
- $currentNode->save();
- $finalResult = 0;
- }
- DB::commit();
- return [true, $currentNode->instance_id, $finalResult];
- } catch (\Exception $e) {
- DB::rollBack();
- return [false, $e->getMessage(), 0];
- }
- }
- private function activateNextNode($instanceId, $nextNodeKey)
- {
- $nextNode = WorkFlowInstancesNodes::where('instance_id', $instanceId)
- ->where('node_key', $nextNodeKey)
- ->where('del_time',0)
- ->first();
- if ($nextNode) {
- $nextNode->status = 1;
- $nextNode->save();
- return true;
- }
- return false;
- }
- }
|