优化数据获取

master
chenzhi 5 years ago
parent 06395bcb9d
commit 4cf967fbcd

@ -31,7 +31,7 @@ class SpendCountSetController extends \Think\Controller
die("init error");
}
$nowdate = date('Y')."-".(date('m') > 9 ? date('m') : "0".date('m'));
// $nowdate = "2017-10";
$nowdate = "2017-10";
//获取所有的月份 pay_time
$res = M("spend",'tab_')->field("FROM_UNIXTIME(pay_time,'%Y-%m') AS pay_time")->buildSql();
$res = M()->table($res.' a')->group('pay_time')->select();
@ -305,6 +305,7 @@ class SpendCountSetController extends \Think\Controller
$this->getRootUser();
$this->getParentUser();
$this->getPromoteUser();
$this->getInsideData2();
}
/** --------以下为辅助函数--------- **/
@ -315,17 +316,17 @@ class SpendCountSetController extends \Think\Controller
$month = $this->date;
$from = "
(
SELECT root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,inside_cash_count FROM tab_spend_count WHERE root_id > 0 and count_date='{$month}' group by root_id
UNION
SELECT promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,inside_cash_count FROM tab_spend_count WHERE parent_id = 0 and count_date='{$month}' group by promote_id
UNION
SELECT parent_id,parent_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,inside_cash_count FROM tab_spend_count WHERE parent_id > 0 and root_id = 0 and count_date='{$month}' group by parent_id
SELECT root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count FROM tab_spend_count WHERE root_id > 0 and count_date='{$month}' group by root_id
UNION ALL
SELECT promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count FROM tab_spend_count WHERE parent_id = 0 and count_date='{$month}' group by promote_id
UNION ALL
SELECT parent_id,parent_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count FROM tab_spend_count WHERE parent_id > 0 and root_id = 0 and count_date='{$month}' group by parent_id
)
";
$subQueryAll = M()->table($from." a")
->field('root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count')
->field('root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count')
->group("a.root_id")
->having("cash_count > 0 or balance_coin_count > 0 or inside_cash_count > 0")
// ->having("cash_count > 0 OR balance_coin_count > 0")
->select();
for ($i=0; $i<count($subQueryAll); $i++) {
$adddata = array(
@ -337,14 +338,14 @@ class SpendCountSetController extends \Think\Controller
"root_name"=>'官方渠道',
"cash_count"=>$subQueryAll[$i]['cash_count'],
"balance_coin_count"=>$subQueryAll[$i]['balance_coin_count'],
"inside_cash_count"=>$subQueryAll[$i]['inside_cash_count'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count']-0+$subQueryAll[$i]['inside_cash_count'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count'],
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
M("spend_user_count","tab_")->add($adddata);
}
}
//获取组长信息
public function getParentUser()
{
@ -352,16 +353,15 @@ class SpendCountSetController extends \Think\Controller
//获取数据
$from = "
(
SELECT parent_id,parent_name,root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,inside_cash_count FROM tab_spend_count WHERE root_id > 0 and count_date='{$month}' group by parent_id
SELECT parent_id,parent_name,root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count FROM tab_spend_count WHERE root_id > 0 and count_date='{$month}' group by parent_id
UNION
SELECT promote_id,promote_account,parent_id,parent_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,inside_cash_count FROM tab_spend_count WHERE parent_id >0 and root_id = 0 and count_date='{$month}' group by promote_id
SELECT promote_id,promote_account,parent_id,parent_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count FROM tab_spend_count WHERE parent_id >0 and root_id = 0 and count_date='{$month}' group by promote_id
)
";
$subQueryAll = M()->table($from." a")
->field('parent_id,parent_name,root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count')
// ->limit(1)
->field('parent_id,parent_name,root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count')
->group("a.parent_id")
->having("cash_count > 0 or balance_coin_count > 0 or inside_cash_count > 0")
// ->having("cash_count > 0 or balance_coin_count > 0 ")
->select();
for ($i=0; $i<count($subQueryAll); $i++) {
$adddata = array(
@ -373,15 +373,14 @@ class SpendCountSetController extends \Think\Controller
"root_name"=>'官方渠道',
"cash_count"=>$subQueryAll[$i]['cash_count'],
"balance_coin_count"=>$subQueryAll[$i]['balance_coin_count'],
"inside_cash_count"=>$subQueryAll[$i]['inside_cash_count'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count']-0+$subQueryAll[$i]['inside_cash_count'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count'],
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
M("spend_user_count","tab_")->add($adddata);
}
# code...
}
//获取组员
public function getPromoteUser()
{
@ -389,14 +388,13 @@ class SpendCountSetController extends \Think\Controller
//获取数据
$from = "
(
SELECT promote_id,promote_account,parent_id,parent_name,root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,inside_cash_count FROM tab_spend_count WHERE parent_id > 0 and root_id > 0 and count_date='{$month}' group by promote_id
SELECT promote_id,promote_account,parent_id,parent_name,root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count FROM tab_spend_count WHERE parent_id > 0 and root_id > 0 and count_date='{$month}' group by promote_id
)
";
$subQueryAll = M()->table($from." a")
->field('*')
// ->limit(1)
->group("a.promote_id")
->having("cash_count > 0 or balance_coin_count > 0 or inside_cash_count > 0")
// ->having("cash_count > 0 or balance_coin_count > 0")
->select();
for ($i=0; $i<count($subQueryAll); $i++) {
$adddata = array(
@ -408,8 +406,7 @@ class SpendCountSetController extends \Think\Controller
"root_name"=>$subQueryAll[$i]['root_name'],
"cash_count"=>$subQueryAll[$i]['cash_count'],
"balance_coin_count"=>$subQueryAll[$i]['balance_coin_count'],
"inside_cash_count"=>$subQueryAll[$i]['inside_cash_count'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count']-0+$subQueryAll[$i]['inside_cash_count'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count'],
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
@ -417,5 +414,63 @@ class SpendCountSetController extends \Think\Controller
}
# code...
}
//处理inside_cash_count
public function getInsideData2()
{
$insideRes = M()
->table("tab_deposit deposit")
->field("promote_id,promote_account,IFNULL(promote.parent_id,0) parent_id,parent_name,sum(pay_amount) inside_cash_count,'{$this->date}' as count_date,'{$this->nowdata}' as create_time")
->where(array(
"deposit.create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
))
->join("tab_promote promote ON deposit.promote_id = promote.id","left")
->group('promote_id')
->select();
//聚合表
if(!empty($insideRes)){
for ($i=0; $i < count($insideRes); $i++) {
# code...
$tempmap = array(
"promote_id"=>$insideRes[$i]['promote_id'],
"count_date"=>$this->date
);
$dbres = M("spend_user_count","tab_")->where($tempmap)->find();
//修正数据
if($dbres['root_id'] > 0 && $dbres['parent_id'] > 0){//是推广员
//修正root
$this->setInsideCount($dbres['root_id'],$insideRes[$i]['inside_cash_count']);
//修正Parent
$this->setInsideCount($dbres['parent_id'],$insideRes[$i]['inside_cash_count']);
//修正自己
$this->setInsideCount($dbres['promote_id'],$insideRes[$i]['inside_cash_count']);
}elseif($dbres['root_id'] == 0 && $dbres['parent_id'] > 0){//是组长
//修正root
$this->setInsideCount($dbres['parent_id'],$insideRes[$i]['inside_cash_count']);
//修正Parent
$this->setInsideCount($dbres['promote_id'],$insideRes[$i]['inside_cash_count']);
}else{//是会长
//修正root
$this->setInsideCount($dbres['promote_id'],$insideRes[$i]['inside_cash_count']);
}
}
}
// return true;
}
//按角色修正inside_cash_count数据
public function setInsideCount($promote_id,$inside_cash_count)
{
$tempmap = array(
"promote_id"=>$promote_id,
"count_date"=>$this->date
);
$dbres = M("spend_user_count","tab_")->where($tempmap)->find();
$savedata = array("inside_cash_count"=>$inside_cash_count-0+$dbres['inside_cash_count'],"all_count"=>$inside_cash_count-0+$dbres['all_count']);
M("spend_user_count","tab_")->where($tempmap)->save($savedata);
# code...
}
}

Loading…
Cancel
Save