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
|
<?php
namespace App\Model\Event;
use App\DB;
class SendResourcesCarriers extends BaseEvent
{
public int $sendResourcesEventId;
public int $source;
public int $destination;
/**
* @return void
*/
public function __invoke(): void
{
// add units to destination
$meta = DB::query('select amount, unit_id from events_meta_send_resources_carriers where event_id=:eventId', ['eventId' => $this->id])->fetchAll();
foreach ($meta as $m) {
$unit = DB::query('select * from village_units where id=:unitId', ['unitId' => $m['unit_id']])->fetch();
DB::query('update village_units set amount=amount+:amount where id=:unitId', ['amount' => $m['amount'], 'unitId' => $unit['id']]);
DB::query(
'update village_units set amount=amount-:amount where type=:unitType and home_village_id=:homeVillageId and residence_village_id=:residenceVillageId and is_traveling=true',
[
'amount' => $m['amount'],
'unitType' => $unit['type'],
'homeVillageId' => $unit['home_village_id'],
'residenceVillageId' => $unit['residence_village_id'],
]
);
}
}
public function cancel(): void
{}
public function dbInsert(): void
{
DB::query(
'insert into events (time, village_id) VALUES (:time, :village_id)',
['time' => $this->event->time->format('c'), 'village_id' => $this->event->villageId]
);
$eventId = DB::$connection->lastInsertId();
DB::query(
<<<SQL
insert into events_send_resources_carriers (event_id, source, destination)
VALUES (:event_id, :source, :destination)
SQL,
[
'event_id' => $eventId,
'source' => $this->source,
'destination' => $this->destination,
]
);
$sendResourcesCarriersEventId = DB::$connection->lastInsertId();
DB::query(
'update events_meta_send_resources_carriers set event_id=:new_id where event_id=:old_id',
['old_id' => $this->sendResourcesEventId, 'new_id' => $sendResourcesCarriersEventId]
);
}
public function dbDelete(): void
{
DB::query('delete from events where id=:id', ['id' => $this->eventId]);
DB::query('delete from events_send_resources_carriers where id=:id', ['id' => $this->id]);
DB::query('delete from events_meta_send_resources_carriers where event_id=:id', ['id' => $this->id]);
}
}
|