REAPI/app/queue/command/monitor/ChannelBalanceMonitor.php
mzeros 21e4c1e37a feat(queue): 添加渠道余额监控预警功能
- 新增 ChannelBalanceMonitor 类实现渠道余额监控
- 在 MerchantBalanceMonitor 中增加余额恢复提醒功能
- 添加 WxWorkBot 类用于发送微信工作台消息- 在 Test 控制器中整合渠道余额监控逻辑
- 更新 sys.php 添加新的渠道余额监控命令
2025-01-06 13:08:15 +08:00

132 lines
4.3 KiB
PHP

<?php
namespace app\queue\command\monitor;
use app\channel\service\ChannelService;
use app\gateway\service\RedisService;
use app\robot\controller\WxWorkBot;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
/**
* 渠道余额监控预警
* Class ChannelBalanceMonitor
* @package app\data\command
*/
class ChannelBalanceMonitor extends Command
{
protected $redis = false;
protected function configure()
{
$this->setName('xQueue:ChannelBalanceMonitor')->setDescription('[ 监控系统 ] 渠道余额监控预警');
}
/**
* @param Input $input
* @param Output $output
* @throws \think\admin\Exception
*/
protected function execute(Input $input, Output $output)
{
ini_set('memory_limit', '1024M');
$this->redis();
$channelService = ChannelService::instance();
$RobotService = new WxWorkBot($this->app);
$channelList = $channelService->db()->where(['status'=>1])->whereLike('other_data','%Monitor_Balance_status%')->select()->toArray();
if(!$channelList) $this->setQueueSuccess("未找到支持监控余额的渠道");
$channelList_num = count($channelList);
$count_sum = 0;
$day = date('Y-m-d', strtotime('-1 day'));
$tip_info=[];
$key = '41b42bd4-c9f9-4617-9531-0a358dd97a82';
foreach($channelList as $channel) {
$other_param = json_decode($channel['other_data'],true);
if(!$other_param){
continue;
}
$Monitor_Balance = $other_param['Monitor_Balance'] ?? null;
if(!$Monitor_Balance){
continue;
}
try{
$account_data = $channelService->call('account', $channel['id'], $day);
if (is_array($account_data) && isset($account_data['account'])) {
$balance = $account_data['account'];
if( $balance <= $Monitor_Balance){
if(!$this->redis->get('Check_channel_account-'.$channel['id'])){
$count_sum ++;
$this->redis->set('Check_channel_account-'.$channel['id'],1);
$tip_info[] = $channel['name']."<font color=\"comment\">余额不足,只剩下</font>:<font color=\"info\">".$balance."</font>";
}
$Monitor_Balance_TOP = $other_param['Monitor_Balance_TOP'] ?? null;
if($Monitor_Balance_TOP && $balance <= $Monitor_Balance_TOP){
if(!$this->redis->get('Check_channel_account_TOP-'.$channel['id'])){
$count_sum ++;
$this->redis->set('Check_channel_account_TOP-'.$channel['id'],1);
$tip_info[] = $channel['name']."<font color=\"comment\">余额不足,只剩下</font>:<font color=\"info\">".$balance."</font>";
}
}
}else{
if($this->redis->get('Check_channel_account-'.$channel['id'])){
$count_sum ++;
$this->redis->delete('Check_channel_account-'.$channel['id']);
$tip_info[] = $channel['name']."<font color=\"comment\">余额已经恢复,现在余额为</font>:<font color=\"info\">".$balance."</font>";
}
if($this->redis->get('Check_channel_account_TOP-'.$channel['id'])){
$this->redis->delete('Check_channel_account_TOP-'.$channel['id']);
}
}
}
}catch (\Exception $e){
}
}
if(!empty($tip_info)){
$errorMsg = "渠道余额监控预警<font color=\"warning\">".$count_sum ."列</font>,请相关同事注意。\n
>";
$errorMsg .= implode(">", $tip_info);
$RobotService->send_markdown($errorMsg,$key);
}
$this->setQueueSuccess("共处理 {$channelList_num} 个渠道, 完成{$count_sum} 个查询处理!");
}
protected function redis()
{
if (!$this->redis) {
$this->redis = RedisService::getInstance();
}
}
}