REAPI/app/queue/command/monitor/MerchantBalanceMonitor.php
mzeros b45d188232 feat(queue): 添加商户余额监控预警功能
- 新增 MerchantBalanceMonitor 类实现商户余额监控
- 添加 Redis 连接方法以支持缓存预警状态
- 实现余额预警逻辑,包括正常预警和严重预警
- 新增预警解除逻辑,当余额恢复正常时发送通知- 在测试控制器中添加余额检查方法,用于手动触发预警
2025-01-06 00:17:09 +08:00

142 lines
5.2 KiB
PHP

<?php
namespace app\queue\command\monitor;
use app\gateway\service\RedisService;
use app\merchant\service\MerchantService;
use app\robot\controller\WeChatBot as WeChatBotC;
use think\admin\Command;
use think\Collection;
use think\console\Input;
use think\console\Output;
/**
* 商户余额监控预警
* Class MerchantBalanceMonitor
* @package app\data\command
*/
class MerchantBalanceMonitor extends Command
{
protected $redis = false;
protected function configure()
{
$this->setName('xQueue:MerchantBalanceMonitor')->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();
$merchantService = MerchantService::instance();
$RobotService = new WeChatBotC($this->app);
$merchantsList = $merchantService->db()->where(['merchant_type'=>1,'status'=>1])->whereLike('other_param','%Monitor_Balance_status%')->select()->toArray();
if(!$merchantsList) $this->setQueueSuccess("未找到支持的商户");
$merchantsList_num = count($merchantsList);
$count_sum = 0;
foreach($merchantsList as $merchants) {
$other_param = json_decode($merchants['other_param'],true);
if(!$other_param){
continue;
}
$Monitor_Balance = $other_param['Monitor_Balance'] ?? null;
if(!$Monitor_Balance){
continue;
}
$roomid = $other_param['QYWX_roomid'];
$sender = $other_param['QYWX_sender'];
$count_sum ++;
if($merchants['account_surplus'] <= $Monitor_Balance){
#进入预警流程
#设置redis,根据mid设置redis,
if(!$this->redis->get('Check_merchant_account-'.$merchants['id'])){
$AllContacts = $RobotService->get_all_contacts();
$Contacts_array = json_decode($AllContacts,true);
$acc = $Contacts_array['data']['contacts'];
$ac = '';
foreach ($acc as $k=>$v){
if($v['wxid'] == $sender){
$ac = $v['name'];
}
}
$this->redis->set('Check_merchant_account-'.$merchants['id'],1);
$RobotService->send_text('余额预警: 您的账户现在只剩下'.$merchants['account_surplus'].'元,低于预警'.$Monitor_Balance.'元,请及时充值, @'.$ac,$roomid,$sender);
$this->setQueueProgress("预警 {$merchants['name']} 余额成功", $count_sum / $merchantsList_num * 100);
}
if($merchants['account_surplus'] <= 500){
if(!$this->redis->get('Check_merchant_account-TOP-'.$merchants['id'])){
if(!isset($ac)){
$AllContacts = $RobotService->get_all_contacts();
$Contacts_array = json_decode($AllContacts,true);
$acc = $Contacts_array['data']['contacts'];
$ac = '';
foreach ($acc as $k=>$v){
if($v['wxid'] == $sender){
$ac = $v['name'];
}
}
}
$this->redis->set('Check_merchant_account-TOP-'.$merchants['id'],1);
$RobotService->send_text('余额预警: 您的账户现在只剩下'.$merchants['account_surplus'].'元,已经严重不足,请关注账户, @'.$ac,$roomid,$sender);
$this->setQueueProgress("预警 {$merchants['name']} 余额成功", $count_sum / $merchantsList_num * 100);
}
}
}else{
if($this->redis->get('Check_merchant_account-'.$merchants['id'])){
$AllContacts = $RobotService->get_all_contacts();
$Contacts_array = json_decode($AllContacts,true);
$acc = $Contacts_array['data']['contacts'];
$ac = '';
foreach ($acc as $k=>$v){
if($v['wxid'] == $sender){
$ac = $v['name'];
}
}
$this->redis->del('Check_merchant_account-'.$merchants['id']);
$RobotService->send_text('余额预警: 您的账户已经恢复正常,现在余额为'.$merchants['account_surplus'].'元,请关注账户, @'.$ac,$roomid,$sender);
}
if($this->redis->get('Check_merchant_account-TOP-'.$merchants['id'])){
$this->redis->del('Check_merchant_account-TOP-'.$merchants['id']);
}
}
}
$this->setQueueSuccess("共处理 {$merchantsList_num} 个商家, 完成{$count_sum} 个订单查询新状态更新!");
}
protected function redis()
{
if (!$this->redis) {
$this->redis = RedisService::getInstance();
}
}
}