userProperty.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. class userProperty extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'userProperty';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Update user property';
  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. $this->start();
  36. }
  37. private function start(){
  38. $date_start = date('Y-m-d',time()-4*86400 );
  39. $date_end = date('Y-m-d',time()-3*86400 );
  40. $SQL = "SELECT id FROM users WHERE created_at >= '%s' and created_at < '%s' and AND EXISTS (
  41. SELECT id FROM orders WHERE orders.uid = users.id AND `status` = 'PAID' LIMIT 1
  42. )";
  43. $result = DB::select(sprintf($SQL,$date_start,$date_end));
  44. foreach ($result as $item){
  45. $property = $this->calculateUserPropertyV2($item->id);
  46. if($property){
  47. $this->update($property);
  48. }
  49. }
  50. }
  51. public function update($data){
  52. DB::table('user_division_cpc_property_v2')->where('openid',$data['openid'])->update([
  53. 'property'=>$data['property'] ,
  54. 'updated_at'=>date('Y-m-d H:i:s')
  55. ]);
  56. }
  57. /**
  58. * Openid一对一注册uid,充值超过30元直接成为高净值用户,否则注册3天后做净值分档;
  59. * Openid一对多注册uid,注册3天以上成为有效uid,有效uid充值总额比uid个数;
  60. * 净值区间无论一对一注册uid还是一对多注册uid:
  61. * 低净值:0<充值<10,openid数占比35.51%,充值占比6.03%;
  62. * 中净值:10=<充值<=30,openid数占比32.89%,充值占比23.81%;
  63. * 高净值:充值>30,openid数占比31.60%,充值占比70.16%;
  64. * @param $uid
  65. */
  66. private function calculateUserPropertyV2($uid)
  67. {
  68. $sql = "SELECT users.id as uid,users.openid,users.created_at as register,(select SUM(price) from orders where orders.uid = users.id and `status` = 'PAID') as price FROM users
  69. WHERE openid in (SELECT openid FROM users WHERE id = $uid)";
  70. $result = DB::select($sql);
  71. if (!$result) return [];
  72. if(count($result) == 1 ){
  73. if($result[0]->price && $result[0]->price>30){
  74. return ['openid'=>$result[0]->openid,'property'=>'high'];
  75. }
  76. if(time()-strtotime($result[0]->register) < 3*86400){
  77. return ['openid'=>$result[0]->openid,'property'=>'undefined'];
  78. }
  79. }
  80. $valid_user_num = 0;
  81. $amount = 0;
  82. $openid = '';
  83. $all_amount = 0;
  84. foreach ($result as $item){
  85. $openid = $item->openid;
  86. if($item->price) $all_amount += $item->price;
  87. if(time()-strtotime($item->register) < 3*86400) continue;
  88. if($item->price) $amount += $item->price;
  89. $valid_user_num += 1;
  90. }
  91. if(!$valid_user_num){
  92. if($all_amount)return ['openid'=>$openid,'property'=>'undefined'];
  93. return [];
  94. }
  95. $average_amount = $amount/$valid_user_num;
  96. if($average_amount >30){
  97. return ['openid'=>$openid,'property'=>'high'];
  98. }elseif ($average_amount >=10){
  99. return ['openid'=>$openid,'property'=>'medium'];
  100. }else{
  101. return ['openid'=>$openid,'property'=>'low'];
  102. }
  103. }
  104. }