<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/11
 * Time: 下午3:08
 */

namespace backend\server;

use yii\base\Component;
use common\models\Syncliverecord;
use common\models\LivesyncRecord;
use common\models\Live;
use common\models\Synchouserecord;
use Yii;
use yii\db\Exception;

class SyncLive extends Component
{
    public function LiveOpe($input)
    {
        $operator = $input['operator'];
        $title = $input['title'];
        $livecontent = json_decode($input['livecontent'], true);
        $HouseInfo = $this->LerifyHouse($livecontent);
        if (!$HouseInfo) {
            return '同步目标站点不存在该楼盘';
        }
        $liveexs = Live::find()->select('id as lid')->where(['hid' => $HouseInfo['hid']])->asArray()->one();
        //楼盘直播视频是否存在
        if (empty($liveexs)) {
            //该直播视频是否同步过
            if(!empty($input['un_id'])){
                $liveexs = Syncliverecord::find()->select('own_lid as lid')->where(['unid' => $input['un_id']])->orderBy(['create_at' => SORT_DESC])->asArray()->one();
            }
            if (empty($liveexs)) {
                $liveexs = Live::find()->select('id as lid')->where(['title' => $title])->asArray()->one();
                // 未同步过但是相同视频是否存在
                if (empty($liveexs)) { //未同步过,并且直播视频不存在
                    return $this->LiveToneOne($operator, $livecontent, $HouseInfo);
                }
            }
        }
//        同步过或者直播视频存在或者楼盘直播视频
        return $this->LiveToneTwo($operator, $livecontent, $HouseInfo, $liveexs['lid']);//修改
    }

    /**
     *同步新增
     * */
    public function LiveToneOne($operator, $livecontent, $HouseInfo)
    {
        $Transaction = Yii::$app->db->beginTransaction();
        $liveInfo = [];
        try {
            $liveInfo['hid'] = $HouseInfo['hid'];
            $liveInfo['city'] = $HouseInfo['city'];
            $liveInfo['title'] = $livecontent['title'];
            $liveInfo['video_url'] = $livecontent['video_url'];
            $liveInfo['rtmp_url'] = $livecontent['rtmp_url'];
            $liveInfo['img'] = $livecontent['img'];
            $liveInfo['time'] = $livecontent['time'];
            $LiveModel = new Live();
            $LiveModel->load($liveInfo, '');
            $t = $LiveModel->save();
            if ($t != true) return '直播添加失败';
            $lid = $LiveModel->attributes['id'];
            $this->SubjoinInfo($operator, $livecontent, $lid, 'add');
            $Transaction->commit();
            return true;
        } catch (Exception $e) {
            $Transaction->rollBack();
            return '同步失败1';
        }
    }

    /**
     *同步修改
     * */
    public function LiveToneTwo($operator, $livecontent, $HouseInfo, $lid)
    {
        $Transaction = Yii::$app->db->beginTransaction();
        $liveInfo = [];
        $thisLiveInfo = null;
        try {
            $LiveModel = Live::findOne($lid);
            if (!empty($LiveModel)) {
                //更新之前先获取原数据
                $thisLiveInfo = $LiveModel->attributes;

                $LiveModel->hid = $HouseInfo['hid'];
                $LiveModel->city = $HouseInfo['city'];
                $LiveModel->title = $livecontent['title'];
                $LiveModel->video_url = $livecontent['video_url'];
                $LiveModel->rtmp_url = $livecontent['rtmp_url'];
                $LiveModel->img = $livecontent['img'];
                $LiveModel->time = $livecontent['time'];
                $t = $LiveModel->save();
                if ($t != true) return '直播更新失败';
                $mode = 'edit';
            } else {
                $liveInfo['hid'] = $HouseInfo['hid'];
                $liveInfo['city'] = $HouseInfo['city'];
                $liveInfo['title'] = $livecontent['title'];
                $liveInfo['video_url'] = $livecontent['video_url'];
                $liveInfo['rtmp_url'] = $livecontent['rtmp_url'];
                $liveInfo['img'] = $livecontent['img'];
                $liveInfo['time'] = $livecontent['time'];
                $LiveModel = new Live();
                $LiveModel->load($liveInfo, '');
                $t = $LiveModel->save();
                if ($t != true) return '直播更新失败';
                $lid = $LiveModel->attributes['id'];
                $mode = 'add';
            }
            $this->SubjoinInfo($operator, $livecontent, $lid, $mode, $thisLiveInfo);
            $Transaction->commit();
            return true;
        } catch (Exception $e) {
            $Transaction->rollBack();
            return '同步失败1';
        }
    }

