<?php

use Illuminate\Support\Facades\Log;

if (!function_exists('rand_number')) {
    //生成随机数字
    function rand_number($length = 6){
        $num = range(1, 9); //1,2,3...9
        $result = array();
        for ($i = 0; $i < $length; $i++) {
            $result[] = $num[array_rand($num)];
        }
        return implode("", $result);
    }
}
/**
 * 创建多级文件夹
 */
 if (!function_exists('createFolder')) {
    function createFolder($path, $folder)
    {
        $fullpath = $path.$folder;
        if (file_exists($fullpath) && is_dir($fullpath)) { //文件或目录是否存在
        } else {
            $res = mkdir($fullpath, 0755, true);
            chmod($fullpath, 0755);  //开放权限
        }
    }
}

//统一输出格式话的json数据
if (!function_exists('out')) {
    function out($data = null, $code = 200, $message = 'success', $e = false)
    {
        $out = ['code' => $code, 'message' => $message, 'data' => $data];

        if ($e !== false) {
            if ($e instanceof Exception) {
                $errMsg = $e->getFile().'文件第'.$e->getLine().'行错误：'.$e->getMessage();
                trace([$message => $errMsg], 'error');
            }
            else {
                trace([$message => $e], 'error');
            }
        }

        return response()->json($out);
    }
}

//日志记录
if (!function_exists('trace')) {
    function trace($log = '', $level = 'info')
    {
        Log::log($level, $log);
    }
}

if (!function_exists('get_order_id')) {
    function get_order_id($user_id,$bus_type=1){
        $userId = substr($user_id, -7);
        $userId = str_pad($userId, 7, "0", STR_PAD_LEFT);
        $u1 = substr($userId, 0, 2);
        $u2 = substr($userId, 2, 2);
        $u3 = substr($userId, 4, 2);
        $u4 = substr($userId, 6, 1);
        unset($userId);
        $d = date("ymd");
        $timestamp = microtime(true) - strtotime(date('Y-m-d'));
        list($t1, $t2) = explode(".", $timestamp);
        unset($timestamp);
        $t1 = str_pad($t1, 5, "0", STR_PAD_LEFT);
        $t2 = intval(substr($t2, 0, 1));
        $b = intval($bus_type);
        unset($busType);
        return "{$b}{$u1}{$d}{$u2}{$t1}{$u3}{$t2}{$u4}";
    }
}
