canyin-project/ybcy/commands/tasks/CommonController.php

468 lines
18 KiB
PHP
Raw Permalink Normal View History

2024-11-01 16:07:54 +08:00
<?php
namespace app\commands\tasks;
use Yii;
use yii\console\Controller;
use yii\console\ExitCode;
use app\models\common\Member;
use app\models\common\Config;
use app\models\common\Order;
use app\models\common\WeChat;
class CommonController extends Controller
{
//查找发放对象
public function actionBirthday(){
$now=time();
$res=(new \yii\db\Query())
->from('{{%ybwm_birthday}}')
->where('deleteAt=0 AND display=1 AND startTime<=:startTime AND endTime>:endTime',[':startTime'=>$now,':endTime'=>$now])
->all();
if($res){
for($i=0;$i<count($res);$i++){
$config=Config::getSystemSet('birthday',$res[$i]['uniacid']);
$day = $config['day'];
$time = time();
if($day){
$time = time() + $day * 86400;
}
$time = date("m-d", $time);
$userTable=(new \yii\db\Query())
->select('id,uniacid')
->from('{{%ybwm_member}}')
->where("deleteAt=0 AND isBlack=2 AND from_unixtime(birthday, '%m-%d ')=:birthday AND uniacid=:uniacid",[':birthday'=>$time,':uniacid'=>$res[$i]['uniacid']]);
$userTable->andWhere('id not in (select userId from {{%ybwm_grant_coupon_receive}} where uniacid='.$res[$i]['uniacid'].' AND activityType=2 AND from_unixtime(createdAt, "%Y ")='.date("Y").' group by userId )');
if($res[$i]['people']==2){
//新用户
$userTable->andWhere('id not in (select userId from {{%ybwm_bill}} where uniacid='.$res[$i]['uniacid'].' AND (refundMoney=0 or is not null) group by userId )');
}
$userData['userArr']=$userTable->all();
if($userData['userArr']){
$userData['balance']=0;
$userData['integral']=0;
$userData['growth']=0;
$userData['coupon']=[];
$userData['id']=$res[$i]['id'];
if($res[$i]['isBalance']==1){
$userData['balance']=$res[$i]['balance']?:0;
}
if($res[$i]['isIntegral']==1){
$userData['integral']=$res[$i]['integral']?:0;
}
if($res[$i]['isGrowth']==1){
$userData['growth']=$res[$i]['growth']?:0;
}
if($res[$i]['isCoupon']==1){
$userData['coupon']=json_decode($res[$i]['coupon'],true)?:[];
}
Yii::$app->redis->rpush("birthdayData",json_encode($userData));
}
}
}
return ExitCode::OK;
}
//发放生日福利
public function actionGrant(){
$num=Yii::$app->redis->llen('birthdayData');
for($i=0;$i<$num;$i++){
$res=Yii::$app->redis->lpop('birthdayData');
$data=json_decode($res,true);
$user=$data['userArr'];
for($u=0;$u<count($user);$u++){
$receive=(new \yii\db\Query())
->from('{{%ybwm_grant_coupon_receive}}')
->where('userId='.$user[$u]['id'].' AND activityType=2 AND from_unixtime(createdAt, "%Y ")='.date("Y").'')
->one();
if(!$receive){
if($data['balance']>0){
Member::saveBalance($user[$u]['id'],2,$data['balance'],1,'生日有礼',$user[$u]['uniacid']);
}
if($data['integral']>0){
Member::saveIntegral($user[$u]['id'],2,$data['integral'],1,'生日有礼',$user[$u]['uniacid']);
}
if($data['coupon']){
Member::receiveCoupon($data['coupon'],$user[$u]['id'],8);
}
$grantData['userId']=$user[$u]['id'];
$grantData['grantId']=$data['id'];
$grantData['activityType']=2;
$grantData['createdAt']=time();
$grantData['uniacid']=$user[$u]['uniacid'];
Yii::$app->db->createCommand()->insert('{{%ybwm_grant_coupon_receive}}', $grantData)->execute();
}
}
}
}
//1取消订单
public function actionOrderCancel(){
$res=(new \yii\db\Query())
->from('{{%ybwm_core_system}}')
->where('ident=:ident',[':ident'=>'currency'])
->all();
$newData=[];
foreach ($res as $v){
$data=json_decode($v['data'], true)?:[];
$query=(new \yii\db\Query())
->select('id')
->from('{{%ybwm_takeout_order}}')
->where('uniacid=:uniacid',[':uniacid'=>$v['uniacid']]);
$orderName='autoClose';
$query->andWhere(['state'=>1]);
if($data['autoClose']==1){
$day=$data['closeTime'];
$time=time();
if($day){
$time=time()-60*$day;
}
$query->andWhere(['<','createdAt',$time]);
$orderData=$query->all();
if($orderData){
$newData[]=$orderData;
}
}
}
if($newData){
foreach ($newData as $v){
foreach ($v as $vo){
if($vo){
Yii::$app->redis->rpush($orderName,json_encode($vo));
}
}
}
}
$num=Yii::$app->redis->llen('autoClose');
if($num){
for ($i=0;$i<$num;$i++){
$res=Yii::$app->redis->lpop('autoClose');
$data=json_decode($res,true);
if($data){
Order::cancel($data['id']);
}
}
}
return ExitCode::OK;
}
//拒单
public function actionRejection(){
$res=(new \yii\db\Query())
->from('{{%ybwm_core_system}}')
->where('ident=:ident',[':ident'=>'currency'])
->all();
$newData=[];
foreach ($res as $v){
$data=json_decode($v['data'], true)?:[];
$query=(new \yii\db\Query())
->select('id')
->from('{{%ybwm_takeout_order}}')
->where('uniacid=:uniacid',[':uniacid'=>$v['uniacid']]);
$orderName='autoRefuse';
$query->andWhere(['state'=>2]);
if($data['autoRefuse']==1){
$day=$data['refuseTime'];
$time=time();
if($day){
$time=time()-60*$day;
}
$query->andWhere(['<','payAt',$time]);
$orderData=$query->all();
if($orderData){
$newData[]=$orderData;
}
}
}
if($newData){
foreach ($newData as $v){
foreach ($v as $vo){
if($vo){
Yii::$app->redis->rpush($orderName,json_encode($vo));
}
}
}
}
$num=Yii::$app->redis->llen('autoRefuse');
if($num){
for ($i=0;$i<$num;$i++){
$res=Yii::$app->redis->lpop('autoRefuse');
$data=json_decode($res,true);
if($data){
Order::rejection($data['id']);
}
}
}
return ExitCode::OK;
}
//确认收货时间
public function actionReceiving(){
$res=(new \yii\db\Query())
->from('{{%ybwm_core_system}}')
->where('ident=:ident',[':ident'=>'currency'])
->all();
$newData=[];
foreach ($res as $v){
$data=json_decode($v['data'], true)?:[];
$query=(new \yii\db\Query())
->select('id,uniacid,storeId,userId')
->from('{{%ybwm_takeout_order}}')
->where('uniacid=:uniacid',[':uniacid'=>$v['uniacid']]);
$orderName='autoComplete';
$query->andWhere(['state'=>4]);
if($data['autoComplete']==1){
$day=$data['completeTime'];
$time=time();
if($day){
$time=time()-60*$day*60;
}
$query->andWhere(['<','serviceTime',$time]);
$orderData=$query->all();
if($orderData){
$newData[]=$orderData;
}
}
}
if($newData){
foreach ($newData as $v){
foreach ($v as $vo){
if($vo){
Yii::$app->redis->rpush($orderName,json_encode($vo));
//集点活动
Member::saveColletCoupon($vo['id'],$vo['uniacid'],$vo['storeId'],$vo['userId']);
}
}
}
}
$num=Yii::$app->redis->llen('autoComplete');
if($num){
for ($i=0;$i<$num;$i++){
$res=Yii::$app->redis->lpop('autoComplete');
$data=json_decode($res,true);
if($data){
Order::receiving($data['id']);
}
}
}
return ExitCode::OK;
}
//快餐确认收货时间
public function actionFastReceiving(){
$res=(new \yii\db\Query())
->from('{{%ybwm_store_set}}')
->where('ident=:ident',[':ident'=>'fastSet'])
->all();
$newData=[];
foreach ($res as $v){
$data=json_decode($v['data'], true)?:[];
$query=(new \yii\db\Query())
->select('id')
->from('{{%ybwm_instore_order}}')
->where('uniacid=:uniacid AND orderMode=2 AND state=3',[':uniacid'=>$v['uniacid']]);
$orderName='autoFastComplete';
if($data['autoComplete']==1){
$day=$data['completeTime'];
$time=time();
if($day){
$time=time()-60*$day;
}
$query->andWhere(['<','receiptAt',$time]);
$orderData=$query->all();
if($orderData){
$newData[]=$orderData;
}
}
}
if($newData){
foreach ($newData as $v){
foreach ($v as $vo){
if($vo){
Yii::$app->redis->rpush($orderName,json_encode($vo));
}
}
}
}
$num=Yii::$app->redis->llen('autoFastComplete');
if($num){
for ($i=0;$i<$num;$i++){
$res=Yii::$app->redis->lpop('autoFastComplete');
$data=json_decode($res,true);
if($data){
Order::fastComplete($data['id']);
}
}
}
return ExitCode::OK;
}
//发放会员优惠券
public function actionReleaseVipCoupon(){
$now=time();//
$res=(new \yii\db\Query())
->select('a.id,a.userId,a.uniacid,a.couponId')
->from('{{%ybwm_user_vip_coupon}} as a')
->join('LEFT JOIN', '{{%ybwm_member}} as b', 'b.id = a.userId')
->where('a.releaseAt<=:releaseAt AND a.state=2 AND b.vipEndTime>:vipEndTime',[':releaseAt'=>$now,':vipEndTime'=>$now])
->all();
// print_R($res);die;
for($i=0;$i<count($res);$i++){
$rebateSet=Config::getSystemSet('payVipCycleRebate',$res[$i]['uniacid']);
Yii::$app->db->createCommand()->update('{{%ybwm_user_vip_coupon}}',['state'=>1],'id=:id', [':id' =>$res[$i]['id']])->execute();
//$rebateSet=Config::getSystemSet('payVipCycleRebate',$res[$i]['uniacid']);
if($rebateSet['open']==1){
$releaseAt=strtotime(date("Y-m-d"))+($rebateSet['day']+1)*86400;
$couponData['userId']=$res[$i]['userId'];
$couponData['couponId']=json_encode($rebateSet['couponArr']);
$couponData['releaseAt']=$releaseAt?:0;
$couponData['state']=2;
$couponData['uniacid']=$res[$i]['uniacid'];
$couponData['createdAt']=time();
$info=(new \yii\db\Query())
->from('{{%ybwm_user_vip_coupon}}')
->where(['userId'=>$res[$i]['userId'],'releaseAt'=>$releaseAt?:0])
->one();
if(!$info){
Yii::$app->db->createCommand()->insert('{{%ybwm_user_vip_coupon}}',$couponData)->execute();
Member::receiveCoupon($rebateSet['couponArr'],$res[$i]['userId'],13);
}
}
}
return ExitCode::OK;
}
/**
* 获取锁
* @param String $key 锁标识
* @param Int $expire 锁过期时间
* @return Boolean
*/
public function lock($key, $expire=5){
$is_lock = Yii::$app->redis->setnx($key, time()+$expire);
// 不能获取锁
if(!$is_lock){
// 判断锁是否过期
$lock_time = Yii::$app->redis->get($key);
// 锁已过期,删除锁,重新获取
if(time()>$lock_time){
$this->unlock($key);
$is_lock = Yii::$app->redis->setnx($key, time()+$expire);
}
}
return $is_lock? true : false;
}
/**
* 释放锁
* @param String $key 锁标识
* @return Boolean
*/
public function unlock($key){
return Yii::$app->redis->del($key);
}
//堂食分账
public function actionProfitSharing(){
$dnOrder=(new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('profitSharingState=2 AND profitSharing=1 AND payMode=1 AND payAt>0')
->all();
if($dnOrder){
for($j=0;$j<count($dnOrder);$j++){
WeChat::profitSharing($dnOrder[$j]['id'],2);//分账
}
}
$order=(new \yii\db\Query())
->from('{{%ybwm_cashier_order}}')
->where('profitSharingState=2 AND profitSharing=1 AND payMode=1 AND payAt>0')
->all();
if($order){
for($i=0;$i<count($order);$i++){
WeChat::profitSharing($order[$i]['id'],3);//分账
}
}
return ExitCode::OK;
}
//查看是会员但是没有发券
public function actionVipTest(){
$member=(new \yii\db\Query())
->select('id,uniacid')
->from('{{%ybwm_member}}')
->where('vipEndTime>'.time())
->all();
for($i=0;$i<count($member);$i++){
$info=(new \yii\db\Query())
->from('{{%ybwm_user_vip_coupon}}')
->where('userId=:userId AND releaseAt=:releaseAt',[':releaseAt'=>strtotime(date("Y-m-d")),':userId'=>$member[$i]['id']])
->one();
if(!$info){
$rebateSet=Config::getSystemSet('payVipCycleRebate',$member[$i]['uniacid']);
$couponData['userId']=$member[$i]['id'];
$couponData['couponId']=json_encode($rebateSet['couponArr']);
$couponData['releaseAt']=strtotime(date("Y-m-d"));
$couponData['state']=2;
$couponData['uniacid']=$member[$i]['uniacid'];
$couponData['createdAt']=time();
Yii::$app->db->createCommand()->insert('{{%ybwm_user_vip_coupon}}',$couponData)->execute();
}
}
}
//次日置满
public function actionGoodsFillUp(){
$transaction = Yii::$app->db->beginTransaction(); //开始事务
try{
$goodsData=(new \yii\db\Query())->select('id')
->from('{{%ybwm_core_goods}}')
->where(['fillType'=>1,'deleteAt'=>0])->all();
$goodsId=array_column($goodsData,'id');
Yii::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['stock'=>999],['in','id',$goodsId])->execute();
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}',['stock'=>999],['in','goodsId',$goodsId])->execute();
$goodsData=(new \yii\db\Query())->select('id')
->from('{{%ybwm_good_specs}}')
->where(['fillType'=>1])->all();
$specsId=array_column($goodsData,'id');
Yii::$app->db->createCommand()->update('{{%ybwm_good_specs}}',['SalesStock'=>999],['in','id',$specsId])->execute();
$storeGoodsData=(new \yii\db\Query())
->from('{{%ybwm_store_goods}}')
->where(['isSpecs'=>1,'deleteAt'=>0])->all();
foreach ($storeGoodsData as $v){
$data=json_decode($v['data'],true);
$specsArr=$data['specs'];
if($specsArr){
foreach ($specsArr as $k=>$vo){
//if($vo['fillType']==1){
$specsArr[$k]['SalesStock']=999;
// }
}
$newData['specs']=$specsArr;
Yii::$app->db->createCommand()->update('{{%ybwm_store_goods}}',['data'=>json_encode($newData)],['id'=>$v['id']])->execute();
}
}
$transaction->commit();//提交事务
}catch(\Exception $e){// 如果有一条查询失败,则会抛出异常
$transaction->rollBack();//事务回滚
}
return ExitCode::OK;
}
//商品名称拼音写入
public function actionInsertGoodName(){
$data=(new \yii\db\Query())->select('id,name')->from('{{%ybwm_core_goods}}')
->where('pinYin is null')->all();
foreach ($data as &$v){
Yii::$app->db->createCommand()->update('{{%ybwm_core_goods}}',['pinYin'=>stringToPinYin($v['name'])],['id'=>$v['id']])->execute();
}
return ExitCode::OK;
}
}