SystemSettingObserver.php
3.7 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* +-----------------------------------------------------------------------------------------------------------------------
* 观察者层:SystemSetting 处理业务逻辑
* +-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Observers\System
* @package App\Observers\System
* @author Richer <yangzi1028@163.com>
* @date 2022年7月26日11:02:58
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Observers\System;
use App\Jobs\User\UserUpdated;
use App\Models\System\SystemSetting as Model;
use App\Models\System\SystemSettingRemote;
use App\Models\Traits\SystemSettingTrait;
/**
* Class SystemSettingObserver
*
* @category App\Observers\System
* @package App\Observers\System
* @author Richer <yangzi1028@163.com>
* @date 2022年7月26日11:02:58
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class SystemSettingObserver
{
use SystemSettingTrait;
/**
* 监听数据即将创建的事件。
*
* @param Model $model
* @return void
*/
public function creating(Model $model)
{
// 设置缓存
$this->getSystemSetting('update', $model);
}
/**
* 监听数据创建后的事件。
*
* @param Model $model
* @return void
*/
public function created(Model $model)
{
//
}
/**
* 监听数据即将更新的事件。
*
* @param Model $model
* @return void
*/
public function updated(Model $model)
{
// 更新缓存
$this->getSystemSetting('update', $model);
// 同步
// $modelRemote = SystemSettingRemote::find($model->id);
$is_updated = false;
$update = [];
$consumption_times = $model->consumption_times;
$consumption_times_orig = $model->getOriginal('consumption_times');
if ($consumption_times != $consumption_times_orig) {
$is_updated = true;
$update['consumption_times'] = $consumption_times;
// $modelRemote->consumption_times = $consumption_times;
}
$context_timeout = $model->context_timeout;
$context_timeout_orig = $model->getOriginal('context_timeout');
if ($context_timeout != $context_timeout_orig) {
$is_updated = true;
$update['context_timeout'] = $context_timeout;
// $modelRemote->context_timeout = $context_timeout;
}
$start_statement = $model->start_statement;
$start_statement_orig = $model->getOriginal('start_statement');
if ($start_statement != $start_statement_orig) {
$is_updated = true;
$update['start_statement'] = $start_statement;
// $modelRemote->start_statement = $start_statement;
}
if ($is_updated === true) {
SystemSettingRemote::where('id', $model->id)->update($update);
// $modelRemote->save();
}
}
/**
* 监听数据即将保存的事件。
*
* @param Model $model
* @return void
*/
public function saving(Model $model)
{
}
/**
* 监听数据保存后的事件。
*
* @param Model $model
* @return void
*/
public function saved(Model $model)
{
//
}
/**
* 监听数据删除后的事件。
*
* @param Model $model
* @return void
*/
public function deleted(Model $model)
{
// 更新缓存
$this->getSystemSetting('update');
}
}