Commit a17c5cb9 authored by zhoujun's avatar zhoujun

预警

parent 2fd46a8b
Pipeline #2091 passed with stage
in 0 seconds
<?php
namespace App\Console\Commands;
use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Facades\Util\Log;
use App\Services\Script\IScriptService;
class SingleWithdrawal extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'SingleWithdrawal';
/**
* The console command description.
*
* @var string
*/
protected $description = '单次提现预警';
/**
* @var ActivityOrder
*/
private $scriptService;
/**
* Create a new command instance.
* @param ProductOrder $productOrder
* @param ScoreDetail $scoreDetail
*/
public function __construct(IScriptService $scriptService)
{
parent::__construct();
$this->scriptService = $scriptService;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
set_time_limit(0);
$this->line('开始');
$this->scriptService->getSingleWithdrawal();
$this->line('结束');
}
}
\ No newline at end of file
<?php <?php
namespace App\Console; namespace App\Console;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
...@@ -13,7 +12,7 @@ class Kernel extends ConsoleKernel ...@@ -13,7 +12,7 @@ class Kernel extends ConsoleKernel
* @var array * @var array
*/ */
protected $commands = [ protected $commands = [
// 'App\Console\Commands\SingleWithdrawal',
]; ];
/** /**
...@@ -24,8 +23,7 @@ class Kernel extends ConsoleKernel ...@@ -24,8 +23,7 @@ class Kernel extends ConsoleKernel
*/ */
protected function schedule(Schedule $schedule) protected function schedule(Schedule $schedule)
{ {
// $schedule->command('inspire') $schedule->command('SingleWithdrawal')->everyMinute()->withoutOverlapping();//每分钟跑一次
// ->hourly();
} }
/** /**
......
<?php
/**
* Created by PhpStorm.
* User: zhoujun
* Date: 2020/10/13
*/
namespace App\Facades\Util;
use Illuminate\Support\Facades\Facade;
class Log extends Facade
{
protected static function getFacadeAccessor()
{
return 'util-log';
}
}
\ No newline at end of file
<?php
namespace App\Models;
use App\Models\BaseModel;
class LastIdModel extends BaseModel
{
protected $connection = 'mysql';
public $timestamps = false;
protected $table = 'club_last_id';
}
\ No newline at end of file
<?php
namespace App\Models;
use App\Models\BaseModel;
class SyceeWithdrawModel extends BaseModel
{
protected $primaryKey = 'serial';
protected $connection = 'mysql';
public $timestamps = false;
protected $table = 'sycee_withdraw';
}
<?php
namespace App\Models;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
class UserCashModel extends BaseModel
{
use SoftDeletes;
protected $connection = 'mysql';
protected $table = 'club_user_cash';
}
\ No newline at end of file
<?php
namespace App\Models;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
class WarnWaitModel extends BaseModel
{
use SoftDeletes;
protected $connection = 'mysql';
protected $table = 'club_warn_wait';
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use App\Services\Util\LogService;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
...@@ -13,10 +14,16 @@ class AppServiceProvider extends ServiceProvider ...@@ -13,10 +14,16 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->bind('util-log', function () {
return new LogService();
});
$this->app->bind('App\Services\User\IUserService', 'App\Services\User\UserService'); $this->app->bind('App\Services\User\IUserService', 'App\Services\User\UserService');
$this->app->bind('App\Services\White\IWhiteService', 'App\Services\White\WhiteService'); $this->app->bind('App\Services\White\IWhiteService', 'App\Services\White\WhiteService');
$this->app->bind('App\Services\Account\IAccountService', 'App\Services\Account\AccountService'); $this->app->bind('App\Services\Account\IAccountService', 'App\Services\Account\AccountService');
$this->app->bind('App\Services\Warn\IWarnService', 'App\Services\Warn\WarnService'); $this->app->bind('App\Services\Warn\IWarnService', 'App\Services\Warn\WarnService');
$this->app->bind('App\Services\Script\IScriptService', 'App\Services\Script\ScriptService');
} }
/** /**
......
<?php
namespace App\Services\Script;
interface IScriptService
{
public function getSingleWithdrawal();//单次最高提现预警
}
<?php
namespace App\Services\Script;
use App\Services\Script\IScriptService;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Facades\Util\Log;
use App\Models\WarnConfigModel;
use App\Models\LastIdModel;
use App\Models\WarnWaitModel;
use App\Models\UserCashModel;
use App\Models\SyceeWithdrawModel;
use App\Models\WarnPeopleModel;
class ScriptService implements IScriptService
{
private $time;
public function __construct()
{
$this->time = Carbon::now()->format('Y-m-d H:i:s');
}
/**
* 单次提现最高预警
* @return int
* @date 2021/08/02
* @author live
*/
public function getSingleWithdrawal()
{
$config = self::getWarnConfig(1);
$oneMin = $config['oneMin'] ?? 0;
if(empty($config) || $oneMin == 0) return true;
$lastId = self::getLastDataId(2);
$warnPeople = self::getWarnPeople();
$logs = SyceeWithdrawModel::where('sycee_withdraw.serial','>',$lastId)->where('sycee_withdraw.cash','>',$oneMin)->join('account','sycee_withdraw.user_id','account.user_id')->orderBy('sycee_withdraw.serial','ASC')->select('account.user_id','account.platform','account.platform_id','account.channel','sycee_withdraw.serial','sycee_withdraw.cash','sycee_withdraw.withdraw_time')->limit(100)->get();
$array = [];
$maxId = 0;
foreach($logs as $log){
$maxId = $log->serial;
$key = $log->platform.'-'.$log->channel;
$warnConfig = $config[$key] ?? [];
if(empty($warnConfig)) continue;
$people = $warnPeople[$key] ?? [];
if(empty($people)) continue;
$tem['ymd'] = date('Ymd',strtotime($log->withdraw_time));
$tem['config_id'] = $warnConfig['id'];
$tem['type'] = 1;
$tem['cash'] = $log->cash;
$tem['warn_value'] = $warnConfig['one'];
$tem['people_id'] = implode('#',$people['id']);
$tem['people_phone'] = implode('#',$people['phone']);
$tem['people_name'] = implode('#',$people['name']);
$tem['user_id'] = $log->platform_id;
$tem['open_id'] = $log->user_id;
$tem['status'] = 0;
$tem['created_at'] = $this->time;
$tem['updated_at'] = $this->time;
$array[] = $tem;
}
DB::beginTransaction();
try{
if(!empty($array)) WarnWaitModel::insert($array);
if($maxId > 0) self::getUpdateLastId(2,$maxId);
DB::commit();
return true;
} catch (\Exception $e){
DB::rollback();
return false;
}
}
/**
* 获取预警人信息
* @return array
* @date 2021/08/02
* @author live
*/
public static function getWarnPeople()
{
$array = [];
$peoples = WarnPeopleModel::where('status',1)->select('id','platform','channel','name','phone')->get();
foreach($peoples as $peo){
$key = $peo->platform.'-'.$peo->channel;
if(isset($array[$key])){
$array[$key]['id'][] = $peo->id;
$array[$key]['name'][] = $peo->name;
$array[$key]['phone'][] = $peo->phone;
}else{
$tem['id'][] = $peo->id;
$tem['name'][] = $peo->name;
$tem['phone'][] = $peo->phone;
$array[$key] = $tem;
}
}
unset($peoples,$peo,$tem);
return $array;
}
/**
* 跑完脚本更新最后一组数据ID
* @return array
* @date 2021/08/02
* @author live
*/
public static function getUpdateLastId($type,$maxId)
{
$last = LastIdModel::where('type',$type)->select('id','last_id')->first();
if($last){
$last->last_id = $maxId;
}else{
$last = new LastIdModel();
$last->type = $type;
$last->last_id = $maxId;
$last->created_at = Carbon::now()->format('Y-m-d H:i:s');
}
$last->save();
return true;
}
/**
* 获取最后一跑数据的ID
* @return array
* @date 2021/08/02
* @author live
*/
public static function getLastDataId($type)
{
$last = LastIdModel::where('type',$type)->select('id','last_id')->first();
return $last->last_id ?? 0;
}
/**
* 获取预警配置
* @return array
* @date 2021/08/02
* @author live
*/
public static function getWarnConfig($type)
{
$array = ['oneMin'=>0,'dayMin'=>0,'userMin'=>0];
$configs = WarnConfigModel::where('status',1)->select('id','platform','channel','one_withdrawal','day_withdrawal','people_day_withdrawal')->get();
if(!$configs->count()) return [];
foreach($configs as $con){
$key = $con->platform.'-'.$con->channel;
if($array['oneMin'] == 0 || $array['oneMin'] > $con->one_withdrawal) $array['oneMin'] = $con->one_withdrawal;
if($array['dayMin'] == 0 || $array['dayMin'] > $con->day_withdrawal) $array['dayMin'] = $con->day_withdrawal;
if($array['userMin'] == 0 || $array['userMin'] > $con->people_day_withdrawal) $array['userMin'] = $con->people_day_withdrawal;
if(isset($array[$key])){
if($array[$key]['one'] > $con->one_withdrawal){
$array[$key]['one'] = $con->one_withdrawal;
if($type == 1) $array[$key]['id'] = $con->id;
}
if($array[$key]['day'] > $con->day_withdrawal){
$array[$key]['day'] = $con->day_withdrawal;
if($type == 2) $array[$key]['id'] = $con->id;
}
if($array[$key]['user'] > $con->people_day_withdrawal){
$array[$key]['user'] = $con->people_day_withdrawal;
if($type == 3) $array[$key]['id'] = $con->id;
}
}else{
$tem['id'] = $con->id;
$tem['one'] = $con->one_withdrawal;
$tem['day'] = $con->day_withdrawal;
$tem['user'] = $con->people_day_withdrawal;
$array[$key] = $tem;
}
}
unset($configs,$con,$tem);
return $array;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: zhoujun
* Date: 2020/10/13
*/
namespace App\Services\Util;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class LogService
{
private $logger;
private $logName;
const MAX_FILE_SIZE = 52428800;
public function __construct()
{
$this->logName = date('Y-m-d') . '.log';
$this->logger = new Logger('local');
}
public function getPath($group, $date)
{
return $group . '/' . $date . '.log';
}
public function getLogs($group)
{
$paths = \Storage::disk('logs')->files($group['folder']);
$logs = [];
foreach($paths as $path) $logs[] = $this->getLog($path);
return collect($logs)->sortBy('timestamp')->reverse();
}
public function setGroup($name)
{
if(!file_exists(storage_path('/logs/'.$name))) $this->getCheckDir(storage_path('logs'),$name);
$handles = [
new StreamHandler(storage_path("/logs/$name/" . $this->logName))
];
$this->logger->setHandlers($handles, Logger::INFO, false);
return $this;
}
public function getCheckDir( $dir ,$folder){
mkdir($dir.'/'.$folder,0775,true);
chmod($dir.'/'.$folder,0775);
}
public function emergency($message, array $context = array())
{
$this->logger->emergency($message, $context);
}
public function alert($message, array $context = array())
{
$this->logger->alert($message, $context);
}
public function critical($message, array $context = array())
{
$this->logger->critical($message, $context);
}
public function error($message, array $context = array())
{
$this->logger->error($message, $context);
}
public function warning($message, array $context = array())
{
$this->logger->warning($message, $context);
}
public function notice($message, array $context = array())
{
$this->logger->notice($message, $context);
}
public function info($message, array $context = array())
{
$this->logger->info($message, $context);
}
public function debug($message, array $context = array())
{
$this->logger->debug($message, $context);
}
}
?>
...@@ -166,7 +166,7 @@ class WarnService implements IWarnService ...@@ -166,7 +166,7 @@ class WarnService implements IWarnService
public function getConfigAdd($postData) public function getConfigAdd($postData)
{ {
if($postData['platform'] < 1 || $postData['channel'] < 1 || $postData['type'] < 1) throw new Exception(config('msg.common.1014'), 1014); if($postData['platform'] < 1 || $postData['channel'] < 1 || $postData['type'] < 1) throw new Exception(config('msg.common.1014'), 1014);
$verity = WarnConfigModel::where('platform', $postData['platform'])->where('channel', $postData['channel'])->where('type',$postData['type'])->where('one_withdrawal',$postData['oneWithdrawal'])->where('day_withdrawal',$postData['dayWithdrawal'])->where('people_day_withdrawal',$postData['userDayWithdrawal'])->select('id')->first(); $verity = WarnConfigModel::where('platform', $postData['platform'])->where('channel', $postData['channel'])->where('type',$postData['type'])->select('id')->first();
if (!$verity) { if (!$verity) {
$row = new WarnConfigModel(); $row = new WarnConfigModel();
$row->platform = $postData['platform'] ?? 0; $row->platform = $postData['platform'] ?? 0;
...@@ -180,7 +180,7 @@ class WarnService implements IWarnService ...@@ -180,7 +180,7 @@ class WarnService implements IWarnService
if ($row->save()) return $row->id; if ($row->save()) return $row->id;
throw new Exception(config('msg.common.1006'), 1006); throw new Exception(config('msg.common.1006'), 1006);
} }
throw new Exception(config('msg.common.1005'), 1005); throw new Exception(config('msg.common.1016'), 1016);
} }
/** /**
...@@ -382,9 +382,9 @@ class WarnService implements IWarnService ...@@ -382,9 +382,9 @@ class WarnService implements IWarnService
if(!empty($conArr)){ if(!empty($conArr)){
foreach($array['data'] as &$val){ foreach($array['data'] as &$val){
$val['oneWithdrawal'] = $conArr[$val['configId']]['oneWithdrawal'] ?? 0.00; $val['oneWithdrawal'] = $conArr[$val['configId']]['oneWithdrawal'] ?? '0.00';
$val['dayWithdrawal'] = $conArr[$val['configId']]['dayWithdrawal'] ?? 0.00; $val['dayWithdrawal'] = $conArr[$val['configId']]['dayWithdrawal'] ?? '0.00';
$val['userDayWithdrawal'] = $conArr[$val['configId']]['userDayWithdrawal'] ?? 0.00; $val['userDayWithdrawal'] = $conArr[$val['configId']]['userDayWithdrawal'] ?? '0.00';
} }
} }
} }
......
...@@ -23,6 +23,7 @@ return [ ...@@ -23,6 +23,7 @@ return [
'1013' => '请填写有效的预警手机号', '1013' => '请填写有效的预警手机号',
'1014' => '平台和渠道为必填项', '1014' => '平台和渠道为必填项',
'1015' => '请选择有效的数据ID', '1015' => '请选择有效的数据ID',
'1016' => '预警数据重复',
], ],
]; ];
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment