<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/12
 * Time: 上午9:57
 * 同步资讯
 */

namespace backend\server;

use Yii;
use yii\db\Exception;
use linslin\yii2\curl;
use common\models\Vr;
use common\models\Syncvrrecord;

class SyncVr
{
    public function userOpe($input)
    {
        try {
            $Transaction = Yii::$app->db->beginTransaction();
            if ($this->isNoll($input['data']) || $this->isNoll($input['append'])) {
                throw new Exception('数据不完整,无法进行同步,请检查数据完整性!');
            }
            $dataCode = $this->operation($input['data'], $input['append']);
            if ($dataCode === true) {
                $Transaction->commit();
            } else {
                $Transaction->rollBack();
            }
            return $dataCode;
        } catch (Exception $e) {
            $Transaction->rollBack();
            return $e->getMessage();
        }
    }

    /**
     * 同步VR信息数据
     * */
    private function operation($data, $append)
    {
        $data = json_decode($data, true);
        $append = json_decode($append, true);
        if ($this->isNoll($data['img']) || $this->isNoll($append['img_url'])) {
            throw new Exception('封面图数据不完整,无法进行同步,请检查封面图数据完整性!');
        }
//        return [$data,$append];
        $vrInfo = [];
        $vrInfo['img'] = $this->PullImg($data['img'], $append['img_url']);
//        $vrInfo['img'] = $data['img'];
        $vrInfo['name'] = $data['name'];
        $vrInfo['abstarct'] = $data['abstarct'];
        $vrInfo['covered_area'] = $data['covered_area'];
        $vrInfo['house_type'] = $this->getHouseTypeId($data);
        $vrInfo['hid'] = $this->getHouseInfo($append);
        $vrInfo['path'] = $data['path'];
        $vrInfo['type'] = $data['type'];
        $vrInfo['state'] = $data['state'];
        $vrInfo['sort'] = $data['sort'];
        $vrInfo['uuid'] = $data['uuid'];

        $this_content = [];
        $this_content['oss_content'] = json_encode($vrInfo, JSON_UNESCAPED_UNICODE);
        $this_content['auditor_name'] = $append['user_name'];
        $sync = $this->isSyncEd($data['uuid']);
        $syncModel = new Syncvrrecord();
        if ($sync === false) {
            $vrModel = new Vr();
            if ($vrModel->load($vrInfo, '') && $vrModel->validate() && $vrModel->save()) {
                $this_content['this_vrid'] = $vrModel->attributes['id'];
                $this_content['code'] = 'add';
            } else {
                throw new Exception('同步添加失败!请联系管理员');
            }
        } else {
            $vrRow = Vr::findOne($sync['id']);
            if ($vrRow->load($vrInfo, '') && $vrRow->update(false)) {
                $this_content['this_vrid'] = $sync['id'];
                $this_content['code'] = 'edit';
                $this_content['this_content'] = json_encode($sync, JSON_UNESCAPED_UNICODE);
            } else {
                throw new Exception('同步更新失败!请联系管理员');
            }
        }
        $syncModel->load($this_content, '');
        if (!$syncModel->save()) {
            throw new Exception('同步纪录失败!请联系管理员');
        }
        return true;
    }

    /**
     * 获取楼盘信息
     * */
    private function getHouseInfo($data)
    {
        $house_name = $data['house_name'];
        if ($data['type'] == 1) {
            $houseInfo = \common\models\House::find()->where(['name' => $house_name, 'del' => 1])->one();
            if (!empty($houseInfo)) {
                return $houseInfo['id'];
            }
        } else {

        }
        throw new Exception('VR对应楼盘不存在,请先同步楼盘信息!');
    }

    /**
     * 获取户型信息
     * */
    public function getHouseTypeId($data)
    {
        if ($data['type'] == 1) {
            $houseType = \common\models\CategoryHousetype::find()->where(['huxing_name' => $data['huxing_name'], 'del' => 1])->one();
            if (!empty($houseType)) {
                return $houseType['id'];
            }
        } else {

        }
        throw new Exception('VR对应楼盘户型不存在,请先同步楼盘户型信息!');
    }

    /**
     * 拉取图片
     */
    private function PullImg($img, $oos)
    {
        if (is_string($img)) {
            $curl = new curl\Curl();
            $resultImg = $curl->get($oos . $img);
            $imgUrl = Yii::$app->params['img_url']['video'];
            if (!is_file($imgUrl . $img)) {
                if (!file_put_contents($imgUrl . $img, $resultImg)) {
                    throw new Exception('图片添加失败,请联系管理员');
                }
            }
            return $img;
        } else {
            throw new Exception('封面图数据不完整,无法进行同步,请检查封面图数据完整性!');
        }
    }

    /**
     * 检测VR是否已同步过
     * */
    private function isSyncEd($uuid)
    {
        $row = (new Vr())->getFindByUuid($uuid);
        if (!empty($row)) {
            return $row;
        } else {
            return false;
        }
    }

    /**
     * 检测数据存在并且不为空
     * */
    private function isNoll($data)
    {
        if (!isset($data) || empty($data)) {
            return true;
        }
        return false;
    }
}