HomeService.php 4.9 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 逻辑层:租客 服务类,处理业务逻辑
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Services
 * @package   App\Services
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年01月28日 16:14:07
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Services;

use App\Models\Lock\Lock;
use App\Models\Smoke\Smoke;
use App\Models\Traits\AdminUserTrait;
use App\Models\User\Tenant;
use App\Models\Warning;

/**
 * Class HomeService.
 *
 * @category  App\Services
 * @package   App\Services
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年01月28日 16:14:07
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class HomeService extends BaseService
{
    /**
     * HomeService constructor.
     */
    public function __construct()
    {
         // 执行父类构造方法
        parent::__construct();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return bool|\Illuminate\Http\Response
     */
    public function home()
    {
        $user = $this->getLoginUser();
        // 管理端用户
        if (is_admin()) {
            // 用户的角色和管理的地区
            $room_ids = $this->getAdminUserRooms($user);
            $query_smoke = Smoke::whereIn('room_id', $room_ids);
            $query_lock = Lock::whereIn('room_id', $room_ids);
            $query_warning = Warning::whereIn('room_id', $room_ids);

            // 1、本区总数据
            // 租客总量
            $tenant_count = Tenant::whereIn('room_id', $room_ids)->count();
            // 智能门锁:正常
            $lock_normal_count = Lock::whereIn('room_id', $room_ids)->where('status', config('constants.STATUS_OK'))->count();
            // 智能门锁:异常
            $lock_abnormal_count = Lock::whereIn('room_id', $room_ids)->where('status', '<>', config('constants.STATUS_OK'))->count();
            // 智慧烟感:正常
            $smoke_normal_count = Smoke::whereIn('room_id', $room_ids)->where('status', config('constants.STATUS_OK'))->count();
            // 智慧烟感:异常
            $smoke_abnormal_count = Smoke::whereIn('room_id', $room_ids)->where('status', '<>', config('constants.STATUS_OK'))->count();

            // 2、本日总数据
            // 智慧烟感报警次数:
            $warnings_today_count = Warning::whereIn('room_id', $room_ids)->whereDate('created_at', '>=', today()->toDateString())->count();
            // 智能门锁异常数量
            $lock_abnormal_today_count = Lock::whereIn('room_id', $room_ids)->whereDate('created_at', '>=', today()->toDateString())
                ->where('status', '<>', config('constants.STATUS_OK'))->count();

            // 3、本周总数据
            // 智慧烟感报警次数:
            $warnings_week_count = Warning::whereIn('room_id', $room_ids)->whereDate('created_at', '>=', now()
                ->startOfWeek()->toDateString())->count();
            // 智能门锁异常数量
            $lock_abnormal_week_count = Lock::whereIn('room_id', $room_ids)
                ->whereDate('created_at', '>=', now()->startOfWeek()->toDateString())
                ->where('status', '<>', config('constants.STATUS_OK'))->count();

            // 4、本月总数据
            // 智慧烟感报警次数:
            $warnings_month_count = Warning::whereIn('room_id', $room_ids)->whereDate('created_at', '>=', now()
                ->startOfMonth()->toDateString())->count();
            // 智能门锁异常数量
            $lock_abnormal_month_count = Lock::whereIn('room_id', $room_ids)
                ->whereDate('created_at', '>=', now()->startOfMonth()->toDateString())
                ->where('status', '<>', config('constants.STATUS_OK'))->count();

            return [
                'tenant_count' => $tenant_count,
                'lock_normal_count' => $lock_normal_count,
                'lock_abnormal_count' => $lock_abnormal_count,
                'smoke_normal_count' => $smoke_normal_count,
                'smoke_abnormal_count' => $smoke_abnormal_count,
                'warnings_today_count' => $warnings_today_count,
                'lock_abnormal_today_count' => $lock_abnormal_today_count,
                'warnings_week_count' => $warnings_week_count,
                'lock_abnormal_week_count' => $lock_abnormal_week_count,
                'warnings_month_count' => $warnings_month_count,
                'lock_abnormal_month_count' => $lock_abnormal_month_count,
            ];
        }

        if ($user == false) {
            return false;
        }

        return $user;
    }
}