* [refac] refactor functions to reorder stories in a product plan to improve efficiency.

This commit is contained in:
liugang
2024-08-01 16:32:03 +08:00
committed by 刘刚
parent caab9e52e0
commit 85d03e2a38
2 changed files with 30 additions and 38 deletions
+6 -4
View File
@@ -433,15 +433,17 @@ class productplanZen extends productplan
*/
public function reorderStories()
{
/* 对需求重新按照父子关系排序,保证进入需求详情后上一页下一页的URL符合预期。 */
/* 获取不分页的 SQL 语句,为重新按照父子关系排序做准备。*/
$sql = $this->dao->get();
if(strpos($sql, 'LIMIT')) $sql = substr($sql, 0, strpos($sql, 'LIMIT'));
$parents = array();
/* 取出用户重新排序的关键字段。*/
$stories = array();
$query = $this->dao->query($sql);
while($story = $query->fetch()) $parents[$story->id] = $story->parent;
while($story = $query->fetch()) $stories[$story->id] = (object)['id' => $story->id, 'parent' => $story->parent, 'grade' => $story->grade];
$objectList = $this->loadModel('story')->reorderStories($parents);
/* 对需求重新按照父子关系排序,保证进入需求详情后上一页下一页的URL符合预期。 */
$objectList = $this->loadModel('story')->reorderStories($stories);
if($objectList) $this->session->set('storyBrowseList', array('sql' => $sql, 'idkey' => 'id', 'objectList' => $objectList), $this->app->tab);
}
}
+24 -34
View File
@@ -2497,53 +2497,43 @@ class storyTao extends storyModel
*/
public function reorderStories(array $stories): array
{
$tree = $this->buildStoryTree($stories);
$storyCount = count($stories);
$result = array();
$this->buildReorderResult($tree, $result);
/* 按照需求的层级倒序排序。*/
usort($stories, function($a, $b) {return $b->grade - $a->grade;});
/* 将需求按照父子关系组成树形结构。*/
foreach($stories as $id => $story)
{
if(!isset($stories[$story->parent])) continue;
$stories[$story->parent]->children[] = $story;
unset($stories[$id]);
}
/* 把树形嵌套数组提取成一维数组。*/
$orderedStories = $this->extractStories($stories);
/* 重排后数量如果和重排之前不一致,说明该列表只有子任务、没有父任务,使用重排之前的顺序。*/
return count($stories) == count($result) ? $result : array_keys($stories);
return $storyCount == count($orderedStories) ? $orderedStories : array_keys($stories);
}
/**
* 递归将一维需求数组构造成父子关系的嵌套数组。
* Build story tree by parent and children.
* 把树形嵌套数组提取成一维数组。
* Extract stories from nested array.
*
* @param array $stories
* @param int $parentId
* @access public
* @return array
*/
public function buildStoryTree(array &$stories, int $parentId = 0): array
public function extractStories(array $stories): array
{
$tree = array();
foreach($stories as $id => $parent)
$result = [];
foreach($stories as $story)
{
if($parent == $parentId)
{
$tree[$id] = $this->buildStoryTree($stories, $id);
}
}
return $tree;
}
/**
* 递归将父子关系的嵌套数组构造成一维需求数组。
* Build story result by parent and children.
*
* @param array $parent
* @param array $result
* @access public
* @return void
*/
public function buildReorderResult(array $parent, array &$result)
{
foreach($parent as $key => $children)
{
$result[$key] = $key;
if(!empty($children)) $this->buildReorderResult($children, $result);
$result[$story->id] = $story->id;
if(!empty($story->children)) $result += $this->extractStories($story->children);
}
return $result;
}
}