where($where) ->field($field) ->order($order) ->page($page, $limit) ->select() ->toArray(); $count = (new SchoolApprovalConfig())->where($where)->count(); return [ 'list' => $list, 'count' => $count ]; } /** * 获取审批流配置详情 * @param int $id * @return array */ public function getInfo(int $id): array { $info = (new SchoolApprovalConfig())->with(['nodes'])->where(['id' => $id])->find(); if (empty($info)) { return []; } return $info->toArray(); } /** * 添加审批流配置 * @param array $data * @return int * @throws \Exception */ public function add(array $data): int { Db::startTrans(); try { $config = [ 'config_name' => $data['config_name'], 'description' => $data['description'] ?? '', 'status' => $data['status'] ?? 1, 'creator_id' => $data['creator_id'] ]; $config_id = (new SchoolApprovalConfig())->insertGetId($config); // 添加节点 if (!empty($data['nodes'])) { $nodes = []; foreach ($data['nodes'] as $sequence => $node) { $nodes[] = [ 'config_id' => $config_id, 'node_name' => $node['node_name'], 'approver_type' => $node['approver_type'], 'approver_ids' => is_array($node['approver_ids']) ? implode(',', $node['approver_ids']) : $node['approver_ids'], 'sign_type' => $node['sign_type'] ?? SchoolApprovalConfigNode::SIGN_TYPE_OR, 'sequence' => $sequence + 1 ]; } (new SchoolApprovalConfigNode())->insertAll($nodes); } Db::commit(); return $config_id; } catch (\Exception $e) { Db::rollback(); throw new Exception($e->getMessage()); } } /** * 编辑审批流配置 * @param array $data * @return bool * @throws \Exception */ public function edit(array $data): bool { Db::startTrans(); try { $config = [ 'config_name' => $data['config_name'], 'description' => $data['description'] ?? '', 'status' => $data['status'] ?? 1 ]; (new SchoolApprovalConfig())->where(['id' => $data['id']])->update($config); // 先删除原有节点 (new SchoolApprovalConfigNode())->where(['config_id' => $data['id']])->delete(); // 添加新节点 if (!empty($data['nodes'])) { $nodes = []; foreach ($data['nodes'] as $sequence => $node) { $nodes[] = [ 'config_id' => $data['id'], 'node_name' => $node['node_name'], 'approver_type' => $node['approver_type'], 'approver_ids' => is_array($node['approver_ids']) ? implode(',', $node['approver_ids']) : $node['approver_ids'], 'sign_type' => $node['sign_type'] ?? SchoolApprovalConfigNode::SIGN_TYPE_OR, 'sequence' => $sequence + 1 ]; } (new SchoolApprovalConfigNode())->insertAll($nodes); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); throw new Exception($e->getMessage()); } } /** * 删除审批流配置 * @param int $id * @return bool * @throws \Exception */ public function delete(int $id): bool { Db::startTrans(); try { // 删除配置 (new SchoolApprovalConfig())->where(['id' => $id])->delete(); // 删除节点 (new SchoolApprovalConfigNode())->where(['config_id' => $id])->delete(); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); throw new Exception($e->getMessage()); } } /** * 修改状态 * @param int $id * @param int $status * @return bool */ public function changeStatus(int $id, int $status): bool { return (new SchoolApprovalConfig())->where(['id' => $id])->update(['status' => $status]) !== false; } }