    /**
     * 附加表信息
     * */
    public function SubjoinInfo($operator, $livecontent, $lid, $mode, $thisLiveInfo = null)
    {
        $syncliverecordInfo = [];
        $livesyncRecordInfo = [];
        $syncliverecordInfo['own_lid'] = $lid;//本地直播视频id
        $syncliverecordInfo['unid'] = $livecontent['uniqid'];
        $syncliverecordInfo['state'] = 1;
        $syncliverecordInfo['content'] = json_encode($livecontent);
        $syncliverecordInfo['name'] = $operator;
        $syncliverecordInfo['create_at'] = time();
        $syncliverecordInfo['ip'] = $ip = Yii::$app->request->userIP;
        if (!empty($thisLiveInfo)) {
            $syncliverecordInfo['edit_content'] = json_encode($thisLiveInfo['liveinfo']);
        }
        $SyncliverecordModel = new Syncliverecord();
        $SyncliverecordModel->load($syncliverecordInfo, '');
        $t = $SyncliverecordModel->save();
        if ($t != true) return 'Syncliverecord纪录添加失败';

        $livesyncRecordInfo['mode'] = $mode;
        $livesyncRecordInfo['operator'] = $operator;
        $livesyncRecordInfo['oss_live_name'] = $livecontent['title'];
        $livesyncRecordInfo['oss_unid'] = $livecontent['uniqid'];
        $livesyncRecordInfo['create_at'] = time();
        $livesyncRecordInfo['oss_live_data'] = json_encode($livecontent);
        if (!empty($thisLiveInfo)) {
            $livesyncRecordInfo['this_live_name'] = json_encode($thisLiveInfo['housename']);
            $livesyncRecordInfo['this_live_data'] = json_encode($thisLiveInfo['liveinfo']);
        }

        $LivesyncRecordModel = new LivesyncRecord();
        $LivesyncRecordModel->load($livesyncRecordInfo, '');
        $t = $LivesyncRecordModel->save();
        if ($t != true) return 'LivesyncRecord纪录添加失败';
    }

    public function getThisLiveInfo($lid)
    {
        $liveInfo = Live::find()->where(['id' => $lid])->asArray()->one();
        $housename = (\common\models\house::findOne($liveInfo['hid']))['name'];
        return ['liveinfo' => $liveInfo, 'housename' => $housename];
    }

    /**
     * 检测同步的直播楼盘是否存在
     * */
    public function LerifyHouse($livecontent)
    {
        $h_name = $livecontent['h_name'];
        $h_unid = $livecontent['h_unid'];
        $city_name = $livecontent['city_name'];
        $city_level = $livecontent['city_level'];
        $hosueInfo = (new Synchouserecord())->getLerifyHouse($h_unid);
        if (empty($hosueInfo)) {
            $hosueInfo = \common\models\House::find()->where(['name' => $h_name, 'del' => 1])->select('id as hid,city')->asArray()->one();
            if (empty($hosueInfo)) {
                return false;
            }
        }
        $cityInfo = \common\models\CategoryCity::find()->where(['city_name' => $city_name, 'del' => 1, 'level' => $city_level])->select('id')->one();
        if (empty($cityInfo)) {
            $city = $hosueInfo['city'];
        } else {
            $city = $cityInfo['id'];
        }
        return ['hid' => $hosueInfo['hid'], 'city' => $city];
    }
}