MainService.class.php 26.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
<?php
/**
 * 直播主表
 * @author: houyingcai
 * @email:    594609175@qq.com
 * @date :  2018-01-08 16:04:39
 * @version $Id$
 */

namespace Common\Service;

use Com\LiveSDK;
use Common\Common\Constant;
use Common\Common\Order;
use Common\Model\CronModel;
use Common\Model\MainModel;
use Common\Model\RangeModel;
use Common\Model\RoleModel;
use Common\Model\StudioModel;
use VcySDK\Cron;
use VcySDK\FileConvert;
use VcySDK\Service;

class MainService extends AbstractService
{
    /**
     * 角色表 Model
     * @var null
     */
    protected $roleModel = null;

    /**
     * 目标范围表 Model
     * @var null
     */
    protected $rangeModel = null;

    // 构造方法
    public function __construct()
    {
        $this->_d = new MainModel();
        $this->rangeModel = new RangeModel();
        $this->roleModel = new RoleModel();

        parent::__construct();
    }

    /**
     * 新建直播活动
     * @param $data
     * @return \Think\mixed
     */
    public function newLiveActivity($data)
    {
        try {
            // 添加主表数据
            $mainInsertData = array_intersect_key($data, array_fill_keys([
                'name',
                'start_time',
                'end_time',
                'estimated_duration',
                'desc',
                'pic',
                'notice_posting',
                'notice_will_start',
                'notice_start',
                'recomend',
                'live_status',
                'range_total'
            ], ''));
            $mainInsertData['updated'] = MILLI_TIME;
            $lmId = $this->_d->insert($mainInsertData);

            // 如果是新装应用,初始化默认讲师信息
            if ($data['type'] == Constant::LIVE_ROLE_TYPE_DEFAULT) {
                $data['lecturer'] = '员圈小助手';
            }
            // 添加直播角色表数据
            $this->roleModel->insert([
                'lm_id' => $lmId,
                'obj_id' => $data['lecturer'],
                'type' => $data['type'],
                'title' => $data['lecturer_title'],
                'desc' => $data['lecturer_desc'],
                'password' => $data['lecturer_pwd'],
            ]);

            // 发布时推送消息提醒
            if ($data['live_status'] == Constant::LIVE_STATUS_PUBLISHED) {
                // 发送消息通知
                $data['lm_id'] = $lmId;
                $data['uids'] = $data['lecturer'];
                $this->sendMsg($data, $this::MSG_LECTURER_APPOINTMENT);
            }

            // 添加目标范围对象表数据
            $this->insertRangeList($data, $lmId);

        } catch (\Exception $e) {

            \Think\Log::record('写入出错:::' . $e->getMessage());
            E($e->getMessage());
        }

        return $lmId;
    }

