selectCrmSites.php 4.9 KB

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