Category.php 2.8 KB
<?php

namespace app\admin\controller\workorder;

use app\common\controller\Backend;
use fast\Tree;

/**
 * 工单类别管理
 *
 * @icon fa fa-circle-o
 */
class Category extends Backend
{

    /**
     * Category模型对象
     * @var \app\admin\model\workorder\Category
     */
    protected $model        = null;
    protected $categorylist = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\workorder\Category;

        $tree = Tree::instance();
        $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
        $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');

        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            $custom   = $this->request->request('custom/a');
            $keyValue = $this->request->request('keyValue');

            //构造父类select列表选项数据
            $list = [];
            if ($keyValue !== null) {
                $keyValue = explode(',', $keyValue);
                foreach ($keyValue as $index => $item) {
                    foreach ($this->categorylist as $key => $value) {
                        if ($value['id'] == $item) {
                            $list[] = $value;
                        }
                    }
                }
            } else {
                foreach ($this->categorylist as $key => $value) {
                    if (isset($custom['pid']) && $custom['pid'] == 0) {
                        if ($value['pid'] == 0) {
                            $list[] = $value;
                        }
                    } else {
                        $we_ids = \think\Db::name('workorder_engineers')
                            ->field('title')
                            ->whereIn('id', $value['we_ids'])
                            ->select();

                        $value['we_ids_text'] = '';
                        foreach ($we_ids as $wkey => $wvalue) {
                            $value['we_ids_text'] .= $wvalue['title'] . ',';
                        }

                        $value['we_ids_text'] = trim($value['we_ids_text'], ',');
                        $list[]               = $value;
                    }
                }
            }

            $total  = count($list);
            $result = ["total" => $total, "rows" => $list];
            return json($result);
        }
        return $this->view->fetch();
    }
}