HomeService.php
4.9 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
<?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;
}
}