channelCpcCode.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Modules\Channel\Services\ChannelService;
  4. use App\Modules\Channel\Services\CompanyService;
  5. use Illuminate\Console\Command;
  6. use Redis;
  7. use DB;
  8. class channelCpcCode extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'channel:cpccode {--get}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'reset cpc code';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $options = $this->option('get');
  39. if($options){
  40. $this->fetchtoDB();
  41. }else{
  42. $this->start();
  43. }
  44. }
  45. private function start()
  46. {
  47. $result = CompanyService::getAllCompanyCpcCode();
  48. //$result = DB::table('')
  49. foreach ($result as $item){
  50. $channel_user = DB::table('channel_users')->where('company_id',$item->id)->select('id')->get();
  51. if($channel_user->isNotEmpty()){
  52. foreach ($channel_user as $channel_user_id_info) {
  53. $channels = ChannelService::getByChannelUserId($channel_user_id_info->id);
  54. $code = empty($item->channel)?'zw005':$item->channel;
  55. if($channels->isNotEmpty()){
  56. foreach ($channels as $channel) {
  57. Redis::hset('channel:setting:'.$channel->id,'cpc_channel',$code);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. private function fetchtoDB()
  65. {
  66. DB::select( 'truncate table cpc_channel_temp');
  67. for ($i = 1;$i<7000;$i++){
  68. //Redis::hset('channel:setting:'.$channel->id,'cpc_channel',$code);
  69. $c = \Redis::hget('channel:setting:'.$i,'cpc_channel');
  70. if ($c) {
  71. \DB::table('cpc_channel_temp')->insert([
  72. 'distribution_channel_id'=>$i,
  73. 'channel'=>$c,
  74. 'created_at'=>date('Y-m-d H:i:s')
  75. ]);
  76. }
  77. }
  78. }
  79. }