增加任务自动化流转

Signed-off-by: vilson <545522390@qq.com>
This commit is contained in:
vilson 2019-06-27 14:46:14 +08:00
parent 4d936e4273
commit 17aed0a29e
5 changed files with 144 additions and 49 deletions

View File

@ -2,6 +2,8 @@
namespace app\common\Model; namespace app\common\Model;
use think\Db;
/** /**
* 任务工作流 * 任务工作流
* Class TaskWorkflow * Class TaskWorkflow
@ -10,4 +12,30 @@ namespace app\common\Model;
class TaskWorkflow extends CommonModel class TaskWorkflow extends CommonModel
{ {
protected $append = []; protected $append = [];
public static function createData($name, $projectCode, $organizationCode)
{
$data = [
'create_time' => nowTime(),
'code' => createUniqueCode('TaskWorkflow'),
'project_code' => $projectCode,
'organization_code' => $organizationCode,
'name' => trim($name),
];
return self::create($data);
}
public static function del($code)
{
Db::startTrans();
try {
self::where(['code' => $code])->delete();
TaskWorkflowRule::where(['workflow_code' => $code])->delete();
} catch (\Exception $exception) {
Db::rollback();
return error(1, $exception->getMessage());
}
Db::commit();
return true;
}
} }

View File

@ -11,6 +11,35 @@ class TaskWorkflowRule extends CommonModel
{ {
protected $append = []; protected $append = [];
/**
* 保存规则
* @param $workflowCode
* @param $taskWorkflowRules Object
*/
public static function saveRules($workflowCode, $taskWorkflowRules)
{
TaskWorkflowRule::createData($workflowCode, 1, $taskWorkflowRules->firstObj);
TaskWorkflowRule::createData($workflowCode, 2, $taskWorkflowRules->firstAction->value, $taskWorkflowRules->firstAction->action);
TaskWorkflowRule::createData($workflowCode, 3, $taskWorkflowRules->firstResult->value, $taskWorkflowRules->firstResult->action);
if ($taskWorkflowRules->lastResult->value) {
TaskWorkflowRule::createData($workflowCode, 4, $taskWorkflowRules->lastResult->value, $taskWorkflowRules->lastResult->action);
}
}
public static function createData($workflowCode, $sort, $objectCode = '', $action = 0, $type = 0)
{
$data = [
'create_time' => nowTime(),
'code' => createUniqueCode('TaskWorkflowRule'),
'type' => $type,
'object_code' => $objectCode,
'action' => $action,
'sort' => $sort,
'workflow_code' => $workflowCode,
];
return self::create($data);
}
public static function getActionByTaskType($type) public static function getActionByTaskType($type)
{ {
switch ($type) { switch ($type) {
@ -50,7 +79,7 @@ class TaskWorkflowRule extends CommonModel
$stage = TaskStages::where(['code' => $toStageCode])->find(); $stage = TaskStages::where(['code' => $toStageCode])->find();
Task::taskHook(getCurrentMember()['code'], $task['code'], 'move', '', '', '', '', '', ['stageName' => $stage['name'], 'is_robot' => true]); Task::taskHook(getCurrentMember()['code'], $task['code'], 'move', '', '', '', '', '', ['stageName' => $stage['name'], 'is_robot' => true]);
} }
} elseif ($do['action'] == 1) { } elseif ($do['action'] == 3) {
//指派给 //指派给
try { try {
TaskMember::inviteMember($do['object_code'], $task['code'], 1, 0, false, true); TaskMember::inviteMember($do['object_code'], $task['code'], 1, 0, false, true);

View File

@ -190,7 +190,11 @@ class Task
if (in_array($data['type'], $workflowActions)) { if (in_array($data['type'], $workflowActions)) {
$taskStageCode = $task['stage_code']; $taskStageCode = $task['stage_code'];
$action = TaskWorkflowRule::getActionByTaskType($data['type']); $action = TaskWorkflowRule::getActionByTaskType($data['type']);
$taskWorkflowRule = TaskWorkflowRule::where(['object_code' => $taskStageCode, 'sort' => 1])->order('id asc')->find(); $taskWorkflowRules = TaskWorkflowRule::where(['object_code' => $taskStageCode, 'sort' => 1])->order('id asc')->select();
logRecord($taskWorkflowRules);
if ($taskWorkflowRules) {
foreach ($taskWorkflowRules as $taskWorkflowRule) {
// $taskWorkflowRule = TaskWorkflowRule::where(['object_code' => $taskStageCode, 'sort' => 1])->order('id asc')->find();
if ($taskWorkflowRule) { if ($taskWorkflowRule) {
$nextTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 2])->find(); $nextTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 2])->find();
if ($nextTaskWorkflowRule) { if ($nextTaskWorkflowRule) {
@ -209,7 +213,8 @@ class Task
} }
} }
} }
}
}
} }
} }
} }

View File

