setName('AdUserDiff') ->setDescription('筛选出当天没有点击福利广告的用户'); } protected function execute(Input $input, Output $output) { Request::instance()->module('admin'); LogService::info('开始执行广告用户筛选:'.date('Y-m-d H:i:s', time())); // 1. 获取可用的福利广告 $adPlans = model('AdManage') ->join('ad_user_group', 'ad_manage.user_group_id = ad_user_group.id', 'left') ->where('ad_type', '=', '2') ->where('show_endtime', '>=', time()) ->where('state', '=', '1') ->where("!ISNULL(user_group_id)") ->where('ad_user_group.group_type', '<>', '0') ->field('ad_manage.*, ad_user_group.group_name, ad_user_group.group_type') ->order('ad_manage.weight', 'desc') ->order('ad_manage.createtime', 'desc') ->select(); if ($adPlans) { // 计算过期时间 24点过期 $tomorrow_time = strtotime(date('Y-m-d 00:00:00', strtotime("+1 day"))); $ttl = $tomorrow_time - time(); $channels_index = []; // 2. 遍历所有的福利广告, 通过广告ID, 获取有哪些渠道$all_channels => AD:JU:9:index foreach ($adPlans as $k => $plan) { $plan_id = $plan->id; $plan_key = AdConstants::AD_JU_KEY . $plan_id . ':index'; $channels_arr = explode(',', Redis::instance()->get($plan_key)); $channels_index = array_merge($channels_index, $channels_arr); if($channels_arr){ foreach ($channels_arr as $channel_key => $channel_id){ // 3. 遍历 $all_channels 获取渠道中所有用户 $uid , 将所有 $uid 按渠道存入一个大集合中。 $channel_key = AdConstants::AD_JU_KEY . $plan_id . ':' . $channel_id; try{ $channer_user_redis_key = AdConstants::AD_JU_USER . ':' . $channel_id; Redis::instance()->sUnionStore($channer_user_redis_key, $channel_key); }catch (\Exception $exception){ $output->write($exception->getMessage()); } // 4. 获取 Redis 中所有点击过广告的用户 $channel_welfare_key = AdConstants::AD_WELFARE . $channel_id; // 5. 将两个大集合做差集比较,将差集存入数据库, JOB通过查询数据库,发送消息 //$diff_data = Redis::instance()->sDiff($channer_user_redis_key, $channel_welfare_key); Redis::instance()->sDiffStore(AdConstants::AD_WELFARE_PUSH.$channel_id, $channer_user_redis_key, $channel_welfare_key); Redis::instance()->expire(AdConstants::AD_WELFARE_PUSH.$channel_id, $ttl); } } } Redis::instance()->set(AdConstants::AD_WELFARE_PUSH_INDEX, implode(',', $channels_index)); Redis::instance()->expire(AdConstants::AD_WELFARE_PUSH_INDEX, $ttl); LogService::info('广告用户筛选完成:' . date('Y-m-d H:i:s', time())); }else{ LogService::info('广告用户筛选完成,没有匹配的广告计划:' . date('Y-m-d H:i:s', time())); } } /** * 造点测试数据 */ /*private function _fakeRedisData() { $plan_id = 5; $channel_arr = [1734, 1735]; $user_list[1734] = [1, 32930, 1222, 94093]; $user_list[1735] = [2238, 9489584, 77377]; $redis_index_key = AdConstants::AD_JU_KEY . $plan_id . ':index'; Redis::instance()->set($redis_index_key, implode(',', $channel_arr)); foreach ($channel_arr as $channel_id) { foreach ($user_list[$channel_id] as $user_id) { $redis_channel_key = AdConstants::AD_JU_KEY . $plan_id . ':' . $channel_id; Redis::instance()->sAdd($redis_channel_key, $user_id); } } }*/ }