<?php
namespace app\controller;

use app\model\DocImportRecord;
use think\facade\Log;
use think\facade\Request;
use think\response\Json;


/**
 * Es索引操作类
 */
class EsIndex extends CommonEsController
{

    /**
     * 创建索引
     */
    public function createIndex()
    {
        $index = Request::param('index') ?? '';
        $type  = Request::param('type')  ?? '';
        if( ! $index || ! $type) {
            return $this->_json_error();
        }

        $params = [
            'index' => $index,
            'type'  => $type,
            'body'  => []
        ];
        $client = $this->getEsClient();

        return $this->_json_succ(
            $client->index($params)
        );
    }


    /**
     * 删除索引
     */
    public function delete():Json
    {
        $index = Request::param('index') ?? 'document';
        if( ! $index) {
            return $this->_json_error('参数错误！');
        }

        $params = ['index' => $index];
        $client = $this->getEsClient();

        return $this->_json_succ(
            $client->indices()->delete($params)
        );
    }


    /**
     * 批量添加
     */
    public function addAlbum()
    {
        $data = file_get_contents('/Users/qdy0517/Downloads/es-document/es-qikan/zhiwang_list.json');

        $contents = explode("\n", $data);
        $newArray = [];
        foreach($contents as $key => $content)
        {
            $content    = json_decode($contents[$key]);
            $newContent = json_decode(json_encode($content) , true);

            $newArray[$key]['album'] = $newContent['专辑名称']?? '';
            $newArray[$key]['publication_place'] = $newContent['出版地']?? '';
            $newArray[$key]['comprehensive_impact_factors'] = $newContent['(2021)综合影响因子']?? '';
            $newArray[$key]['composite_impact_factor']      = $newContent['(2021)复合影响因子']?? '';
            $newArray[$key]['bj_city_source_journals']      = $newContent['北京大学《中文核心期刊要目总览》来源期刊']?? '';
            $newArray[$key]['ISSN']      = $newContent['ISSN']?? '';
            $newArray[$key]['format']    = $newContent['开本'] ?? '';
            $newArray[$key]['host_unit'] = $newContent['主办单位']?? '';
            $newArray[$key]['CN']        = $newContent['CN']?? '';
            $newArray[$key]['published_literature_volume'] = $newContent['出版文献量']?? '';
            $newArray[$key]['type']         = $newContent['type']?? '';
            $newArray[$key]['special_name'] = $newContent['专题名称']?? '';
            $newArray[$key]['total_download_times'] = $newContent['总下载次数']?? '';
            $newArray[$key]['total_citations_number'] = $newContent['总被引次数']?? '';
            $newArray[$key]['url'] = $newContent['url']?? '';
            $newArray[$key]['cn_name'] = $newContent['cn_name']?? '';
            $newArray[$key]['en_name'] = $newContent['en_name']?? '';
            $newArray[$key]['language']   = $newContent['语种']?? '';
            $newArray[$key]['publication_cycle'] = $newContent['出版周期']?? '';
            $newArray[$key]['postal_distribution_code'] = $newContent['邮发代号']?? '';
            $newArray[$key]['first_time']  = $newContent['创刊时间']?? '';
            $newArray[$key]['md5']         = $newContent['md5']?? '';
            $newArray[$key]['included_in'] = $newContent['该刊被以下数据库收录']?? '';
        }

        $count = count($newArray);
        for ($i = 0; $i < $count; $i++) {
            $params['body'][] = [
                'index' => [   #创建
                    '_index' => 'album_v20221009',
                    '_type'  => 'album',
//                    '_id'    => $i,
                ]
            ];
            $params['body'][]=$newArray[$i];
        }

        $client = $this->getEsClient();
        $result = $client->bulk($params);

        return  $this->_json_succ($result);
    }



