<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2019/3/22
 * Time: 10:53
 */

namespace App\Modules\User\Services;

use App\Modules\User\Models\UserDivisionProperty;
use DB;

class UserDivisionPropertyService
{

    const PROPERTY='property';

    public static function getUserProperty($uid){
        if(($result = self::getUserPropertyFromRedis($uid)))  return $result;

        if(($result = self::getUserPropertyFromTable($uid))) return $result;

        return self::getUserPropertyFromStats($uid);
    }

    public static function getUserPropertyFromRedis($uid){
        return ReadRecordService::getByField($uid,self::PROPERTY);
    }
    public static function getUserPropertyFromTable($uid){
        $result = UserDivisionProperty::where('uid',$uid)->where('is_enable',1)->select('property','created_at')->first();
        if($result){
            return $result->property;
        }
        return '';
    }

    public static function getUserPropertyFromStats($uid){
        $sql = "SELECT f.uid,f.subscribe_time,f.appid,(o.price) FROM force_subscribe_users f  LEFT JOIN orders o on f.uid = o.uid and  o.`status` = 'PAID' 
and o.created_at>=f.subscribe_time and o.created_at <= DATE_ADD(f.subscribe_time,INTERVAL 3 day) where f.is_subscribed = 1 and f.uid = %s ";
        $res = DB::select(sprintf($sql,$uid));
        if(!$res){
            return '';
        }
        $data = [];
        foreach ($res as $value){
            $data[] = ['subscribe_time'=>$value->subscribe_time,'appid'=>$value->appid,'price'=>$value->price];
        }
        if(!$data) return '';
        $collection = collect($data);
        $sort = $collection->groupBy('appid')->sortByDesc(function($item, $key){
            return $item[0]['subscribe_time'];
        });

        foreach ($sort as $key=>$v){
            if(isset($v[0]) && !empty($v[0])){
                $charge_amount = $v[0]['price']?$v[0]['price']:0;
                if($charge_amount >=50){
                    self::createUser($uid,'high');
                    ReadRecordService::setByField($uid,self::PROPERTY,'high');
                    return 'high';
                }
                if(time()-strtotime($v[0]['subscribe_time']) >= 3*86400){
                    //强关时间大于3天
                    if($charge_amount >=50){
                        self::createUser($uid,'high');
                        ReadRecordService::setByField($uid,self::PROPERTY,'high');
                        return 'high';
                    }elseif($charge_amount>2){
                        self::createUser($uid,'medium');
                        ReadRecordService::setByField($uid,self::PROPERTY,'medium');
                        return 'medium';
                    }elseif ($charge_amount==2){
                        self::createUser($uid,'low');
                        ReadRecordService::setByField($uid,self::PROPERTY,'low');
                        return 'low';
                    }else{
                        self::createUser($uid,'none');
                        ReadRecordService::setByField($uid,self::PROPERTY,'none');
                        return 'none';
                    }
                }
            }
        }
        return '';
    }

    public static function createUser($uid,$property){
        UserDivisionProperty::where('uid',$uid)->update(['is_enable'=>0]);
        UserDivisionProperty::create([
            'uid'=>$uid,
            'property'=>$property,
            'is_enable'=>1
        ]);
    }

    public static function getUserBindPhoneStatus($uid,$page){
        $value_format = 'sign_readrecord_%s_%s';
        if( !($property = self::getUserProperty($uid)) ){
            return '';
        }
        $result = [
            'sign'=>0,
            'readrecord'=>0,
            'is_bind'=>0,
            'bind_day'=>0
        ];
        $info = ReadRecordService::getByField($uid,'bind_phone_status');
        if($info){
            $info = json_decode($info,1);
        }else{
            $page == 'sign' && $result['sign'] = 1;
            $page == 'readrecord' && $result['readrecord'] = 1;
            ReadRecordService::setByField($uid,'bind_phone_status',json_encode($result));
        }

        return $result;
    }
}