diff --git a/module/report/model.php b/module/report/model.php index a4c0f89781..55bac7a66c 100644 --- a/module/report/model.php +++ b/module/report/model.php @@ -357,84 +357,18 @@ class reportModel extends model } /** + * 获取用户某年的产品下创建的需求、计划,创建和关闭的需求数量统计。 * Get count of created story,plan and closed story by accounts every product in this year. * * @param array $accounts - * @param int $year + * @param string $year * @access public * @return array */ - public function getUserYearProducts($accounts, $year) + public function getUserYearProducts(array $accounts, string $year): array { /* Get changed products in this year. */ - $products = $this->dao->select('id,name')->from(TABLE_PRODUCT) - ->where('deleted')->eq(0) - ->andWhere('LEFT(createdDate, 4)')->eq($year) - ->beginIF($accounts) - ->andWhere('createdBy', true)->in($accounts) - ->orWhere('PO')->in($accounts) - ->orWhere('QD')->in($accounts) - ->orWhere('RD')->in($accounts) - ->markRight(1) - ->fi() - ->andWhere('shadow')->eq(0) - ->fetchAll('id'); - - /* Get created plans in this year. */ - $plans = $this->dao->select('t1.id,t1.product')->from(TABLE_PRODUCTPLAN)->alias('t1') - ->leftJoin(TABLE_ACTION)->alias('t2')->on("t1.id=t2.objectID and t2.objectType='productplan'") - ->where('LEFT(t2.date, 4)')->eq($year) - ->andWhere('t1.deleted')->eq(0) - ->andWhere('t1.product')->in(array_keys($products)) - ->beginIF($accounts) - ->andWhere('t2.actor')->in($accounts) - ->fi() - ->andWhere('t2.action')->eq('opened') - ->fetchAll(); - - $planProducts = array(); - $planGroups = array(); - foreach($plans as $plan) - { - $planProducts[$plan->product] = $plan->product; - $planGroups[$plan->product][$plan->id] = $plan->id; - } - - $createStoryProducts = $this->dao->select('DISTINCT product')->from(TABLE_STORY) - ->where('LEFT(openedDate, 4)')->eq($year) - ->andWhere('deleted')->eq(0) - ->andWhere('product')->in(array_keys($products)) - ->beginIF($accounts)->andWhere('openedBy')->in($accounts)->fi() - ->fetchPairs('product', 'product'); - $closeStoryProducts = $this->dao->select('DISTINCT product')->from(TABLE_STORY) - ->where('LEFT(closedDate, 4)')->eq($year) - ->andWhere('deleted')->eq(0) - ->andWhere('product')->in(array_keys($products)) - ->beginIF($accounts)->andWhere('closedBy')->in($accounts)->fi() - ->fetchPairs('product', 'product'); - if($createStoryProducts or $closeStoryProducts) - { - $products += $this->dao->select('id,name')->from(TABLE_PRODUCT) - ->where('id')->in($createStoryProducts + $closeStoryProducts + $planProducts) - ->andWhere('deleted')->eq(0) - ->fetchAll('id'); - } - - $createdStoryStats = $this->dao->select("product,sum(if((type = 'requirement'), 1, 0)) as requirement, sum(if((type = 'story'), 1, 0)) as story")->from(TABLE_STORY) - ->where('product')->in(array_keys($products)) - ->andWhere('deleted')->eq(0) - ->andWhere('LEFT(openedDate, 4)')->eq($year) - ->beginIF($accounts)->andWhere('openedBy')->in($accounts)->fi() - ->groupBy('product') - ->fetchAll('product'); - - $closedStoryStats = $this->dao->select("product,sum(if((status = 'closed'), 1, 0)) as closed")->from(TABLE_STORY) - ->where('product')->in(array_keys($products)) - ->andWhere('deleted')->eq(0) - ->andWhere('LEFT(closedDate, 4)')->eq($year) - ->beginIF($accounts)->andWhere('closedBy')->in($accounts)->fi() - ->groupBy('product') - ->fetchAll('product'); + list($products, $planGroups, $createdStoryStats, $closedStoryStats) = $this->reportTao->getAnnualProductStat($accounts, $year); /* Merge created plan, created story and closed story in every product. */ foreach($products as $productID => $product) diff --git a/module/report/tao.php b/module/report/tao.php new file mode 100644 index 0000000000..75eda77fc7 --- /dev/null +++ b/module/report/tao.php @@ -0,0 +1,64 @@ + + * @package report + * @link https://www.zentao.net + */ +class reportTao extends reportModel +{ + /** + * 获取某年的年度产品数据。 + * Get annual product stat. + * + * @param array $accounts + * @param string $year + * @access public + * @return array + */ + protected function getAnnualProductStat(array $accounts, string $year): array + { + /* Get created plans, created stories and closed stories in this year. */ + $planGroups = $this->dao->select('t1.id,t1.product')->from(TABLE_PRODUCTPLAN)->alias('t1') + ->leftJoin(TABLE_ACTION)->alias('t2')->on("t1.id=t2.objectID and t2.objectType='productplan'") + ->where('t1.deleted')->eq(0) + ->andWhere('LEFT(t2.date, 4)')->eq($year) + ->beginIF($accounts)->andWhere('t2.actor')->in($accounts)->fi() + ->andWhere('t2.action')->eq('opened') + ->fetchGroup('product', 'id'); + $createdStoryStats = $this->dao->select("product,sum(if((type = 'requirement'), 1, 0)) as requirement, sum(if((type = 'story'), 1, 0)) as story")->from(TABLE_STORY) + ->where('deleted')->eq(0) + ->andWhere('LEFT(openedDate, 4)')->eq($year) + ->beginIF($accounts)->andWhere('openedBy')->in($accounts)->fi() + ->groupBy('product') + ->fetchAll('product'); + $closedStoryStats = $this->dao->select("product,sum(if((status = 'closed'), 1, 0)) as closed")->from(TABLE_STORY) + ->where('deleted')->eq(0) + ->andWhere('LEFT(closedDate, 4)')->eq($year) + ->beginIF($accounts)->andWhere('closedBy')->in($accounts)->fi() + ->groupBy('product') + ->fetchAll('product'); + + /* Get products created or operated in this year. */ + $products = $this->dao->select('id,name')->from(TABLE_PRODUCT) + ->where('deleted')->eq(0) + ->andWhere('LEFT(createdDate, 4)', true)->eq($year) + ->beginIF($accounts) + ->andWhere('createdBy', true)->in($accounts) + ->orWhere('PO')->in($accounts) + ->orWhere('QD')->in($accounts) + ->orWhere('RD')->in($accounts) + ->markRight(1) + ->fi() + ->orWhere('id')->in(array_merge(array_keys($planGroups), array_keys($createdStoryStats), array_keys($closedStoryStats))) + ->markRight(1) + ->andWhere('shadow')->eq(0) + ->fetchAll('id'); + + return array($products, $planGroups, $createdStoryStats, $closedStoryStats); + } +} diff --git a/module/report/test/model/getuseryearproducts.php b/module/report/test/model/getuseryearproducts.php index fc435a7220..b1dabf1c8d 100755 --- a/module/report/test/model/getuseryearproducts.php +++ b/module/report/test/model/getuseryearproducts.php @@ -1,8 +1,15 @@ #!/usr/bin/env php gen(20); +zdTable('story')->gen(20); +zdTable('productplan')->gen(20); +zdTable('user')->gen(1); + +su('admin'); /** @@ -10,19 +17,14 @@ title=测试 reportModel->getUserYearProducts(); cid=1 pid=1 -测试获取本年度 po1 的产品数 >> 1,11,21,31,41,51,61,71,81,91,101,111 -测试获取本年度 dev17 的产品数 >> 17,117 -测试获取本年度 test18 的产品数 >> 18,118 -测试获取本年度 po1 dev17 的产品数 >> 1,11,17,21,31,41,51,61,71,81,91,101,111,117 -测试获取本年度 po1 test18 的产品数 >> 1,11,18,21,31,41,51,61,71,81,91,101,111,118 - */ -$account = array('po1', 'dev17', 'test18', 'po1,dev17', 'po1,test18'); +$account = array(array('admin'), array('dev17'), array('test18'), array('admin', 'dev17'), array('admin', 'test18'), array()); $report = new reportTest(); -r($report->getUserYearProductsTest($account[0])) && p() && e('1,11,21,31,41,51,61,71,81,91,101,111'); // 测试获取本年度 po1 的产品数 -r($report->getUserYearProductsTest($account[1])) && p() && e('17,117'); // 测试获取本年度 dev17 的产品数 -r($report->getUserYearProductsTest($account[2])) && p() && e('18,118'); // 测试获取本年度 test18 的产品数 -r($report->getUserYearProductsTest($account[3])) && p() && e('1,11,17,21,31,41,51,61,71,81,91,101,111,117'); // 测试获取本年度 po1 dev17 的产品数 -r($report->getUserYearProductsTest($account[4])) && p() && e('1,11,18,21,31,41,51,61,71,81,91,101,111,118'); // 测试获取本年度 po1 test18 的产品数 \ No newline at end of file +r($report->getUserYearProductsTest($account[0])) && p() && e('1,2,3,4,5'); // 测试获取本年度 admin 的产品数 +r($report->getUserYearProductsTest($account[1])) && p() && e('17'); // 测试获取本年度 dev17 的产品数 +r($report->getUserYearProductsTest($account[2])) && p() && e('1,18'); // 测试获取本年度 test18 的产品数 +r($report->getUserYearProductsTest($account[3])) && p() && e('1,2,3,4,5,17'); // 测试获取本年度 admin dev17 的产品数 +r($report->getUserYearProductsTest($account[4])) && p() && e('1,2,3,4,5,18'); // 测试获取本年度 admin test18 的产品数 +r($report->getUserYearProductsTest($account[5])) && p() && e('1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20'); // 测试获取本年度 所有用户 的产品数 diff --git a/module/report/test/report.class.php b/module/report/test/report.class.php index 444d9e1e5f..43f3edca0d 100644 --- a/module/report/test/report.class.php +++ b/module/report/test/report.class.php @@ -394,17 +394,16 @@ class reportTest } /** + * 测试获取本年度用户相关的每个产品的创建的需求和计划,关闭的需求数据。 * Test get count of created story,plan and closed story by accounts every product in this year. * - * @param mixed $accounts + * @param array $accounts * @access public - * @return void + * @return string|array */ - public function getUserYearProductsTest($accounts) + public function getUserYearProductsTest(array $accounts): string|array { - $year = date('Y'); - - $objects = $this->objectModel->getUserYearProducts($accounts, $year); + $objects = $this->objectModel->getUserYearProducts($accounts, date('Y')); if(dao::isError()) return dao::getError();