
- 新增 get_contacts 方法,用于获取指定微信 ID 的联系人名称 - 在 WeChatBot 控制器中集成 RedisService 以使用 Redis 缓存 - 修改 MerchantBalanceMonitor 中获取联系人名称的逻辑,使用新的 get_contacts 方法 - 优化 WeChatBot 中处理消息通知的逻辑,使用新的 get_contacts 方法获取联系人名称
172 lines
6.1 KiB
PHP
172 lines
6.1 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 ++;
|
|
$ac = $RobotService->get_contacts($sender) ;
|
|
if(!$ac){
|
|
$ac = '';
|
|
}
|
|
$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)){
|
|
$ac = $RobotService->get_contacts($sender) ;
|
|
if(!$ac){
|
|
$ac = '';
|
|
}
|
|
}
|
|
|
|
$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 ++;
|
|
$ac = $RobotService->get_contacts($sender) ;
|
|
if(!$ac){
|
|
$ac = '';
|
|
}
|
|
$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);
|
|
$other_params = json_decode($merchantMid['other_param'],true);
|
|
$roomid = $other_params['QYWX_roomid']??null;
|
|
$sender = $other_params['QYWX_sender']??null;
|
|
if($roomid){
|
|
$ac = $RobotService->get_contacts($sender) ;
|
|
if(!$ac){
|
|
$ac = '';
|
|
}
|
|
$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();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|