<?php

namespace App\Api\Controllers;

use App\Http\Controllers\Controller;
use Storage;
use App\Model\Series;
use App\Model\MoviePushLog;
use ArrayToXml;
use Illuminate\Http\Request;

class UpdateController extends Controller
{
	public function gxyd(Request $request)
	{
        //获取所有剧集
        if($request->input("series_id")){
            $series = Series::where("series_id",$request->input("series_id"))->select('series_id', 'series_name', 'description', 'release_year', 'orgair_date', 'volumn_count')->get();
            if($series->isEmpty()){
                echo "没有对应剧集";
                return;
            }
        }else{
            //获取已经推送的剧集ID
            $series = MoviePushLog::select("series_id", "series_name")->distinct("series_name")->whereNotNull("view_url")->get();
            foreach ($series as $k => $v) {
                $ids[] = Series::where("series_name", $v->series_name)->value("series_id");
            }
            $ids =  array_filter($ids);
            if(count($ids) < 0){
                echo "没有对应剧集";
                return;
            }
            $series = Series::select('series_id', 'series_name', 'description', 'release_year', 'orgair_date', 'volumn_count')->whereIn("series_id", $ids)->get();
        }
        foreach ($series as $k => $v) {
            $series_id  = 'HC_v1_Series_' . str_pad((int) $v->series_id, 10, "0", STR_PAD_LEFT);
            $http_picurl = "http://183.6.116.30:10060/media/gx/yd/{$v->series_id}.jpg";

            $series_data = [
                '_attributes' => [
                    'Action' => 'UPDATE',
                    'Code' => $series_id,
                    'ElementType' => 'Series',
                    'ID' => $series_id,
                ],
                'Property' => [
                    ['_attributes'=>['Name'=>'Name'],'_cdata'=>$v->series_name],
                    ['_attributes'=>['Name'=>'LicensingWindowStart'],'_cdata'=>'20181009120000'],
                    ['_attributes'=>['Name'=>'LicensingWindowEnd'],'_cdata'=>'20381009120000'],
                    ['_attributes'=>['Name'=>'Code'],'_cdata'=>$series_id],
                    ['_attributes'=>['Name'=>'Description'],'_cdata'=> $v->description],
                    ['_attributes'=>['Name'=>'Language'],'_cdata'=>'普通话'],
                    ['_attributes'=>['Name'=>'Definition'],'_cdata'=>'HD'],
                    ['_attributes'=>['Name'=>'IsCharge'],'_cdata'=>'0'],
                    ['_attributes'=>['Name'=>'Definition'],'_cdata'=>'HD'],
                    ['_attributes'=>['Name'=>'PictureURL1'],'_cdata'=>$http_picurl],
                    ['_attributes'=>['Name'=>'ProgramType'],'_cdata'=>'体育'],
                    ['_attributes'=>['Name'=>'ProgramType2'],'_cdata'=>'游戏竞技'],
                    ['_attributes'=>['Name'=>'Score'],'_cdata'=>'8.0'],
                    ['_attributes'=>['Name'=>'ActorDisplay'],'_cdata'=>'未知'],
                    ['_attributes'=>['Name'=>'WriterDisplay'],'_cdata'=>'未知'],
                    ['_attributes'=>['Name'=>'OriginalCountry'],'_cdata'=>'中国大陆'],
                    ['_attributes'=>['Name'=>'ReleaseYear'],'_cdata'=>$v->release_year],
                    ['_attributes'=>['Name'=>'OrgAirDate'],'_cdata'=>$v->orgair_date],
                    ['_attributes'=>['Name'=>'VolumnCount'],'_cdata'=> (int) $v->volumn_count],
                    ['_attributes'=>['Name'=>'Duration'],'_cdata'=> (int) $v->duration],
                ],
            ];
            //生成 XML 信息
            $root = [
                        '_attributes' => [
                            'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
                            'BizDomain' => 2,
                        ],
                        'rootElementName' => 'ADI',
                    ];

            $objects = array();
            $objects['Object'][] = $series_data;
            $all_data = ["Objects"=>$objects];
            $result = ArrayToXml::convert($all_data,$root,true, 'utf-8');

            //上传XML
            file_put_contents("ftp://gzhc:GZhc2018@183.6.116.30:22111/media/gx/yd/pushxml/update_series_{$v->series_id}.xml", $result);

            $http_xml_file = "http://183.6.116.30:10060/media/gx/yd/pushxml/update_series_{$v->series_id}.xml";
            $ftp_xml_file = "ftp://gzhc:GZhc2018@183.6.116.30:22111/media/gx/yd/pushxml/update_series_{$v->series_id}.xml";

            //通知获取XML
            $wsdl_location = 'http://117.169.11.222:9000/ott4__gxyst.wsdl';
            $params = array(
                'CSPID' =>'GX-DJBL',
                'LSPID' =>'JDGX_TV_COS',
                'CorrelateID' => $series_id,   
                'CmdFileURL' => $ftp_xml_file,
            );

            ini_set('display_errors', true);
            ini_set("soap.wsdl_cache_enabled", "0");
            $client = new \SoapClient($wsdl_location, array('encoding' => 'UTF-8'));
            $client->soap_defencoding = 'UTF-8';
            $client->xml_encoding = 'UTF-8';
            $result = $client->ExecCmd($params['CSPID'], $params['LSPID'], $params['CorrelateID'], $params['CmdFileURL']);
        }
        echo "处理完成";
	}

}