增加任务自动化流转

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;
use think\Db;
/**
* 任务工作流
* Class TaskWorkflow
@ -10,4 +12,30 @@ namespace app\common\Model;
class TaskWorkflow extends CommonModel
{
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 = [];
/**
* 保存规则
* @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)
{
switch ($type) {
@ -50,7 +79,7 @@ class TaskWorkflowRule extends CommonModel
$stage = TaskStages::where(['code' => $toStageCode])->find();
Task::taskHook(getCurrentMember()['code'], $task['code'], 'move', '', '', '', '', '', ['stageName' => $stage['name'], 'is_robot' => true]);
}
} elseif ($do['action'] == 1) {
} elseif ($do['action'] == 3) {
//指派给
try {
TaskMember::inviteMember($do['object_code'], $task['code'], 1, 0, false, true);

View File

@ -190,26 +190,31 @@ class Task
if (in_array($data['type'], $workflowActions)) {
$taskStageCode = $task['stage_code'];
$action = TaskWorkflowRule::getActionByTaskType($data['type']);
$taskWorkflowRule = TaskWorkflowRule::where(['object_code' => $taskStageCode, 'sort' => 1])->order('id asc')->find();
if ($taskWorkflowRule) {
$nextTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 2])->find();
if ($nextTaskWorkflowRule) {
if ($nextTaskWorkflowRule['action'] == $action) {
$goon = true;
if ($action == 3) {
//设置执行人
$toMemberCode = $toMember['code'];
if ($toMemberCode != $nextTaskWorkflowRule['object_code']) {
$goon = false;
$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) {
$nextTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 2])->find();
if ($nextTaskWorkflowRule) {
if ($nextTaskWorkflowRule['action'] == $action) {
$goon = true;
if ($action == 3) {
//设置执行人
$toMemberCode = $toMember['code'];
if ($toMemberCode != $nextTaskWorkflowRule['object_code']) {
$goon = false;
}
}
if ($goon) {
$doTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 3])->find();
$doTaskWorkflowRule && TaskWorkflowRule::doAction($task, $doTaskWorkflowRule);
}
}
}
if ($goon) {
$doTaskWorkflowRule = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code'], 'sort' => 3])->find();
$doTaskWorkflowRule && TaskWorkflowRule::doAction($task, $doTaskWorkflowRule);
}
}
}
}
}
}

View File

@ -45,6 +45,18 @@ class TaskStages extends BasicApi
$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

View File

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