123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- class userProperty extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'userProperty';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Update user property';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->start();
- }
- private function start(){
- $date_start = date('Y-m-d',time()-4*86400 );
- $date_end = date('Y-m-d',time()-3*86400 );
- $SQL = "SELECT id FROM users WHERE created_at >= '%s' and created_at < '%s' AND EXISTS (
- SELECT id FROM orders WHERE orders.uid = users.id AND `status` = 'PAID' LIMIT 1
- )";
- $result = DB::select(sprintf($SQL,$date_start,$date_end));
- foreach ($result as $item){
- $property = $this->calculateUserPropertyV2($item->id);
- if($property){
- $this->update($property);
- }
- }
- }
- public function update($data){
- DB::table('user_division_cpc_property_v2')->where('openid',$data['openid'])->update([
- 'property'=>$data['property'] ,
- 'updated_at'=>date('Y-m-d H:i:s')
- ]);
- }
- /**
- * Openid一对一注册uid,充值超过30元直接成为高净值用户,否则注册3天后做净值分档;
- * Openid一对多注册uid,注册3天以上成为有效uid,有效uid充值总额比uid个数;
- * 净值区间无论一对一注册uid还是一对多注册uid:
- * 低净值:0<充值<10,openid数占比35.51%,充值占比6.03%;
- * 中净值:10=<充值<=30,openid数占比32.89%,充值占比23.81%;
- * 高净值:充值>30,openid数占比31.60%,充值占比70.16%;
- * @param $uid
- */
- private function calculateUserPropertyV2($uid)
- {
- $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
- WHERE openid in (SELECT openid FROM users WHERE id = $uid)";
- $result = DB::select($sql);
- if (!$result) return [];
- if(count($result) == 1 ){
- if($result[0]->price && $result[0]->price>30){
- return ['openid'=>$result[0]->openid,'property'=>'high'];
- }
- if(time()-strtotime($result[0]->register) < 3*86400){
- return ['openid'=>$result[0]->openid,'property'=>'undefined'];
- }
- }
- $valid_user_num = 0;
- $amount = 0;
- $openid = '';
- $all_amount = 0;
- foreach ($result as $item){
- $openid = $item->openid;
- if($item->price) $all_amount += $item->price;
- if(time()-strtotime($item->register) < 3*86400) continue;
- if($item->price) $amount += $item->price;
- $valid_user_num += 1;
- }
- if(!$valid_user_num){
- if($all_amount)return ['openid'=>$openid,'property'=>'undefined'];
- return [];
- }
- $average_amount = $amount/$valid_user_num;
- if($average_amount >30){
- return ['openid'=>$openid,'property'=>'high'];
- }elseif ($average_amount >=10){
- return ['openid'=>$openid,'property'=>'medium'];
- }else{
- return ['openid'=>$openid,'property'=>'low'];
- }
- }
- }
|