123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2020/6/29
- * Time: 10:32
- */
- namespace app\admin\command;
- use app\admin\service\LogService;
- use app\common\library\Redis;
- use app\common\model\ReferralDayCollect;
- use app\main\constants\AdConstants;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\console\input\Argument;
- use think\Config;
- use think\Db;
- use think\Log;
- use think\Request;
- class AdUserDiff extends Command
- {
- protected function configure()
- {
- $this->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);
- }
- }
- }*/
- }
|