RefreshRefExtCache.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wanggb
  5. * Date: 2019/1/2
  6. * Time: 14:13
  7. */
  8. namespace app\admin\command;
  9. use app\common\library\Redis;
  10. use think\console\Command;
  11. use think\console\Input;
  12. use think\console\Output;
  13. use think\Request;
  14. class RefreshRefExtCache extends Command
  15. {
  16. protected function configure()
  17. {
  18. $this->setName('RefreshRefExtCache')
  19. ->setDescription('更新客服消息推广链接与ext关联关系');
  20. }
  21. protected function execute(Input $input, Output $output)
  22. {
  23. Request::instance()->module('admin');
  24. $batch = 1000;
  25. $count = model('Short_url')->whereLike('url', '%ext%')->count();
  26. $times = ceil($count / 1000);
  27. $sum = 0;
  28. for ($i = 0; $i < $times; $i++) {
  29. $data = model('Short_url')->whereLike('url', '%ext=%')->limit($i * $batch, $batch)->select();
  30. foreach ($data as $item) {
  31. preg_match('/ext=({.*?})/', $item['url'], $match);
  32. if (!$match) {
  33. continue;
  34. }
  35. $ext = $match[1];
  36. preg_match('/\/t\/(\d+)/', $item['url'], $match_short_referral_id);
  37. preg_match('/referral_id=(\d+)/', $item['url'], $match_referral_id);
  38. $referral_id = 0;
  39. if ($match_short_referral_id) {
  40. $referral_id = $match_short_referral_id[1];
  41. }
  42. if ($match_referral_id) {
  43. $referral_id = $match_referral_id[1];
  44. }
  45. if ($ext) {
  46. $cacheExt = 'RefExt:' . md5($ext);
  47. if ($referral_id) {
  48. $sum += Redis::instance()->set($cacheExt, $referral_id);
  49. }
  50. }
  51. }
  52. }
  53. echo "ok,处理数量" . $sum;
  54. }
  55. }