645 lines
27 KiB
PHP
645 lines
27 KiB
PHP
<?php
|
|
namespace app\controllers\channelApi;
|
|
use Composer\Package\Loader\ValidatingArrayLoader;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yii;
|
|
use app\models\common\Config;
|
|
use app\models\common\Order;
|
|
use app\models\common\Good;
|
|
class CouponController extends CommonController{
|
|
|
|
//优惠券列表
|
|
public function actionCouponList(){
|
|
$result=axios_request();
|
|
$page=$result['page']?:1;
|
|
$size=$result['size']?:10;
|
|
$now=time();
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$list= (new \yii\db\Query())
|
|
->from('{{%ybwm_coupon}}')
|
|
->where('deleteAt=0 AND uniacid=:uniacid AND startTime<=:startTime AND endTime>=:endTime AND display=1 AND stock>0',['startTime'=>$now,'endTime'=>$now,':uniacid'=>$uniacid]);
|
|
$userCoupon=(new \yii\db\Query())
|
|
->select('couponId')
|
|
->from('{{%ybwm_user_coupon}}')
|
|
->where('(state=:state or useEndTime<:time) AND userId=:userId',[':time'=>$now,':state'=>2,':userId'=>$this->wqData['userId']])
|
|
->all();
|
|
$userCouponId = array_column($userCoupon, 'couponId')?:[];
|
|
// print_R($userCouponId);die;
|
|
$list=$list->andWhere(['not in','id',$userCouponId])->offset(($page - 1) * $size)->limit($size)->orderBy('id desc')->all();
|
|
for($i=0;$i<count($list);$i++){
|
|
if($list[$i]['timeType']==1){
|
|
//日期范围
|
|
$list[$i]['useExplain']="有效期至".date("Y-m-d H:i",$list[$i]['useEndTime']);
|
|
}else{
|
|
if($list[$i]['isNextDay']==1){
|
|
$list[$i]['useExplain']="领取后".$list[$i]['day']."天内有效(次日起)";
|
|
}else{
|
|
$list[$i]['useExplain']="领取后".$list[$i]['day']."天内有效";
|
|
}
|
|
}
|
|
$list[$i]['explain']='';
|
|
if($list[$i]['isFull']==1){
|
|
$list[$i]['explain'] .='无门槛,';
|
|
}else{
|
|
$list[$i]['explain'] .='满'.$list[$i]['fullMoney'].'可用,';
|
|
}
|
|
if($list[$i]['storeType']==1){
|
|
$list[$i]['explain'] .='单店可用,';
|
|
}elseif($list[$i]['storeType']==2){
|
|
$list[$i]['explain'] .='全部门店可用,';
|
|
}else{
|
|
$list[$i]['explain'] .='部分门店可用,';
|
|
}
|
|
if($list[$i]['goodsType']==1){
|
|
$list[$i]['explain'] .='全部商品可用,';
|
|
}elseif($list[$i]['goodsType']==2){
|
|
$list[$i]['explain'] .='指定商品可用,';
|
|
}elseif($list[$i]['goodsType']==3){
|
|
$list[$i]['explain'] .='指定商品不可用,';
|
|
}else{
|
|
$list[$i]['explain'] .='指定分类商品可用,';
|
|
}
|
|
$useType=json_decode($list[$i]['useType'],true);
|
|
|
|
if(in_array('1',$useType)){
|
|
$list[$i]['explain'] .='外卖可用';
|
|
}
|
|
if(in_array('2',$useType)){
|
|
$list[$i]['explain'] .='堂食可用';
|
|
}
|
|
if(in_array('3',$useType)){
|
|
$list[$i]['explain'] .='快餐可用';
|
|
}
|
|
}
|
|
return $this->result(1,'成功',$list);
|
|
}
|
|
//优惠券详情
|
|
public function actionCouponInfo(){
|
|
$result=axios_request();
|
|
$now=time();
|
|
if($result['id']){
|
|
$list= (new \yii\db\Query())
|
|
->from('{{%ybwm_coupon}}')
|
|
->where('id=:id',[':id'=>$result['id']])->one();
|
|
$storeArr=[];
|
|
$storeArr=(new \yii\db\Query())
|
|
->from('{{%ybwm_store}}')
|
|
->where('id=:id',[':id'=>$list['storeId']])->one();
|
|
$list['storeName']=$storeArr['name'];
|
|
if($list['storeType']==1){
|
|
$storeArr=(new \yii\db\Query())
|
|
->from('{{%ybwm_store}}')->select('id,name')
|
|
->where('id=:id',[':id'=>$list['storeId']])->all();
|
|
$list['storeData']=$storeArr;
|
|
}elseif($list['storeType']==2){
|
|
$list['storeData']='全部门店可用,';
|
|
}else{
|
|
$storeArr=(new \yii\db\Query())
|
|
->from('{{%ybwm_store}}')->select('id,name')
|
|
->where(['in','id',json_decode($list['storeArr'])])->all();
|
|
$list['storeData']=$storeArr;
|
|
}
|
|
$list['explain']=$list['color'];
|
|
if($list['type']==3){
|
|
if($list['goodsArr']){
|
|
$outGoods=(new \yii\db\Query())
|
|
->from('{{%ybwm_core_goods}}')
|
|
->where(['in','id',json_decode($list['goodsArr'],true)])->all();
|
|
$outGoodsName=implode(',',array_column($outGoods,'name'));
|
|
}
|
|
if($list['goodsArr2']){
|
|
$inGoods=(new \yii\db\Query())
|
|
->from('{{%ybwm_core_goods}}')
|
|
->where(['in','id',json_decode($list['goodsArr2'],true)])->all();
|
|
|
|
$inGoodsName=implode(',',array_column($inGoods,'name'));
|
|
}
|
|
$list['goodsName']=$outGoodsName.','.$inGoodsName;
|
|
}
|
|
$coupon= (new \yii\db\Query())
|
|
->from('{{%ybwm_user_coupon}}')
|
|
->where('couponId=:couponId AND state=2 AND useEndTime>'.time(),[':couponId'=>$result['id']])
|
|
->andWhere(['userId'=> $uniacid=$this->wqData['userId']])
|
|
->one();
|
|
if($coupon){
|
|
$list['useState']=2;//点击使用
|
|
}else{
|
|
$list['useState']=1;//点击领取
|
|
}
|
|
if($list['timeType']==1){
|
|
//日期范围
|
|
$list['useExplain']="有效期至".date("Y-m-d H:i",$list['useEndTime']);
|
|
}else{
|
|
if($list['isNextDay']==1){
|
|
$list['useExplain']="领取后".$list['day']."天内有效(次日起)";
|
|
}else{
|
|
$list['useExplain']="领取后".$list['day']."天内有效";
|
|
}
|
|
}
|
|
}else{
|
|
$list= (new \yii\db\Query())
|
|
->from('{{%ybwm_user_coupon}}')
|
|
->where('id=:id',[':id'=>$result['receiveId']])->one();
|
|
$coupon= (new \yii\db\Query())
|
|
->from('{{%ybwm_coupon}}')
|
|
->where('id=:id',[':id'=>$list['couponId']])->one();
|
|
$list['isHx']=$coupon['isHx'];
|
|
$list['color']=$coupon['color'];
|
|
$list['useExplain']="有效期".date("Y-m-d H:i",$list['useStartTime'])."至".date("Y-m-d H:i",$list['useEndTime']);
|
|
if($list['state']==1){
|
|
$list['useState']=3;//已使用
|
|
}else{
|
|
$list['useState']=2;//点击使用
|
|
}
|
|
if($list['useStartTime']>$now){
|
|
$list['useState']=4;//未到使用时间
|
|
}
|
|
if($list['useEndTime']<$now){
|
|
$list['useState']=5;//已过期
|
|
}
|
|
if($coupon['type']==3){
|
|
if($coupon['goodsArr']){
|
|
$outGoods=(new \yii\db\Query())
|
|
->from('{{%ybwm_core_goods}}')
|
|
->where(['in','id',json_decode($coupon['goodsArr'],true)])->all();
|
|
$outGoodsName=implode(',',array_column($outGoods,'name'));
|
|
}
|
|
if($coupon['goodsArr2']){
|
|
$inGoods=(new \yii\db\Query())
|
|
->from('{{%ybwm_core_goods}}')
|
|
->where(['in','id',json_decode($coupon['goodsArr2'],true)])->all();
|
|
|
|
$inGoodsName=implode(',',array_column($inGoods,'name'));
|
|
}
|
|
$list['goodsName']=$outGoodsName.','.$inGoodsName;
|
|
}
|
|
$storeArr=[];
|
|
$storeArr=(new \yii\db\Query())
|
|
->from('{{%ybwm_store}}')
|
|
->where('id=:id',[':id'=>$list['storeId']])->one();
|
|
$list['storeName']=$storeArr['name'];
|
|
if($list['storeType']==1){
|
|
$storeArr=(new \yii\db\Query())
|
|
->from('{{%ybwm_store}}')->select('id,name')
|
|
->where('id=:id',[':id'=>$list['storeId']])->all();
|
|
$list['storeData']=$storeArr;
|
|
}elseif($list['storeType']==2){
|
|
$list['storeData']='全部门店可用,';
|
|
}else{
|
|
$storeArr=(new \yii\db\Query())
|
|
->from('{{%ybwm_store}}')->select('id,name')
|
|
->where(['in','id',json_decode($list['storeArr'])])->all();
|
|
$list['storeData']=$storeArr;
|
|
}
|
|
$list['explain']=$coupon['color'];
|
|
}
|
|
// if($list['goodsType']==2 || $list['goodsType']==3){
|
|
// $goodsArr=json_decode($list['goodsArr'],true);
|
|
// $goods=(new \yii\db\Query())
|
|
// ->select('name')
|
|
// ->from('{{%ybwm_core_goods}}')
|
|
// ->where(['in','id',$goodsArr])
|
|
// ->all();
|
|
|
|
// }
|
|
// if($list['goodsType']==4){
|
|
// $goodsArr=json_decode($list['goodsArr'],true);
|
|
// $goods=(new \yii\db\Query())
|
|
// ->select('name')
|
|
// ->from('{{%ybwm_core_category}}')
|
|
// ->where(['in','id',$goodsArr])
|
|
// ->all();
|
|
// }
|
|
return $this->result(1, '成功',$list);
|
|
}
|
|
//领取优惠券
|
|
public function actionReceiveCoupon(){
|
|
$result=axios_request();
|
|
$couponId=$result['couponId'];
|
|
$userId=$this->wqData['userId'];
|
|
$uniacid=$this->wqData['uniacid'];
|
|
$now=time();
|
|
|
|
$coupon=(new \yii\db\Query())
|
|
->from('{{%ybwm_coupon}}')
|
|
->where('id=:id',['id'=>$couponId])
|
|
->one();
|
|
|
|
$user=(new \yii\db\Query())
|
|
->from('{{%ybwm_member}}')
|
|
->where('id=:id',[':id'=>$userId])->one();
|
|
if($coupon['startTime']>$now || $coupon['endTime']<$now){
|
|
return $this->result(2, '活动未到时间不可领取!');
|
|
}
|
|
if($coupon['stock']<=0){
|
|
return $this->result(2, '优惠券已领完!');
|
|
}
|
|
if($coupon['memberType']==2){
|
|
if(!$user['level']){
|
|
return $this->result(2, '该券仅限会员用户领取哦!');
|
|
}
|
|
}
|
|
if($coupon['memberType']==3){
|
|
$userBind=(new \yii\db\Query())
|
|
->from('{{%ybwm_member_bind}}')
|
|
->where('userId=:userId AND storeId=:storeId',[':userId'=>$userId,':storeId'=>$coupon['storeId']])->one();
|
|
$memberLabel=json_decode($coupon['memberLabel'],true);
|
|
if(!in_array($userBind['vipLabel'],$memberLabel)){
|
|
return $this->result(2, '该券需指定标签用户才能领取!');
|
|
}
|
|
}
|
|
$myCoupon=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_coupon}}')
|
|
->where('couponId=:couponId AND userId=:userId',['userId'=>$userId,'couponId'=>$couponId])
|
|
->count();
|
|
if($coupon['maxNum']<=$myCoupon AND $coupon['maxNum']>0){
|
|
return $this->result(2, '已达到每人限领次数!');
|
|
}
|
|
$data['couponId']=$couponId;
|
|
$data['userId']=$userId;
|
|
$data['type']=$coupon['type'];
|
|
$data['money']=$coupon['money']?:0;
|
|
$data['fullMoney']=$coupon['fullMoney']?:0;
|
|
$data['discount']=$coupon['discount']?:0;
|
|
if($coupon['timeType']==1){
|
|
$data['useStartTime']=$coupon['useStartTime'];
|
|
$data['useEndTime']=$coupon['useEndTime'];
|
|
}else{
|
|
if($coupon['isNextDay']==1){//次日起
|
|
$data['useStartTime']=time()+86400;
|
|
$data['useEndTime']=time()+$coupon['day']*86400+86400;
|
|
}else{
|
|
$data['useStartTime']=time();
|
|
$data['useEndTime']=time()+$coupon['day']*86400;
|
|
}
|
|
}
|
|
$data['useType']=$coupon['useType']?:json_encode([1]);
|
|
$data['couponName']=$coupon['name'];
|
|
|
|
$data['couponDetails']=$coupon['details'];
|
|
$data['storeType']=$coupon['storeType'];
|
|
$data['storeArr']=$coupon['storeArr']?:'';
|
|
$data['goodsType']=$coupon['goodsType'];
|
|
$data['goodsArr']=$coupon['goodsArr']?:'';
|
|
$data['goodsArr2']=$coupon['goodsArr2']?:'';
|
|
$data['memberLabel']=$coupon['memberLabel']?:'';
|
|
$data['memberType']=$coupon['memberType']?:1;
|
|
$data['storeId']=$coupon['storeId'];
|
|
$data['uniacid']=$uniacid;
|
|
$data['subsidy']=0;
|
|
if($coupon['storeType']==2 || $coupon['storeType']==3){
|
|
$data['subsidy']=$coupon['subsidy'];//平台补贴
|
|
}
|
|
|
|
$data['createdAt']=time();
|
|
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_user_coupon}}', $data)->execute();
|
|
if($res){
|
|
Yii::$app->db->createCommand()->update('{{%ybwm_coupon}}', ['stock'=>$coupon['stock']-1], 'id=:id', ['id' =>$coupon['id']])->execute();
|
|
return $this->result(1, '成功');
|
|
}else{
|
|
return $this->result(2, '失败');
|
|
}
|
|
}
|
|
//查看我的优惠券
|
|
public function actionMyCoupon(){
|
|
$now=time();
|
|
$result=axios_request();
|
|
$page=$result['page']?:1;
|
|
$size=$result['size']?:10;
|
|
$state=$result['state'];
|
|
$receiveType=$result['receiveType'];
|
|
$type=$result['type'];
|
|
$userId=$this->wqData['userId'];
|
|
$list=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_coupon}}')
|
|
->where('userId=:userId',[':userId'=>$userId]);
|
|
if($state==1){
|
|
//可使用
|
|
$list->andWhere('state=2 AND useEndTime>:useEndTime',[':useEndTime'=>$now]);
|
|
}elseif($state==2){
|
|
$list->andWhere('state=1');
|
|
}elseif($state==3){
|
|
$list->andWhere('useEndTime<:useEndTime',[':useEndTime'=>$now]);
|
|
}
|
|
if($type==1){
|
|
$list->andWhere(['like','useType',1]);
|
|
}
|
|
if($type==2){
|
|
$list->andWhere(['or',['like','useType',2],['like','useType',3]]);
|
|
}
|
|
if($receiveType){
|
|
$list->andWhere('receiveType='.$receiveType);
|
|
}
|
|
//var_dump(ddSql($list));die;
|
|
$list=$list->offset(($page - 1) * $size)->limit($size)->orderBy('useEndTime asc,id desc')->all();
|
|
|
|
for($i=0;$i<count($list);$i++){
|
|
$list[$i]['useExplain']="有效期至".date("Y-m-d H:i",$list[$i]['useEndTime']);
|
|
$list[$i]['explain']='';
|
|
if($list[$i]['fullMoney']==0){
|
|
$list[$i]['explain'] .='无门槛,';
|
|
}else{
|
|
$list[$i]['explain'] .='满'.$list[$i]['fullMoney'].'可用,';
|
|
}
|
|
$storeArr=[];
|
|
if($list[$i]['storeType']==1){
|
|
$storeArr=(new \yii\db\Query())
|
|
->from('{{%ybwm_store}}')
|
|
->where('id=:id',[':id'=>$list[$i]['storeId']])->one();
|
|
|
|
$list[$i]['explain'] .=$storeArr['name'].'可用,';
|
|
}elseif($list[$i]['storeType']==2){
|
|
$list[$i]['explain'] .='全部门店可用,';
|
|
}else{
|
|
$storeData=(new \yii\db\Query())
|
|
->from('{{%ybwm_store}}')->select('name')
|
|
->where(['in','id',json_decode($list[$i]['storeArr'])])->all();
|
|
$storeArr=implode(',',array_column($storeData,'name'));
|
|
$list[$i]['explain'] .=$storeArr.'可用,';
|
|
}
|
|
if($list[$i]['goodsType']==1){
|
|
$list[$i]['explain'] .='全部商品可用,';
|
|
}elseif($list[$i]['goodsType']==2){
|
|
$list[$i]['explain'] .='指定商品可用,';
|
|
}elseif($list[$i]['goodsType']==3){
|
|
$list[$i]['explain'] .='指定商品不可用,';
|
|
}else{
|
|
$list[$i]['explain'] .='指定分类商品可用,';
|
|
}
|
|
$useType=json_decode($list[$i]['useType'],true);
|
|
if(in_array('1',$useType)){
|
|
$list[$i]['explain'] .='外卖可用';
|
|
}
|
|
$list[$i]['useState']=$state;
|
|
|
|
}
|
|
|
|
return $this->result(1,'成功',$list);
|
|
}
|
|
//查看可用优惠券
|
|
public function actionAvailableCoupon(){
|
|
$result=axios_request();
|
|
$storeId=$result['storeId'];
|
|
$userId=$this->wqData['userId'];
|
|
$type=$result['type'];// 1外卖2店内3.快餐
|
|
$tableId=$result['tableId']?:0;
|
|
$storeType=$result['storeType'];//1.店铺优惠2平台红包
|
|
$now=time();
|
|
$car=Order::getCarMoney($userId,$storeId,$type,$tableId);
|
|
$storeSet = Config::getStoreSet('instoreSet', $result['storeId']);
|
|
$currency=Config::getSystemSet('currency',$this->wqData['uniacid']);
|
|
$actGoods = (new \yii\db\Query())
|
|
->from('{{%ybwm_shop_car}}')
|
|
->where(['tableId'=>$tableId,'item'=>$type,'uniacid' => $this->wqData['uniacid'], 'storeId' => $result['storeId']])
|
|
->andWhere('discountType!=0')
|
|
->one();
|
|
|
|
if($storeSet['orderMode']==2 || $type!=2){
|
|
$actGoods = (new \yii\db\Query())
|
|
->from('{{%ybwm_shop_car}}')
|
|
->where(['userId'=>$userId,'tableId'=>$tableId,'item'=>$type,'uniacid' => $this->wqData['uniacid'], 'storeId' => $result['storeId']])
|
|
->andWhere('discountType!=0')
|
|
->one();
|
|
}
|
|
if(in_array('2',$currency['goodsActivityRule']) AND $actGoods){
|
|
return $this->result(1, '成功',[]);
|
|
}
|
|
|
|
$carMoney=$car['goodsMoney'];
|
|
$coupon=(new \yii\db\Query())
|
|
->from('{{%ybwm_user_coupon}}')
|
|
->where('userId=:userId AND state=2 AND useStartTime<=:useStartTime AND useEndTime>:useEndTime AND (fullMoney=0 || fullMoney<=:fullMoney)',[':fullMoney'=>$carMoney,':useStartTime'=>$now,':useEndTime'=>$now,':userId'=>$userId]);
|
|
if($type){
|
|
$coupon->andWhere(['like', 'useType', $type]);
|
|
}
|
|
//门店优惠券
|
|
if($storeType==1){
|
|
$coupon->andWhere(['and',
|
|
['in','storeType',[1,3]],
|
|
['in','type',[1,2,3]]
|
|
]);
|
|
}
|
|
//平台优惠券
|
|
if($storeType==2){
|
|
$coupon->andWhere('storeType=2 AND type in (1,2)');
|
|
}
|
|
|
|
$coupon=$coupon->orderBy('useEndTime asc')->all();
|
|
$userBind=(new \yii\db\Query())
|
|
->from('{{%ybwm_member_bind}}')
|
|
->where('userId=:userId AND storeId=:storeId',[':userId'=>$userId,':storeId'=>$storeId])->one();
|
|
$useCouponArr=[];
|
|
for($i=0;$i<count($coupon);$i++){
|
|
if($type==2){
|
|
if($storeSet['orderMode']==2){
|
|
$where = 'userId='.$userId.' AND tableId='.$tableId.' AND storeId='.$storeId.' AND item=2';
|
|
}else{
|
|
$where = 'tableId='.$tableId.' AND storeId='.$storeId.' AND item=2';
|
|
}
|
|
}else{
|
|
$where = 'userId='.$userId.' AND storeId='.$storeId.' AND item='.$type;
|
|
}
|
|
$uniacid=$this->wqData['uniacid'];
|
|
if($type==1){
|
|
if($coupon[$i]['goodsArr']){
|
|
$goodsId=json_decode($coupon[$i]['goodsArr'],true)[0];
|
|
if($coupon[$i]['type']==3){
|
|
$coupon[$i]['money']=Good::getGoodInfo($uniacid,$storeId,$goodsId)['price'];
|
|
}
|
|
}
|
|
}else{
|
|
if($coupon[$i]['goodsArr2']){
|
|
$goodsId=json_decode($coupon[$i]['goodsArr2'],true)[0];
|
|
if($coupon[$i]['type']==3){
|
|
$coupon[$i]['money']=Good::getGoodInfo($uniacid,$storeId,$goodsId)['price'];
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if($coupon[$i]['type']==2){
|
|
$coupon[$i]['money']=bcmul($carMoney,(10-$coupon[$i]['discount'])/10,2);
|
|
}
|
|
$coupon[$i]['useExplain']="有效期至".date("Y-m-d H:i",$coupon[$i]['useEndTime']);
|
|
$coupon[$i]['explain']='';
|
|
if($coupon[$i]['fullMoney']==0){
|
|
$coupon[$i]['explain'] .='无门槛,';
|
|
}else{
|
|
$coupon[$i]['explain'] .='满'.$coupon[$i]['fullMoney'].'可用,';
|
|
}
|
|
|
|
if($coupon[$i]['storeType']==1){
|
|
$coupon[$i]['explain'] .='单店可用,';
|
|
}elseif($coupon[$i]['storeType']==2){
|
|
$coupon[$i]['explain'] .='全部门店可用,';
|
|
}else{
|
|
$coupon[$i]['explain'] .='部分门店可用,';
|
|
}
|
|
if($coupon[$i]['goodsType']==1){
|
|
$coupon[$i]['explain'] .='全部商品可用,';
|
|
}elseif($coupon[$i]['goodsType']==2){
|
|
$coupon[$i]['explain'] .='指定商品可用,';
|
|
}elseif($coupon[$i]['goodsType']==3){
|
|
$coupon[$i]['explain'] .='指定商品不可用,';
|
|
}else{
|
|
$coupon[$i]['explain'] .='指定分类商品可用,';
|
|
}
|
|
$useType=json_decode($coupon[$i]['useType'],true);
|
|
if(in_array('1',$useType)){
|
|
$coupon[$i]['explain'] .='外卖可用';
|
|
}
|
|
if($coupon[$i]['memberType']==3){
|
|
$memberLabel=json_decode($coupon[$i]['memberLabel'],true);
|
|
if(!in_array($userBind['vipLabel'],$memberLabel)){
|
|
continue;
|
|
}
|
|
}
|
|
if($coupon[$i]['storeType']==1){
|
|
//本店
|
|
if($storeId!=$coupon[$i]['storeId'] AND $coupon[$i]['storeId']>0){
|
|
continue;
|
|
}
|
|
}
|
|
if($coupon[$i]['storeType']==3){
|
|
//指定门店
|
|
$storeArr=[];
|
|
$storeArr=json_decode($coupon[$i]['storeArr'],true);
|
|
if(!in_array($storeId,$storeArr)){
|
|
continue;
|
|
}
|
|
}
|
|
if($coupon[$i]['goodsType']==2){
|
|
//指定商品可用
|
|
if($type==1){
|
|
$goodsArr=json_decode($coupon[$i]['goodsArr'],true);
|
|
}else{
|
|
$goodsArr=json_decode($coupon[$i]['goodsArr2'],true);
|
|
}
|
|
if($goodsArr){
|
|
$goods=(new \yii\db\Query())
|
|
->from('{{%ybwm_shop_car}}')
|
|
->where($where)
|
|
->andWhere(['in','goodsId',$goodsArr])
|
|
->one();
|
|
|
|
// if($coupon[$i]['id']==611){
|
|
// var_dump(ddSql($goods));die;
|
|
// }
|
|
$goodsMoney=(new \yii\db\Query())
|
|
->from('{{%ybwm_shop_car}}')
|
|
->where($where)
|
|
->andWhere(['in','goodsId',$goodsArr])
|
|
->sum('money*(num-discountNum)+discountPrice*discountNum+materialMoney*num')?:0;
|
|
|
|
if(!$goods or ($coupon[$i]['fullMoney']>$goodsMoney AND $coupon[$i]['fullMoney']>0)){
|
|
continue;
|
|
}
|
|
if($coupon[$i]['type']==2){
|
|
$coupon[$i]['money']=bcmul($goodsMoney,(10-$coupon[$i]['discount'])/10,2);
|
|
}
|
|
}
|
|
|
|
}
|
|
if($coupon[$i]['goodsType']==3){
|
|
$goodsArr=json_decode($coupon[$i]['goodsArr'],true);
|
|
if($goodsArr) {
|
|
$goods=(new \yii\db\Query())
|
|
->from('{{%ybwm_shop_car}}')
|
|
->where($where)
|
|
->andWhere(['not in','goodsId',$goodsArr])
|
|
->one();
|
|
$goodsMoney=(new \yii\db\Query())
|
|
->from('{{%ybwm_shop_car}}')
|
|
->where($where)
|
|
->andWhere(['not in','goodsId',$goodsArr])
|
|
->sum('money*(num-discountNum)+discountPrice*discountNum+materialMoney*num')?:0;
|
|
if(!$goods or ($coupon[$i]['fullMoney']>$goodsMoney AND $coupon[$i]['fullMoney']>0)){
|
|
continue;
|
|
}
|
|
if($coupon[$i]['type']==2){
|
|
$coupon[$i]['money']=bcmul($goodsMoney,(10-$coupon[$i]['discount'])/10,2);
|
|
}
|
|
}
|
|
}
|
|
if($coupon[$i]['goodsType']==4){
|
|
if($type==2){
|
|
if($storeSet['orderMode']==2){
|
|
$where = 'a.userId='.$userId.' AND a.tableId='.$tableId.' AND a.storeId='.$storeId.' AND a.item=2';
|
|
}else{
|
|
$where = 'a.tableId='.$tableId.' AND a.storeId='.$storeId.' AND a.item=2';
|
|
}
|
|
}else{
|
|
$where = 'a.userId='.$userId.' AND a.storeId='.$storeId.' AND a.item='.$type;
|
|
}
|
|
//指定商品分类可用
|
|
if($type==1){
|
|
$goodsArr=json_decode($coupon[$i]['goodsArr'],true);
|
|
}else{
|
|
$goodsArr=json_decode($coupon[$i]['goodsArr2'],true);
|
|
}
|
|
|
|
if($goodsArr) {
|
|
$goods = (new \yii\db\Query())
|
|
->from('{{%ybwm_shop_car}} as a')
|
|
->join('LEFT JOIN', '{{%ybwm_core_goods}} as b', 'b.id = a.goodsId')
|
|
->where($where)
|
|
->andWhere(['in', 'b.typePid', $goodsArr])
|
|
->one();
|
|
|
|
$goodsMoney = (new \yii\db\Query())
|
|
->from('{{%ybwm_shop_car}} as a')
|
|
->join('LEFT JOIN', '{{%ybwm_core_goods}} as b', 'b.id = a.goodsId')
|
|
->where($where)
|
|
->andWhere(['in', 'b.typePid', $goodsArr])
|
|
->sum('a.money*(a.num-a.discountNum)+a.discountPrice*a.discountNum+a.materialMoney*a.num')?:0;
|
|
if(!$goods or ($coupon[$i]['fullMoney']>$goodsMoney AND $coupon[$i]['fullMoney']>0)){
|
|
continue;
|
|
}
|
|
if($coupon[$i]['type']==2){
|
|
$coupon[$i]['money']=bcmul($goodsMoney,(10-$coupon[$i]['discount'])/10,2);
|
|
}
|
|
}
|
|
}
|
|
$useCouponArr[]=$coupon[$i];
|
|
// array_values($coupon);
|
|
}
|
|
|
|
// $coupon=array_values($coupon);
|
|
return $this->result(1, '成功',$useCouponArr);
|
|
}
|
|
public function actionMealCode(){
|
|
$result=axios_request();
|
|
$coupon=(new \yii\db\Query())
|
|
->select('id,userId,selfCode')
|
|
->from('{{%ybwm_user_coupon}}')
|
|
->where('id=:id',[':id'=>$result['id']])
|
|
->one();
|
|
if(!$coupon){
|
|
return $this->result(1,'成功','');
|
|
}
|
|
if(empty($coupon['safeCode'])){
|
|
$code = $this->getCode();
|
|
Yii::$app->db->createCommand()->update('{{%ybwm_user_coupon}}', ['selfCode'=>$code], 'id=:id', ['id' =>$coupon['id']])->execute();
|
|
}else{
|
|
$code = $coupon['safeCode'];
|
|
}
|
|
|
|
//$res='https://qrcode.jp/qr?q='.$order['selfCode'].'&s=10';
|
|
$res='https://api.pwmqr.com/qrcode/create/?url='.$code;
|
|
return $this->result(1,'成功',$res);
|
|
}
|
|
|
|
public function getCode(){
|
|
$code = rand(11111111,9999999);
|
|
$data = (new \yii\db\Query())
|
|
->from('{{%ybwm_user_coupon}}')
|
|
->where(['selfCode' => $code])
|
|
->one();
|
|
if(empty($data)){
|
|
return $code;
|
|
}else{
|
|
return $this->getCode();
|
|
}
|
|
}
|
|
}
|