DeviceCatController.php 24.7 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 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
<?php

namespace app\ht\modules\device\controllers;

use Yii;
use yii\db\Exception;
use yii\base\ErrorException;
use yii\data\Pagination;
use app\ht\controllers\BaseController;
use common\components\adminlog\AdminLogs;
use common\helpers\ImageManager;
use domain\device\DeviceCatRepository;
use domain\device\models\DeviceCat  as DeviceCatModel;
use domain\device\models\Device     as DeviceModel;
use stdClass;
use function json_encode;
use function array_merge;
use function array_multisort;
use function count;
use function explode;
use function end;
use function max;

// 引入PHPExcel库(后续要统一修改为命名空间方式引用)

require_once Yii::getAlias('@exts').'/PHPExcel.php';
require_once Yii::getAlias('@exts').'/PHPExcel/Writer/Excel2007.php';
require_once Yii::getAlias('@exts').'/PHPExcel/Reader/Excel2007.php';
/*
require dirname(dirname(dirname(dirname(__FILE__)))).'/exts/PHPExcel.php';
require dirname(dirname(dirname(dirname(__FILE__)))).'/exts/PHPExcel/Writer/Excel2007.php';
require dirname(dirname(dirname(dirname(__FILE__)))).'/exts/PHPExcel/Reader/Excel2007.php';
*/

/**
 * 设备分类-控制器
 */
class DeviceCatController extends BaseController
{
    const CAT_TREE_DEFAULT_OPEN = 0;
    const VIRTUAL_ROOT_CAT_ID = 0; // 虚拟根分类ID

    /**
     * 首页
     */
    public function actionIndex()
    {
        // 搜索
        $parameter = new stdClass();

        // 获取数值
        $catId = (int)$this->request->get('cid');
        $parameter->catId = $catId;
        $parameter->headline = '根分类';
        $parameter->logoUrl = '';

        if ($cat = DeviceCatRepository::selectOne($catId)) {
            if ($cat->img_path) {
                $parameter->logoUrl = ImageManager::getUrl($cat->img_path);
            }
        }

        // 获取所有 < DeviceCatModel::CAT_TREE_MAX_LEVEL 的树
        $nodes = DeviceCatRepository::getCatsByWhere(['<', 'level', DeviceCatModel::CAT_TREE_MAX_LEVEL], false);

        /**
         * 树处理
         */
        //父ID
        $parentId = 0;
        //点击节点
        $selectedId = $catId;
        $treeArr = $this->buildTree($nodes, $parentId, $selectedId);
        $treeArr = [
            'id'        => "0",
            'text'      => '设备分类',
            'children'  => $treeArr,
            'state'     => [
                'opened' => true
            ]
        ];
        $catTree = json_encode($treeArr);

        $deviceList = array();
        $pages = null;
        if (!empty($cat['parent_id'])) {
            $map = array();
            if (!empty($cat['id'])) {
                $map[] = array("=","d.device_cat_id",$cat['id'],"I");
            }
            /**
             * 分页处理
             */
            $deviceModel = new DeviceModel();
            $pageSize = $this->request->get("pageSize") ? (int) $this->request->get("pageSize") : 20;
            $pages = new Pagination(['totalCount' => $deviceModel->getDeviceListCount($map), 'pageSize' => $pageSize]);
            $deviceList = $deviceModel->getDeviceList($pages->offset,$pages->limit,$map);
        }

        return $this->render("index",[
            'catTree'       => $catTree,
            'parameter'     => $parameter,
            'treeOptions'   => json_encode((array)$this->buildTreeOptions($nodes, 0)),
            'deviceList'    => $deviceList,
            'pages'         => $pages
        ]);
    }

    /**
     * 获取所有设备分类节点
     * 获取路径长度不能超过 CAT_TREE_MAX_LEVEL
     * @return array|\yii\db\ActiveRecord[]
     */
    private function getAllNode()
    {
        // 获取所有类目
        $nodes = DeviceCatRepository::getCatsByWhere(['<', 'level', DeviceCatModel::CAT_TREE_MAX_LEVEL], false);

        return $nodes;
    }

    /**
     * 返回指定ID的[子类目ID及本身ID]
     * @param $id
     * @return array
     * 由ID 组成的数组
     */
    private function getChildrenIds($treeDatas, $id)
    {
        $result = [];

        // 处理获取的数据
        $id = (int)$id;
        $treeDatas = (array)$treeDatas;

        // ID处理
        if (!$id) {
            return $result;
        }

        $result[] = $id;
        foreach ($treeDatas AS $i => $treeData) {
            if ($id == $treeData['id']) {
                // 删除已读取的数据
                unset($treeDatas[$i]);
                $result = array_merge($result, $this->getChildrenIds($treeDatas, $treeData['id']));
            }
        }

        return $result;
    }

