<?php
/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/10/24
 */

namespace crmeb\services;


use app\models\routine\RoutineCode;
use app\models\system\SystemAttachment;
use app\models\wechat\WechatQrcode as QrcodeModel;

class QrcodeService
{
    /**
     * 获取临时二维码  单个
     * */
    public static function getTemporaryQrcode($type, $id)
    {
        return QrcodeModel::getTemporaryQrcode($type, $id)->toArray();
    }

    /**
     * 获取永久二维码  单个
     * */
    public static function getForeverQrcode($type, $id)
    {
        return QrcodeModel::getForeverQrcode($type, $id)->toArray();
    }

    public static function getQrcode($id, $type = 'id')
    {
        return QrcodeModel::getQrcode($id, $type);
    }

    public static function scanQrcode($id, $type = 'id')
    {
        return QrcodeModel::scanQrcode($id, $type);
    }

    /**
     * 获取二维码完整路径，不存在则自动生成
     * @param string $name 路径名
     * @param string $link 需要生成二维码的跳转路径
     * @param int $type https 1 = http , 0 = https
     * @param bool $force 是否返回false
     * @return bool|mixed|string
     */
    public static function getWechatQrcodePath(string $name, string $link, bool $force = false)
    {
        try {
            $imageInfo = SystemAttachment::getInfo($name, 'name');
            $siteUrl = sys_config('site_url');
            if (!$imageInfo) {
                $codeUrl = UtilService::setHttpType($siteUrl . $link, request()->isSsl() ? 0 : 1);//二维码链接
                $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
                if (is_string($imageInfo) && $force)
                    return false;
                if (is_array($imageInfo)) {
                    SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
                    $url = $imageInfo['dir'];
                } else {
                    $url = '';
                    $imageInfo = ['image_type' => 0];
                }
            } else $url = $imageInfo['att_dir'];
            if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
            return $url;
        } catch (\Throwable $e) {
            if ($force)
                return false;
            else
                return '';
        }
    }

    /**
     * 获取h5页面二维码完整路径，不存在则自动生成
     * @param string $name 路径名
     * @param string $link 需要生成二维码的跳转路径
     * @param int $type https 1 = http , 0 = https
     * @param bool $force 是否返回false
     * @return bool|mixed|string
     */
    public static function getQrcodePath(string $name, string $link, bool $force = false, $mer_id = false)
    {
        try {
            $imageInfo = SystemAttachment::getInfo($name, 'name');
            $siteUrl = sys_config('site_url');
            if (!$imageInfo) {
                $codeUrl = UtilService::setHttpType('https://auti.boofly.cn' . $link, request()->isSsl() ? 0 : 1);//二维码链接
                $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
                if (is_string($imageInfo) && $force)
                    return false;
                if (is_array($imageInfo)) {
                    SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 7, $imageInfo['image_type'], $imageInfo['time'], 2, $mer_id);
                    $url = $imageInfo['dir'];
                } else {
                    $url = '';
                    $imageInfo = ['image_type' => 0];
                }
            } else $url = $imageInfo['att_dir'];
            if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
            return $url;
        } catch (\Throwable $e) {
            if ($force)
                return false;
            else
                return '';
        }
    }

    /**
     * 获取小程序分享二维码
     * @param int $id
     * @param int $uid
     * @param int $type 1 = 拼团,2 = 秒杀,3 = 助力
     * @param array $parame
     * @param bool $mer_id
     * @return bool|string
     */
    public static function getRoutineQrcodePath(int $id, int $uid, int $type, array $parame = [], $mer_id = false)
    {
        $page = '';
        $namePath = '';
        $data = 'id=' . $id . '&pid=' . $uid;
        switch ($type) {
            case 1:
                $page = 'pages/activity/goods_combination_details/index';
                $namePath = 'combination_' . $id . '_' . $uid . '.jpg';
                break;
            case 2:
                $page = 'pages/activity/goods_seckill_details/index';
                $namePath = 'seckill_' . $id . '_' . $uid . '.jpg';
                if (isset($parame['stop_time']) && $parame['stop_time']) {
                    $data .= '&time=' . $parame['stop_time'];
                    $namePath = $parame['stop_time'] . $namePath;
                }
                break;
            case 3:
                $page = 'pages/zhuli/product_detail/index';
                $namePath = 'assistance_' . $id . '_' . $uid . '.jpg';
                break;
            case 4:
                $page = 'pages/zhuli/help_zhuli/index';
                $namePath = 'assistance_active_' . $id . '_' . $uid . '.jpg';
                break;
        }
        if (!$page || !$namePath) {
            return false;
        }
        try {
            $imageInfo = SystemAttachment::getInfo($namePath, 'name');
            $siteUrl = sys_config('site_url', '', $mer_id);
            if (!$imageInfo) {
                $res = RoutineCode::getPageCode($page, $data, 280, $mer_id);
                if (!$res) return false;
                $uploadType = (int)sys_config('upload_type', 1, $mer_id);
                $upload = UploadService::init(null, $mer_id);
                $res = $upload->to('routine/product')->validate()->stream($res, $namePath);
                if ($res === false) {
                    return false;
                }
                $imageInfo = $upload->getUploadInfo();
                $imageInfo['image_type'] = $uploadType;
                if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
                else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
                if (!$remoteImage['status']) return false;
                SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
                $url = $imageInfo['dir'];
            } else $url = $imageInfo['att_dir'];
            if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
            return $url;
        } catch (\Throwable $e) {
            return false;
        }
    }
}