<?php

use EasyWeChat\MiniApp\Application as MiniApplication;
use EasyWeChat\Pay\Application as PayApplication;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Route;

if (!function_exists('jsonResponse')) {
    /**
     * json返回
     * @param int $status_code
     * @param string $msg
     * @param array $data
     * @return JsonResponse
     */
    function jsonResponse(int $status_code, string $msg, array $data = []): JsonResponse
    {
        return response()->json([
            'message'     => $msg,
            'data'        => $data,
            'status_code' => $status_code
        ], $status_code)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }
}

if (!function_exists('jsonResponseToHis')) {
    /**
     * json返回 to his
     * @param int $status_code
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return JsonResponse
     */
    function jsonResponseToHis(int $status_code, int $code, string $msg, array $data = []): JsonResponse
    {
        return response()->json([
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ], $status_code)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }
}

if (!function_exists('validateIDCard')) {
    /**
     * 校验身份证格式
     * @param string $id_card
     * @return bool
     */
    function validateIDCard(string $id_card): bool
    {
        $id_card = strtoupper($id_card);
        $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
        $arr_split = [];

        if (!preg_match($regx, $id_card)) {
            return false;
        }

        if (15 == strlen($id_card)) {
            //检查15位
            $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
            @preg_match($regx, $id_card, $arr_split);

            //检查生日日期是否正确
            $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];

            if (!strtotime($dtm_birth)) {
                return false;
            }

            return true;
        } else {
            //检查18位
            $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
            @preg_match($regx, $id_card, $arr_split);

            //检查生日日期是否正确
            $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];

            if (!strtotime($dtm_birth)) {
                return false;
            } else {
                //检验18位身份证的校验码是否正确。
                //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
                $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                $sign = 0;

                for ( $i = 0; $i < 17; $i++ ) {
                    $b = (int) $id_card[$i];
                    $w = $arr_int[$i];
                    $sign += $b * $w;
                }

                $n = $sign % 11;
                $val_num = $arr_ch[$n];

                if ($val_num != substr($id_card,17, 1)) {
                    return false;
                }

                return true;
            }
        }
    }
}

if (!function_exists('getGenderByIdCard'))
{
    /**
     * 根据 身份证 / 2023 外国人永居证 获取性别
     * @param string $id_card_no
     * @return int
     */
    function getGenderByIdCard(string $id_card_no): int
    {
        if (strlen($id_card_no) == 18) {
            //18位身份证 第17位 奇男 偶女
            $number = substr($id_card_no, -2, 1);
        } else {
            //15位身份证 第15位 奇男 偶女
            $number = substr($id_card_no, -1, 1);
        }

        //求余
        if (intval($number) % 2 !== 0) {
            return 1;
        } else {
            return 2;
        }
    }
}

if (!function_exists('validate2017ForeignersIDCard')) {
    /**
     * 2017 版外国人永居证号码校验
     * @param string $id_card
     * @return bool
     */
    function validate2017ForeignersIDCard(string $id_card): bool
    {
        //检查15位
        if (15 !== strlen($id_card)) {
            return false;
        }

        // 2017 版外国人永居证 前 3 位是大写字母 后 12 位是数字
        if (!preg_match( "/^[A-Z]{3}[0-9]{12}$/", $id_card)) {
            return false;
        }

        // 除最后一位外都为计算位数
        $length = 15 - 1;
        // 本体码
        $local = [];
        // 乘积
        $product = [];
        // 731 算法,加权因子
        $widths = [7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3, 1, 7, 3];

        for ($i = $length; $i > 0; $i--) {
            // 前三位为大写字母,映射为10-35的十进制数字
            if ($i > $length - 3) {
                $local[$length - $i] = mapLetterToNumber($id_card[$length - $i]);
            } else {
                $local[$length - $i] = intval($id_card[$length - $i]);
            }
            // 乘积
            $product[$length - $i] = $local[$length - $i] * $widths[$length - $i];
        }
        // 乘积之和对10取模
        $modulus = array_sum($product) % 10;
        // 最后一位为校验码
        $check_digit = (int) $id_card[$length];
        // 校验码与计算得到结果比对
        return $modulus == $check_digit;
    }
}

