<?php

namespace Api\PhpServices\Certification;
use Api\PhpUtils\Http\Request;

class SocialCreditCertification
{
    private const account           = 'BJyimingzhineng';
    private const app_key           = 'deee9c9877e5452f8838cfc5130ac5ad';
    private const ocr_bussiness_url = 'http://apistore.xmturui.com/apistore/ocr-new';
    private const engine_type       = 'blic-new';

    /**
     * 校验社会统一信用代码格式是否合法
     * @param $social_creadit_code
     * @return bool
     */
    public static function checkSocialCredit(string $social_creadit_code) :bool
    {
        // todo 长度判断
        if (strlen($social_creadit_code) !== 18) {
            return false;
        }

        $one = '159Y';//第一位可以出现的字符
        $two = '12391';//第二位可以出现的字符
        $str = strtoupper($social_creadit_code);
        if (strpos($one, $str[1]) === false && strpos($two, $str[2]) === false && !empty($array[substr($str, 2, 6)])) {
            return false;  //有误
        }
        $wi = array(1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28);//加权因子数值
        $str_organization = substr($str, 0, 17);
        $num = 0;
        for ($i = 0; $i < 17; $i++) {
            $num += self::transformation($str_organization[$i]) * $wi[$i];
        }
        switch ($num % 31) {
            case '0':
                $result = 0;
                break;
            default:
                $result = 31 - $num % 31;
                break;
        }

        if ($str[strlen($str) - 1] === self::transformation($result, true)) {
            return true;
        }

        return false;
    }

    private static function transformation($num, $status = false)
    {
        $list = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'J' => 18, 'K' => 19, 'L' => 20, 'M' => 21, 'N' => 22, 'P' => 23, 'Q' => 24, 'R' => 25, 'T' => 26, 'U' => 27, 'W' => 28, 'X' => 29, 'Y' => 30);//值转换
        if ($status === true) {
            $list = array_flip($list);
        }
        return $list[$num];
    }

    /**
     * 营业执照ocr 识别
     * @param $business_image string image_url
     * @return array
     */
    public function businessLicenseOcr($business_image)
    {

        $base64_image = self::imgBase64Encode($business_image);
        if ($base64_image  === false) {
            return ['code'=>-1];
        }

        $post_data = [
            'account' => self::account,
            'key' => self::app_key,
            'enginetype' => self::engine_type,
            'databaseImageContent' => $base64_image
        ];

        $result = (new Request())->post(self::ocr_bussiness_url, $post_data, 3000, 'json',[],1);

        if (isset($result['code'],$result['response']) && $result['code'] === 0 ) {
            return $result['response'];
        }

        return ['code'=>-1];
    }

    /**
     * base64_encode_image
     * @param string $img
     * @return bool|string
     */
    public static function imgBase64Encode($img = '')
    {
        if (strpos($img, 'http') === false && !file_exists($img)) {
            return false;
        }
        //获取文件内容
        $file_content = file_get_contents($img);
        if ($file_content === false) {
            return false;
        }
        return chunk_split(base64_encode($file_content));
    }
}