canyin-project/ybcy/models/common/Window.php

236 lines
10 KiB
PHP
Raw Permalink Normal View History

2024-11-01 16:07:54 +08:00
<?php
namespace app\models\common;
use Yii;
use yii\db\ActiveRecord;
use app\models\common\Store;
class Window extends ActiveRecord{
//获取弹窗优惠券
public static function getWindowCoupon($location,$storeId,$userId) {
$now=time();
$window=(new \yii\db\Query())
->from('{{%ybwm_window_coupon}}')
->where('storeId=:storeId AND deleteAt=0 AND display=1 AND startTime<=:startTime AND endTime>:endTime AND location=:location',[':location'=>$location,':startTime'=>$now,':endTime'=>$now,':storeId'=>$storeId])
->one();
if(!$window){
return [];
}
$bill=(new \yii\db\Query())
->from('{{%ybwm_bill}}')
->where('storeId=:storeId AND userId=:userId',[':storeId'=>$storeId,':userId'=>$userId])
->one();
if($window['people']==2 AND $bill){
return [];
}
if($window['people']==3 AND !$bill){
return [];
}
$window=(new \yii\db\Query())
->from('{{%ybwm_window_coupon}}')
->where('storeId=:storeId AND deleteAt=0 AND display=1 AND startTime<=:startTime AND endTime>:endTime AND location=:location',[':location'=>$location,':startTime'=>$now,':endTime'=>$now,':storeId'=>$storeId])
->one();
$receive=(new \yii\db\Query())
->from('{{%ybwm_window_coupon_receive}}')
->where('windowId=:windowId AND userId=:userId',[':windowId'=>$window['id'],':userId'=>$userId])
->one();
if($receive){
return [];
}
$list=(new \yii\db\Query())
->select('id,name,timeType,useEndTime,isNextDay,day,isFull,fullMoney,type,money,discount')
->from('{{%ybwm_coupon}}')
->where(['in','id',json_decode($window['coupon'],true)])
->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'].'可用';
}
}
$window['couponList']=$list;
return $window;
}
//获取商家优惠券$type1外卖2店内3快餐
public static function getStoreCoupon($storeId,$userId,$type=1) {
$now=time();
$store=(new \yii\db\Query())
->select('uniacid')
->from('{{%ybwm_store}}')->where('id=:id',[':id'=>$storeId])->one();
$storeStr='"'.$storeId.'"';
$list= (new \yii\db\Query())
->from('{{%ybwm_coupon}}')
->where("display=1 AND uniacid=:uniacid AND deleteAt=0 AND ( (storeType=1 AND storeId=:storeId) or storeType=2 or (storeType=3 AND storeArr like '%".$storeStr."%' ) ) AND startTime<=:startTime AND endTime>=:endTime",[':uniacid'=>$store['uniacid'],':startTime'=>$now,':endTime'=>$now,':storeId'=>$storeId]);
if($type==1){
$list->andWhere(['like','useType',1]);
}
if($type==2){
$list->andWhere(['like','useType',3]);
}
if($type==3){
$list->andWhere(['like','useType',3]);
}
$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'=>$userId])
->all();
$userCouponId = array_column($userCoupon, 'couponId')?:[];
$list=$list->andWhere(['not in','id',$userCouponId])->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]['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'] .='外卖可用';
}
}
return $list?:[];
}
public static function getIssueCoupons($uniacid,$userId){
$now=time();
$storeId=Store::getMainStore($uniacid)['id'];
$monthArr = getMonthRange();
$weekArr = getWeekRange();
$member = (new \yii\db\Query())
->from('{{%ybwm_member}}')
->where(['id'=>$userId])
->one();
$user = (new \yii\db\Query())
->from('{{%ybwm_member_bind}}')
->where(['userId'=>$userId,'storeId'=>$storeId])
->one();
$newOrder = (new \yii\db\Query())
->from('{{%ybwm_bill}}')
->where(['userId'=>$userId])
->andWhere(['in','origin',[1]])
->one();//新用户
$issueCoupons = (new \yii\db\Query())
->from('{{%ybwm_issue_coupons}}')
->where('uniacid=:uniacid AND deleteAt=0 AND display=1 AND startTime<:time AND endTime>:time',[':uniacid'=>$uniacid,':time'=>$now])
->all();
if(!$issueCoupons){
return [];
}
for($i=0;$i<count($issueCoupons);$i++){
$userArr=json_decode($issueCoupons[$i]['userArr'],true);
$userArr=array_column($userArr,'id');
if($issueCoupons[$i]['people']==2 AND $newOrder){
//新用户
continue;
}
if($issueCoupons[$i]['people']==3 AND !$member['level']){
//指定会员
continue;
}
if($issueCoupons[$i]['people']==5 AND !in_array($user['vipLabel'],json_decode($issueCoupons[$i]['userLabel'],true))){
continue;
}
if($issueCoupons[$i]['people']==6 AND !in_array($userId,$userArr)){
continue;
}
$receive=[];
if ($issueCoupons[$i]['numType'] == 1) {
$receive =(new \yii\db\Query())
->from('{{%ybwm_grant_coupon_receive}}')
->where(['grantId'=>$issueCoupons[$i]['id'],'userId'=>$userId,'activityType'=>1])
->one();
}
if ($issueCoupons[$i]['numType'] == 2) {
//每月一次
$receive = (new \yii\db\Query())
->from('{{%ybwm_grant_coupon_receive}}')
->where('grantId=:grantId AND activityType=1 AND userId=:userId AND createdAt>:startTime AND createdAt<=:endTime',[':startTime'=>$monthArr['start'],'endTime'=>$monthArr['end'],'grantId'=>$issueCoupons['id'],'userId'=>$userId])
->one();
}
if ($issueCoupons[$i]['numType'] == 3) {
//每周一次
$receive = (new \yii\db\Query())
->from('{{%ybwm_grant_coupon_receive}}')
->where('grantId=:grantId AND userId=:userId AND activityType=1 AND createdAt>:startTime AND createdAt<=:endTime',[':startTime'=>$weekArr['start'],'endTime'=>$weekArr['end'],'grantId'=>$issueCoupons['id'],'userId'=>$userId])
->one();
}
if (!$receive) {
$data=array('activityType'=>1,'userId' => $userId, 'grantId' => $issueCoupons[$i]['id'], 'createdAt' => time(), 'uniacid' => $uniacid);
Yii::$app->db->createCommand()->insert('{{%ybwm_grant_coupon_receive}}', $data)->execute();
Member::receiveCoupon(json_decode($issueCoupons[$i]['coupon'],true), $userId, 6);
// $list=(new \yii\db\Query())
// ->select('id,name,timeType,useEndTime,isNextDay,day,isFull,fullMoney,type,money,discount')
// ->from('{{%ybwm_coupon}}')
// ->where(['in','id',json_decode($issueCoupons[$i]['coupon'],true)])
// ->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'].'可用,';
// }
// }
// $issueCoupons[$i]['couponArr'] = $list;
// return $issueCoupons;
}
}
return [];
}
public static function rollBag($storeId){
$res=(new \yii\db\Query())
->from('{{%ybwm_roll_bag}}')
->where('storeId=:storeId AND deleteAt=0 AND display=1',[':storeId'=>$storeId])->all();
if($res){
for($i=0;$i<count($res);$i++){
$res[$i]['coupon']=json_decode($res[$i]['coupon'],true);
}
}
return $res;
}
}