<?php

use Illuminate\Support\Arr;
use \Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;


if (! function_exists('user_admin_config')) {
    function user_admin_config($key = null, $value = null)
    {
        $session = session();

        if (! $config = $session->get('admin.config')) {
            $config = config('admin');

            $config['lang'] = config('app.locale');
        }

        if (is_array($key)) {
            // 保存
            foreach ($key as $k => $v) {
                Arr::set($config, $k, $v);
            }

            $session->put('admin.config', $config);

            return;
        }

        if ($key === null) {
            return $config;
        }

        return Arr::get($config, $key, $value);
    }
}

//生成随机码
function create_invite_code() {
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $rand = $code[rand(0,25)]
        .strtoupper(dechex(date('m')))
        .date('d')
        .substr(time(),-5)
        .substr(microtime(),2,5)
        .sprintf('%02d',rand(0,99));
    for(
        $a = md5( $rand, true ),
        $s = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        $d = '',
        $f = 0;
        $f < 6;
        $g = ord( $a[ $f ] ),
        $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
        $f++
    );
    return $d;
}

function create_order_number()
{
    return date('YmdHis') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
}

/**
 * curl 请求
 * @param $url
 * @param null $header
 * @param null $data
 * @return mixed
 */
function curlRequest($url, $header = null, $data = null)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    if ($data) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    if ($header) {
        curl_setopt($ch, CURLOPT_HEADER, $header);
    }
    $ret = curl_exec($ch);
    curl_close($ch);

    return $ret;
}

/**
 * 时间格式化（时间戳）
 * @param $ptime
 * @return false|string
 */
function uc_time_ago($ptime)
{
    date_default_timezone_set('PRC');
    $etime = time() - $ptime;
    switch ($etime) {
        case $etime <= 60:
            $msg = '刚刚';
            break;
        case $etime > 60 && $etime <= 60 * 60:
            $msg = floor($etime / 60) . '分钟前';
            break;
        case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
            $msg = date('Ymd', $ptime) == date('Ymd', time()) ? '今天 ' . date('H:i', $ptime) : '昨天 ' . date('H:i', $ptime);
            break;
        case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
            $msg = date('Ymd', $ptime) + 1 == date('Ymd', time()) ? '昨天 ' . date('H:i', $ptime) : '前天 ' . date('H:i', $ptime);
            break;
        case $etime > 2 * 24 * 60 * 60 && $etime <= 12 * 30 * 24 * 60 * 60:
            $msg = date('Y', $ptime) == date('Y', time()) ? date('m-d H:i', $ptime) : date('Y-m-d H:i', $ptime);
            break;
        default:
            $msg = date('Y-m-d H:i', $ptime);
    }
    return $msg;
}

/**
 * 根据生日计算年龄
 * @param $birthday  1999-02-01
 * @return int|string   21
 */
function birthday($birthday){
    list($year,$month,$day) = explode("-",$birthday);
    $year_diff = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff  = date("d") - $day;
    if ($day_diff < 0 || $month_diff < 0)
        $year_diff--;
    return $year_diff;
}

/**
 *  谁看了我
 * @param $user_id  //操作人id
 * @param $look_id  //被查看人的id
 * @return bool
 */
function look_log($user_id,$look_id){
    $last_look = DB::table('users_look')->where(['user_id'=>$user_id,'look_id'=>$look_id])->first();
    if($last_look){
        //如果看过 刷新看人的时间
        DB::table('users_look')->where(['user_id'=>$user_id,'look_id'=>$look_id])->update(['atime'=>date("Y-m-d H:i:s")]);
        return true;
    }
    $ins['user_id'] = $user_id;
    $ins['look_id'] = $look_id;
    $ins['atime'] = date("Y-m-d H:i:s");
    DB::table('users_look')->insert($ins);
    DB::table('users')->where('id','=',$look_id)->increment('look_num',1);
    return true;
}

/**
 * 根据经纬度算距离，返回结果单位是公里，先纬度，后经度
 * @param $lat1
 * @param $lng1
 * @param $lat2
 * @param $lng2
 * @return float|int
 */
function GetDistance($lat1, $lng1, $lat2, $lng2)
{
    $EARTH_RADIUS = 6378.137;
    $radLat1 = rad($lat1);
    $radLat2 = rad($lat2);
    $a = $radLat1 - $radLat2;
    $b = rad($lng1) - rad($lng2);
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $s = $s * $EARTH_RADIUS;
    $s = round($s * 10000) / 10000;
    return $s;
}

function rad($d)
{
    return floatval($d) * M_PI / 180.0;
}

//获取vip配置内容
function get_vip_config($key){
   return \App\Models\VipConfig::query()->where('id',2)->value($key);
}


function del_file($filename){
    $oss_config = config("filesystems.disks.oss");
    $head = "https://".$oss_config['bucket'].'.'.$oss_config['endpoint'].'/';
    $disk = Storage::disk('oss');
    $url = str_replace($head,"",$filename);
    if($disk->exists($url)){
        $disk->delete($url);
    }
}
