增加任务自动化流转

Signed-off-by: vilson <545522390@qq.com>
This commit is contained in:
vilson 2019-06-26 17:46:38 +08:00
parent 703e7a7c3e
commit 3fd91ba2bb
5 changed files with 126 additions and 6 deletions

View File

@ -19,12 +19,14 @@ class TaskMember extends CommonModel
* @param $taskCode * @param $taskCode
* @param int $isExecutor * @param int $isExecutor
* @param int $isOwner * @param int $isOwner
* @param bool $fromCreate
* @param bool $isRobot 是否机器人
* @return TaskMember|bool * @return TaskMember|bool
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException * @throws \think\exception\DbException
*/ */
public static function inviteMember($memberCode, $taskCode, $isExecutor = 0, $isOwner = 0, $fromCreate = false) public static function inviteMember($memberCode, $taskCode, $isExecutor = 0, $isOwner = 0, $fromCreate = false, $isRobot = false)
{ {
!$memberCode && $memberCode = ''; !$memberCode && $memberCode = '';
$task = Task::where(['code' => $taskCode, 'deleted' => 0])->find(); $task = Task::where(['code' => $taskCode, 'deleted' => 0])->find();
@ -47,7 +49,7 @@ class TaskMember extends CommonModel
if ($memberCode == $currentMember['code']) { if ($memberCode == $currentMember['code']) {
$logType = 'claim'; $logType = 'claim';
} }
Task::taskHook($currentMember['code'], $taskCode, $logType, $memberCode); Task::taskHook($currentMember['code'], $taskCode, $logType, $memberCode, 0, '', '', '', ['is_robot' => $isRobot]);
// throw new \Exception('该成员已参与任务', 2); // throw new \Exception('该成员已参与任务', 2);
return true; return true;
} }
@ -57,7 +59,7 @@ class TaskMember extends CommonModel
Task::update(['assign_to' => $memberCode], ['code' => $taskCode]); Task::update(['assign_to' => $memberCode], ['code' => $taskCode]);
if (!$fromCreate) { if (!$fromCreate) {
if ($taskExecutor) { if ($taskExecutor) {
Task::taskHook($currentMember['code'], $taskCode, 'removeExecutor', $taskExecutor['member_code']); Task::taskHook($currentMember['code'], $taskCode, 'removeExecutor', $taskExecutor['member_code'], 0, '', '', '', ['is_robot' => $isRobot]);
} }
} }
return true; return true;
@ -75,9 +77,9 @@ class TaskMember extends CommonModel
if ($isExecutor) { if ($isExecutor) {
Task::update(['assign_to' => $memberCode], ['code' => $taskCode]); Task::update(['assign_to' => $memberCode], ['code' => $taskCode]);
if ($memberCode == $currentMember['code']) { if ($memberCode == $currentMember['code']) {
Task::taskHook($currentMember['code'], $taskCode, 'claim'); Task::taskHook($currentMember['code'], $taskCode, 'claim','',0, '', '', '', ['is_robot' => $isRobot]);
} else { } else {
Task::taskHook($currentMember['code'], $taskCode, 'assign', $memberCode); Task::taskHook($currentMember['code'], $taskCode, 'assign', $memberCode,0, '', '', '', ['is_robot' => $isRobot]);
} }
} }
if ($memberCode) { if ($memberCode) {

View File

@ -0,0 +1,13 @@
<?php
namespace app\common\Model;
/**
* 任务工作流
* Class TaskWorkflow
* @package app\common\Model
*/
class TaskWorkflow extends CommonModel
{
protected $append = [];
}

View File

@ -0,0 +1,66 @@
<?php
namespace app\common\Model;
/**
* 任务工作流规则
* Class TaskWorkflow
* @package app\common\Model
*/
class TaskWorkflowRule extends CommonModel
{
protected $append = [];
public static function getActionByTaskType($type)
{
switch ($type) {
case 'done':
return 1;
case 'redo':
return 2;
case 'assign':
return 3;
case 'setEndTime':
return 4;
case 'pri':
return 5;
default:
return 0;
}
}
/**
* 执行策略
* @param $task
* @param $do
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function doAction($task, $do)
{
if ($do['action'] == 0) {
//跳转到
$oldStageCode = $task->stage_code;
$toStageCode = $do['object_code'];
$task->stage_code = $toStageCode;
$task->save();
if ($oldStageCode != $toStageCode) {
$stage = TaskStages::where(['code' => $toStageCode])->find();
Task::taskHook(getCurrentMember()['code'], $task['code'], 'move', '', '', '', '', '', ['stageName' => $stage['name'], 'is_robot' => true]);
}
} elseif ($do['action'] == 1) {
//指派给
try {
TaskMember::inviteMember($do['object_code'], $task['code'], 1, 0, false, true);
} catch (\Exception $exception) {
}
}
$next = self::where(['workflow_code' => $do['workflow_code'], 'sort' => $do['sort'] + 1])->find();
if ($next) {
return self::doAction($task, $next);
}
return true;
}
}

View File

@ -16,6 +16,7 @@ use app\common\Model\ProjectLog;
use app\common\Model\ProjectVersion; use app\common\Model\ProjectVersion;
use app\common\Model\TaskMember; use app\common\Model\TaskMember;
use app\common\Model\TaskStages; use app\common\Model\TaskStages;
use app\common\Model\TaskWorkflowRule;
use message\DingTalk; use message\DingTalk;
use service\MessageService; use service\MessageService;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
@ -35,7 +36,10 @@ class Task
public function run($data) public function run($data)
{ {
Log::init(['path' => 'log/task']); Log::init(['path' => 'log/task']);
$logData = ['member_code' => $data['memberCode'], 'source_code' => $data['taskCode'], 'remark' => $data['remark'], 'type' => $data['type'], 'content' => $data['content'], 'is_comment' => $data['isComment'], 'to_member_code' => $data['toMemberCode'], 'create_time' => nowTime(), 'code' => createUniqueCode('projectLog'), 'action_type' => 'task']; $isRobot = (isset($data['data']) && isset($data['data']['is_robot']) && $data['data']['is_robot']) ? 1 : 0;
logRecord($isRobot,'robot');
$logData = ['member_code' => $data['memberCode'], 'source_code' => $data['taskCode'], 'remark' => $data['remark'], 'type' => $data['type'], 'content' => $data['content'], 'is_comment' => $data['isComment'], 'to_member_code' => $data['toMemberCode'], 'create_time' => nowTime(), 'code' => createUniqueCode('projectLog'), 'action_type' => 'task', 'is_robot' => $isRobot];
logRecord($data);
$task = \app\common\Model\Task::where(['code' => $data['taskCode']])->find(); $task = \app\common\Model\Task::where(['code' => $data['taskCode']])->find();
$logData['project_code'] = $task['project_code']; $logData['project_code'] = $task['project_code'];
$toMember = []; $toMember = [];
@ -172,6 +176,9 @@ class Task
break; break;
} }
$logData['icon'] = $icon; $logData['icon'] = $icon;
if ($logData['is_robot']) {
$logData['icon'] = 'alert';
}
if (!$data['remark']) { if (!$data['remark']) {
$logData['remark'] = $remark; $logData['remark'] = $remark;
} }
@ -179,6 +186,34 @@ class Task
$logData['content'] = $content; $logData['content'] = $content;
} }
ProjectLog::create($logData); ProjectLog::create($logData);
//工作流事件
$workflowActions = ['create', 'move', 'done', 'redo', 'assign', 'setEndTime', 'pri'];
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) {
$taskWorkflowRuleList = TaskWorkflowRule::where(['workflow_code' => $taskWorkflowRule['workflow_code']])->order('sort asc')->select();
if ($taskWorkflowRuleList) {
$condition = $taskWorkflowRuleList[1];
if ($condition['action'] == $action) {
$goon = true;
if ($action == 3) {
//设置执行人
$toMemberCode = $toMember['code'];
if ($toMemberCode != $condition['object_code']) {
$goon = false;
}
}
if ($goon) {
$do = $taskWorkflowRuleList[2];
TaskWorkflowRule::doAction($task, $do);
}
}
}
}
}
//触发推送的事件 //触发推送的事件
$notifyActions = ['done', 'redo', 'assign']; $notifyActions = ['done', 'redo', 'assign'];
if (in_array($data['type'], $notifyActions)) { if (in_array($data['type'], $notifyActions)) {

View File

@ -451,6 +451,10 @@ class Task extends BasicApi
} }
if ($list['list']) { if ($list['list']) {
foreach ($list['list'] as &$item) { foreach ($list['list'] as &$item) {
if ($item['is_robot'] && $item['type'] != 'claim') {
$item['member'] = ['name' => 'PP Robot'];
continue;
}
$member = Member::where(['code' => $item['member_code']])->field('id,name,avatar,code')->find(); $member = Member::where(['code' => $item['member_code']])->field('id,name,avatar,code')->find();
!$member && $member = []; !$member && $member = [];
$item['member'] = $member; $item['member'] = $member;