    /**
     * 更新直播活动
     * @param $data
     * @return array
     */
    public function updateLiveActivity($data)
    {
        // 获取直播活动详情
        $liveDetail = $this->_d->get($data['lm_id']);
        if (empty($liveDetail)) {
            E('_ERR_LIVE_IS_NOT_EXIST');
        }

        try {
            /**
             * 业务逻辑
             * (1)草稿:快速发布(检验开始时间)/编辑(无限制)/删除(二次确认);
             * (2)未开始:编辑(无限制)/删除(二次确认);
             * (3)进行中:编辑(开始时间/参与范围/讲师/口令 不允许修改)/删除(二次确认);
             */
            switch ($liveDetail['live_status']) {
                // 草稿
                case Constant::LIVE_STATUS_DEAFT:
                    // 判断开始时间
                    if ($data['start_time'] <= MILLI_TIME) {
                        E(L('_ERR_PARAMS_NON_COMPLIANT', ['name' => '开始时间', 'why' => '不得早于当前时间']));
                    }

                    // 更新活动数据
                    $this->updateLiveActivityData($data, $liveDetail);

                    break;
                // 已发布
                case Constant::LIVE_STATUS_PUBLISHED:
                    // 不可逆判断 (发布变为草稿)
                    if ($data['live_status'] == Constant::LIVE_STATUS_DEAFT) {
                        E(L('_ERR_LIVE_STATUS', ['status' => '发布']));
                    }

                    // 未开始
                    if ($liveDetail['start_time'] >= MILLI_TIME) {
                        // 判断开始时间
                        if ($data['start_time'] <= MILLI_TIME) {
                            E(L('_ERR_PARAMS_NON_COMPLIANT', ['name' => '开始时间', 'why' => '不得早于当前时间']));
                        }

                        $this->updateLiveActivityData($data, $liveDetail);
                    } else {
                        // 进行中
                        $roleDetail = $this->roleModel->get_by_conds(['lm_id' => $data['lm_id']]);

                        // 直播已经开始,不能编辑开始时间、参与范围、讲师及直播口令
                        if ($data['start_time'] != $liveDetail['start_time'] ||
                            $data['lecturer'] != $roleDetail['obj_id'] ||
                            $data['lecturer_pwd'] != $roleDetail['password'] ||
                            $this->checkRightChange($data)
                        ) {
                            E('_ERR_LIVE_IS_STARTED_NOT_EDIT');
                        }

                        // 更新主表数据
                        $this->updateModelDiffData($data, $liveDetail, [
                            'name',
                            'estimated_duration',
                            'desc',
                            'pic',
                            'notice_posting',
                            'notice_will_start',
                            'notice_start',
                            'recomend',
                        ], $this->_d, $data['lm_id']);

                        // 更新直播角色表数据
                        $this->updateModelDiffData([
                            'title' => $data['lecturer_title'],
                            'desc' => $data['lecturer_desc'],
                        ], $liveDetail, [
                            'title',
                            'desc',
                        ], $this->roleModel, $roleDetail['lr_id']);
                    }
                    break;
                // 已结束
                case Constant::LIVE_STATUS_OVER:
                    E(L('_ERR_LIVE_STATUS', ['status' => '结束']));
                    break;
            }

            // 清空原目标分为对象表数据
            $this->rangeModel->delete_by_conds(['lm_id'=>$data['lm_id']]);

            // 添加目标范围对象表数据
            $this->insertRangeList($data, $data['lm_id']);

        } catch (\Exception $e) {
            \Think\Log::record('更新错误:::' . $e->getMessage());
            E($e->getCode() . ':' . $e->getMessage());
        }

        return $liveDetail;
    }

    /**
     * 直播状态转化函数
     * @author houyingcai
     * @param string $live_status 直播状态
     * @param string $begin_time 开始时间
     * @return int 直播状态 1:草稿,2:未开始,3:进行中,4:已结束
     */
    public function live_status($live_status = '0', $begin_time = '0')
    {
        $status = 0;

        if (Constant::LIVE_STATUS_DEAFT == $live_status) {
            // 草稿
            $status = Constant::LIVE_WEB_STATUS_DRAFT;
        } elseif (Constant::LIVE_STATUS_OVER == $live_status) {
            // 已终止
            $status = Constant::LIVE_WEB_STATUS_END;
        } else {
            // 开始时间允许提前准备
            // $begin_time -= Constant::LIVE_ALLOW_EARLY_START_TIME;
            // 已发布
            if ($begin_time > MILLI_TIME) {
                // 未开始
                $status = Constant::LIVE_WEB_STATUS_NOT_START;
            } elseif ($begin_time <= MILLI_TIME) {
                // 进行中
                $status = Constant::LIVE_WEB_STATUS_ING;
            }
        }

        return $status;
    }


    /**
     * 发送消息
     * @param array $data   直播详情数据
     * @param array $range  直播权限范围
     * @return bool
     */
    public function pushMsg($data, $range)
    {
        // 写入消息记录
        $insertMsgLogData = [];
        // 全公司
        if (Constant::RANGE_IS_ALL_TRUE == $range['is_all']) {
            $insertMsgLogData[] = [
                'lm_id' => $data['lm_id'],
                'obj_id' => Constant::RANGE_TYPE_ALL,
                'type' => Constant::TYPE_LIVE_PUSH
            ];
        } else {
            // 获取权限范围内人员UID列表
            $rangeServ = new RangeService();
            list(, , $uids) = $rangeServ->getWatchDataUids($data['lm_id']);

            // 组装发消息记录数组
            $this->dealWithMsgLogInserData(
                $data['lm_id'],
                $uids,
                $insertMsgLogData
            );
        }

        // 消息记录入库
        $msgLogServ = new MsgLogService();
        $msgLogServ->insert_all($insertMsgLogData);

        // 组装发送消息数据
        $msgParams = [
            'lm_id' => $data['lm_id'],
            'subject' => $data['name'],
            'start_time' => $data['start_time'],
            'estimated_duration' => $data['estimated_duration'],
            'desc' => $data['desc'],
        ];

        $msgParams = array_merge($msgParams, $range);

        $this->sendMsg($msgParams, RangeService::MSG_LIVE_PUBLISH);

        return true;
    }

