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

286 lines
13 KiB
PHP

<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
use app\models\common\Store;
use yii\db\Expression;
class PayVipController extends CommonController{
public $enableCsrfValidation = false;
private $shop_id;
public function init(){
parent::init();
$this->shop_id=$this->storeId?:Store::getMainStore($this->wqData['uniacid'])['id'];
$this->shop_id=$this->shop_id?:0;
}
//收银统计
public function actionOrderStatistics(){
$uniacid=$this->wqData['uniacid'];
$startTime=strtotime(date("Y-m-d")." 00:00:00");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
$res=(new \yii\db\Query())
->select('count(id) as count,sum(money) as money')
->from('{{%ybwm_user_vip_order}}')
->where('uniacid=:uniacid AND state=2',['uniacid'=>$uniacid])->one();
$data['count']=$res['count']?:0;
$data['money']=$res['money']?:0;
$todayRes=(new \yii\db\Query())
->select('count(id) as count,sum(money) as money')
->from('{{%ybwm_user_vip_order}}')
->where('uniacid=:uniacid AND state=2 AND payAt>=:startTime AND payAt<=:endTime',
[':uniacid'=>$uniacid,':startTime'=>$startTime,':endTime'=>$endTime])->one();
$data['todayCount']=$todayRes['count']?:0;
$data['todayMoney']=$todayRes['money']?:0;
return $this->result(1,'成功',$data);
}
//购买记录
public function actionOrderList(){
$result=axios_request();
$timeType=$result['timeType']?:1;
$keyword=$result['keyword'];
$startTime=$result['startTime'];
$endTime=$result['endTime'];
$page=$result['page']?:1;
$size=$result['size']?:10;
$orderBy='id desc';
$table=(new \yii\db\Query())
->select(['a.id','a.outTradeNo','a.money','a.payMode','from_unixtime(a.endTime)endTime','from_unixtime(a.payAt)payAt','a.state','c.userName','c.portrait','a.userId'])
->from('{{%ybwm_user_vip_order}} as a')
->join('LEFT JOIN', '{{%ybwm_member}} as c', 'c.id = a.userId')
->where('a.uniacid=:uniacid AND a.state=2',[':uniacid'=>$this->wqData['uniacid']]);
if($startTime){
$startTime=strtotime($startTime." 00:00:00");
$endTime=strtotime($endTime." 23:59:59");
}else{
if($timeType==2){
$startTime=strtotime(date("Y-m-d")." 00:00:00 -1day");
$endTime=strtotime(date("Y-m-d")." 23:59:59 -1day");
}elseif($timeType==3){
$startTime=strtotime(date("Y-m-d")." 00:00:00 -6day");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
}else{
$startTime=strtotime(date("Y-m-d")." 00:00:00");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
}
}
if($keyword){
$table->andWhere(['like','a.outTradeNo',$keyword]);
}
if($startTime){
$table->andWhere('a.createdAt>=:startTime AND a.createdAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime]);
}
//
$count=$table->count();
$table->offset(($page - 1) * $size)->limit($size)->orderby($orderBy);
$res=$table->all();
return $this->result(1,'成功',$res,$count);
}
//查看优惠券列表
public function actionCouponList(){
$result=axios_request();
$storeId=0;
$page=$result['page'];
$num=($page-1)*10;
$limit=10;
$table=(new \yii\db\Query())
->from('{{%ybwm_coupon}}')
->select('id,name,type,from_unixtime(createdAt) createdAt,display,from_unixtime(startTime) startTime,from_unixtime(endTime) endTime,useType,money,fullMoney,discount,isFull,stock')
->where(['isVip'=>1,'deleteAt'=>0,'uniacid'=>$this->wqData['uniacid'],'storeId'=>$storeId]);
if($page){
$table->offset($num)
->limit($limit);
}
$list=$table
->orderBy('id desc')
->all();
$count=$table->count();
return $this->result(1, '成功',$list,$count);
}
//保存优惠券
public function actionSaveCoupon(){
$result=axios_request();
$request = Yii::$app->request;
if($request->isGet){
$info=(new \yii\db\Query())
->from('{{%ybwm_coupon}}')
->where('id=:id',[':id'=>$result['id']])->one();
$category=(new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_core_category}}')
->where(['display'=>1,'storeId'=>$this->shop_id,'item'=>6,'deleteAt'=>0,'pid'=>0,'uniacid'=>$this->wqData['uniacid']])
->orderBy('sort asc,id desc')
->all();
$info['category']=$category;
$info['useType']=json_decode($info['useType'],true)?:[];
$storeArr=json_decode($info['storeArr'],true)?:[];
$info['storeArr']=$storeArr;
$info['storeInfo']=(new \yii\db\Query())
->select('id,name,address,storeTel,icon')
->from('{{%ybwm_store}}')
->where(['in','id',$storeArr])->all();
$goodsArr=json_decode($info['goodsArr'],true)?:[];
$info['goodsArr']=[];
if($goodsArr AND ($info['goodsType']==3 || $info['goodsType']==2) ){
for($i=0;$i<count($goodsArr);$i++){
$goods= (new \yii\db\Query())
->select('id,name,stock,icon')
->from('{{%ybwm_core_goods}}')
->where('id=:id',[':id'=>$goodsArr[$i]])->one();
$info['goodsArr'][]=array(
'id'=>$goods['id'],
'name'=>$goods['name'],
'stock'=>$goods['stock'],
'icon'=>$goods['icon'],
);
}
}else{
$info['goodsArr']= $goodsArr;
}
return $this->result(1, '成功',$info);
}
if(!$result['name']){
return $this->result(2,'优惠券名称不能为空!');
}
if(!$result['details']){
return $this->result(2,'优惠券详情不能为空!');
}
$data['name']=$result['name'];
$data['type']=$result['type'];
$data['fullMoney']=$result['fullMoney'];
if($result['isFull']==1){
$data['fullMoney']=0;
}
$data['isFull']=$result['isFull'];
if($result['type']==1){//1.优惠券2.折扣券
$data['money']=$result['money'];
}else{
$data['discount']=$result['discount'];
}
$data['timeType']=$result['timeType'];
if($result['timeType']==1){//1.日期范围2领取后多少天
$data['useStartTime']=strtotime($result['useStartTime']);
$data['useEndTime']=strtotime($result['useEndTime']);
}else{
$data['day']=$result['day'];
}
$data['useType']=json_encode($result['useType']);//使用类型1.外卖2自提
$data['details']=$result['details'];
$data['storeType']=$result['storeType'];
$data['storeArr']=json_encode($result['storeArr']);
$data['goodsType']=$result['goodsType'];
$data['goodsArr']=json_encode($result['goodsArr']);
$data['storeId']=0;
$data['color']=$result['color'];
$data['memberType']=1;
$data['isNextDay']=$result['isNextDay'];
$data['shareImg']=$result['shareImg'];
$data['shareTitle']=$result['shareTitle'];
$data['display']=$result['display'];
$data['subsidy']=$result['subsidy']?:0;//平台补贴
$data['isVip']=1;
if($result['id']){
$data['changeAt']=time();
$re = Yii::$app->db->createCommand()->update('{{%ybwm_coupon}}', $data, 'id=:id', ['id' =>$result['id']])->execute();
}else{
$data['uniacid']=$this->wqData['uniacid'];
$data['createdAt']=time();
$re=Yii::$app->db->createCommand()->insert('{{%ybwm_coupon}}', $data)->execute();
}
if($re){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//修改优惠券
public function actionModifyCoupon(){
$post=axios_request();
if($post['type']==1){
//显示隐藏
$data['display']=$post['display'];
}
if($post['type']==2){
//删除
$data['deleteAt']=time();
}
$data['changeAt']=time();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_coupon}}', $data, 'id=:id', ['id' =>$post['id']])->execute();
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//收费会员卡统计
public function actionVipStatistics(){
$uniacid=$this->wqData['uniacid'];
$request=axios_request();
$requestStartTime=strtotime($request['startTime']." 00:00:00");
$requestEndTime=strtotime($request['endTime']." 23:59:59");
$startTime=strtotime(date("Y-m-d")." 00:00:00");
$endTime=strtotime(date("Y-m-d")." 23:59:59");
$yesStartTime=strtotime(date("Y-m-d")." 00:00:00 -1day");
$yesEndTime=strtotime(date("Y-m-d")." 23:59:59 -1day");
$vipOrder= (new \yii\db\Query())
->select('count(id) as count,sum(money) as money')
->from('{{%ybwm_user_vip_order}}')
->where('uniacid=:uniacid AND state=2',[':uniacid'=>$uniacid]);
$all=$vipOrder->one();
$today=(clone $vipOrder)->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$startTime,':endTime'=>$endTime])->one();
$yesterday=(clone $vipOrder)->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$yesStartTime,':endTime'=>$yesEndTime])->one();
$data['allNum']=$all['count']?:0;//总数量
$data['allMoney']=$all['money']?:0.00;//总金额
$data['todayNum']=$today['count']?:0;//今天数量
$data['todayMoney']=$today['money']?:0.00;//今天金额
$data['yesterdayNum']=$yesterday['count']?:0;//昨天数量
$data['yesterdayMoney']=$yesterday['money']?:0.00;//昨天金额
$member=(new \yii\db\Query())
->select('count(*) as count')
->from('{{%ybwm_member}}')
->where('uniacid=:uniacid',[':uniacid'=>$uniacid]);
$data['openNum']=(clone $member)->andWhere('vipEndTime>'.time())->one()['count']?:0;
$data['endNum']=(clone $member)->andWhere('vipEndTime>0 AND vipEndTime<'.time())->one()['count']?:0;
$beforeStartTime=strtotime(date('Y-m-d 00:00:00',$endTime-14*24*60*60));
$moneyArr= (new \yii\db\Query())
->select(new Expression("from_unixtime(payAt,'%Y-%m-%d') as hours,sum(money)money"))
->from('{{%ybwm_user_vip_order}}')
->where(['uniacid'=>$uniacid])
->andWhere('payAt>=:starTime AND payAt<=:endTime',[':starTime'=>$beforeStartTime,':endTime'=>$endTime])
->groupBy(new Expression("from_unixtime(payAt,'%Y-%m-%d')"))
->all();
$data['moneyArr']=dataByTime($moneyArr,date("Y-m-d",$endTime));
$numArr= (new \yii\db\Query())
->select(new Expression("from_unixtime(payAt,'%Y-%m-%d') as hours,count(id)money"))
->from('{{%ybwm_user_vip_order}}')
->where(['uniacid'=>$uniacid])
->andWhere('payAt>=:starTime AND payAt<=:endTime',[':starTime'=>$beforeStartTime,':endTime'=>$endTime])
->groupBy(new Expression("from_unixtime(payAt,'%Y-%m-%d')"))
->all();
$data['numArr']=dataByTime($numArr,date("Y-m-d",$endTime));
$requestData=(clone $vipOrder)->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$requestStartTime,':endTime'=>$requestEndTime])->one();
$oldUser=(clone $vipOrder)->andWhere('payAt>=:startTime AND payAt<=:endTime AND first=2',[':startTime'=>$requestStartTime,':endTime'=>$requestEndTime])->one();
$newUser=(clone $vipOrder)->andWhere('payAt>=:startTime AND payAt<=:endTime AND first=1',[':startTime'=>$requestStartTime,':endTime'=>$requestEndTime])->one();
$userNum=(clone $vipOrder)->andWhere('payAt>=:startTime AND payAt<=:endTime',[':startTime'=>$requestStartTime,':endTime'=>$requestEndTime])->groupBy('userId')->count();
$allUserNum=(clone $vipOrder)->groupBy('userId')->count();
$endUserNum=(clone $member)->andWhere('vipEndTime>0 AND vipEndTime>=:startTime AND vipEndTime<=:endTime AND vipEndTime<:time',[':startTime'=>$requestStartTime,':endTime'=>$requestEndTime,':time'=>time()])->count();
$allEndUserNum=(clone $member)->andWhere('vipEndTime>0 AND vipEndTime<:time',[':time'=>time()])->count();
$data['requestNum']=$requestData['count']?:0;
$data['requestMoney']=$requestData['money']?:0.00;
$data['oldUser']=$oldUser['count']?:0;
$data['newUser']=$newUser['count']?:0;
$data['userNum']=$userNum?:0;
$data['allUserNum']=$allUserNum?:0;
$data['endUserNum']=$endUserNum?:0;
$data['allEndUserNum']=$allEndUserNum?:0;
return $this->result(1,'成功',$data);
}
}