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'] <= 500){ 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']); } } } $this->setQueueSuccess("共处理 {$merchantsList_num} 个商家, 完成{$count_sum} 个查询处理!"); } protected function redis() { if (!$this->redis) { $this->redis = RedisService::getInstance(); } } }