    /**
     * 开启或者关闭 推荐
     * @param array $data   直播详情数组
     * @param array $range  权限范围
     * @param bool  $isOpen 是否开启推荐
     */
    public function openOrCloseRecommender($data, $range = [], $isOpen = true)
    {
        if ($isOpen) {

            $rpcUrl = rpcUrl('/Public/Rpc/Recommender/ArticleUpdate');
            $rpcParams = [
                'app' => APP_DIR,
                'dataCategoryId' => '',
                'dataId' => $data['lm_id'],
                'title' => $data['name'],
                'summary' => $data['desc'],
                'attachId' => $data['pic'],
                'pic' => $this->formatCover($data['pic']),
                'url' => 'Live/Frontend/Index/Recommend?lm_id=' . $data['lm_id'],
                'endTime' => $data['estimated_duration'],
                'dateline' => $data['start_time'],
            ];
            // 推荐权限范围
            $feed_right = [];
            // 非全公司
            if (Constant::RANGE_IS_ALL_TRUE != $range['is_all']) {

                $feed_right['users'] = $range['uids'];
                $feed_right['departments'] = $range['dp_ids'];
                $feed_right['jobs'] = $range['job_ids'];
                $feed_right['roles'] = $range['role_ids'];
                $feed_right['tags'] = $range['tag_ids'];
            }

            $rpcParams['right'] = $feed_right;

            \Com\Rpc::phprpc($rpcUrl)->invoke('Index', $rpcParams);

        } else {

            \Com\Rpc::phprpc(rpcUrl('/Public/Rpc/Recommender/ArticleDelete'))->invoke('Index', [
                'app' => APP_DIR,
                'dataCategoryId' => '',
                'dataId' => $data['lm_id'],
            ]);
        }
    }

    /**
     * 处理消息发送记录数据
     * @param int $lmId 直播ID
     * @param array $objIds 人员UID列表
     * @param $insertMsgLogData
     */
    private function dealWithMsgLogInserData($lmId, $objIds, &$insertMsgLogData)
    {
        foreach (array_chunk($objIds, Constant::OBJ_ID_SINGLE_TOTAL) as $item) {
            $insertMsgLogData[] = [
                'lm_id' => $lmId,
                'obj_id' => implode(',', $item),
                'type' => Constant::TYPE_LIVE_PUSH
            ];
        }
    }

    /**
     * 写入直播对象范围数据
     * @param $data
     * @param $lmId
     * @return bool
     */
    private function insertRangeList($data, $lmId)
    {
        if (!isset($data['range']) || empty($data['range'])) {
            E(L('_ERR_PARAMS_CAN_NOT_EMPTY', ['name' => '目标对象']));
        }

        $rangeInsertData = [];
        // 对象: 所有
        if (isset($data['range']['is_all']) && ($data['range']['is_all'] == Constant::RANGE_IS_ALL_TRUE)) {
            $rangeInsertData[] = [
                'lm_id' => $lmId,
                'obj_id' => Constant::RANGE_TYPE_ALL,
                'type' => Constant::RANGE_TYPE_ALL,
            ];
        } else {
            // 对象范围种类
            $rangeType = [
                'uids' => Constant::RANGE_TYPE_USER,
                'dp_ids' => Constant::RANGE_TYPE_DEPARTMENT,
                'job_ids' => Constant::RANGE_TYPE_JOB,
                'role_ids' => Constant::RANGE_TYPE_ROLE,
                'tag_ids' => Constant::RANGE_TYPE_TAG,
            ];
            foreach ($rangeType as $type => $typeNumber) {
                foreach ($data['range'][$type] as $id) {
                    if (empty($id)) {
                        continue;
                    }

                    $rangeInsertData[] = [
                        'lm_id' => $lmId,
                        'obj_id' => $id,
                        'type' => $typeNumber,
                    ];
                }
            }
        }

        if (empty($rangeInsertData)) {
            E(L('_ERR_PARAMS_CAN_NOT_EMPTY', ['name' => '目标对象']));
        }
        $this->rangeModel->insert_all($rangeInsertData);

        return true;
    }