if (!function_exists('mapLetterToNumber')) {
    /**
     * 2017 版外国人永居证 证件前三位拉丁字母映射为 10-35的十进制数字
     * @param string $letter 单个拉丁字母
     * @return int 数字
     */
    function mapLetterToNumber(string $letter): int
    {
        return ord($letter) - ord('A') + 10;
    }
}

if (!function_exists('getIDCardType')) {
    /**
     * 判断身份证件类型
     * @param string $id_card
     * @return int 0 其他, 1 身份证,2 港澳台社保卡号, 3 2017 外国人永居证,4 2023 外国人永居证
     */
    function getIDCardType(string $id_card): int
    {
        // 身份证
        $length = strlen($id_card);
        if (($length === 18 || $length === 15) && (int) substr($id_card, 0, 1) !== 9 && validateIDCard($id_card)) {
            return 1;
        }

        // 港澳台 社保卡号
        if (preg_match("/^[HKG|MAC|TWN][0-9]{9}$/", $id_card)) {
            return 2;
        }

        // 2017 版外国人永居证 前 3 位是大写字母 后 12 位是数字
        if (preg_match("/^[A-Z]{3}[0-9]{12}$/", $id_card)) {
            return 3;
        }

        // 2023 版外国人永居证第一位是 9
        if ((int) substr($id_card, 0, 1) === 9) {
            return 4;
        }

        return 0;
    }
}

if (!function_exists('getBirthdayByIdCard'))
{
    /**
     * 根据 身份证 / 2023 外国人永居证 获取出生日期
     * @param string $id_card_no
     * @param string $format_rule
     * @return string
     */
    function getBirthdayByIdCard(string $id_card_no, string $format_rule = 'Y-m-d'): string
    {
        if (strlen($id_card_no) == 18) {
            //18位身份证
            //第7、8、9、10位为出生年份(四位数)
            //第11、第12位为出生月份
            //第13、14位代表出生日期
            $year = substr($id_card_no, 6, 4);
            $month = substr($id_card_no, 10, 2);
            $day = substr($id_card_no, 12, 2);

            $birthday = $year. '-'. $month. '-'. $day;
        } else {
            //15位身份证
            //第7、8位为出生年份(两位数)
            //第9、10位为出生月份
            //第11、12位代表出生日期
            $year = substr($id_card_no, 6, 2);
            $month = substr($id_card_no, 8, 2);
            $day = substr($id_card_no, 10, 2);

            $birthday = '19'. $year. '-'. $month. '-'. $day;
        }

        return date($format_rule, strtotime($birthday));
    }
}


if (!function_exists('getBirthdayBy2017ForeignersIDCard'))
{
    /**
     * 根据 2017 外国人永居证 获取出生日期
     * @param string $id_card_no
     * @param string $format_rule
     * @return string
     */
    function getBirthdayBy2017ForeignersIDCard(string $id_card_no, string $format_rule = 'Y-m-d'): string
    {
        // 2017年外国人永居证
        // 第8、9位为出生年份(两位数)
        // 第10、11位为出生月份
        // 第12、13位代表出生日期
        $year = substr($id_card_no, 7, 2);
        $month = substr($id_card_no, 8, 2);
        $day = substr($id_card_no, 11, 2);

        $birthday = '19'. $year. '-'. $month. '-'. $day;

        return date($format_rule, strtotime($birthday));
    }
}

if (!function_exists('checkMobilePhone')) {
    /**
     * 检测手机号码
     * @param string $mobile_phone
     * @return bool
     */
    function checkMobilePhone(string $mobile_phone): bool
    {
        $mobile_phone = trim($mobile_phone);
        $regex = "/^1(3|4|5|6|7|8|9)\d{9}$/";

        if (!preg_match($regex, $mobile_phone)) {
            return false;
        }

        return true;
    }
}

if (!function_exists('checkFixedTelephone')) {
    /**
     * 检测固定电话
     * @param string $fixed_telephone
     * @return bool
     */
    function checkFixedTelephone(string $fixed_telephone): bool
    {
        $fixed_telephone = trim($fixed_telephone);
        $regex = "/^(0[0-9]{2,3})?([2-9][0-9]{6,7})+([0-9]{1,4})?$/";

        if (!preg_match($regex, $fixed_telephone)) {
            return false;
        }

        return true;
    }
}