    /**
     * 构造树数据
     * @param $catDatas 一维数组
     * @param int $parentId 父ID
     * @param int $checkboxId 默认选中
     * @return array 返回树状的数组
     */
    private function buildTree($catDatas, $parentId = 0, $selectedId = 0)
    {
        // 处理送过来的数据
        $catDatas = (array)$catDatas;
        $parentId = (int)$parentId;

        /**
         * 处理数据
         */
        $treeChilds = [];
        foreach ($catDatas AS $i => $catData) {
            if ($catData['parent_id'] == $parentId) {
                $child = [];
                $child['id'] = $catData['id'];
                $child['text'] = $catData['name'];
                $child['sort_order'] = $catData['sort_order'];

                // 默认打开
                if (self::CAT_TREE_DEFAULT_OPEN >= $catData['level']) {
                    // 默认选中
                    if ($catData['id'] == $selectedId) {
                        $child['state'] = ["opened" => true, "selected" => true];
                    } else {
                        $child['state'] = ["opened" => true ];
                    }
                } else {
                    // 默认选中
                    if ($catData['id'] == $selectedId) {
                        $child['state'] = ["selected" => true, "opened" => true];
                    }
                }

                // 删除已调用的数组
                unset($catDatas[$i]);

                // 回调数据
                $buildTree = $this->buildTree($catDatas, $catData['id'], $selectedId);
                $child['children'] = !empty($buildTree) ? $buildTree : false;

                $treeChilds[] = $child;
            }
        }

        // 排序
        $sortOrders = [];
        foreach ($treeChilds as $child) {
            $sortOrders[] = $child['sort_order'];
        }
        array_multisort($sortOrders, SORT_ASC, $treeChilds);

        return $treeChilds;
    }

    /**
     * 构造从类目 $deviceCatId 到一级类目的中文路径
     * @param $treeDates
     * @param $deviceCatId
     * @return string
     */
    private function buildPath($treeDates, $deviceCatId)
    {
        $returnPath = '';

        $treeDates = (array)$treeDates;
        $deviceCatId = (int)$deviceCatId;

        foreach ($treeDates as $treedate) {
            if ($deviceCatId == $treedate['id']) {
                if (0 != $treedate['parent_id']) {
                    $returnPath = $returnPath ? $returnPath . '/' . $treedate['name'] : $treedate['name'];
                    $returnCallback = $this->buildPath($treeDates, $treedate['parent_id']);
                    $returnPath = $returnCallback ? $returnCallback . '/' . $returnPath : $returnPath;
                }
                break;
            }
        }

        return $returnPath;
    }

    /**
     * 构造类目select显示数组
     * @param $treeDates
     * @param $parentId
     * @param array $returnOptions
     * @return array
     */
    private function buildTreeOptions($treeDates, $parentId, $returnOptions = [])
    {
        $treeDates = (array)$treeDates;
        $parentId = (int)$parentId;
        foreach ($treeDates as $treeDate) {
            if ($parentId == $treeDate['parent_id']) {
                if (0 != $treeDate['parent_id']) {
                    $returnOptions[] = [
                        'id'      => $treeDate['id'],
                        'catName' => $this->buildPath($treeDates, $treeDate['id'])
                    ];
                }

                $returnOptions = $this->buildTreeOptions($treeDates, $treeDate['id'], $returnOptions);
            }
        }

        return $returnOptions;
    }

