selectCrmSites.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use Redis;
  6. class selectCrmSites extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'selectCrmSites';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $result = $this->selectSites();
  37. $data = [];
  38. $times = DB::table('crm_other_channels')->max('times');
  39. $times = $times?$times:0;
  40. $i = 1;
  41. foreach ($result as $item){
  42. $data[] = [
  43. 'distribution_channel_id'=>$item,
  44. 'times'=>$times+1,
  45. 'created_at'=>date('Y-m-d H:i:s'),
  46. 'updated_at'=>date('Y-m-d H:i:s')
  47. ];
  48. if($i% 100 == 0){
  49. DB::table('crm_other_channels')->insert($data);
  50. $data = [];
  51. }
  52. $i++;
  53. }
  54. if($data){
  55. DB::table('crm_other_channels')->insert($data);
  56. }
  57. Redis::del('crm:out_channel_sites');
  58. Redis::sadd('crm:out_channel_sites',$result);
  59. }
  60. public function selectSites(){
  61. $distribution_channel_id = $this->getSiteNoRegister();
  62. $setServiceSites = $this->getSiteSetServices();
  63. $opTenCompanyInfo = $this->getTopTenCompanySite();
  64. $innerSites = $this->getInnerSite();
  65. $result = [];
  66. foreach ($distribution_channel_id as $item){
  67. if(Redis::Hexists('specialChannelAuthInfoV2',$item)) continue;
  68. //去除设置了二维码的站点
  69. if(in_array($item,$setServiceSites)) continue;
  70. //去除内部站点
  71. if(in_array($item,$innerSites)) continue;
  72. //1个月的流水排名前十公司排除
  73. if(in_array($item,$opTenCompanyInfo)) continue;
  74. $result[] = $item;
  75. }
  76. return $result;
  77. }
  78. public function getTopTenCompanySite(){
  79. $data = $this->getTopTenCompanyInfo();
  80. $res = [];
  81. foreach ($data as $v){
  82. $res = array_merge($res,$v['distribution_channel_id']);
  83. }
  84. return $res;
  85. }
  86. //最近1个月的流水排名前十公司
  87. public function getTopTenCompanyInfo(){
  88. //order_day_stats
  89. $sql = 'SELECT company_id,GROUP_CONCAT( DISTINCT a.distribution_channel_id) as distribution_channel_id,GROUP_CONCAT(DISTINCT b.channel_user_id) as channel_user_ids ,SUM(a.total_recharge_amount) as total_recharge_amount FROM order_day_stats a JOIN
  90. distribution_channels b on a.distribution_channel_id = b.id
  91. JOIN channel_users c on b.channel_user_id = c.id
  92. where a.date >= date(DATE_ADD(NOW(),INTERVAL -1 MONTH))
  93. and c.company_id not in (1,2,3,18,760)
  94. GROUP BY c.company_id
  95. ORDER BY total_recharge_amount desc
  96. LIMIT 10';
  97. $result = DB::select($sql);
  98. $data = [];
  99. foreach ($result as $item){
  100. $data[] = [
  101. 'company_id'=>$item->company_id,
  102. 'distribution_channel_id'=>explode(',',$item->distribution_channel_id),
  103. 'channel_user_ids'=>explode(',',$item->channel_user_ids),
  104. 'total_recharge_amount'=>$item->total_recharge_amount
  105. ];
  106. }
  107. $res = [];
  108. foreach ($data as $v){
  109. $res = array_merge($res,$v['distribution_channel_id']);
  110. }
  111. return $data;
  112. }
  113. //内部账号
  114. public function getInnerSite():array{
  115. $sql = 'SELECT distribution_channels.id FROM distribution_channels JOIN channel_users on distribution_channels.channel_user_id = channel_users.id
  116. where channel_users.company_id in (1,2,3,18,760)';
  117. $result = DB::select($sql);
  118. $data = [];
  119. foreach ($result as $item){
  120. $data[] = $item->id;
  121. }
  122. return $data;
  123. }
  124. //最近两周没有新增注册用户的其他渠道站点(小于100)
  125. public function getSiteNoRegister():array{
  126. $sql = 'SELECT distribution_channel_id,COUNT(users.id) FROM users
  127. JOIN distribution_channels on users.distribution_channel_id = distribution_channels.id
  128. WHERE users.created_at >= date(DATE_ADD(NOW(),INTERVAL -2 WEEK))
  129. and distribution_channels.created_at <= date(DATE_ADD(NOW(),INTERVAL -1 MONTH))
  130. and distribution_channel_id >0
  131. GROUP BY distribution_channel_id
  132. HAVING COUNT(users.id) <100';
  133. $result = DB::select($sql);
  134. $data = [];
  135. foreach ($result as $item){
  136. $data[] = $item->distribution_channel_id;
  137. }
  138. return $data;
  139. }
  140. //站点所用客服二维码为平台官方
  141. public function getSiteSetServices():array{
  142. $sql = 'SELECT distribution_channel_id FROM distribution_channel_settings';
  143. $result = DB::select($sql);
  144. $data = [];
  145. foreach ($result as $item){
  146. $data[] = $item->distribution_channel_id;
  147. }
  148. return $data;
  149. }
  150. }