AdUserDiff.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2020/6/29
  6. * Time: 10:32
  7. */
  8. namespace app\admin\command;
  9. use app\admin\service\LogService;
  10. use app\common\library\Redis;
  11. use app\common\model\ReferralDayCollect;
  12. use app\main\constants\AdConstants;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\Output;
  16. use think\console\input\Argument;
  17. use think\Config;
  18. use think\Db;
  19. use think\Log;
  20. use think\Request;
  21. class AdUserDiff extends Command
  22. {
  23. protected function configure()
  24. {
  25. $this->setName('AdUserDiff')
  26. ->setDescription('筛选出当天没有点击福利广告的用户');
  27. }
  28. protected function execute(Input $input, Output $output)
  29. {
  30. Request::instance()->module('admin');
  31. LogService::info('开始执行广告用户筛选:'.date('Y-m-d H:i:s', time()));
  32. // 1. 获取可用的福利广告
  33. $adPlans = model('AdManage')
  34. ->join('ad_user_group', 'ad_manage.user_group_id = ad_user_group.id', 'left')
  35. ->where('ad_type', '=', '2')
  36. ->where('show_endtime', '>=', time())
  37. ->where('state', '=', '1')
  38. ->where("!ISNULL(user_group_id)")
  39. ->where('ad_user_group.group_type', '<>', '0')
  40. ->field('ad_manage.*, ad_user_group.group_name, ad_user_group.group_type')
  41. ->order('ad_manage.weight', 'desc')
  42. ->order('ad_manage.createtime', 'desc')
  43. ->select();
  44. if ($adPlans) {
  45. // 计算过期时间 24点过期
  46. $tomorrow_time = strtotime(date('Y-m-d 00:00:00', strtotime("+1 day")));
  47. $ttl = $tomorrow_time - time();
  48. $channels_index = [];
  49. // 2. 遍历所有的福利广告, 通过广告ID, 获取有哪些渠道$all_channels => AD:JU:9:index
  50. foreach ($adPlans as $k => $plan) {
  51. $plan_id = $plan->id;
  52. $plan_key = AdConstants::AD_JU_KEY . $plan_id . ':index';
  53. $channels_arr = explode(',', Redis::instance()->get($plan_key));
  54. $channels_index = array_merge($channels_index, $channels_arr);
  55. if($channels_arr){
  56. foreach ($channels_arr as $channel_key => $channel_id){
  57. // 3. 遍历 $all_channels 获取渠道中所有用户 $uid , 将所有 $uid 按渠道存入一个大集合中。
  58. $channel_key = AdConstants::AD_JU_KEY . $plan_id . ':' . $channel_id;
  59. try{
  60. $channer_user_redis_key = AdConstants::AD_JU_USER . ':' . $channel_id;
  61. Redis::instance()->sUnionStore($channer_user_redis_key, $channel_key);
  62. }catch (\Exception $exception){
  63. $output->write($exception->getMessage());
  64. }
  65. // 4. 获取 Redis 中所有点击过广告的用户
  66. $channel_welfare_key = AdConstants::AD_WELFARE . $channel_id;
  67. // 5. 将两个大集合做差集比较,将差集存入数据库, JOB通过查询数据库,发送消息
  68. //$diff_data = Redis::instance()->sDiff($channer_user_redis_key, $channel_welfare_key);
  69. Redis::instance()->sDiffStore(AdConstants::AD_WELFARE_PUSH.$channel_id, $channer_user_redis_key, $channel_welfare_key);
  70. Redis::instance()->expire(AdConstants::AD_WELFARE_PUSH.$channel_id, $ttl);
  71. }
  72. }
  73. }
  74. Redis::instance()->set(AdConstants::AD_WELFARE_PUSH_INDEX, implode(',', $channels_index));
  75. Redis::instance()->expire(AdConstants::AD_WELFARE_PUSH_INDEX, $ttl);
  76. LogService::info('广告用户筛选完成:' . date('Y-m-d H:i:s', time()));
  77. }else{
  78. LogService::info('广告用户筛选完成,没有匹配的广告计划:' . date('Y-m-d H:i:s', time()));
  79. }
  80. }
  81. /**
  82. * 造点测试数据
  83. */
  84. /*private function _fakeRedisData()
  85. {
  86. $plan_id = 5;
  87. $channel_arr = [1734, 1735];
  88. $user_list[1734] = [1, 32930, 1222, 94093];
  89. $user_list[1735] = [2238, 9489584, 77377];
  90. $redis_index_key = AdConstants::AD_JU_KEY . $plan_id . ':index';
  91. Redis::instance()->set($redis_index_key, implode(',', $channel_arr));
  92. foreach ($channel_arr as $channel_id) {
  93. foreach ($user_list[$channel_id] as $user_id)
  94. {
  95. $redis_channel_key = AdConstants::AD_JU_KEY . $plan_id . ':' . $channel_id;
  96. Redis::instance()->sAdd($redis_channel_key, $user_id);
  97. }
  98. }
  99. }*/
  100. }