    /**
     * 无限制更新直播活动数据 (草稿、 未开始 状态)
     * @param $data
     * @param $liveDetail
     * @return bool
     */
    private function updateLiveActivityData($data, $liveDetail)
    {
        $roleDetail = $this->roleModel->get_by_conds(['lm_id' => $data['lm_id']]);
        $rangeList = $this->rangeModel->list_by_conds(['lm_id' => $data['lm_id']]);

        // 更新主表数据
        $this->updateModelDiffData($data, $liveDetail, [
            'name',
            'start_time',
            'estimated_duration',
            'desc',
            'pic',
            'notice_posting',
            'notice_will_start',
            'notice_start',
            'recomend',
            'live_status',
            'range_total'
        ], $this->_d, $data['lm_id']);

        // 更新直播角色表数据
        $lecturerDiffData = $this->updateModelDiffData([
            'obj_id' => $data['lecturer'],
            'title' => $data['lecturer_title'],
            'desc' => $data['lecturer_desc'],
            'password' => $data['lecturer_pwd'],
        ], $liveDetail, [
            'obj_id',
            'title',
            'desc',
            'password',
        ], $this->roleModel, $roleDetail['lr_id']);
        // 讲师变更了 发送消息提醒
        if (isset($lecturerDiffData['obj_id'])) {
            $this->sendMsg($data, $this::MSG_LECTURER_APPOINTMENT);
        }

        // 更新直播对象数据
        // 如果之前有全公司了
        if (in_array(Constant::RANGE_TYPE_ALL, array_column($rangeList, 'type'))) {
            // 提交上来的没有全公司
            if (isset($data['range']['is_all']) && ($data['range']['is_all'] == Constant::RANGE_IS_ALL_FALSE)) {
                // 删除全公司数据
                $this->rangeModel->delete_by_conds(['lm_id' => $data['lm_id']]);
                // 写入直播对象数据
                $this->insertRangeList($data, $data['lm_id']);
            }
        } else {
            // 提交上来是全公司
            if (isset($data['range']['is_all']) && ($data['range']['is_all'] == Constant::RANGE_IS_ALL_TRUE)) {
                // 删除之前的范围对象数据
                $this->rangeModel->delete_by_conds(['lm_id' => $data['lm_id']]);
                // 写入直播对象数据
                $this->insertRangeList($data, $data['lm_id']);
                // 之前不是全公司并且 提交的也不是全公司
            } else {
                // 按照类型 分类数据库数据
                $dbRole = [
                    Constant::RANGE_TYPE_USER => [],
                    Constant::RANGE_TYPE_DEPARTMENT => [],
                    Constant::RANGE_TYPE_JOB => [],
                    Constant::RANGE_TYPE_ROLE => [],
                    Constant::RANGE_TYPE_TAG => []
                ];
                foreach ($rangeList as $item) {
                    $dbRole[$item['type']][] = $item['obj_id'];
                }
                // (提交上来)删除的数据
                $deleteRangeData = [];
                // (提交上来)新增的数据
                $insertRangeData = [
                    'range' => [
                        'uids' => [],
                        'dp_ids' => [],
                        'job_ids' => [],
                        'role_ids' => [],
                        'tag_ids' => []
                    ],
                ];
                $rangeFrontendField = [
                    Constant::RANGE_TYPE_USER => 'uids',
                    Constant::RANGE_TYPE_DEPARTMENT => 'dp_ids',
                    Constant::RANGE_TYPE_JOB => 'job_ids',
                    Constant::RANGE_TYPE_ROLE => 'role_ids',
                    Constant::RANGE_TYPE_TAG => 'tag_ids'
                ];
                $insertRangeDataAll = [];
                foreach ($dbRole as $type => $item) {
                    // 数据库类型转换为字符串实际名称
                    $frontendFile = $rangeFrontendField[$type];

                    // 要删除的数据
                    $deleteRangeData = array_merge(
                        array_diff($item, $data['range'][$frontendFile] ?: []),
                        $deleteRangeData ?: []
                    );

                    // 要新增的数据
                    $insertRangeData['range'][$frontendFile] = array_diff(
                        $data['range'][$frontendFile],
                        $dbRole[$type]
                    );
                    // 所有新增数据
                    $insertRangeDataAll = array_merge($insertRangeDataAll, $insertRangeData['range'][$frontendFile]);
                }

                if (!empty($insertRangeDataAll)) {
                    $this->insertRangeList($insertRangeData, $data['lm_id']);
                }
                if (!empty($deleteRangeData)) {
                    $this->rangeModel->delete_by_conds([
                        'obj_id' => $deleteRangeData,
                        'lm_id' => $data['lm_id'],
                    ]);
                }
            }
        }

        return true;
    }

