From 7a1c42f4f77ca5fd3ab32729ea2229aab16868ef Mon Sep 17 00:00:00 2001 From: liweisen Date: Sat, 9 May 2020 11:45:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=8E=92=E5=BA=8F=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common/Model/Task.php | 103 ++++++++++++++---- application/common/Model/TaskStages.php | 43 ++++++-- application/project/controller/Task.php | 6 +- application/project/controller/TaskStages.php | 2 +- 4 files changed, 122 insertions(+), 32 deletions(-) diff --git a/application/common/Model/Task.php b/application/common/Model/Task.php index 772bd86..ed80896 100644 --- a/application/common/Model/Task.php +++ b/application/common/Model/Task.php @@ -19,7 +19,7 @@ use think\facade\Hook; */ class Task extends CommonModel { - protected $append = ['priText','statusText', 'liked', 'stared', 'tags', 'childCount', 'hasUnDone', 'parentDone', 'hasComment', 'hasSource', 'canRead']; + protected $append = ['priText', 'statusText', 'liked', 'stared', 'tags', 'childCount', 'hasUnDone', 'parentDone', 'hasComment', 'hasSource', 'canRead']; public function read($code) { @@ -174,7 +174,7 @@ class Task extends CommonModel if ($like) { $result = self::where(['code' => $code])->setInc('like'); } else { - $result = self::where(['code' => $code])->setDec('like');; + $result = self::where(['code' => $code])->setDec('like'); } $member = getCurrentMember(); TaskLike::likeTask($code, $member['code'], $like); @@ -202,7 +202,7 @@ class Task extends CommonModel if ($star) { $result = self::where(['code' => $code])->setInc('star'); } else { - $result = self::where(['code' => $code])->setDec('star');; + $result = self::where(['code' => $code])->setDec('star'); } $member = getCurrentMember(); Collection::starTask($code, $member['code'], $star); @@ -268,6 +268,8 @@ class Task extends CommonModel if (!$maxNum) { $maxNum = 0; } + $maxSort = self::where('project_code', $projectCode)->where('stage_code', $stageCode)->max('sort'); + $maxSort = $maxSort ?? 0; $path = ''; if ($parentCode) { $parentTask['path'] && $parentTask['path'] = ",{$parentTask['path']}"; @@ -279,6 +281,7 @@ class Task extends CommonModel 'create_by' => $memberCode, 'assign_to' => $assignTo, 'id_num' => $maxNum + 1, + 'sort' => $maxSort + 500, 'project_code' => $projectCode, 'pcode' => $parentCode, 'path' => $path, @@ -456,6 +459,61 @@ class Task extends CommonModel // return ProjectLog::create($data); } + /** + * 任务排序,事件:把 $preCode 移动到 $nextCode 前面 + * @param $preCode string 前一个移动的列表 + * @param $nextCode string 后一个移动的列表 + * @param $toStageCode string 要移动到到的分组 + * @return bool + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function sort($preCode, $nextCode, $toStageCode) + { + $preTask = self::where(['code' => $preCode])->field('sort,stage_code,done')->find(); + if ($preCode == $nextCode) { + return false; + } + if ($preTask) { + $done = $preTask['done']; + if ($nextCode) { + $nextTask = self::where(['code' => $nextCode])->field('sort')->find(); + $nextPreTask = self::where('sort', '<', $nextTask['sort'])->where('code', '<>', $nextCode)->where('stage_code', '=', $toStageCode)->where('done', $done)->order('sort desc')->find(); + $nextPreTaskSort = $nextPreTask ? $nextPreTask['sort'] : 0; + $newSort = (int)($nextTask['sort'] - $nextPreTaskSort) / 2; + } else { + $maxSort = self::where('stage_code', '=', $toStageCode)->where('done', $done)->max('sort'); + $newSort = $maxSort + 500; + } + if ($newSort and $newSort > 50) { + $preTask->stage_code = $toStageCode; + $preTask->sort = $newSort; + $preTask->save(); + } else { +// 小于安全值 +// $this->resetSort($preTask['project_code']); +// $this->sort($preCode, $nextCode); + } + return true; + } + return false; + } + + + public function resetSort($stageCode) + { + $taskList = self::where('stage_code', $stageCode)->order('sort asc, id asc')->select(); + if ($taskList) { + $sort = 500; + foreach ($taskList as $task) { + $task->sort = $sort; + $task->save(); + $sort += 500; + } + } + } + /** * 任务排序 * @param $stageCode string 移到的任务列表code @@ -465,24 +523,26 @@ class Task extends CommonModel * @throws DbException * @throws ModelNotFoundException */ - public function sort($stageCode, $codes) - { - if (!$codes) { - return false; - } - if ($codes) { - $stage = TaskStages::where(['code' => $stageCode])->find(); - foreach ($codes as $key => $code) { - $task = self::where(['code' => $code])->find(); - self::update(['sort' => $key, 'stage_code' => $stageCode], ['code' => $code]); - if ($task['stage_code'] != $stageCode) { - self::taskHook(getCurrentMember()['code'], $code, 'move', '', '', '', '', '', ['stageName' => $stage['name']]); - } - } - return true; - } - return false; - } + /* public function sort($stageCode, $codes) + { + if (!$codes) { + return false; + } + if ($codes) { + $stage = TaskStages::where(['code' => $stageCode])->find(); + $sort = 0; + foreach ($codes as $key => $code) { + $task = self::where(['code' => $code])->find(); + self::update(['sort' => $sort, 'stage_code' => $stageCode], ['code' => $code]); + $sort += 500; + if ($task['stage_code'] != $stageCode) { + self::taskHook(getCurrentMember()['code'], $code, 'move', '', '', '', '', '', ['stageName' => $stage['name']]); + } + } + return true; + } + return false; + }*/ /** * 成员任务 @@ -723,6 +783,7 @@ class Task extends CommonModel $status = [0 => '普通', 1 => '紧急', 2 => '非常紧急']; return $status[$data['pri']]; } + public function getStatusTextAttr($value, $data) { if (!isset($data['status'])) { diff --git a/application/common/Model/TaskStages.php b/application/common/Model/TaskStages.php index 379e120..c0e810e 100644 --- a/application/common/Model/TaskStages.php +++ b/application/common/Model/TaskStages.php @@ -148,14 +148,16 @@ class TaskStages extends CommonModel if (!$project) { throw new \Exception('该项目已失效', 3); } + $maxSort = self::where('project_code', $projectCode)->max('sort'); + $maxSort = $maxSort ?? 0; $data = [ 'create_time' => nowTime(), 'code' => createUniqueCode('taskStages'), 'project_code' => $projectCode, + 'sort' => $maxSort + 500, 'name' => trim($name), ]; $result = self::create($data)->toArray(); - self::update(['sort' => $result['id']], ['id' => $result['id']]); if ($result) { unset($result['id']); $result['tasksLoading'] = false; //任务加载状态 @@ -168,7 +170,7 @@ class TaskStages extends CommonModel } /** - * 列表排序(交换两个列表的sort) + * 列表排序,事件:把 $preCode 移动到 $nextCode 前面 * @param $preCode string 前一个移动的列表 * @param $nextCode string 后一个移动的列表 * @return bool @@ -178,19 +180,46 @@ class TaskStages extends CommonModel */ public function sort($preCode, $nextCode) { - $preStage = self::where(['code' => $preCode])->field('sort')->find(); - $nextStage = self::where(['code' => $nextCode])->field('sort')->find(); + $preStage = self::where(['code' => $preCode])->field('sort,project_code')->find(); + $nextStage = self::where(['code' => $nextCode])->field('sort, project_code')->find(); + $projectCode = $preStage['project_code']; if ($preCode == $nextCode) { return false; } - if ($preStage !== false && $preStage !== false) { - self::update(['sort' => $nextStage['sort']], ['code' => $preCode]); - self::update(['sort' => $preStage['sort']], ['code' => $nextCode]); + if ($preStage) { + if ($nextCode) { + $nextPreStage = self::where('sort', '<', $nextStage['sort'])->where('code', '<>', $nextCode)->where('project_code', '=', $projectCode)->order('sort desc')->find(); + $nextPreStageSort = $nextPreStage ? $nextPreStage['sort'] : 0; + $newSort = (int)($nextStage['sort'] - $nextPreStageSort) / 2; + } else { + $maxSort = self::where('project_code', $projectCode)->max('sort'); + $newSort = $maxSort + 500; + } + if ($newSort and $newSort > 50) { + self::update(['sort' => $newSort], ['code' => $preCode]); + } else { + //小于安全值 + $this->resetSort($preStage['project_code']); + $this->sort($preCode, $nextCode); + } return true; } return false; } + public function resetSort($projectCode) + { + $taskStagesList = self::where('project_code', $projectCode)->order('sort asc, id asc')->select(); + if ($taskStagesList) { + $sort = 500; + foreach ($taskStagesList as $taskStage) { + $taskStage->sort = $sort; + $res = $taskStage->save(); + $sort += 500; + } + } + } + /** * 删除列表 * @param $code diff --git a/application/project/controller/Task.php b/application/project/controller/Task.php index 4d6ce8e..382c3cb 100644 --- a/application/project/controller/Task.php +++ b/application/project/controller/Task.php @@ -248,12 +248,12 @@ class Task extends BasicApi */ public function sort(Request $request) { - $data = $request::only('stageCode,codes'); - if (!$request::post('codes')) { + $data = $request::only('preTaskCode,nextTaskCode,toStageCode'); + if (!$request::post('preTaskCode')) { $this->error("参数有误"); } try { - $this->model->sort($data['stageCode'], explode(',', $data['codes'])); + $this->model->sort($data['preTaskCode'], $data['nextTaskCode'], $data['toStageCode']); } catch (Exception $e) { $this->error($e->getMessage(), $e->getCode());; } diff --git a/application/project/controller/TaskStages.php b/application/project/controller/TaskStages.php index 2db7a27..73aef3c 100644 --- a/application/project/controller/TaskStages.php +++ b/application/project/controller/TaskStages.php @@ -91,7 +91,7 @@ class TaskStages extends BasicApi public function sort(Request $request) { $data = $request::only('preCode,nextCode'); - if (!$request::post('preCode') || !$request::post('nextCode')) { + if (!$request::post('preCode')) { $this->error("参数有误"); } try {