if (!function_exists('checkDateFormat')) {
    /**
     * 检查日期格式
     * @param string $date_str
     * @param string $rules
     * @return bool
     */
    function checkDateFormat(string $date_str, string $rules = 'Y-m-d'): bool
    {
        try {
            return date($rules, strtotime($date_str)) == $date_str;
        } catch (\Exception $e) {
            return false;
        }
    }
}

if (!function_exists('xmlArrayToListByKey')) {
    /**
     * xml的array转成lists
     * @param array $data
     * @param string $key
     * @return array
     */
    function xmlArrayToListByKey(array $data, string $key): array
    {
        //判断是否存在0,1,2,3等键值数组
        if(
            isset($data[$key]) &&
            (!isset($data[$key][0]) || reset($data[$key]) !== $data[$key][0])
        ) {
            $data[$key] = [$data[$key]];
        }

        return $data;
    }
}

if (!function_exists('objectToArray')) {
    /**
     * object to array
     * @param object $data
     * @return array
     */
    function objectToArray(object $data): array
    {
        return json_decode(json_encode($data), true);
    }
}

if (!function_exists('recordLog')) {
    /**
     * 保存日志记录
     * @param string $module_name
     * @param string $content
     */
    function recordLog(string $module_name, string $content): void
    {
        date_default_timezone_set("Asia/Shanghai");

        $file_path = app()->storagePath('logs'). DIRECTORY_SEPARATOR. $module_name. 'Log'. DIRECTORY_SEPARATOR. date('Ym'). DIRECTORY_SEPARATOR;
        $file_name = date('d'). '.log';

        !is_dir($file_path) && mkdir($file_path, 0755, true);

        $msg = '['. date('Y-m-d H:i:s'). ']'. PHP_EOL . $content . PHP_EOL . PHP_EOL;

        file_put_contents( $file_path. $file_name, $msg, FILE_APPEND);
    }
}

if (!function_exists('getCustomConfig')) {
    /**
     * 获取自定义配置
     * @param string $name
     * @param string $key
     * @return mixed
     */
    function getCustomConfig(string $name, string $key): mixed
    {
        $config = config('custom.'. $name);
        return isset($config[$key]) ? $config[$key] : reset($config);
    }
}

if (!function_exists('getMonthDateList')) {
    /**
     * 获取当月列表
     * @return array
     */
    function getMonthDateList(): array
    {
        $lists = [];
        $first_day = strtotime(date('Y-m-01'));
        $i = 0;
        $last_day = strtotime(date('Y-m-01'). ' +1 month');

        while ($first_day + $i * 86400 < $last_day) {
            $lists[] = date('Y-m-d', $first_day + $i * 86400);
            $i++;
        }

        return $lists;
    }
}

if (!function_exists('arrayToXml')) {
    /**
     * 数组转xml
     * @param array $data
     * @return string
     */
    function arrayToXml(array $data): string
    {
        $xml = '';
        foreach ($data as $key => $val)
        {
            if (is_array($val)) {
                $xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">";
            } else {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            }
        }

        return $xml;
    }
}

if (!function_exists('getHisPayTypeByCustomPayType')) {
    /**
     * 获取his支付类型
     * @param int $pay_type 自定义支付类型
     * @param bool $is_staff 是否为职工挂号
     * @return int
     */
    function getHisPayTypeByCustomPayType(int $pay_type, bool $is_staff): int
    {
        return !$is_staff ? (config('custom.pay_type_to_his')[$pay_type] ?? 0) : 4;
    }
}