    /**
     * 更新表 需要更新的字段数据
     * @param $updateData
     * @param $originalData
     * @param $field
     * @param $model
     * @param $primaryValue
     * @return array
     */
    private function updateModelDiffData($updateData, $originalData, $field, $model, $primaryValue)
    {
        $updateMainData = array_diff_assoc_key($updateData, $originalData, $field);

        if (!empty($updateMainData)) {
            $model->update($primaryValue, $updateMainData);
            return $updateMainData;
        }

        return [];
    }

    /**
     * 结束直播
     * @param array $mainDetail 直播详情
     * @param array $studioDetail 直播室详情
     * @return bool
     */
    public function endLive($mainDetail, $studioDetail)
    {
        // 获取当前企业剩余并发人数
        $orderUtil = new Order();
        $liveOrder = $orderUtil->getLiveCastBasic();
        $surplusNum = $liveOrder['liveTotal'] - $liveOrder['liveExpend'];

        // 直播结束
        $mainServ = new MainService();
        $mainServ->update_by_conds(['lm_id' => $mainDetail['lm_id']], [
            'live_status' => Constant::LIVE_STATUS_OVER,
            'end_time' => MILLI_TIME,
            // 结束时企业还剩余并发人数
            'end_concurrent_member_number' => $surplusNum,
        ], true);

        // 禁止推流
        $liveSdk = new LiveSDK();
        $studioServ = new StudioService();
        $liveSdk->channelSetStatus($studioDetail['stream_id'], $liveSdk::CHANNEL_STATUS_DISABLED);
        $studioServ->update_by_conds(
            ['lm_id' => $mainDetail['lm_id']],
            ['stream_status' => Constant::STREAM_TYPE_STATUS_DISABLED],
            true
        );

        // 给 UC 记录视频占用的空间
        $fileServ = new StudioFileService();
        $fileList = $fileServ->list_by_conds(['lm_id' => 4], null, [], 'file_size,file_id,created');
        if (!empty($fileList)) {
            // 因为自动拼接会在同一个 fileid 上并且最后一个最大 且一个直播活动会有多个 fileid 的情况
            $fileSize = [];
            $fileSdk = new FileConvert(Service::instance());
            foreach ($fileList as $item) {
                if (!isset($fileSize[$item['file_id']])) {
                    $fileSize[$item['file_id']] = $item['file_size'];
                } elseif ($item['file_size'] > $fileSize[$item['file_id']]) {
                    $fileSize[$item['file_id']] = $item['file_size'];
                }

                $fileSdk->videoCreate([
                    'qtnBusinessId' => $mainDetail['lm_id'],
                    'qtnChannel' => 2,
                    'qtnType' => 2,
                    'fileSize' => $fileSize[$item['file_id']],
                    'qtnFileId' => $item['file_id']
                ]);
            }
        }
        
        // 删除计划任务
        $cronServ = new CronService();
        $cronSdk = new Cron(Service::instance());
        $cronList = $cronServ->list_by_conds(['lm_id' => $mainDetail['lm_id']], [], true);
        foreach ($cronList as $cron) {
            // 不需要UC删除的计划任务
            if (in_array($cron['type'], Constant::CRON_TYPE_NOT_DELETE)) {

                continue;
            }
            // 删除计划任务
            try {
                $cronSdk->delete($cron['cron_id']);
            } catch (\Exception $e) {
                // 不处理
            }
        }

        // 让 UC Job 去推送 拉取历史消息
        $insertData = [];
        $cronResult = $cronSdk->add([
            'crRemark' => 'live_chat_room_for_historical_message',
            'crType' => Cron::CR_TYPE_CALLBACK,
            'crParams' => json_encode([
                'timeRangeStart' => $mainDetail['start_time'] - 60 * 60 * 1000,
                // 这里加一小时 因为腾讯云那里处理历史记录是小时整点后完毕 并且需要两次 Job 回调去拉最后一小时内的
                'timeRangeEnd' => MILLI_TIME + 60 * 60 * 1000
            ]),
            'crMethod' => 'POST',
            // 回调地址(去企业标识)
            'crReqUrl' => oaUrl('Frontend/Callback/ImMsgHistory/Index', [], 'comm'),
            'crTimes' => 1,
            // 两小时后获取历史记录
            'crCron' => rgmdate(NOW_TIME + 2 * 60 * 60, 's i G j n ? Y'),
            'crMonitorExecution' => $cronSdk::CR_MONITOR_EXECUTION_FALSE,
            'crDescription' => '直播结束后拉取聊天记录',
        ]);
        $insertData[] = [
            'lm_id' => $mainDetail['lm_id'],
            'cron_id' => $cronResult['crId'],
            'type' => CronModel::TYPE_LIVE_IM_ROOM_MESSAGE,
            'domain' => $mainDetail['domain']
        ];

        // 直播结束后 进行回放视频转码
        $cronResult = $cronSdk->add([
            'crRemark' => 'live_video_playback_transcoding',
            'crType' => Cron::CR_TYPE_CALLBACK,
            'crParams' => json_encode([
                'lm_id' => $mainDetail['lm_id']
            ]),
            'crMethod' => 'POST',
            // 回调地址
            'crReqUrl' => oaUrl('Frontend/Callback/Transcoding/Index', [], $mainDetail['domain']),
            'crTimes' => 1,
            // 给腾讯云 15分钟 文件落地时间
            'crCron' => rgmdate(NOW_TIME + 15 * 60, 's i G j n ? Y'),
            'crMonitorExecution' => $cronSdk::CR_MONITOR_EXECUTION_FALSE,
            'crDescription' => '直播结束后回放视频转码',
        ]);
        $insertData[] = [
            'lm_id' => $mainDetail['lm_id'],
            'cron_id' => $cronResult['crId'],
            'type' => CronModel::TYPE_LIVE_TRANSCODING,
            'domain' => $mainDetail['domain']
        ];
        // 记录计划任务
        $cronServ->insert_all($insertData);

        return true;
    }

