<?php

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;


if (!function_exists('get_req')) {
    // 查询限制本人
    function get_req($req, $user){ // $this->user()
        if (empty($req['user_id']) && in_array($user->role, [1,3,4])) {
             // 销售 物流 售后 才限制
            $req['user_id'] = $user->id;
        }
        return $req;
    }
}

if (!function_exists('plus_minus_conversion')) {
    //正负数相互转换--支持小数
    function plus_minus_conversion($value){
        return $value > 0 ? -1 * $value : abs($value);
    }
}

if (!function_exists('get_change_num')) {
    // 变动类型
    // 默认对于仓库没有变化 1 增加 -1 减少
    function get_change_num($type, $number = 0) {
        $change_type = 0;
        if(in_array($type, config('map.goods_stock_increase'))){
            $change_type = 1;
        }
        if(in_array($type, config('map.goods_stock_decrease'))){
            $change_type = -1;
        }
        return $change_type * $number;
    }
}

//上传单个文件到阿里云
if (!function_exists('upload_file')) {
    function upload_file($file, $dir = '')
    {
        if (empty($dir)) {
            $dir = date('Y-m-d');
        }
        if (empty($file)) {
            exit_out(null, 10001, '请上传文件');
        }

        $disk = Storage::disk('oss');
        $file_url = $disk->put('yzzb2021/' . $dir, $file);

        return 'https://swdz-xcx.oss-cn-beijing.aliyuncs.com/' . $file_url;
    }
}


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);
    }
}

//AES加密
if (!function_exists('aes_encrypt')) {
    function aes_encrypt($data)
    {
        if (is_array($data)) {
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        }
        $key = config('config.admin_aes_key');
        $iv  = config('config.admin_aes_key');

        $cipher_text = openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
        $cipher_text = base64_encode($cipher_text);

        return urlencode($cipher_text);
    }
}

//AES解密
if (!function_exists('aes_decrypt')) {
    function aes_decrypt($encryptData)
    {
        $encryptData = urldecode($encryptData);
        $encryptData = base64_decode($encryptData);

        $key = config('config.admin_aes_key');
        $iv  = config('config.admin_aes_key');

        $original_plaintext = openssl_decrypt($encryptData, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);

        return json_decode($original_plaintext, true);
    }
}

//下划线转驼峰
if (!function_exists('camelize')) {
    function camelize($uncamelized_words, $separator='_')
    {
        $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words));
        return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator);
    }
}
