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

214 lines
9.0 KiB
PHP

<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
class OldWithNewController extends CommonController{
public $enableCsrfValidation = false;
//活动列表
public function actionActivityList(){
$result=axios_request();
$page=$result['page']?:1;
$num=($page-1)*10;
$limit=10;
$table=(new \yii\db\Query())
->select('id,name,from_unixtime(createdAt) createdAt,from_unixtime(startTime) startTime,from_unixtime(endTime) endTime,display')
->from('{{%ybwm_old_with_new}}')
->where(['deleteAt'=>0,'uniacid'=>$this->wqData['uniacid']]);
$list=$table
->offset($num)
->limit($limit)
->orderBy('id desc')
->all();
$count=$table->count();
return $this->result(1, '成功',$list,$count);
}
//保存活动
public function actionSaveActivity(){
$result=axios_request();
$request = Yii::$app->request;
if($request->isGet){
$info=(new \yii\db\Query())
->from('{{%ybwm_old_with_new}}')
->where('id=:id',[':id'=>$result['id']])->one();
$info['rewardArr']=json_decode($info['rewardArr'],true)?:[];
$info['couponId']=json_decode($info['couponId'],true)?:[];
$info['startTime']=date("Y-m-d H:i:s",$info['startTime']);
$info['endTime']=date("Y-m-d H:i:s",$info['endTime']);
return $this->result(1, '成功',$info);
}
if(!$result['name']){
return $this->result(2,'活动名称不能为空!');
}
if(!$result['startTime']){
return $this->result(2,'活动时间不能为空!');
}
if(!$result['endTime']){
return $this->result(2,'活动时间不能为空!');
}
$data['name']=$result['name'];
$data['startTime']=strtotime($result['startTime']);
$data['endTime']=strtotime($result['endTime']);
$data['type']=$result['type'];
$data['money']=$result['money'];
$data['num']=$result['num'];
$data['shareTitle']=$result['shareTitle'];
$data['shareIcon']=$result['shareIcon'];
$data['shareRule']=$result['shareRule'];
array_multisort(array_column($result['rewardArr'],'people'),SORT_ASC,$result['rewardArr']);
$data['rewardArr']=json_encode($result['rewardArr'])?:'';
$data['isCoupon']=$result['isCoupon'];
$data['couponId']=json_encode($result['couponId'])?:'';
$data['isBalance']=$result['isBalance'];
$data['balance']=$result['balance'];
$data['isIntegral']=$result['isIntegral'];
$data['integral']=$result['integral'];
if($result['id']){
$data['changeAt']=time();
$re = Yii::$app->db->createCommand()->update('{{%ybwm_old_with_new}}', $data, 'id=:id', ['id' =>$result['id']])->execute();
}else{
$data['uniacid']=$this->wqData['uniacid'];
$data['createdAt']=time();
$re=Yii::$app->db->createCommand()->insert('{{%ybwm_old_with_new}}', $data)->execute();
}
if($re){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//修改活动
public function actionModifyActivity(){
$post=axios_request();
if($post['type']==1){
//显示隐藏
$data['display']=$post['display'];
}
if($post['type']==2){
//删除
$data['deleteAt']=time();
}
$data['changeAt']=time();
if(is_array($post['id'])){
$res = Yii::$app->db->createCommand()->update('{{%ybwm_old_with_new}}', $data, ['in','id',$post['id']])->execute();
}else{
$res = Yii::$app->db->createCommand()->update('{{%ybwm_old_with_new}}', $data, 'id=:id', ['id' =>$post['id']])->execute();
}
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//邀新记录
public function actionInvitation(){
$post=axios_request();
$page=$post['page']?:1;
$num=($page-1)*10;
$limit=10;
$actId=$post['actId'];
$andWhere=[];
if($actId){
$andWhere=['actId'=>$actId];
}
$data['joinPeople']=(new \yii\db\Query())
->from('{{%ybwm_old_with_new_bind}}')
->where(['uniacid'=>$this->wqData['uniacid']])->andWhere($andWhere)->groupBy('userId')->count()?:0;//参与人数
$data['newPeople']=(new \yii\db\Query())
->from('{{%ybwm_old_with_new_bind}}')
->where(['uniacid'=>$this->wqData['uniacid']])->andWhere($andWhere)->groupBy('bindId')->count()?:0;//新用户人数
$data['completePeople']=(new \yii\db\Query())
->from('{{%ybwm_old_with_new_bind}}')
->where(['uniacid'=>$this->wqData['uniacid'],'state'=>1])->andWhere($andWhere)->groupBy('bindId')->count()?:0;//完成人数
$data['completeMoney']=(new \yii\db\Query())
->from('{{%ybwm_old_with_new_detailed}}')
->where(['uniacid'=>$this->wqData['uniacid']])->andWhere($andWhere)->sum('money')?:0;//奖励总额
$userIds=(new \yii\db\Query())
->select('userId')
->from('{{%ybwm_old_with_new_bind}}')
->where(['uniacid'=>$this->wqData['uniacid']])->andWhere($andWhere)->all();
$userIdArr=array_column($userIds,'userId');
$user=(new \yii\db\Query())
->select('id,userName,portrait')
->from('{{%ybwm_member}}')
->where(['in','id',$userIdArr])->offset($num)
->limit($limit)->all();
for($i=0;$i<count($user);$i++){
$user[$i]['completeNum']=(new \yii\db\Query())
->from('{{%ybwm_old_with_new_bind}}')
->where(['userId'=>$user[$i]['id'],'state'=>1])->andWhere($andWhere)->count()?:0;
$user[$i]['completeMoney']=(new \yii\db\Query())
->from('{{%ybwm_old_with_new_detailed}}')
->where(['userId'=>$user[$i]['id']])->andWhere($andWhere)->sum('money')?:0;
$partakeAt=(new \yii\db\Query())
->from('{{%ybwm_old_with_new_bind}}')
->where(['userId'=>$user[$i]['id']])->andWhere($andWhere)->orderBy('id asc')->one();
$user[$i]['partakeAt']=date("Y-m-d H:i:s",$partakeAt['createdAt']);
}
$data['list']=$user;
return $this->result(1,'成功',$data);
}
//奖励明细
public function actionAwardDetails(){
$result=axios_request();
$type=$result['type'];
$keyword=$result['keyword'];
$startTime=strtotime($result['startTime']);
$endTime=strtotime($result['endTime']);
$page=$result['page']?:1;
$num=($page-1)*10;
$limit=10;
if($type==1){
$table=(new \yii\db\Query())
->select('b.userName,b.portrait,a.money,from_unixtime(a.createdAt) createdAt,a.type,a.note,a.userId')
->from('{{%ybwm_user_bonus}} as a')
->leftJoin('{{%ybwm_member}} b','a.userId=b.id')
->where(['a.uniacid'=>$this->wqData['uniacid'],'a.note'=>'老带新奖励']);
}else{
$table=(new \yii\db\Query())
->select('b.userName,b.portrait,a.integral,from_unixtime(a.createdAt) createdAt,a.type,a.note,a.userId')
->from('{{%ybwm_user_integral}} as a')
->leftJoin('{{%ybwm_member}} b','a.userId=b.id')
->where(['a.uniacid'=>$this->wqData['uniacid'],'a.note'=>'老带新奖励']);
}
if($keyword){
$table->andWhere(['or',['like','b.userName',$keyword],['like','b.userTel',$keyword]]);
}
if($startTime AND $endTime){
$table->andWhere('a.createdAt>=:startTime AND a.createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]);
}
$list=$table
->offset($num)
->limit($limit)
->orderBy('a.id desc')
->all();
$count=$table->count();
return $this->result(1, '成功',$list,$count);
}
//查看邀请的人
public function actionInvitationList(){
$result=axios_request();
$userId=$result['userId'];
$actId=$result['actId'];
$page=$result['page']?:1;
$num=($page-1)*10;
$limit=10;
$table=(new \yii\db\Query())
->select('b.userName,b.portrait,a.state,from_unixtime(a.createdAt) createdAt,a.bindId')
->from('{{%ybwm_old_with_new_bind}} as a')
->leftJoin('{{%ybwm_member}} b','a.bindId=b.id')
->where(['a.userId'=>$userId]);
if($actId){
$table->andWhere('a.actId='.$actId);
}
$list=$table
->offset($num)
->limit($limit)
->orderBy('a.id desc')
->all();
$count=$table->count();
return $this->result(1, '成功',$list,$count);
}
}