    /**
     * 检查权限数据是否有变更
     * @author tangxingguo
     * @param string $data 用户提交的数据
     * @return bool
     */
    public function checkRightChange($data)
    {
        // 数据库内权限数据
        $rangeList = $this->rangeModel->list_by_conds(['lm_id' => $data['lm_id']]);

        // 提交上来的是全公司
        if (isset($data['range']['is_all']) && ($data['range']['is_all'] == Constant::RANGE_IS_ALL_TRUE)) {
            // 数据库不是全公司
            if (!in_array(Constant::RANGE_TYPE_ALL, array_column($rangeList, 'type'))) {
                return true;
            }
        }

        // 数据库是全公司
        if (in_array(Constant::RANGE_TYPE_ALL, array_column($rangeList, 'type'))) {
            // 提交上来的不是全公司
            if (!isset($data['range']['is_all']) || ($data['range']['is_all'] == Constant::RANGE_IS_ALL_FALSE)) {
                return true;
            }
        } else {
            // 数据库权限数据
            $dbRights = [];
            // 按照类型 分类数据库数据
            $rules = [
                Constant::RANGE_TYPE_USER => 'uids',
                Constant::RANGE_TYPE_DEPARTMENT => 'dp_ids',
                Constant::RANGE_TYPE_JOB => 'job_ids',
                Constant::RANGE_TYPE_ROLE => 'role_ids',
                Constant::RANGE_TYPE_TAG => 'tag_ids'
            ];
            foreach ($rangeList as $item) {
                $dbRights[$rules[$item['type']]][] = $item['obj_id'];
            }

            // 移除全公司类型字段
            if (isset($data['range']['is_all'])) {
                unset($data['range']['is_all']);
            }

            // 对比权限类型是否相同
            $dbRules = array_keys($dbRights);
            $postRules = array_keys($data['range']);
            if (count($postRules) != count($dbRules) ||
                count(array_intersect($dbRules, $postRules)) != count($dbRules)) {
                return true;
            }

            // 对比详细数据
            foreach ($dbRights as $rule => $right) {
                if (count($right) != count($data['range'][$rule]) ||
                    count(array_intersect($right, $data['range'][$rule])) != count($right)) {
                    return true;
                }
            }
        }

        return false;
    }
}