* Refactor getList function.

This commit is contained in:
daitingting
2023-09-27 05:21:15 +00:00
parent 7787a824f1
commit 4d16e8d9eb
2 changed files with 65 additions and 44 deletions
+6 -44
View File
@@ -19,14 +19,15 @@ class projectreleaseModel extends model
* @param int $projectID
* @param string $type
* @param string $orderBy
* @param object $pager
* @access public
* @return array
*/
public function getList($projectID, $type = 'all', $orderBy = 't1.date_desc', $pager = null)
public function getList(int $projectID, string $type = 'all', string $orderBy = 't1.date_desc', object $pager = null): array
{
$releases = $this->dao->select('t1.*, t2.name as productName, t2.type as productType')->from(TABLE_RELEASE)->alias('t1')
$releases = $this->dao->select('t1.*, t2.name AS productName, t2.type AS productType')->from(TABLE_RELEASE)->alias('t1')
->leftJoin(TABLE_PRODUCT)->alias('t2')->on('t1.product = t2.id')
->where('t1.deleted')->eq(0)
->where('t1.deleted')->eq('0')
->andWhere("FIND_IN_SET($projectID, t1.project)")
->beginIF($type != 'all' && $type != 'review')->andWhere('t1.status')->eq($type)->fi()
->beginIF($type == 'review')->andWhere("FIND_IN_SET('{$this->app->user->account}', t1.reviewers)")->fi()
@@ -38,54 +39,15 @@ class projectreleaseModel extends model
$productIdList = array();
foreach($releases as $release)
{
$release->project = trim($release->project, ',');
$release->branch = trim($release->branch, ',');
$release->build = trim($release->build, ',');
$buildIdList = array_merge($buildIdList, explode(',', $release->build));
$productIdList[$release->product] = $release->product;
}
$branchGroup = $this->loadModel('branch')->getByProducts($productIdList);
$builds = $this->dao->select("id,project,product,branch,execution,name,scmPath,filePath")->from(TABLE_BUILD)->where('id')->in(array_unique($buildIdList))->fetchAll('id');
foreach($releases as $release)
{
$branchName = '';
if(isset($branchGroup[$release->product]))
{
$branches = $branchGroup[$release->product];
foreach(explode(',', $release->branch) as $releaseBranch)
{
if($releaseBranch == '') continue;
$branchName .= zget($branches, $releaseBranch, '');
$branchName .= ',';
}
$branchName = trim($branchName, ',');
}
$release->branchName = $branchName;
$builds = $this->dao->select("id, project, product, branch, execution, name, scmPath, filePath")->from(TABLE_BUILD)->where('id')->in(array_unique($buildIdList))->fetchAll('id');
$release->buildInfos = array();
foreach(explode(',', $release->build) as $buildID)
{
if(empty($buildID)) continue;
foreach($releases as $release) $this->projectreleaseTao->processRelease($release, $branchGroup, $builds);
$build = $builds[$buildID];
$branchName = '';
if(isset($branchGroup[$build->product]))
{
$branches = $branchGroup[$build->product];
foreach(explode(',', $build->branch) as $buildBranch)
{
if($buildBranch == '') continue;
$branchName .= zget($branches, $buildBranch, '');
$branchName .= ',';
}
$branchName = trim($branchName, ',');
}
$build->branchName = $branchName;
$release->buildInfos[$buildID] = $build;
}
}
return $releases;
}
+59
View File
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
class projectreleaseTao extends projectreleaseModel
{
/**
* 处理项目发布信息,包括分支名称、版本信息等。
* Process release.
*
* @param int $release
* @param int $branchGroup
* @param int $builds
* @access protected
* @return void
*/
protected function processRelease(object $release, array $branchGroup, array $builds): void
{
$release->project = trim($release->project, ',');
$release->branch = trim($release->branch, ',');
$release->build = trim($release->build, ',');
$release->branchName = $this->getBranchName($release->product, $release->branch, $branchGroup);
$release->buildInfos = array();
foreach(explode(',', $release->build) as $buildID)
{
if(empty($buildID)) continue;
$build = $builds[$buildID];
$build->branchName = $this->getBranchName($build->product, $build->branch, $branchGroup);
$release->buildInfos[$buildID] = $build;
}
}
/**
* 获取分支名称。
* Get branch name.
*
* @param int $productID
* @param string $branch
* @param array $branchGroup
* @access private
* @return string
*/
private function getBranchName(int $productID, string $branch, array $branchGroup): string
{
$branchName = '';
if(!isset($branchGroup[$productID])) return $branchName;
$branches = $branchGroup[$productID];
foreach(explode(',', $branch) as $branchID)
{
if($branchID == '') continue;
$branchName .= zget($branches, $branchID, '');
$branchName .= ',';
}
return trim($branchName, ',');
}
}