|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asylamba\Modules\Athena\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Asylamba\Classes\Entity\AbstractRepository; |
|
6
|
|
|
|
|
7
|
|
|
use Asylamba\Modules\Athena\Model\RecyclingMission; |
|
8
|
|
|
|
|
9
|
|
|
class RecyclingMissionRepository extends AbstractRepository |
|
10
|
|
|
{ |
|
11
|
|
|
public function select($clause = '', $parameters = []) |
|
12
|
|
|
{ |
|
13
|
|
|
$statement = $this->connection->prepare( |
|
14
|
|
|
"SELECT rm.*, |
|
15
|
|
|
p.typeOfPlace AS typeOfPlace, |
|
16
|
|
|
p.position AS position, |
|
17
|
|
|
p.population AS population, |
|
18
|
|
|
p.coefResources AS coefResources, |
|
19
|
|
|
p.coefHistory AS coefHistory, |
|
20
|
|
|
p.resources AS resources, |
|
21
|
|
|
p.rSystem AS systemId, |
|
22
|
|
|
s.xPosition AS xPosition, |
|
23
|
|
|
s.yPosition AS yPosition, |
|
24
|
|
|
s.typeOfSystem AS typeOfSystem, |
|
25
|
|
|
s.rSector AS sectorId |
|
26
|
|
|
FROM recyclingMission AS rm |
|
27
|
|
|
LEFT JOIN place AS p |
|
28
|
|
|
ON rm.rTarget = p.id |
|
29
|
|
|
LEFT JOIN system AS s |
|
30
|
|
|
ON p.rSystem = s.id $clause" |
|
31
|
|
|
); |
|
32
|
|
|
$statement->execute($parameters); |
|
33
|
|
|
return $statement; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function get($id) |
|
37
|
|
|
{ |
|
38
|
|
|
if (($rm = $this->unitOfWork->getObject(RecyclingMission::class, $id)) !== null) { |
|
39
|
|
|
return $rm; |
|
40
|
|
|
} |
|
41
|
|
|
$statement = $this->select('WHERE rm.id = :id', ['id' => $id]); |
|
42
|
|
|
if (($row = $statement->fetch()) === false) { |
|
43
|
|
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
$recyclingMission = $this->format($row); |
|
46
|
|
|
$this->unitOfWork->addObject($recyclingMission); |
|
47
|
|
|
return $recyclingMission; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getAll() |
|
51
|
|
|
{ |
|
52
|
|
|
$statement = $this->select(); |
|
53
|
|
|
|
|
54
|
|
|
$data = []; |
|
55
|
|
|
while ($row = $statement->fetch()) { |
|
56
|
|
|
if (($rm = $this->unitOfWork->getObject(RecyclingMission::class, (int) $row['id'])) !== null) { |
|
57
|
|
|
$data[] = $rm; |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
$recyclingMission = $this->format($row); |
|
61
|
|
|
$this->unitOfWork->addObject($recyclingMission); |
|
62
|
|
|
$data[] = $recyclingMission; |
|
63
|
|
|
} |
|
64
|
|
|
return $data; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getBaseMissions($baseId) |
|
68
|
|
|
{ |
|
69
|
|
|
$statement = $this->select('WHERE rm.rBase = :base_id', ['base_id' => $baseId]); |
|
70
|
|
|
|
|
71
|
|
|
$data = []; |
|
72
|
|
|
while ($row = $statement->fetch()) { |
|
73
|
|
|
if (($rm = $this->unitOfWork->getObject(RecyclingMission::class, (int) $row['id'])) !== null) { |
|
74
|
|
|
$data[] = $rm; |
|
75
|
|
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
$recyclingMission = $this->format($row); |
|
78
|
|
|
$this->unitOfWork->addObject($recyclingMission); |
|
79
|
|
|
$data[] = $recyclingMission; |
|
80
|
|
|
} |
|
81
|
|
|
return $data; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getBaseActiveMissions($baseId) |
|
85
|
|
|
{ |
|
86
|
|
|
$statement = $this->select( |
|
87
|
|
|
'WHERE rm.rBase = :base_id AND rm.statement IN (' . implode(',', [RecyclingMission::ST_ACTIVE, RecyclingMission::ST_BEING_DELETED]) . ')', |
|
88
|
|
|
['base_id' => $baseId] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
$data = []; |
|
92
|
|
|
while ($row = $statement->fetch()) { |
|
93
|
|
|
if (($rm = $this->unitOfWork->getObject(RecyclingMission::class, (int) $row['id'])) !== null) { |
|
94
|
|
|
$data[] = $rm; |
|
95
|
|
|
continue; |
|
96
|
|
|
} |
|
97
|
|
|
$recyclingMission = $this->format($row); |
|
98
|
|
|
$this->unitOfWork->addObject($recyclingMission); |
|
99
|
|
|
$data[] = $recyclingMission; |
|
100
|
|
|
} |
|
101
|
|
|
return $data; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function insert($recyclingMission) |
|
105
|
|
|
{ |
|
106
|
|
|
$statement = $this->connection->prepare( |
|
107
|
|
|
'INSERT INTO |
|
108
|
|
|
recyclingMission(rBase, rTarget, cycleTime, recyclerQuantity, addToNextMission, uRecycling, statement) |
|
109
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?)' |
|
110
|
|
|
); |
|
111
|
|
|
$statement->execute(array( |
|
112
|
|
|
$recyclingMission->rBase, |
|
113
|
|
|
$recyclingMission->rTarget, |
|
114
|
|
|
$recyclingMission->cycleTime, |
|
115
|
|
|
$recyclingMission->recyclerQuantity, |
|
116
|
|
|
$recyclingMission->addToNextMission, |
|
117
|
|
|
$recyclingMission->uRecycling, |
|
118
|
|
|
$recyclingMission->statement |
|
119
|
|
|
)); |
|
120
|
|
|
$recyclingMission->id = $this->connection->lastInsertId(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function update($recyclingMission) |
|
124
|
|
|
{ |
|
125
|
|
|
$statement = $this->connection->prepare( |
|
126
|
|
|
'UPDATE recyclingMission SET |
|
127
|
|
|
rBase = ?, |
|
128
|
|
|
rTarget = ?, |
|
129
|
|
|
cycleTime = ?, |
|
130
|
|
|
recyclerQuantity = ?, |
|
131
|
|
|
addToNextMission = ?, |
|
132
|
|
|
uRecycling = ?, |
|
133
|
|
|
statement = ? |
|
134
|
|
|
WHERE id = ?'); |
|
135
|
|
|
$statement->execute(array( |
|
136
|
|
|
$recyclingMission->rBase, |
|
137
|
|
|
$recyclingMission->rTarget, |
|
138
|
|
|
$recyclingMission->cycleTime, |
|
139
|
|
|
$recyclingMission->recyclerQuantity, |
|
140
|
|
|
$recyclingMission->addToNextMission, |
|
141
|
|
|
$recyclingMission->uRecycling, |
|
142
|
|
|
$recyclingMission->statement, |
|
143
|
|
|
$recyclingMission->id |
|
144
|
|
|
)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function remove($recyclingMission) |
|
148
|
|
|
{ |
|
149
|
|
|
$statement = $this->connection->prepare('DELETE FROM recyclingMission WHERE id = :id'); |
|
150
|
|
|
$statement->execute(['id' => $recyclingMission->id]); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function removeBaseMissions($baseId) |
|
154
|
|
|
{ |
|
155
|
|
|
$statement = $this->connection->prepare( |
|
156
|
|
|
'DELETE rm, rl FROM recyclingMission rm INNER JOIN recyclingLog rl ON rl.rRecycling = rm.id WHERE rm.rBase = :base_id' |
|
157
|
|
|
); |
|
158
|
|
|
$statement->execute(['base_id' => $baseId]); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function format($data) |
|
162
|
|
|
{ |
|
163
|
|
|
$recyclingMission = new RecyclingMission(); |
|
164
|
|
|
|
|
165
|
|
|
$recyclingMission->id = (int) $data['id']; |
|
166
|
|
|
$recyclingMission->rBase = (int) $data['rBase']; |
|
167
|
|
|
$recyclingMission->rTarget = (int) $data['rTarget']; |
|
168
|
|
|
$recyclingMission->cycleTime = $data['cycleTime']; |
|
169
|
|
|
$recyclingMission->recyclerQuantity = (int) $data['recyclerQuantity']; |
|
170
|
|
|
$recyclingMission->addToNextMission = (int) $data['addToNextMission']; |
|
171
|
|
|
$recyclingMission->uRecycling = $data['uRecycling']; |
|
172
|
|
|
$recyclingMission->statement = (int) $data['statement']; |
|
173
|
|
|
|
|
174
|
|
|
$recyclingMission->typeOfPlace = (int) $data['typeOfPlace']; |
|
175
|
|
|
$recyclingMission->position = (int) $data['position']; |
|
176
|
|
|
$recyclingMission->population = (int) $data['population']; |
|
177
|
|
|
$recyclingMission->coefResources = (int) $data['coefResources']; |
|
178
|
|
|
$recyclingMission->coefHistory = (int) $data['coefHistory']; |
|
179
|
|
|
$recyclingMission->resources = (int) $data['resources']; |
|
180
|
|
|
$recyclingMission->systemId = (int) $data['systemId']; |
|
181
|
|
|
$recyclingMission->xSystem = (int) $data['xPosition']; |
|
182
|
|
|
$recyclingMission->ySystem = (int) $data['yPosition']; |
|
183
|
|
|
$recyclingMission->typeOfSystem = (int) $data['typeOfSystem']; |
|
184
|
|
|
$recyclingMission->sectorId = (int) $data['sectorId']; |
|
185
|
|
|
|
|
186
|
|
|
return $recyclingMission; |
|
187
|
|
|
} |
|
188
|
|
|
} |