    /**
     * ajax
     * 新增加类目
     */
    public function actionAjaxCreateNode()
    {
        // 初始化
        $result = new stdClass();
        $result->code = null;
        $result->success = false;
        $result->datas = [];

        // 父类ID
        $parentId = (int)$this->request->post('id');
        // 新建类名称
        $newName = (string)$this->request->post('text');
        // 父类下的原有所有子类目IDS,如为空 即新建类目sort_order = 1;
        $nodeSort = 1;
        $lastChildCat = DeviceCatModel::find()
            ->where(['parent_id' => $parentId])
            ->orderBy(['sort_order' => SORT_DESC])
            ->one();
        if ($lastChildCat) {
            $nodeSort = $lastChildCat->sort_order + 1;
        }

        // 父类目数据
        $parent = null;
        if ($parentId != 0) {
            $parent = DeviceCatRepository::selectOne($parentId);
            if (!$parent) {
                $result->code = 101; // 操作有误,请重新操作!
                return json_encode($result);
            }
        }

        // 新建
        $cat = new DeviceCatModel();
        $cat->parent_id = $parentId;
        $cat->name = $newName ? $newName : '';
        $cat->level = $parent ? ($parent->level + 1) : 1;
        $cat->path = $parent ? $parent->path : '';
        $cat->sort_order = $nodeSort;

        // 新建类目, 不能超过设定的数量
        if ($cat->level >= DeviceCatModel::CAT_TREE_MAX_LEVEL) {
            $result->code = 201;//新建类目,不能超于设定的长度
            return json_encode($result);
        }

        $tr = Yii::$app->db->beginTransaction();
        try {
            if ($cat->save()) {
                $saveCatId = $cat->attributes['id'];
                if ($parentId != 0) {  // 一级分类不需要更新路径
                    // 更新新建子类的路径
                    $cat = DeviceCatRepository::selectOne($saveCatId);
                    $cat->path = $parent ? $parent->path . ',' .$saveCatId : $saveCatId;
                    if ($cat->save()) {
                        $result->success = true;
                        $result->datas = $saveCatId;
                    } else {
                        throw new ErrorException('save create dir path fail.');
                    }
                }
            } else {
                throw new ErrorException('create dir fail.');
            }

            $tr->commit();

        } catch (Exception $e) {
            $tr->rollBack();
        }

        return json_encode($result);
    }

    /**
     * AJAX
     * 修改类目名字
     */
    public function actionAjaxRenameNode()
    {
        $result = new stdClass();
        $result->code = null;
        $result->success = false;
        $result->datas = [];

        // 类目ID
        $catId = (int)$this->request->post('id');
        // 更新后的类目名称, jsTree提交过来数据
        $newName = (string)$this->request->post('text');
        // 类目数据
        $cat = DeviceCatRepository::selectOne($catId);

        if (!$cat) {
            $result->code = 101;
            return json_encode($result);
        }

        $cat->name = $newName;
        if ($cat->save()) {
            $result->success = true;
        }

        return json_encode($result);
    }

    /**
     * ajax
     * 类目移动
     */
    public function actionAjaxMoveNode()
    {
        $result = new stdClass();
        $result->code = null;
        $result->success = false;
        $result->datas = [];

        // 获取数据
        $newParentId = (int)$this->request->post('newParentId');
        $newChildNodeIds = (array)$this->request->post('newChildNodeIds');
        $currentNodeId = (int)$this->request->post('currentNodeId');

        // 处理
        if (0 == $currentNodeId) {
            $result->code = 101; // 请选择操作的类目;
            return json_encode($result);
        }

        // 大于1000不能操作~~
        if (1000 < count($newChildNodeIds)) {
            $result->code = 201; // 操作类目的数量过多,请联系管理员;
            return json_encode($result);
        }

        if (0 == intval($newParentId)) {
            $order = 0;
            foreach ($newChildNodeIds as $i=>$newNodeId) {
                $parentCat = DeviceCatRepository::selectOne($newNodeId);
                $parentCat->sort_order = $order;
                $parentCat->save();
                $order++;
            }
        } else {
            // 开始排序的基数
            $orderBase = 0;
            // 新父节点数据
            $newParentCat = DeviceCatRepository::selectOne($newParentId);
            // 移动节点数据
            $nodeCat = DeviceCatRepository::selectOne($currentNodeId);
            if (!$nodeCat) {
                $result->code = 101; // 请选择操作的类目;
                return json_encode($result);
            }

            // 启动事务入库
            $tr = Yii::$app->db->beginTransaction();
            try {
                // 处理 新父类 下子节点的排序
                $order = $orderBase;
                $result->datas = $newChildNodeIds;
                foreach ($newChildNodeIds as $i=>$newNodeId) {
                    $cat = DeviceCatRepository::selectOne($newNodeId);
                    // 移动节点
                    if ($currentNodeId == $newNodeId) {
                        $cat->parent_id = $newParentCat ? $newParentCat->id : 0;
                        $cat->path = $newParentCat ? $newParentCat->path . ',' . $currentNodeId : $currentNodeId;
                        $cat->level = $newParentCat ? $newParentCat->level + 1 : 1;
                    }

                    $cat->sort_order = $order;
                    if (!$cat->save()) {
                        throw new Exception('save id='. $newNodeId . ' fail.');
                    }
                    $order++;
                }

                $tr->commit();
            } catch (Exception $e) {
                $tr->rollBack();
            }
        }

        $result->success = true;

        return json_encode($result);
    }

