diff --git a/app/Http/Controllers/Api/ProjectController.php b/app/Http/Controllers/Api/ProjectController.php index 8070c53e..2515486d 100755 --- a/app/Http/Controllers/Api/ProjectController.php +++ b/app/Http/Controllers/Api/ProjectController.php @@ -204,6 +204,9 @@ class ProjectController extends AbstractController * @apiParam {String} name 项目名称 * @apiParam {String} [desc] 项目介绍 * @apiParam {String} [columns] 列表,格式:列表名称1,列表名称2 + * @apiParam {String} [flow] 开启流程 + * - open: 开启 + * - close: 关闭(默认) * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) @@ -215,6 +218,7 @@ class ProjectController extends AbstractController // 项目名称 $name = trim(Request::input('name', '')); $desc = trim(Request::input('desc', '')); + $flow = trim(Request::input('flow', 'close')); if (mb_strlen($name) < 2) { return Base::retError('项目名称不可以少于2个字'); } elseif (mb_strlen($name) > 32) { @@ -251,7 +255,7 @@ class ProjectController extends AbstractController 'desc' => $desc, 'userid' => $user->userid, ]); - AbstractModel::transaction(function() use ($insertColumns, $project) { + AbstractModel::transaction(function() use ($flow, $insertColumns, $project) { $project->save(); ProjectUser::createInstance([ 'project_id' => $project->id, @@ -268,6 +272,10 @@ class ProjectController extends AbstractController } $project->dialog_id = $dialog->id; $project->save(); + // + if ($flow == 'open') { + $project->addFlow(Base::json2array('[{"id":"-10","name":"\u5f85\u5904\u7406","status":"start","turns":["-10","-11","-12","-13"],"usertype":"add","userlimit":"0","sort":"0"},{"id":"-11","name":"\u8fdb\u884c\u4e2d","status":"progress","turns":["-10","-11","-12","-13"],"usertype":"add","userlimit":"0","sort":"1"},{"id":"-12","name":"\u5df2\u5b8c\u6210","status":"end","turns":["-10","-11","-12","-13"],"usertype":"add","userlimit":"0","sort":"2"},{"id":"-13","name":"\u5df2\u53d6\u6d88","status":"end","turns":["-10","-11","-12","-13"],"usertype":"add","userlimit":"0","sort":"3"}]')); + } }); // $data = Project::find($project->id); @@ -1641,95 +1649,7 @@ class ProjectController extends AbstractController // $project = Project::userProject($project_id, true, true); // - return AbstractModel::transaction(function() use ($project, $flows) { - $projectFlow = ProjectFlow::whereProjectId($project->id)->first(); - if (empty($projectFlow)) { - $projectFlow = ProjectFlow::createInstance([ - 'project_id' => $project->id, - 'name' => 'Default' - ]); - if (!$projectFlow->save()) { - throw new ApiException('工作流创建失败'); - } - } - // - $ids = []; - $idc = []; - $hasStart = false; - $hasEnd = false; - foreach ($flows as $item) { - $id = intval($item['id']); - $turns = Base::arrayRetainInt($item['turns'] ?: [], true); - $userids = Base::arrayRetainInt($item['userids'] ?: [], true); - $usertype = trim($item['usertype']); - $userlimit = intval($item['userlimit']); - if ($usertype == 'replace' && empty($userids)) { - throw new ApiException("状态[{$item['name']}]设置错误,设置流转模式时必须填写状态负责人"); - } - if ($usertype == 'merge' && empty($userids)) { - throw new ApiException("状态[{$item['name']}]设置错误,设置剔除模式时必须填写状态负责人"); - } - if ($userlimit && empty($userids)) { - throw new ApiException("状态[{$item['name']}]设置错误,设置限制负责人时必须填写状态负责人"); - } - $flow = ProjectFlowItem::updateInsert([ - 'id' => $id, - 'project_id' => $project->id, - 'flow_id' => $projectFlow->id, - ], [ - 'name' => trim($item['name']), - 'status' => trim($item['status']), - 'sort' => intval($item['sort']), - 'turns' => $turns, - 'userids' => $userids, - 'usertype' => trim($item['usertype']), - 'userlimit' => $userlimit, - ]); - if ($flow) { - $ids[] = $flow->id; - if ($flow->id != $id) { - $idc[$id] = $flow->id; - } - if ($flow->status == 'start') { - $hasStart = true; - } - if ($flow->status == 'end') { - $hasEnd = true; - } - } - } - if (!$hasStart) { - throw new ApiException('至少需要1个开始状态'); - } - if (!$hasEnd) { - throw new ApiException('至少需要1个结束状态'); - } - ProjectFlowItem::whereFlowId($projectFlow->id)->whereNotIn('id', $ids)->chunk(100, function($list) { - foreach ($list as $item) { - $item->deleteFlowItem(); - } - }); - // - $projectFlow = ProjectFlow::with(['projectFlowItem'])->whereProjectId($project->id)->find($projectFlow->id); - $itemIds = $projectFlow->projectFlowItem->pluck('id')->toArray(); - foreach ($projectFlow->projectFlowItem as $item) { - $turns = $item->turns; - foreach ($idc as $oid => $nid) { - if (in_array($oid, $turns)) { - $turns = array_diff($turns, [$oid]); - $turns[] = $nid; - } - } - if (!in_array($item->id, $turns)) { - $turns[] = $item->id; - } - $turns = array_values(array_filter(array_unique(array_intersect($turns, $itemIds)))); - sort($turns); - $item->turns = $turns; - ProjectFlowItem::whereId($item->id)->update([ 'turns' => Base::array2json($turns) ]); - } - return Base::retSuccess('保存成功', $projectFlow); - }); + return Base::retSuccess('保存成功', $project->addFlow($flows)); } /** diff --git a/app/Models/Project.php b/app/Models/Project.php index 76e452a3..a327595a 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Exceptions\ApiException; +use App\Module\Base; use App\Tasks\PushTask; use Carbon\Carbon; use DB; @@ -350,6 +351,104 @@ class Project extends AbstractModel } } + /** + * 添加工作流 + * @param $flows + * @return mixed + */ + public function addFlow($flows) + { + return AbstractModel::transaction(function() use ($flows) { + $projectFlow = ProjectFlow::whereProjectId($this->id)->first(); + if (empty($projectFlow)) { + $projectFlow = ProjectFlow::createInstance([ + 'project_id' => $this->id, + 'name' => 'Default' + ]); + if (!$projectFlow->save()) { + throw new ApiException('工作流创建失败'); + } + } + // + $ids = []; + $idc = []; + $hasStart = false; + $hasEnd = false; + foreach ($flows as $item) { + $id = intval($item['id']); + $turns = Base::arrayRetainInt($item['turns'] ?: [], true); + $userids = Base::arrayRetainInt($item['userids'] ?: [], true); + $usertype = trim($item['usertype']); + $userlimit = intval($item['userlimit']); + if ($usertype == 'replace' && empty($userids)) { + throw new ApiException("状态[{$item['name']}]设置错误,设置流转模式时必须填写状态负责人"); + } + if ($usertype == 'merge' && empty($userids)) { + throw new ApiException("状态[{$item['name']}]设置错误,设置剔除模式时必须填写状态负责人"); + } + if ($userlimit && empty($userids)) { + throw new ApiException("状态[{$item['name']}]设置错误,设置限制负责人时必须填写状态负责人"); + } + $flow = ProjectFlowItem::updateInsert([ + 'id' => $id, + 'project_id' => $this->id, + 'flow_id' => $projectFlow->id, + ], [ + 'name' => trim($item['name']), + 'status' => trim($item['status']), + 'sort' => intval($item['sort']), + 'turns' => $turns, + 'userids' => $userids, + 'usertype' => trim($item['usertype']), + 'userlimit' => $userlimit, + ]); + if ($flow) { + $ids[] = $flow->id; + if ($flow->id != $id) { + $idc[$id] = $flow->id; + } + if ($flow->status == 'start') { + $hasStart = true; + } + if ($flow->status == 'end') { + $hasEnd = true; + } + } + } + if (!$hasStart) { + throw new ApiException('至少需要1个开始状态'); + } + if (!$hasEnd) { + throw new ApiException('至少需要1个结束状态'); + } + ProjectFlowItem::whereFlowId($projectFlow->id)->whereNotIn('id', $ids)->chunk(100, function($list) { + foreach ($list as $item) { + $item->deleteFlowItem(); + } + }); + // + $projectFlow = ProjectFlow::with(['projectFlowItem'])->whereProjectId($this->id)->find($projectFlow->id); + $itemIds = $projectFlow->projectFlowItem->pluck('id')->toArray(); + foreach ($projectFlow->projectFlowItem as $item) { + $turns = $item->turns; + foreach ($idc as $oid => $nid) { + if (in_array($oid, $turns)) { + $turns = array_diff($turns, [$oid]); + $turns[] = $nid; + } + } + if (!in_array($item->id, $turns)) { + $turns[] = $item->id; + } + $turns = array_values(array_filter(array_unique(array_intersect($turns, $itemIds)))); + sort($turns); + $item->turns = $turns; + ProjectFlowItem::whereId($item->id)->update([ 'turns' => Base::array2json($turns) ]); + } + return $projectFlow; + }); + } + /** * 获取项目信息(用于判断会员是否存在项目内) * @param int $project_id