feat(queue): 添加商户余额监控预警功能

- 新增 MerchantBalanceMonitor 类实现商户余额监控
- 添加 Redis 连接方法以支持缓存预警状态
- 实现余额预警逻辑,包括正常预警和严重预警
- 新增预警解除逻辑,当余额恢复正常时发送通知- 在测试控制器中添加余额检查方法,用于手动触发预警
This commit is contained in:
mzeros 2025-01-06 00:17:09 +08:00
parent 81feb225eb
commit b45d188232
4 changed files with 177 additions and 108 deletions

View File

@ -3,11 +3,21 @@
namespace app\openapi\controller;
use app\channel\service\ChannelService;
use app\gateway\service\RedisService;
use think\facade\Db;
class Port extends Core
{
protected $check = false;
# redis连接
protected $redis = false;
protected function redis()
{
if (!$this->redis) {
$this->redis = RedisService::getInstance();
}
}
#检测异常接口预警
public function check_notify()
@ -74,6 +84,27 @@ class Port extends Core
$this->yes('ok','暂时正常');
}
#检测商户余额并同志
public function check_merchant_account()
{
$mid = input('mid');
$jk_account = input('jk_account');
$merchant = DB::name('merchant_list')->field("id,name,account_surplus,account_baitiao")->where('status',1)->where('id',$mid)->find();
if(!$merchant){
$this->yes('ok','无此商户');
}
if($merchant['account_surplus'] <= $jk_account){
#进入预警流程
#设置redis,根据mid设置redis,
if(!$this->redis->get('Cherck_merchnat_account-'.$mid)){
$this->redis->set('Cherck_merchnat_account-'.$mid,1);
$this->no(0 ,$merchant['name'].'余额不足,只剩下'.$merchant['account_surplus'].'请及时充值');
}
}
}

View File

