123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- class userProperty extends Command
- {
-
- protected $signature = 'userProperty';
-
- protected $description = 'Update user property';
-
- public function __construct()
- {
- parent::__construct();
- }
-
- 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')
- ]);
- }
-
- 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'];
- }
- }
- }
|