AreaTrait.php 8.3 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * trait :区域
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Models\Traits
 * @package   App\Models\Traits
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年1月29日10:59:31
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Models\Traits;

use App\Models\Admin\User;
use App\Models\Area\Building;
use App\Models\Area\Group;
use App\Models\Area\Room;
use App\Models\Area\Village;
use App\Models\Lock\Lock;
use App\Models\Lock\LockPassword;
use App\Models\Smoke\Smoke;
use App\Models\Smoke\SmokeReplaceRecord;
use App\Models\System\Area;
use App\Models\User\Tenant;
use Encore\Admin\Auth\Database\Administrator;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Arr;

/**
 * Trait AreaTrait
 *
 * @category  App\Models\Traits
 * @package   App\Models\Traits
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年1月29日10:59:31
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
trait AreaTrait
{
    /**
     * @return BelongsTo
     */
    public function village(): BelongsTo
    {
//        return $this->belongsTo(Area::class, 'village_id', 'id');
        return $this->belongsTo(Village::class, 'village_id', 'id');
    }

    /**
     * @return BelongsTo
     */
    public function group(): BelongsTo
    {
//        return $this->belongsTo(Area::class, 'group_id', 'id');
        return $this->belongsTo(Group::class, 'group_id', 'id');
    }

    /**
     * @return BelongsTo
     */
    public function building(): BelongsTo
    {
//        return $this->belongsTo(Area::class, 'building_id', 'id');
        return $this->belongsTo(Building::class, 'building_id', 'id');
    }

    /**
     * @return BelongsTo
     */
    public function room(): BelongsTo
    {
//        return $this->belongsTo(Area::class, 'room_id', 'id');
        return $this->belongsTo(Room::class, 'room_id', 'id');
    }

    /**
     * 根据 社区、小区、楼栋的id获取房间的信息
     * @param $id
     * @return
     */
    public function getHouseByAreaId($id)
    {
        $list = Area::where('level', Area::LEVEL_ROOM)->where(function ($query) use ($id) {
            return $query->where('path', 'LIKE', '%,'.$id.',%')->orWhere('id', $id);
        })->get(['id', 'pid', 'admin_id', 'title', 'level', 'phone', 'address']);

        return $list;
    }

    /**
     * @param $nodes
     * @param string $prefix
     * @return array
     */
    public function getAreaTrees($nodes = [], $prefix = '')
    {
        $options = [];
        if (empty($nodes)) {
            $nodes = $this->allNodes();
        }
        $space = "&nbsp;";
        $decoration = '┝';
        foreach ($nodes as $index => $node) {
            $title = $prefix. $space. $node->title;

            $childrenPrefix = str_replace($decoration, str_repeat($space, 6), $prefix).$decoration.str_replace([$decoration, $space], '', $prefix);

            $children = $this->getAreaTrees($node->allChildren, $childrenPrefix);

            $options[$node->id] = $title;

            if ($children) {
                $options += $children;
            }
        }

        return $options;
    }

    /**
     * 树形结构
     *
     * @return array
     */
    public function toTree($user_id = 0)
    {
        // add by Richer 于 2021年6月25日16:57:56 由于修改了数据库表结构,修改逻辑
//        $village_ids = [];
//        $group_ids = [];
//        $building_ids = [];
//        $room_ids = [];
//        if ($user_id) {
//           $user = User::find($user_id);
//            $village_ids = $user->village_ids;
//            $group_ids = $user->group_ids;
//            $building_ids = $user->building_ids;
//            $room_ids = $user->room_ids;
//        }
//
////        dump($village_ids);
//
//        // 获取全部的社区、村
//        $villages = Village::where('status', config('constants.STATUS_OK'))->get(['id','title']);
//        foreach ($villages as &$village) {
//            $village['field'] = 'village_ids[]';
//            $village['spread'] = true;
//            if ($village_ids && in_array($village->id, $village_ids)) {
////                $village['checked'] = true;
//            }
//
//            // 获取全部的小区
//            $groups = Group::where('village_id', $village->id)->where('status', config('constants.STATUS_OK'))->get(['id','title']);
//            foreach ($groups as &$group) {
//                $group['field'] = 'group_ids[]';
//                $group['spread'] = true;
//
//
//                // 获取全部的小区
//                $buildings = Building::where('group_id', $group->id)->where('status', config('constants.STATUS_OK'))->get(['id','title']);
//                foreach ($buildings as &$building) {
//                    $building['field'] = 'building_ids[]';
//                    $building['spread'] = true;
//
//                    // 获取全部的小区
//                    $rooms = Room::where('building_id', $building->id)->where('status', config('constants.STATUS_OK'))->get(['id','title']);
//                    foreach ($rooms as &$room) {
//                        $room['field'] = 'room_ids[]';
//                        $room['spread'] = true;
//                        $room['checked'] = true;
//
//                    }
//
//                    $building['children'] = $rooms->toArray();
//                }
//                $group['children'] = $buildings->toArray();
//            }
//            $village['children'] = $groups->toArray();
//        }
//
////        dd($villages->toArray());
//
//        return $villages->toArray();
        $branch = [];

        $nodes = $this->allNodes();

        foreach ($nodes as $node) {
            $data = [
                'title' => Arr::get($node, 'title'),
                'id' => Arr::get($node, 'id'),
                'spread' => true,
                'field' => 'toAreas[]'

            ];
            $children = Arr::get($node, 'all_children');
            if ($children) {
                $data['children'] = $this->buildNestedArray($children);
            }

            $branch[] = $data;
        }

        return $branch;
    }

    /**
     * @param array $children
     * @return array
     */
    protected function buildNestedArray(array $children = [])
    {
        $data = [];
        foreach ($children as $child) {
            $node = [
                'title' => Arr::get($child, 'title'),
                'id' => Arr::get($child, 'id'),
                'spread' => true,
                'field' => 'toAreas[]',
            ];
            $children = Arr::get($child, 'all_children');
            if ($children) {
                $node['children'] = $this->buildNestedArray($children);
            }

            $data[] = $node;
        }

        return $data;
    }

    /**
     * @return mixed
     */
    public function allNodes()
    {
        return Area::where('pid', 0)->with('allChildren')->get()->toArray();
    }

    /**
     * 删除关联数据
     *
     * @param $model
     */
    public static function deleteArea($model)
    {
        // 删除管理账号
        $model->admin()->delete();

        // 删除关联的层级
        $admin_ids = Area::where('path', 'LIKE', '%,'.$model->id.',%')->all()->pluck('admin_id');
        Area::where('path', 'LIKE', '%,'.$model->id.',%')->delete();

        // 删除关联层级管理账号
        if ($admin_ids) {
            Administrator::whereIn('id', $admin_ids)->delete();
        }

        // 所有房间
        $room_ids = self::getHouseByAreaId($model->id)->map(function ($item) {
            return $item->id;
        })->all();
        if ($room_ids) {
            // 删除设备
            Lock::whereIn('room_id', $room_ids)->delete();
            LockPassword::whereIn('room_id', $room_ids)->delete();
            Smoke::whereIn('room_id', $room_ids)->delete();
            SmokeReplaceRecord::whereIn('room_id', $room_ids)->delete();

            // 删除租客
            Tenant::whereIn('room_id', $room_ids)->delete();

            //
        }
    }
}