    /**
     * AJAX
     * 删除类目的显示
     */
    public function actionAjaxRemoveNode()
    {
        $result = new stdClass();
        $result->code = null;
        $result->success = false;
        $result->datas = [];

        $catIds = (array) $this->request->post('catIds');

        // 大于100不能操作~~
        if (100 < count($catIds)) {
            $result->code = 301; // 操作类目的数量过多,请联系管理员;
            return json_encode($result);
        }

        // 验证
        $children = DeviceCatRepository::getCatsByWhere(['parent_id' => $catIds], 100);
        if ($children) {
            $result->code = 101; // 类目还有子类目,不能进行删除操作
            return json_encode($result);
        }

        $cats = DeviceCatRepository::getCatsByIds($catIds);
        $cats = (array)$cats;
        $catResult = [];
        foreach ($cats AS $cat) {
            if (!empty($cat->devices)) {
                $result->code = 201; // 类目下还有设备, 不能进行删除操作
                return json_encode($result);
            }

            $catResult[] = [
                'id' => $cat->id
            ];
        }

        $result->datas = $catResult;
        $result->success = true;

        return json_encode($result);
    }

    /**
     * AJAX
     * 删除类目 删除
     */
    public function actionAjaxDoRemoveNode()
    {
        $result = new stdClass();
        $result->code = null;
        $result->datas = [];
        $result->success = false;

        $catIds = (array)$this->request->post('ids');

        // 大于100不能操作~~
        if (1000 < count($catIds)) {
            $result->code = 401; // 操作类目的数量过多,请联系管理员;
            return json_encode($result);
        }

        // 处理获取的数据
        $cats = DeviceCatRepository::getCatsByIds($catIds);
        if (!$cats) {
            $result->code = 101; // 操作有误,请重新操作
            return json_encode($result);
        }

        $cats = (array)$cats;
        foreach ($cats as $cat) {
            if ((DeviceCatModel::CAT_TREE_MAX_LEVEL > $cat->level)) { // 不是根目类目,也不是末位类目
                // 验证是不明是末位类目
                if (!$this->validateLastNode($cat->id)) {
                    $result->code = 301; // 该类目下还有子类目,请删除该类目的子类.
                    return json_encode($result);
                }
            }
        }

        // 删除类目
        //$all = DeviceCatModel::find()->where(['id' => $catIds])->all();
        foreach ($cats as $a) {
            $a->delete();
        }

        $result->datas = $catIds;
        $result->pid = $cats[0]->parent_id;
        $result->success = true;

        return json_encode($result);
    }

    /**
     * 判断是否叶子节点
     * @param $dirId
     * @return bool
     * 返回 TRUE 表示是末位 false表示不是
     */
    private function validateLastNode($catId)
    {
        //true 是末位 false 不是末位
        $result = false;

        // 处理获取数据
        $catId = (int)$catId;

        // 查询数据
        $cat = DeviceCatRepository::getSubCatsByParentId($catId);
        if (!$cat) {
            $result = true;
        }

        return $result;
    }

    /**
     * 三级联动回调
     */
    public function actionAjaxChildren($parentId)
    {
        $options = DeviceCatRepository::options($parentId);
        echo $this->renderPartial('options', [
            'options' => $options,
        ]);
    }

    /**
     * AJAX
     * 保存分类图标题
     */
    public function actionAjaxUploadLogo($cid)
    {
        $cat = DeviceCatRepository::selectOne($cid);

        if (empty($_FILES['logo']['name'])) {
            return json_encode([
                'success' => false
            ]);
        }

        $image = $_FILES['logo'];

        // 图片路径数据整理
        $tmp = explode('.', $image['name']);
        $suffix = end($tmp);
        $savePath =  ImageManager::getDeviceCatIconPath($cid, $suffix);
        ImageManager::add($image["tmp_name"], $savePath);
        $cat->img_path = $savePath;
        $cat->save();

        /**
         * 后台操作日志
         */
        AdminLogs::doImport('设备分类图片', 1);

        return json_encode([
            'success' => true,
        ]);
    }

