198 lines
8.0 KiB
PHP
198 lines
8.0 KiB
PHP
<?php
|
|
/**
|
|
* @link http://www.yiiframework.com/
|
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
|
* @license http://www.yiiframework.com/license/
|
|
*/
|
|
|
|
namespace app\commands;
|
|
|
|
use app\models\Activity;
|
|
use app\models\ActivityUserLog;
|
|
use app\models\ActivityUserPrize;
|
|
use app\models\ActivityUserRate;
|
|
use app\models\BallCart;
|
|
use app\models\BallMark;
|
|
use app\models\Order;
|
|
use app\models\UserCoupon;
|
|
use app\modules\api\components\Mqtt;
|
|
use yii\console\Controller;
|
|
use yii\console\ExitCode;
|
|
use \PhpMqtt\Client\MQTTClient;
|
|
use \PhpMqtt\Client\ConnectionSettings;
|
|
use app\models\Store;
|
|
|
|
/**
|
|
* This command echoes the first argument that you have entered.
|
|
*
|
|
* This command is provided as an example for you to learn how to create console commands.
|
|
*
|
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
* @since 2.0
|
|
*/
|
|
class Script1minController extends Controller
|
|
{
|
|
/**
|
|
* This command echoes what you have entered as the message.
|
|
* @param string $message the message to be echoed.
|
|
* @return int Exit code
|
|
*/
|
|
public function actionIndex()
|
|
{
|
|
$redis_name = "cxaibc:console:script1min_lock";
|
|
try {
|
|
$get = \Yii::$app->redis->setnx($redis_name,1);
|
|
if(empty($get)){
|
|
$ttl = \Yii::$app->redis->ttl($redis_name);
|
|
if($ttl === -1){
|
|
\Yii::$app->redis->expire($redis_name,60);
|
|
}
|
|
var_dump("1分钟脚本正在执行中");
|
|
exit();
|
|
}
|
|
\Yii::$app->redis->expire($redis_name,60*10);
|
|
// 执行体
|
|
$this->ActivityData();
|
|
// 结束执行
|
|
\Yii::$app->redis->del($redis_name);
|
|
}catch (\Exception $e){
|
|
\Yii::$app->redis->del($redis_name);
|
|
var_dump($e->getMessage());
|
|
}
|
|
echo "1分钟脚本";
|
|
exit();
|
|
}
|
|
|
|
/**
|
|
* 活动数据
|
|
*/
|
|
public function ActivityData(){
|
|
try{
|
|
$activity = Activity::find()->andWhere([
|
|
'status' => 1,
|
|
'is_delete' => 0,
|
|
])->andWhere([
|
|
'<=','start_time',time()
|
|
])->andWhere([
|
|
'>=','end_time',time()
|
|
])->asArray()->all();
|
|
var_dump($activity);
|
|
foreach ($activity as $k=>$v){
|
|
$activity[$k]['data'] = json_decode($v['data'],true);
|
|
}
|
|
// 获取未执行数据
|
|
$select = ActivityUserLog::find()->andWhere([
|
|
'status' => 1,
|
|
'is_delete' => 0,
|
|
])->all();
|
|
if(empty($select)){
|
|
return False;
|
|
}
|
|
foreach ($select as $key=>$val){
|
|
if(empty($activity)){
|
|
$val->delete();
|
|
continue;
|
|
}
|
|
$json_de = json_decode($val['data'],true);
|
|
if($json_de['type'] === 1){
|
|
// 获取订单数据
|
|
$find_order = Order::findOne([
|
|
'id' => $json_de['data'],
|
|
]);
|
|
if(empty($find_order)){
|
|
$val->delete();
|
|
continue;
|
|
}
|
|
if(empty($find_order->is_pay)){// 未支付
|
|
continue;
|
|
}
|
|
if(!empty($find_order->is_delete)){// 被删除
|
|
$val->delete();
|
|
continue;
|
|
}
|
|
if($find_order->cancel_status == 2){//进行申请退款
|
|
continue;
|
|
}
|
|
if($find_order->cancel_status === 1){//退款成功
|
|
$val->delete();
|
|
continue;
|
|
}
|
|
foreach ($activity as $k=>$v){
|
|
if(floatval($v['data']['pz']['order_min_money']) > $find_order->total_pay_price){
|
|
// 订单实付金额小于设置金额,跳过,下一个
|
|
continue;
|
|
}
|
|
// 添加数据
|
|
$find_rate = ActivityUserRate::findOne([
|
|
'user_id' => $val->user_id,
|
|
'activity_id' => $v['id'],
|
|
'is_delete' => 0,
|
|
]);
|
|
if(empty($find_rate)){
|
|
$find_rate = new ActivityUserRate();
|
|
$find_rate->user_id = $val->user_id;
|
|
$find_rate->activity_id = $v['id'];
|
|
$find_rate->rate = 0;
|
|
$find_rate->created_at = time();
|
|
$find_rate->is_delete = 0;
|
|
$find_rate->deleted_at = 0;
|
|
}
|
|
$find_rate->all_rate = intval($v['data']['pz']['data']);
|
|
$find_rate->rate += 1;
|
|
$find_rate->updated_at = time();
|
|
if($find_rate->all_rate <= $find_rate->rate){
|
|
// 可以领奖
|
|
// 获取获奖次数
|
|
$count_prize = ActivityUserPrize::find()->andWhere([
|
|
'activity_id' => $v['id'],
|
|
'user_id' => $val->user_id,
|
|
'is_delete' => 0,
|
|
])->count()??0;
|
|
if($count_prize < intval($v['data']['user_max'])){
|
|
if(!empty($v['data']['prize_arr'])){
|
|
foreach ($v['data']['prize_arr'] as $k1=>$v1){
|
|
// 奖项数据
|
|
for($i=0;$i<intval($v1['num']);$i++){
|
|
$obj_coupon = new UserCoupon();
|
|
$obj_coupon->user_id = $val->user_id;
|
|
$obj_coupon->coupon_id = $v1['id'];
|
|
$obj_coupon->status = 0;
|
|
$obj_coupon->created_at = time();
|
|
$obj_coupon->updated_at = time();
|
|
$obj_coupon->is_delete = 0;
|
|
$obj_coupon->deleted_at = 0;
|
|
$obj_coupon->is_admin_send = 1;
|
|
$obj_coupon->save();
|
|
}
|
|
}
|
|
}
|
|
// 进行领奖
|
|
$obj_prize = new ActivityUserPrize();
|
|
$obj_prize->user_id = $val->user_id;
|
|
$obj_prize->activity_id = $v['id'];
|
|
$obj_prize->data = json_encode($v['data']['prize_arr'],JSON_UNESCAPED_UNICODE);
|
|
$obj_prize->created_at = time();
|
|
$obj_prize->updated_at = time();
|
|
$obj_prize->is_delete = 0;
|
|
$obj_prize->deleted_at = 0;
|
|
$obj_prize->save();
|
|
// 更新最后的进度
|
|
$find_rate->rate = 0;
|
|
$find_rate->updated_at = time();
|
|
}
|
|
}
|
|
$find_rate->save();
|
|
}
|
|
}
|
|
// 变更为已执行完成
|
|
$val->status = 2;
|
|
$val->save();
|
|
}
|
|
}catch (\Exception $e){
|
|
var_dump($e->getMessage());
|
|
var_dump($e->getLine());
|
|
|
|
}
|
|
}
|
|
}
|