@ -37,6 +37,7 @@ use app\order\service\AfterSalesOrderService;
use app\order\service\Kami91OrderService;
use app\order\service\OrderService as OrderBase;
use app\robot\controller\api\Qqbot;
use app\robot\controller\WeChatBot as WeChatBotC;
use app\setting\service\BlackCardService;
use app\setting\service\StatService;
use dever\Log;
@ -1055,78 +1056,86 @@ class Test extends Core
public function execute()
{
ini_set('memory_limit', '1024M');
$channelService = ChannelService::instance();
$channelList = $channelService->db()->where(['cancel_status'=>1,'status'=>1])->select()->toArray();
$this->redis();
$merchantService = MerchantService::instance();
$merchantList = $merchantService->db()->where(['cancel_support'=>1,'status'=>1])->select()->toArray();
$merchantList_num = count($merchantList);
$RobotService = new WeChatBotC($this->app);
$merchantsList = $merchantService->db()->where(['merchant_type'=>1,'status'=>1])->whereLike('other_param','%Monitor_Balance_status%')->select()->toArray();
$merchantsList_num = count($merchantsList);
$count_sum = 0;
$total_sum=0;
// $channelList_num = count($channelList);
// $count_sum = 0;
// $total_sum=0;
$class = OrderService::instance();
foreach ($merchantList as $merchant) {
$mid = $merchant['id'];
$other_param = $merchant['other_param'];
$array = json_decode($other_param,true);
$timeout_cancel = 3600;
if(isset($array['timeout_cancel'])){
$timeout_cancel = $array['timeout_cancel'];
foreach($merchantsList as $merchants) {
$other_param = json_decode($merchants['other_param'],true);
if(!$other_param){
continue;
}
// if(isset($array['timeout_cancel_form'])){
// $timeout_cancel = $array['timeout_cancel_form'];
// }
$Monitor_Balance = $other_param['Monitor_Balance'] ?? null;
if(!$Monitor_Balance){
continue;
}
$roomid = $other_param['QYWX_roomid'];
$sender = $other_param['QYWX_sender'];
foreach($channelList as $channel) {
if(isset($array['timeout_cancel_form'])){
$timeout_cancel = $array['timeout_cancel_form'];
$where = [['status','=',4],['mid','=',$mid],['cid','=',$channel['id']],['create_at','>=',date('Y-m-d H:i:s',time()-$timeout_cancel)]];
$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('余额预警: 您的账户现在只剩下'.$Monitor_Balance.'元,请及时充值, @'.$ac,$roomid,$sender);
}
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('余额预警: 您的账户现在只剩下'.$Monitor_Balance.'元,已经严重不足,请关注账户, @'.$ac,$roomid,$sender);
}
}
}else{
$where = [['status','=',4],['mid','=',$mid],['cid','=',$channel['id']],['expire_time','<=',date('Y-m-d H:i:s',time()+$timeout_cancel)]];
}
list($count, $total) = [0, $class->db()->where($where)->order('id asc')->count()];
$total_sum += $total;
// $data = $class->db()->where($where)->limit(100)->select()->toArray();
$class->db()->where($where)->order('id asc')->chunk(100, function (Collection $data) use (&$count, $total,$channel,$channelService,&$count_sum,$class) {
$array = $data->toArray();
if($array){
foreach ($array as $k => $vo) {
$class->db()->where(['order_id'=>$vo['order_id']] )->update(['apply_refund'=>1]);
if(isset($vo['num']) && $vo['num']>0) {
$vo['order_id'] = $vo['order_id'].'_'.$vo['num'];
}
var_dump($vo);die;
$result = $channelService->call('cancel', $vo['cid'], $vo);
if ($result) {
$count_sum++;
$count++;
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']);
}
}
}

View File

@ -3,11 +3,9 @@
namespace app\queue\command\monitor;
use app\channel\service\ChannelService;
use app\gateway\controller\api\Notify;
use app\gateway\service\RedisService;
use app\merchant\service\MerchantService;
use app\merchant\service\OrderService;
use app\robot\controller\WeChatBot as WeChatBotC;
use think\admin\Command;
use think\Collection;
use think\console\Input;
@ -20,9 +18,10 @@ use think\console\Output;
*/
class MerchantBalanceMonitor extends Command
{
protected $redis = false;
protected function configure()
{
$this->setName('xQueue:MerchantBalanceMonitor')->setDescription('[ 监控系统 ] 商户余额监控预警');
}
@ -34,80 +33,108 @@ class MerchantBalanceMonitor extends Command
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])->select()->toArray();
// if(!$channelList) $this->setQueueSuccess("未找到支持查询的渠道");
$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;
$total_sum=0;
$redisStatus = sysconf('settingRedisQueryOrder');
foreach($merchantsList as $merchants) {
$class = OrderService::instance();
$where['cid'] = $channel['id'];
$where['status'] = 4;
if($redisStatus == 1){
#多线程此处只传入队列
$orderList = $class->db()->field('cid,order_id')->where($where)->order('id asc')->select()->toArray();
foreach ($orderList as $vo) {
$this->queue('query', $vo);
$other_param = json_decode($merchants['other_param'],true);
if(!$other_param){
continue;
}
// $this->queue('query', $orderList);
$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{
list($count, $total) = [0, $class->db()->where($where)->order('id asc')->count()];
$total_sum += $total;
// $data = $class->db()->where($where)->limit(100)->select()->toArray();
$class->db()->where($where)->order('id asc')->chunk(1000, function (Collection $data) use (&$count, $total,$channel,$channelService,&$count_sum) {
$array = $data->toArray();
foreach ($array as $k => $vo) {
if(isset($vo['num']) && $vo['num']>0) {
$vo['order_id'] = $vo['order_id'].'_'.$vo['num'];
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']);
$result = $channelService->call('query', $vo['cid'], $vo);
if ($result['status'] != 4) {
$count_sum++;
$count++;
$notifyClass = new Notify($this->app);
$notifyClass->queue_query($vo['order_id'], $result);
$this->setQueueProgress("查询 {$vo['order_id']} 新的状态进行主动更新", $count / $total * 100);
}
}
}
});
}
$this->setQueueSuccess("共处理 {$merchantsList_num} 个商家, 完成{$count_sum} 个订单查询新状态更新!");
}
$this->setQueueSuccess("共处理 {$channelList_num} 个渠道,共有 {$total_sum} 个订单处理查询中, 完成{$count_sum} 个订单查询新状态更新!");
}
# 队列
protected function queue($key, $value = false)
protected function redis()
{
$redis = RedisService::getInstance();
if (!$value) {
return $redis->pop($key);
} else {
return $redis->push($key, $value);
if (!$this->redis) {
$this->redis = RedisService::getInstance();
}
}
}

View File

@ -32,6 +32,8 @@ if ($app->request->isCli()) {
$console->addCommand(\app\queue\command\taobao\TaobaoAfterSalesOrderUpStatus::class);
$console->addCommand(\app\queue\command\taobao\TaobaoBlacklistRules::class);
$console->addCommand(\app\queue\command\taobao\TaobaoFailOrderSecondNotify::class);
#监控
$console->addCommand(\app\queue\command\monitor\MerchantBalanceMonitor::class);
});