AreaGrid.php 6.7 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 管理端列表grid trait层:渲染生成 区域 grid 列表
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Admin\Grids\Area
 * @package   App\Admin\Grids\Area
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年1月27日11:18:22
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Admin\Grids\Area;

use App\Models\Area\Area;
use App\Models\Area\City;
use App\Models\Area\County;
use App\Models\Area\Group;
use App\Models\Area\Province;
use App\Models\Area\Township;
use App\Models\Area\Village;
use Encore\Admin\Grid;

/**
 * Class AreaGrid.
 *
 * @category  App\Admin\Grids\Area
 * @package   App\Admin\Grids\Area
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年1月27日11:18:22
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
trait AreaGrid
{
    /**
     * 设置默认查询条件
     * 增加数据权限的判断。步骤:
     * 1、判断用户是否是超级管理员,并判断用户的pid,如果pid为0 则为平台总用户,不进行数据权限的判断
     * 2、如果pid大于0 则为 平台子管理员,则需要进行数据权限的判断
     *
     * @return void
     */
    public function setGridQuery()
    {
        $with = [];
        switch ($this->type) {
            case Area::GROUP:
                $with[] = 'village';
            case Area::VILLAGE:
                $with[] = 'township';
            case Area::TOWNSHIP:
                $with[] = 'county';
            case Area::COUNTY:
                $with[] = 'city';
            case Area::CITY:
                $with[] = 'province';
                break;
        }
        if ($with) {
            $this->grid->model()->with($with);
        }
    }

    /**
     * 设置默认排序规则
     *
     */
    public function setGridOrder()
    {
        if (!request('_sort')) {
            switch ($this->type) {
                case Area::GROUP:
                    $this->grid->model()->orderBy('village_id');
                case Area::VILLAGE:
                    $this->grid->model()->orderBy('township_id');
                case Area::TOWNSHIP:
                    $this->grid->model()->orderBy('county_id');
                case Area::COUNTY:
                    $this->grid->model()->orderBy('city_id');
                case Area::CITY:
                    $this->grid->model()->orderBy('province_id');
                    break;
            }

            $this->grid->model()->orderBy('sort')->latest();
        }
    }

    /**
     * 为grid增加筛选条件
     *
     * @return \Encore\Admin\Grid
     */
    public function renderGridFilter()
    {
        $this->grid->selector(
            function (Grid\Tools\Selector $selector) {
                /* TODO 补充
                $selector->select(
                    'price',
                    $this->title.'价位',
                    \Config::get('constants.price_range_options'),
                    function ($query, $value) {
                        $query->whereBetween('price', explode('-', Arr::first($value)));
                    }
                );
                */
            }
        );

        $this->grid->filter(
            function ($filter) {
                // 筛选条件默认展开
                $filter->expand();

                if ($this->level > Area::LEVEL_PROVINCE) {
                    $filter->equal('province_id')->select(Province::getSelectOptions())->load("city_id", "/admin/area/province/select-options")->placeholder(__(Province::OBJ_NAME));
                }

                if ($this->level > Area::LEVEL_CITY) {
                    $filter->equal('city_id')->select([])->load("county_id", "/admin/area/city/select-options")->placeholder(__(City::OBJ_NAME));
                }

                if ($this->level > Area::LEVEL_COUNTY) {
                    $filter->equal('county_id')->select([])->load("township_id", "/admin/area/county/select-options")->placeholder(__(County::OBJ_NAME));
                }

                if ($this->level > Area::LEVEL_TOWNSHIP) {
                    $filter->equal('township_id')->select([])->load("village_id", "/admin/area/township/select-options")->placeholder(__(Township::OBJ_NAME));
                }

                if ($this->level > Area::LEVEL_VILLAGE) {
                    $filter->equal('village_id')->select([])->placeholder(__(Village::OBJ_NAME));
                }

                $filter->like('name', __($this->title). ' ' . strtolower(__('name')));

                //去掉默认的搜索
                $filter->disableIdFilter();
            }
        );

        return $this->grid;
    }

    /**
     * 渲染grid字段
     *
     * @return void
     */
    public function renderGridFields()
    {
        $this->gridRowNo();
//        $this->gridTextField($this->grid, 'admin.username', trans('admin.username'));
        if ($this->level > Area::LEVEL_PROVINCE) {
            $this->gridTextField($this->grid, 'province_id', __(Province::OBJ_NAME))->display(function () {
                return optional($this->province)->name;
            });
        }
        if ($this->level > Area::LEVEL_CITY) {
            $this->gridTextField($this->grid, 'city_id', __(City::OBJ_NAME))->display(function () {
                return optional($this->city)->name;
            });
        }

        if ($this->level > Area::LEVEL_COUNTY) {
            $this->gridTextField($this->grid, 'county_id', __(County::OBJ_NAME))->display(function () {
                return optional($this->county)->name;
            });
        }

        if ($this->level > Area::LEVEL_TOWNSHIP) {
            $this->gridTextField($this->grid, 'township_id', __(Township::OBJ_NAME))->display(function () {
                return optional($this->township)->name;
            });
        }

        if ($this->level > Area::LEVEL_VILLAGE) {
            $this->gridTextField($this->grid, 'village_id', __(Village::OBJ_NAME))->display(function () {
                return optional($this->village)->name;
            });
        }

        $this->gridTextField($this->grid, 'name',  __($this->title). ' ' . strtolower(__('name')))->sortable();
        $this->gridNumberField($this->grid, 'sort', __('sort'))->editable()->sortable();
//        $this->gridTextField($this->grid, 'phone', '办公电话');
//        $this->gridTextField($this->grid, 'address', $this->title.'地址')->sortable();
        $this->generateGridDateField($this->grid)->sortable();
    }
}