    /**
     * 批量添加
     */
    public function addDoc()
    {
        Log::info('info='.__FUNCTION__);

        $docRecord = DocImportRecord::where('type' , 'zw')->order('id' , 'desc')->limit(1)->find();
        if(empty($docRecord)) {
            $fileNumVal = 0;
        } else {
            $value = $docRecord->value;
            $fileNumVal = $value + 1;
        }

        Log::info('$fileNumVal='.$fileNumVal);

        //$fileDir = '/Users/qdy0517/Downloads/es-document/zhiwang'.$fileNumVal.'.json';
        $fileDir   = 'https://jm.jiankangche.cn/zhiwang02/zhiwang_'.$fileNumVal.'.json';

        Log::info('$fileDir1='.$fileDir);


        $data       = file_get_contents($fileDir);
        $contents   = explode("\n" , $data);
        //$contents   = array_slice($contents, 0,50000);

        $count = count($contents);
        $params['body'] = [];
        for ($i = 0; $i < $count; $i++) {
            $params['body'][] = [
                'index' => [
                    '_index' => 'document_v1',
                    '_type'  => 'zhiwang',
                ]
            ];

            $singleDoc = json_decode($contents[$i] , true);

            if(isset($singleDoc['references']) && $singleDoc['references']) {
                $singleDoc['references'] = array_reduce($singleDoc['references'], 'array_merge', array());
            }

            if(isset($singleDoc['citationDocument']) && $singleDoc['citationDocument']) {
                $singleDoc['citationDocument'] = array_reduce($singleDoc['citationDocument'], 'array_merge', array());
            }

            json_encode($singleDoc);
            $params['body'][]=$singleDoc;
        }

        $client = $this->getEsClient();
        $result = $client->bulk($params);

        if($result) {
            DocImportRecord::create(
                [
                    'type'  => 'zw' ,
                    'value' => $fileNumVal
                ]
            );
        }

        return  $this->_json_succ($result);
    }


    /**
     * 单条添加
     */
    public function add($data)
    {
        $params = [
            'index' => $data['index'] ?? 'document',
            'id'    => $data['id'],
            'body'  => $data,
            'type'  => $data['type'] ?? 'document',
        ];
        $client = $this->getEsClient();
        $result = $client->index($params);
        return $result;
    }


    /**
     * 批量更新
     */
    public function batchUpdate()
    {
        $fileDir    = '/Users/qdy0517/Downloads/zhiwang.json';
        $data       = file_get_contents($fileDir);
        $contents   = explode("\n" , $data);

        //return count($contents);die; 3249403
        //$contents   = array_slice($contents, 0,2000000);

        foreach($contents as $key => $content) {
            $newContent = $contents[$key];
            $newContent = str_replace("'", '"', $newContent);
            $newData    = json_decode($newContent, true);

            $newArray[$key]['title']        = $newData['标题'] ?? '';
            $newArray[$key]['author']       = $newData['作者'] ?? '';
            $newArray[$key]['organization'] = $newData['院校'] ?? '';
            $newArray[$key]['journal']      = $newData['杂志'] ?? '';
        }


        $count = count($newArray);
        $params['body'] = [];
        for ($i = 0; $i < $count; $i++) {
            $params['body'][] = [
                'index' => [
                    '_index' => 'author_v1',
                    '_type'  => 'zhiwang',
                ]
            ];

            $params['body'][] = $newArray[$i];
        }

        $client = $this->getEsClient();
        $result = $client->bulk($params);
        return  $this->_json_succ(
            $result
        );
    }


    /**
     * author_v2
     */
    public function newAuthorVTwoMultiAdd()
    {
        $fileDir    = '/Users/qdy0517/Downloads/zhiwang.json';
        $data       = file_get_contents($fileDir);
        $contents   = explode("\n" , $data);

        foreach($contents as $key => $content) {
            $newContent = $contents[$key];
            $newContent = str_replace("'", '"', $newContent);
            $newData    = json_decode($newContent, true);

            $newArray[$key]['title']        = $newData['标题'] ?? '';
            $newArray[$key]['author']       = $newData['作者'] ?? '';
            $newArray[$key]['organization'] = $newData['院校'] ?? '';
            $newArray[$key]['journal']      = $newData['杂志'] ?? '';
        }

        /**
         * 增加机构数量
         */
//        foreach ($newArray as $k => $v) {
//            $organization = $v['organization'];
//            if(stripos($organization , ';') !== false) {
//                $organization = str_replace(';' , ',' , $organization);
//            }
//            $newArray[$k]['org_count'] = count(array_unique(explode(',' , $organization)));
//        }

        $count = count($newArray);
        $params['body'] = [];
        for ($i = 0; $i < $count; $i++) {
            $params['body'][] = [
                'index' => [
                    '_index' => 'author_v2',
                    '_type'  => 'zhiwang',
                ]
            ];
            $params['body'][] = $newArray[$i];
        }

        $client = $this->getEsClient();
        $result = $client->bulk($params);
        return  $this->_json_succ(
            $result
        );
    }

}