    /**
     * 执行运营分类数据导入操作
     * @return string
     * @throws ErrorException
     * @throws yii\db\Exception
     */
    public function actionAjaxDoImport()
    {
        // 采用虚拟根目录方式
        $virtaulRootcat = new DeviceCatModel();
        $virtaulRootcat->id = $this::VIRTUAL_ROOT_CAT_ID;
        $virtaulRootcat->parent_id = -1;
        $virtaulRootcat->name = '设备分类';
        $virtaulRootcat->level = 0;
        $virtaulRootcat->sort_order = 0;
        $virtaulRootcat->path = '0';

        // 初始化
        $result = new stdClass();
        $result->code = null;
        $result->datas = [];
        $result->success = false;

        // 先清空运营分类表
        Yii::$app->db->createCommand()->truncateTable('device_cat')->execute();

        // 换成用户上传的Excel表文件路径
        $importFilePath = Yii::getAlias('@webroot') . '/tmp/device_cat.xlsx';;

        $objReader = null;
        $objPHPExcel = null;
        $currentSheet = null;

        try {
            $objReader = \PHPExcel_IOFactory::createReaderForFile($importFilePath);
            $objPHPExcel = $objReader->load($importFilePath);
            $objPHPExcel->setActiveSheetIndex(0);
            $currentSheet = $objPHPExcel->getSheet(0);//载入文件并获取第一个sheet
            $total_line = $currentSheet->getHighestRow();
            $total_column = $currentSheet->getHighestColumn();
        }
        catch (\Exception $e) {
            $result->code = 101; // Excel表格式错误;
            return json_encode($result);
        }

        // 当前一级父类ID
        $parentId1 = $this::VIRTUAL_ROOT_CAT_ID;
        // 当前二级父类ID
        $parentId2 = $this::VIRTUAL_ROOT_CAT_ID;

        // 事务
        $transaction = Yii::$app->db->beginTransaction();
        try {
            for ($row = 2; $row <= $total_line; $row++) {
                $data = array();
                for ($column = 'A'; $column <= $total_column; $column++) {
                    $cellValue = $currentSheet->getCell($column . $row)->getValue();
                    if (!$cellValue) {
                        continue; // 无效空单元格(因为分类表按照三级结构,出现占用多个单元格情况)
                    }

                    // 当前单元格内容值作为要创建的[分类名称]
                    $catName = $cellValue;
                    // 当前分类层级
                    $level = ord($column) - ord('A') + 1;
                    $createParentId = $this::VIRTUAL_ROOT_CAT_ID;

                    // 一级分类直接插入数据库, 并更新当前父类ID:
                    if ($level == 1) {
                        // 当前创建的是一级分类, 父类是[根分类]
                        $createParentId = $this::VIRTUAL_ROOT_CAT_ID;
                    } elseif ($level == 2) {
                        // 当前创建的是二级分类
                        $createParentId = $parentId1;
                    } elseif ($level == 3) {
                        // 当前创建的是三级分类
                        $createParentId = $parentId2;
                    }

                    // 父类目数据
                    $parent = null;
                    if ($level == 1) {
                        $parent = $virtaulRootcat;
                    } else {
                        $parent = DeviceCatRepository::selectOne($createParentId);
                        if (!$parent) {
                            $result->code = 201; // 找不到父级分类;
                            return json_encode($result);
                        }
                    }

                    // 父类下的原有所有子类目IDS,如为空 即新建类目sort_order = 1;
                    $nodes = DeviceCatRepository::findAll(['parent_id' => $createParentId]);
                    $sortOrderArray[0] = $parent['sort_order']; // 第一个元素是父类的排序ID
                    $index = 0;
                    foreach ($nodes AS $nodeData) {
                        $sortOrderArray[$index++] = $nodeData['sort_order'];
                    }

                    // 获取最后的一个ID
                    $sort_order = max($sortOrderArray);

                    unset($nodes);
                    unset($sortOrderArray);

                    // 新建类目的SORT_ORDER值
                    $nodeSort = $sort_order + 1;

                    // 新建
                    $cat = new DeviceCatModel();
                    $cat->parent_id = $parent->id;
                    $cat->name = $catName ? $catName : '';
                    $cat->level = (int)$parent->level + 1;
                    $cat->path = $parent->path;
                    $cat->sort_order = $nodeSort;

                    // 保存
                    if ($cat->save()) {
                        // 获取新记录的ID
                        $saveCatId = $cat->attributes['id'];

                        // 更新新建子类的路径
                        $cat = DeviceCatRepository::selectOne($saveCatId);
                        $cat->path = $parent->path . ',' . $saveCatId;
                        if ($cat->save()) {
                            // 添加成功, 把当前分类ID设置为父类ID, 用于创建它的子分类
                            if ($level == 1) {
                                $parentId1 = $saveCatId;
                            } elseif ($level == 2) {
                                $parentId2 = $saveCatId;
                            } elseif ($level == 3) {
                            }
                        } else {
                            throw new ErrorException('添加分类path路径失败!');
                        }
                    } else {
                        throw new ErrorException('添加分类失败!');
                    }

                    // 销毁变量
                    unset($cat, $parent);
                }
            }

            // 提交事务
            $transaction->commit();

        }
        catch (Exception $e)
        {
            // 回滚
            $transaction->rollBack();
            $result->code = 301; // 数据库写入失败;
            return json_encode($result);
        }

        $result->success = true;

        return json_encode($result);
    }
}