1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\Min; |
9: | |
10: | use Aurora\Modules\Min\Models\MinHash; |
11: | use Illuminate\Database\Capsule\Manager as Capsule; |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | class Manager extends \Aurora\System\Managers\AbstractManager |
21: | { |
22: | public function __construct(\Aurora\System\Module\AbstractModule $oModule = null) |
23: | { |
24: | parent::__construct($oModule); |
25: | } |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | public function createMin($sHashID, $aParams, $iUserId = null, $iExpireDate = null) |
34: | { |
35: | $mResult = false; |
36: | $sNewMin = ''; |
37: | |
38: | if (is_string($sHashID) && 0 < strlen($sHashID) && false !== $this->getMinByID($sHashID)) { |
39: | return false; |
40: | } |
41: | |
42: | while (true) { |
43: | $sNewMin = \Illuminate\Support\Str::random(10); |
44: | if (false === $this->getMinByHash($sNewMin)) { |
45: | break; |
46: | } |
47: | } |
48: | |
49: | if (0 < strlen($sNewMin)) { |
50: | $aParams['__hash_id__'] = $sHashID; |
51: | $aParams['__hash__'] = $sNewMin; |
52: | $aParams['__time__'] = time(); |
53: | $aParams['__time_update__'] = time(); |
54: | |
55: | if (MinHash::create([ |
56: | 'Hash' => $sNewMin, |
57: | 'HashId' => md5($sHashID), |
58: | 'UserId' => $iUserId, |
59: | 'Data' => @\json_encode($aParams), |
60: | 'ExpireDate' => $iExpireDate |
61: | ])) { |
62: | $mResult = $sNewMin; |
63: | } |
64: | } |
65: | |
66: | return $mResult; |
67: | } |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | private function parseGetMinDbResult($oMin) |
75: | { |
76: | $mResult = false; |
77: | |
78: | if ($oMin && !empty($oMin->Data)) { |
79: | $aData = @\json_decode($oMin->Data, true); |
80: | if (is_array($aData) && 0 < count($aData)) { |
81: | $mResult = $aData; |
82: | |
83: | if ($oMin->ExpireDate) { |
84: | $mResult['ExpireDate'] = $oMin->ExpireDate; |
85: | } |
86: | } |
87: | } |
88: | |
89: | return $mResult; |
90: | } |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | public function getMinByID($sHashID) |
98: | { |
99: | $oMin = MinHash::firstWhere('HashId', \md5($sHashID)); |
100: | return $this->parseGetMinDbResult($oMin); |
101: | } |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | public function getMinByHash($sHash) |
109: | { |
110: | $oMin = MinHash::where(Capsule::connection()->raw('BINARY `Hash`'), $sHash)->first(); |
111: | return $this->parseGetMinDbResult($oMin); |
112: | } |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | public function getMinListByUserId($iUserId) |
121: | { |
122: | return MinHash::where('UserId', $iUserId)->get(); |
123: | } |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | public function deleteMinByID($sHashID) |
131: | { |
132: | return !!MinHash::where('HashId', \md5($sHashID))->delete(); |
133: | } |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | public function deleteMinByHash($sHash) |
141: | { |
142: | return !!MinHash::where(Capsule::connection()->raw('BINARY `Hash`'), $sHash)->delete(); |
143: | } |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | public function updateMinByID($sHashID, $aParams, $sNewHashID = null) |
153: | { |
154: | $aPrevParams = $this->getMinByID($sHashID); |
155: | if (isset($aPrevParams['__hash__'])) { |
156: | $aParams['__hash__'] = $aPrevParams['__hash__']; |
157: | } |
158: | if (!empty($sNewHashID)) { |
159: | $aParams['__hash_id__'] = $sNewHashID; |
160: | } |
161: | if (isset($aPrevParams['__time__'])) { |
162: | $aParams['__time__'] = $aPrevParams['__time__']; |
163: | } |
164: | |
165: | $aParams['__time_update__'] = time(); |
166: | $aMergedParams = array_merge($aPrevParams, $aParams); |
167: | |
168: | $aUpdate = [ |
169: | 'Data' => @\json_encode($aMergedParams) |
170: | ]; |
171: | if (!empty($sNewHashID)) { |
172: | $aUpdate['HashId'] = \md5($sNewHashID); |
173: | } |
174: | |
175: | return MinHash::where('HashId', \md5($sHashID))->update($aUpdate); |
176: | } |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | public function updateMinByHash($sHash, $aParams, $sNewHashID = null) |
186: | { |
187: | $aPrevParams = $this->getMinByHash($sHash); |
188: | if (isset($aPrevParams['__hash_id__'])) { |
189: | $aParams['__hash_id__'] = $aPrevParams['__hash_id__']; |
190: | } |
191: | if (!empty($sNewHashID)) { |
192: | $aParams['__hash_id__'] = $sNewHashID; |
193: | } |
194: | if (isset($aPrevParams['__time__'])) { |
195: | $aParams['__time__'] = $aPrevParams['__time__']; |
196: | } |
197: | |
198: | $aParams['__time_update__'] = time(); |
199: | |
200: | $aUpdate = [ |
201: | 'Data' => @\json_encode($aParams) |
202: | ]; |
203: | if (!empty($sNewHashID)) { |
204: | $aUpdate['HashId'] = \md5($sNewHashID); |
205: | } |
206: | |
207: | return MinHash::where('Hash', $sHash)->update($aUpdate); |
208: | } |
209: | } |
210: | |