<?php
namespace model;
use \model;
use model\wdzy\factory;

/**
 * Class Rca接口
 * @package model
 */
class ypRcaApi extends rcaApi
{
    protected $bypassStopCheck = true;
    function __construct()
    {
        $this->config = \mvc::$cfg['YPRCAAPI'];
        $this->tokenName = 'yprca.token.json';
        $this->apiName  = 'HDC 宜配资源';
    }

    /**
     * @method 获取token
     * @author zhengweihua 
     * @copyright 2023-12-12
     */
    public function getToken()
    {
        if ($this->hasValidToken()) {
            return json_decode(file_get_contents($this->getTokenFile()), true)['token'];
        }

        $urlPath = 'tokens/get';
        $data = [
            'grantType'=>'client_credentials',
            'clientId' => $this->config['clientId'],
            'clientSecret' => $this->config['clientSecret']
        ];
        $res = $this->Get($urlPath, [], $data);
        $token = $res['data'];
       
        $this->setToken($token['token'], $token['expires']);
        return $token['token'];
    }


    /**
     * @param  [type] $params
     * @param  [type] $numArr
     * @method 获取资源搜索结果
     * @author zhengweihua 
     * @copyright 2025-07-23
     */
    public function getSourceSearch($numArr,$type='num')
    {
        if (!$numArr) return [];
        $result = [];

        foreach ($numArr as $num) {
            $data = [
                'input' => $num,
                'priority' => 1,
                'fromPlatform' => 'frey',
                'fromCustomer' => 'frey',
                'limit'=>200
            ];
            $arr = $this->Get('resources/searchbyindex', [], $data);
            if ($arr['data']) {
                foreach ($arr['data'] as $key => &$val) {
                    $val['searchNumber'] = $num;
                }
                unset($val);
                $result = array_merge($result, $arr['data'] ?: []);
            }
        }

        if ($result) {
            usort($result, function ($a, $b) {
                return strtotime($b['creationTime']) - strtotime($a['creationTime']);
            });

            $returnData = [];
            foreach ($result as &$val) {
                //快照原始数据的图片列为空，补充图片列
                if($val['rowData'] )
                {
                    foreach ($val['rowData'][0] as $field => &$v) 
                    {
                        if((stripos($field, '图片') !== false ||stripos($field, 'PHOTO') !== false||
                        stripos($field, 'picture') !== false ||
                        stripos($field, 'DIAGRAM') !== false ) && !$v['Value'] && $val['pictureUrls']){
                            $v['Value'] = $val['pictureUrls'];
                            $v['Type'] = 'picture';
                            break;
                        }
                        $v = str_replace(["\n","\r\n"],"<br />",$v);
                    }
                    unset($v);
                }
                $val['creationTime'] = $val['creationTime']?date('Y-m-d', strtotime($val['creationTime'])):'';
                $returnData[] = $val;
            }
            unset($val);
        }
        return $returnData;
    }

    public function getFactorySearch($keyword)
    {
        $data = [
            'input'=> '',
            'proCategoryNames'=>[$keyword],
            'fromPlatform' => 'frey',
            'fromCustomer' => 'frey'
        ];
        $arr = $this->Get('resourcefactory/listsimple', [], $data);
        return $arr['data']?array_slice($arr['data'],0,10):[];
    }
}