if (!function_exists('getHisPayOrderNoByHisPayType')) {
    /**
     * 获取his支付类型
     * @param int $his_pay_type his支付类型
     * @param array $pay_res_data 是否为职工挂号
     * @return string
     */
    function getHisPayOrderNoByHisPayType(int $his_pay_type, array $pay_res_data): string
    {
        switch ($his_pay_type) {
            case 0:
            case 1:
                if (isset($pay_res_data['orderNo'], $pay_res_data['merchantId'])) {
                    // 数字人民币
                    $order_no = $pay_res_data['orderNo'];
                } else {
                    // 正常银联支付
                    $order_no = $pay_res_data['retCardNo'] ?? '';// UnMoney.Str2 / retCardNo
                }
                break;
            case 2:
            case 3:
            default:
                $order_no = $pay_res_data['out_trade_no'] ?? '';
                break;
            case 5:
                if (isset($pay_res_data['hospitalSerialNo']) && strlen($pay_res_data['hospitalSerialNo']) === 20) {
                    // 挂号信用付
                    $order_no = $pay_res_data['hospitalSerialNo'];
                } else {
                    // 住院信用付
                    $order_no = '';
                }
                break;
        }

        return $order_no;
    }
}

if (!function_exists('routeResource')) {
    /**
     * 设置资源路由
     * @param Route $router 路由handle
     * @param string $uri 路由名称
     * @param string $controller 控制器名称
     * @param array $allow 允许的方法
     * @return void
     */
    function routeResource(Route &$router, string $uri, string $controller, array $allow = ['index', 'store', 'show', 'update', 'destory']): void
    {
        if (in_array('index', $allow)) {
            $router->get($uri, $controller. '@index');
        }

        if (in_array('store', $allow)) {
            $router->post($uri, $controller. '@store');
        }

        if (in_array('show', $allow)) {
            $router->get($uri.'/{id:[0-9]+}', $controller. '@show');
        }

        if (in_array('update', $allow)) {
            $router->put($uri.'/{id:[0-9]+}', $controller. '@update');
            $router->patch($uri.'/{id:[0-9]+}', $controller. '@update');
        }

        if (in_array('destory', $allow)) {
            $router->delete($uri . '/{id:[0-9]+}', $controller . '@destroy');
        }
    }
}

if (!function_exists('getUrlQueryParams')) {
    /**
     * 获取url参数数组
     * @param string $url
     * @return array
     */
    function getUrlQueryParams(string $url): array
    {
        $query_param_str = parse_url($url, PHP_URL_QUERY);
        if (empty($query_param_str)) {
            return [];
        }

        $params_arr = explode('&', $query_param_str);

        $params = [];
        foreach ($params_arr as $k => $v) {
            $arr = explode('=', $v);
            $params[$arr[0]] = $arr[1];
        }

        return $params;
    }
}

if (!function_exists('getCsvFileContent')) {
    /**
     * 读取csv文件
     * @param string $file_path        文件路径
     * @param int    $ignore_head_line 忽略头部行数
     * @param int    $ignore_foot_line 忽略脚部行数
     * @return array
     */
    function getCsvFileContent(string $file_path, int $ignore_head_line = 0, int $ignore_foot_line = 0): array
    {
        if (!file_exists($file_path)) {
            return [false, '文件不存在'];
        }

        $i = 0;
        $data = [];
        $handle = fopen($file_path, 'r');
        while (($content = fgetcsv($handle)) !== false) {
            // 忽略头部行数
            if ($i < $ignore_head_line) {
                $i++;
                continue;
            }

            $i++;
            $data[] = $content;
        }

        // 忽略尾部行数
        if ($ignore_foot_line > 0) {
            $data = array_chunk($data, $i - $ignore_foot_line - $ignore_head_line);
            if (!is_array($data)) {
                return [true, []];
            }

            $data = $data[0];
        }

        return [true, $data];
    }
}

if (!function_exists('getFormatDateTimeStr')) {
    /**
     * 获取当前格式化后的时间字符串
     * @param string $format  格式化后的字符串
     * @param int $pow_num 保留几位秒数
     * @return string
     */
    function getFormatDateTimeStr(string $format = 'Y-m-d H:i:s', int $pow_num = 6): string
    {
        date_default_timezone_set('Asia/Shanghai');
        // 带微秒的时间戳
        $u_timestamp = sprintf("%.6f", microtime(true));

        $timestamp = floor($u_timestamp);
        $microseconds = round(($u_timestamp - $timestamp) * pow(10, $pow_num));

        return date($format, $timestamp) . $microseconds;
    }
}

