REAPI/app/queue/command/monitor/MerchantBalanceMonitor.php
mzeros 25439b988e feat(robot): 增加商户余额监控和订单查询功能
- 新增商户余额监控功能,支持异步消息提醒
- 添加订单查询功能,可查询当天和最近一周的订单
- 实现携转检测功能,可查询手机号是否携转及新旧运营商
- 优化机器人自动回复逻辑,支持多种查询命令
2025-01-08 16:05:57 +08:00

194 lines
7.4 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'];
if($merchants['account_surplus'] <= $Monitor_Balance){
#进入预警流程
#设置redis,根据mid设置redis,
if(!$this->redis->get('Check_merchant_account-'.$merchants['id'])){
$count_sum ++;
$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'] <= 5000){
if(!$this->redis->get('Check_merchant_account-TOP-'.$merchants['id'])){
$count_sum ++;
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'])){
$count_sum ++;
$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->delete('Check_merchant_account-'.$merchants['id']);
$RobotService->send_text('余额预警: 您的账户已经恢复正常,现在余额为'.$merchants['account_surplus'].'元,请关注账户, @'.$ac,$roomid,$sender);
}
if($this->redis->get('Check_merchant_account-TOP-'.$merchants['id'])){
$count_sum ++;
$this->redis->delete('Check_merchant_account-TOP-'.$merchants['id']);
}
}
}
$wh = true;
$add_msg_count = 0;
#添加余额消息预警
while($wh) {
try{
$datas = $this->redis->pop('merchant_recharge_msg');
if($datas) {
$wh = true;
$mid = $datas['mid']??null;
if($mid){
$merchantMid = array_filter($merchantsList, function($merchant) use ($mid) {
return $merchant['id'] == $mid;
});
// 获取第一个匹配的元素
$merchantMid = reset($merchantMid);
$ac = '';
$other_params = json_decode($merchantMid['other_param'],true);
$roomid = $other_params['QYWX_roomid']??null;
$sender = $other_params['QYWX_sender']??null;
if($roomid){
if(!isset($acc)){
$AllContacts = $RobotService->get_all_contacts();
$Contacts_array = json_decode($AllContacts,true);
$acc = $Contacts_array['data']['contacts'];
}
foreach ($acc as $k=>$v){
if($v['wxid'] == $sender){
$ac = $v['name'];
}
}
$RobotService->send_text("余额预警: \n 检测到您的账户余额有变动,\n 变动金额为:".$datas['num']."元, \n 变动描述:".$datas['desc']." \n 变动款时间:".$datas['time']." \n 请关注账户 @".$ac,$roomid,$sender);
$add_msg_count++;
}
}
// 根据mid=1的数据定义为变量
}else{
$wh = false;
}
}catch (\Exception $e){
$wh = false;
}
}
$this->setQueueSuccess("共处理 {$merchantsList_num} 个商家, 完成{$count_sum} 个查询处理!和{$add_msg_count} 个加款消息预警!");
}
protected function redis()
{
if (!$this->redis) {
$this->redis = RedisService::getInstance();
}
}
}