canyin-project/ybcy/controllers/channel/DistributionController.php
2024-11-01 16:07:54 +08:00

208 lines
10 KiB
PHP

<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
use yii\web\Controller;
use app\models\common\Config;
use app\models\common\Power;
use yii\db\Expression;
class DistributionController extends CommonController
{
//分销商列表
public function actionDistributionList(){
$result=axios_request();
$page=$result['page']?:1;
$num=($page-1)*10;
$limit=10;
$uniacid=$this->wqData['uniacid'];
$state=$result['state']?:1;
$keyword=$result['keyword'];
$startTime=strtotime($result['startTime']);
$endTime=strtotime($result['endTime']);
$list=(new \yii\db\Query())
->select('a.id,a.name,a.tel,a.state,from_unixtime(a.createdAt) createdAt,from_unixtime(a.examineAt) examineAt,a.userId,b.userName,b.portrait')
->from('{{%ybwm_distribution}} as a')
->join('LEFT JOIN', '{{%ybwm_member}} as b', 'b.id = a.userId')
->where(['a.uniacid'=>$uniacid,'a.deleteAt'=>0,'a.state'=>$state]);
if($startTime AND $endTime){
$list->andWhere('a.createdAt>=:startTime AND a.createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]);
}
if ($keyword) {
$list->andwhere(['or', ['like', 'a.name', $keyword], ['like', 'b.userName', $keyword]]);
}
$data=$list->offset($num)
->limit($limit)->orderBy('a.id desc')->all();
$count=$list->count();
for($i=0;$i<count($data);$i++){
$yit=(new \yii\db\Query())
->from('{{%ybwm_bonus_withdrawal}}')
->where(['userId'=>$data[$i]['userId'],'item'=>2,'state'=>2])->sum('money')?:'0.00';//已提现
$tx=(new \yii\db\Query())
->from('{{%ybwm_bonus_withdrawal}}')
->where(['userId'=>$data[$i]['userId'],'item'=>2])
->andWhere(['in','state',[1,2]])
->sum('money')?:0;//已提现和提现中
$userMoney=(new \yii\db\Query())
->select('commission')
->from('{{%ybwm_member}}')
->where(['id'=>$data[$i]['userId']])->one()['commission']?:0;//用户佣金
$data[$i]['alreadyMoney']=$yit;//已提现
$data[$i]['allMoney']=bcadd($tx,$userMoney,2);//累计
$onePeople=(new \yii\db\Query())
->from('{{%ybwm_distribution_bind}}')
->where(['userId'=>$data[$i]['userId']])
->count()?:0;
$twoPeople=(new \yii\db\Query())
->from('{{%ybwm_distribution_bind}}')
->where(['twoId'=>$data[$i]['userId']])
->count()?:0;
$data[$i]['twoPeople']=$twoPeople;
$data[$i]['onePeople']=$onePeople;
}
return $this->result(1, '成功', $data,$count);
}
//查看下线
public function actionGetOffline(){
$result=axios_request();
$type=$result['type'];
$userId=$result['userId'];
$page=$result['page']?:1;
$num=($page-1)*10;
$limit=10;
$list=(new \yii\db\Query())
->select('b.userName,b.portrait,a.bindId,from_unixtime(a.createdAt) createdAt')
->from('{{%ybwm_distribution_bind}} as a')
->join('LEFT JOIN', '{{%ybwm_member}} as b', 'b.id = a.bindId');
if($type==1){
$list->andWhere(['a.userId'=>$userId]);
}else{
$list->andWhere(['a.twoId'=>$userId]);
}
$data=$list->offset($num)
->limit($limit)->orderBy('a.id desc')->all();
$count=$list->count();
return $this->result(1, '成功', $data,$count);
}
//审核
public function actionModifyDistribution(){
$result=axios_request();
if($result['type']==1){
//通过
$data['state']=2;
$data['examineAt']=time();
}
if($result['type']==2){
//拒绝
$data['state']=3;
}
if($result['type']==3){
//删除
$data['deleteAt']=time();
}
$data['changeAt']=time();
$res=Yii::$app->db->createCommand()->update('{{%ybwm_distribution}}', $data,['id'=>$result['id']])->execute();
if($res){
return $this->result(1, '成功');
}else{
return $this->result(2, '失败');
}
}
//查看分销订单
public function actionDistributionOrder(){
$result=axios_request();
$page=$result['page']?:1;
$num=($page-1)*10;
$limit=10;
$uniacid=$this->wqData['uniacid'];
$keyword=$result['keyword'];
$userId=$result['userId'];
$item=$result['item'];
$startTime=strtotime($result['startTime']);
$endTime=strtotime($result['endTime']);
$list=(new \yii\db\Query())
->select('a.level,b.userName,b.portrait,a.userId,c.name as distributionName,c.tel as distributionTel,a.money,from_unixtime(a.createdAt) createdAt,a.outTradeNo,a.type')
->from('{{%ybwm_distribution_order}} as a')
->join('LEFT JOIN', '{{%ybwm_member}} as b', 'b.id = a.userId')
->join('LEFT JOIN', '{{%ybwm_distribution}} as c', 'c.userId = a.distributionId')
->where(['a.uniacid'=>$uniacid]);
if($startTime AND $endTime){
$list->andWhere('a.createdAt>=:startTime AND a.createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]);
}
if($item){
$list->andwhere('a.item='.$item);
}
if($userId){
$list->andwhere('a.distributionId='.$userId);
}
if ($keyword) {
$list->andwhere(['or', ['like', 'a.outTradeNo', $keyword],['like', 'c.name', $keyword], ['like', 'b.userName', $keyword]]);
}
$data=$list->offset($num)
->limit($limit)->orderBy('a.id desc')->all();
$count=$list->count();
return $this->result(1, '成功', $data,$count);
}
//分销提现统计
public function actionWithdrawalStatistics(){
$uniacid=$this->wqData['uniacid'];
$data['wait']=(new \yii\db\Query())
->from('{{%ybwm_bonus_withdrawal}}')
->where('uniacid=:uniacid AND item=2 AND state=1',[':uniacid'=>$uniacid])->sum('money')?:0;//待审核
$data['frozen']=(new \yii\db\Query())
->from('{{%ybwm_distribution_order}}')
->where('uniacid=:uniacid AND type=1',[':uniacid'=>$uniacid])->sum('money')?:0;//冻结
$data['sure']=(new \yii\db\Query())
->from('{{%ybwm_member}}')
->where('uniacid=:uniacid',[':uniacid'=>$uniacid])->sum('commission')?:0;//可提现
$data['already']=(new \yii\db\Query())
->from('{{%ybwm_bonus_withdrawal}}')
->where('uniacid=:uniacid AND state=2 AND item=2',[':uniacid'=>$uniacid])->sum('money')?:0;//已提现
$data['cumulative']=(new \yii\db\Query())
->from('{{%ybwm_distribution_order}}')
->where('uniacid=:uniacid AND type=2',[':uniacid'=>$uniacid])->sum('money')?:0;//累计
return $this->result(1, '成功', $data);
}
public function actionIndex(){
$uniacid=$this->wqData['uniacid'];
$startTime=strtotime(date("Y-m-d")." 00:00:00");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
$beforeStartTime=strtotime(date("Y-m-d")." 00:00:00 -1day");
$beforeEndTime=strtotime(date("Y-m-d")." 23:59:59 -1day");
$distribution=(new \yii\db\Query())
->from('{{%ybwm_distribution}}')->where('uniacid=:uniacid AND deleteAt=0',[':uniacid'=>$uniacid]);
$data['todayWait']=(clone $distribution)->andWhere('state=1 AND createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])->count()?:0;//今日待审核
$data['yesWait']=(clone $distribution)->andWhere('state=1 AND createdAt>=:startTime AND createdAt<=:endTime',[':startTime'=>$beforeStartTime,':endTime'=>$beforeEndTime])->count()?:0;//昨日待审核
$data['todayOk']=(clone $distribution)->andWhere('state=2 AND examineAt>=:startTime AND examineAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])->count()?:0;//今日分销商
$data['yesOk']=(clone $distribution)->andWhere('state=2 AND examineAt>=:startTime AND examineAt<=:endTime',[':startTime'=>$beforeStartTime,':endTime'=>$beforeEndTime])->count()?:0;//昨日分销商
$data['moneyRank']=(new \yii\db\Query())
->select('b.name,count(a.id) count,sum(a.money) money')
->from('{{%ybwm_distribution_order}} as a')
->join('LEFT JOIN', '{{%ybwm_distribution}} as b', 'a.distributionId = b.userId')
->where('a.uniacid=:uniacid AND a.type=2 AND b.deleteAt=0',[':uniacid'=>$uniacid])->groupBy('a.distributionId')->limit(10)->orderBy('money desc')->all();
$data['subordinateRank']=(new \yii\db\Query())
->select('a.name,(select count(id) from {{%ybwm_distribution_bind}} b where a.userId=b.userId) count,(select count(id) from {{%ybwm_distribution_bind}} c where a.userId=c.twoId) twoCount')
->from('{{%ybwm_distribution}} as a')
->where('a.uniacid=:uniacid AND a.state=2 AND a.deleteAt=0',[':uniacid'=>$uniacid])->limit(10)->orderBy('(count+twoCount) desc')->all();
$beforeTime=strtotime(date('Y-m-d 00:00:00',$endTime-14*24*60*60));
$waitArr= (new \yii\db\Query())
->select(new Expression("from_unixtime(createdAt,'%Y-%m-%d') as hours,count(id)money"))
->from('{{%ybwm_distribution}}')
->where(['uniacid'=>$uniacid,'state'=>1,'deleteAt'=>0])
->andWhere('createdAt>=:starTime AND createdAt<=:endTime',[':starTime'=>$beforeTime,':endTime'=>$endTime])
->groupBy(new Expression("from_unixtime(createdAt,'%Y-%m-%d')"))
->all();
$data['waitAr']=dataByTime($waitArr,date("Y-m-d",$endTime));
$okArr= (new \yii\db\Query())
->select(new Expression("from_unixtime(examineAt,'%Y-%m-%d') as hours,count(id)money"))
->from('{{%ybwm_distribution}}')
->where(['uniacid'=>$uniacid,'state'=>2,'deleteAt'=>0])
->andWhere('examineAt>=:starTime AND examineAt<=:endTime',[':starTime'=>$beforeTime,':endTime'=>$endTime])
->groupBy(new Expression("from_unixtime(examineAt,'%Y-%m-%d')"))
->all();
$data['okArr']=dataByTime($okArr,date("Y-m-d",$endTime));
return $this->result(1, '成功', $data);
}
}