if (!file_exists('getPaymentOutTradeOrderId')) {
    /**
     * 根据支付类型返回支付平台订单ID
     * @param int   $pay_type 支付类型
     * @param array $pay_result 支付返回
     * @return mixed|string
     */
    function getPaymentOutTradeOrderId(int $pay_type, array $pay_result): mixed
    {
        switch ($pay_type) {
            case 2:
            case 5:
            case 9:
                $out_trade_id = $pay_result['retFlowWaterNo'] ?? '';
                break;
            case 3:
            case 6:
                // 查找顺序 微信支付 -> HIS支付平台
                $out_trade_id = $pay_result['transaction_id'] ?? ($pay_result['platform_order_no'] ?? '');
                break;
            case 4:
            case 7:
                // 查找顺序 支付宝支付 -> HIS支付平台
                $out_trade_id = $pay_result['trade_no'] ?? ($pay_result['platform_order_no'] ?? '');
                break;
            case 10:
            case 11:
                $out_trade_id = $pay_result['orderNo'] ?? '';
                break;
            case 12:
                $out_trade_id = $pay_result['transNo'] ?? '';
                break;
            default:
                $out_trade_id = '';
                break;
        }

        return $out_trade_id;
    }
}

if (!function_exists('getArrayByKeyList')) {
    /**
     * 根据键值列表获取数据
     * @param array $array 原数据
     * @param array $list 需要取得键值数组
     * @return array
     */
    function getArrayByKeyList(array $array, array $list): array
    {
        if (empty($array) || empty($list)) {
            return [];
        }

        $n_array = [];
        foreach ($list as $k => $v) {
            $n_array[$v] = $array[$v] ?? '';
        }

        return $n_array;
    }
}

if (!function_exists('getArrayColumnListsByKey')) {
    /**
     * 根据键值获取数组列的列表
     * @param $array
     * @param $key
     * @return array
     */
    function getArrayColumnListsByKey($array, $key): array
    {
        if (empty($array)) {
            return [];
        }

        $lists = [];
        foreach ($array as $k => $v) {
            if (!isset($v[$key])) {
                continue;
            }

            $lists[$v[$key]] = $v;
        }

        return $lists;
    }
}

if (!function_exists('getElectronHealthConfig')) {
    /**
     * 获取电子健康卡配置
     * @param string $name
     * @param string $key
     * @return mixed
     */
    function getElectronHealthConfig(string $name, string $key): mixed
    {
        $config = config('health.'. $name);
        return isset($config[$key]) ? $config[$key] : reset($config);
    }
}

if (!function_exists('generateTree')) {
    /**
     * 分类树
     * @param array  $array    分类数据
     * @param string $s_key    子类IDkey名称
     * @param string $p_key    父类IDkey名称
     * @param string $item_key 子类存储key名称
     * @return array
     */
    function generateTree(array $array, string $s_key, string $p_key, string $item_key): array
    {
        $items = [];
        foreach($array as $v){
            $items[$v[$s_key]] = $v;
        }

        $tree = [];
        foreach($items as $k => $v){
            if(isset($items[$v[$p_key]])){
                $items[$v[$p_key]][$item_key][] = &$items[$k];
            }else{
                $tree[] = &$items[$k];
            }
        }
        return $tree;
    }
}

if (!function_exists('getWeChatMiniProgramApp')) {
    /**
     * 获取小程序app示例
     * @return MiniApplication
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    function getWeChatMiniProgramApp(): MiniApplication
    {
        return new MiniApplication(config('wechat.mini'));
    }
}

if (!function_exists('getWeChatMiniProgramPaymentApp')) {
    /**
     * 获取小程序支付app
     * @return PayApplication
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    function getWeChatMiniProgramPaymentApp(): PayApplication
    {
        return new PayApplication(config('wechat.payment'));
    }
}

if (!function_exists('replaceSpecialChar')) {
    /**
     * 过滤特殊字符
     * @param string $string
     * @return array|string|string[]|null
     */
    function replaceSpecialChar(string $string): array|string|null
    {
        $regex = "-[/~!@#$%^&*()_+{}:<>?\[\],.;`'\-=|]-";
        return preg_replace($regex, '', $string);
    }
}