@ -45,6 +45,18 @@ class TaskStages extends BasicApi
$this->success('', $list); $this->success('', $list);
} }
public function _getAll()
{
$where = [];
$code = Request::post('projectCode');
if (!$code) {
$this->error("请选择一个项目");
}
$where[] = ['project_code', '=', $code];
$list = $this->model->where($where)->select();
$this->success('', $list);
}
/** /**
* 显示资源列表 * 显示资源列表
* @return void * @return void

View File

@ -2,7 +2,9 @@
namespace app\project\controller; namespace app\project\controller;
use app\common\Model\TaskWorkflowRule;
use controller\BasicApi; use controller\BasicApi;
use think\Db;
use think\facade\Request; use think\facade\Request;
/** /**
@ -30,7 +32,7 @@ class TaskWorkflow extends BasicApi
$this->error("请选择一个项目"); $this->error("请选择一个项目");
} }
$where[] = ['project_code', '=', $code]; $where[] = ['project_code', '=', $code];
$list = $this->model->where($where)->select(); $list = $this->model->where($where)->order('id asc')->select();
if ($list) { if ($list) {
foreach ($list as &$item) { foreach ($list as &$item) {
unset($item['id']); unset($item['id']);
@ -39,27 +41,36 @@ class TaskWorkflow extends BasicApi
$this->success('', $list); $this->success('', $list);
} }
public function _getTaskWorkflowRules()
{
$code = Request::post('taskWorkflowCode');
$list = TaskWorkflowRule::where(['workflow_code' => $code])->order('sort asc')->select();
$this->success('', $list);
}
/** /**
* 新增 * 新增
* @param Request $request * @param Request $request
* @return void * @return void
*/ */
public function save(Request $request) public function save()
{ {
$data = $request::only('name,projectCode'); $projectCode = Request::param('projectCode');
if (!$request::post('name')) { $taskWorkflowName = Request::param('taskWorkflowName');
$taskWorkflowRules = Request::param('taskWorkflowRules', '');
if (!trim($taskWorkflowName)) {
$this->error("请填写规则名称"); $this->error("请填写规则名称");
} }
try { if (!$taskWorkflowRules) {
$result = $this->model->createStage($data['name'], $data['projectCode']); $this->error("请定义具体规则");
} catch (\Exception $e) {
$this->error($e->getMessage(), $e->getCode());;
} }
if ($result) { $taskWorkflow = \app\common\Model\TaskWorkflow::createData($taskWorkflowName, $projectCode, getCurrentOrganizationCode());
$this->success('添加成功', $result); $taskWorkflow = \app\common\Model\TaskWorkflow::get($taskWorkflow['id']);
} $taskWorkflowRules = json_decode($taskWorkflowRules);
$this->error("操作失败,请稍候再试!"); TaskWorkflowRule::saveRules($taskWorkflow['code'], $taskWorkflowRules);
$this->success('添加成功', []);
// $this->error("操作失败,请稍候再试!");
} }
/** /**
@ -70,25 +81,38 @@ class TaskWorkflow extends BasicApi
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException * @throws \think\exception\DbException
*/ */
public function edit(Request $request) public function edit()
{ {
$data = $request::only('name,stageCode'); $taskWorkflowCode = Request::param('taskWorkflowCode');
if (!$request::post('name')) { $taskWorkflowName = Request::param('taskWorkflowName');
$taskWorkflowRules = Request::param('taskWorkflowRules', '');
$this->success('');
if (!trim($taskWorkflowName)) {
$this->error("请填写规则名称"); $this->error("请填写规则名称");
} }
if (!$data['stageCode']) { if (!$taskWorkflowRules) {
$this->error("请选择一个规则"); $this->error("请定义具体规则");
}
$template = $this->model->where(['code' => $data['stageCode']])->field('id')->find();
if (!$template) {
$this->error("该规则已失效");
}
$result = $this->model->_edit(['name' => $data['name']], ['code' => $data['stageCode']]);
if ($result) {
$this->success('');
} }
$taskWorkflow = \app\common\Model\TaskWorkflow::where(['code' => $taskWorkflowCode])->find();
if (!$taskWorkflow) {
$this->error("操作失败,请稍候再试!"); $this->error("操作失败,请稍候再试!");
} }
Db::startTrans();
try {
$taskWorkflow->name = $taskWorkflowName;
$taskWorkflow->update_time = nowTime();
$taskWorkflow->save();
TaskWorkflowRule::where(['workflow_code' => $taskWorkflowCode])->delete();
$taskWorkflowRules = json_decode($taskWorkflowRules);
TaskWorkflowRule::saveRules($taskWorkflow['code'], $taskWorkflowRules);
} catch (\Exception $exception) {
Db::rollback();
$this->error("操作失败,请稍候再试!");
}
Db::commit();
$this->success('');
}
/** /**
* 删除规则 * 删除规则
@ -96,17 +120,14 @@ class TaskWorkflow extends BasicApi
*/ */
public function delete() public function delete()
{ {
$code = Request::post('code'); $code = Request::post('taskWorkflowCode');
if (!$code) { if (!$code) {
$this->error("请选择一个规则"); $this->error("请选择一个规则");
} }
try { $result = \app\common\Model\TaskWorkflow::del($code);
$result = $this->model->deleteStage($code); if (!isError($result)) {
} catch (\Exception $e) { $this->success('删除成功');
$this->error($e->getMessage(), $e->getCode());;
}
if ($result) {
$this->success('');
} }
$this->error("操作失败,请稍候再试!");
} }
} }