|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Orm\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
|
8
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
|
9
|
|
|
use Stu\Module\PlayerSetting\Lib\UserEnum; |
|
10
|
|
|
use Stu\Orm\Entity\Fleet; |
|
11
|
|
|
use Stu\Orm\Entity\FleetInterface; |
|
12
|
|
|
use Stu\Orm\Entity\UserInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @extends EntityRepository<Fleet> |
|
16
|
|
|
*/ |
|
17
|
|
|
final class FleetRepository extends EntityRepository implements FleetRepositoryInterface |
|
18
|
|
|
{ |
|
19
|
|
|
public function prototype(): FleetInterface |
|
20
|
|
|
{ |
|
21
|
|
|
return new Fleet(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function save(FleetInterface $fleet): void |
|
25
|
|
|
{ |
|
26
|
|
|
$em = $this->getEntityManager(); |
|
27
|
|
|
|
|
28
|
|
|
$em->persist($fleet); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function delete(FleetInterface $fleet): void |
|
32
|
|
|
{ |
|
33
|
|
|
$em = $this->getEntityManager(); |
|
34
|
|
|
|
|
35
|
|
|
$em->remove($fleet); |
|
36
|
|
|
$em->flush(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function truncateByUser(UserInterface $user): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->getEntityManager()->createQuery( |
|
42
|
|
|
sprintf( |
|
43
|
|
|
'DELETE FROM %s f WHERE f.user_id = :user', |
|
44
|
|
|
Fleet::class |
|
45
|
|
|
) |
|
46
|
|
|
) |
|
47
|
|
|
->setParameters(['user' => $user]) |
|
48
|
|
|
->execute(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getByUser(int $userId): array |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->findBy( |
|
54
|
|
|
['user_id' => $userId], |
|
55
|
|
|
['sort' => 'desc', 'id' => 'desc'] |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getCountByUser(int $userId): int |
|
60
|
|
|
{ |
|
61
|
|
|
return (int) $this->getEntityManager()->createQuery( |
|
62
|
|
|
sprintf( |
|
63
|
|
|
'SELECT COUNT(f) |
|
64
|
|
|
FROM %s f |
|
65
|
|
|
WHERE f.user_id = :userId', |
|
66
|
|
|
Fleet::class |
|
67
|
|
|
) |
|
68
|
|
|
)->setParameter('userId', $userId) |
|
69
|
|
|
->getSingleScalarResult(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getHighestSortByUser(int $userId): int |
|
73
|
|
|
{ |
|
74
|
|
|
$rsm = new ResultSetMapping(); |
|
75
|
|
|
$rsm->addScalarResult('newsort', 'newsort'); |
|
76
|
|
|
|
|
77
|
|
|
return (int)$this->getEntityManager() |
|
78
|
|
|
->createNativeQuery( |
|
79
|
|
|
'SELECT COALESCE(MAX(GREATEST(f.sort, f.id)), 0) + 1 as newsort FROM stu_fleets f WHERE f.user_id = :userId', |
|
80
|
|
|
$rsm |
|
81
|
|
|
) |
|
82
|
|
|
->setParameters([ |
|
83
|
|
|
'userId' => $userId |
|
84
|
|
|
]) |
|
85
|
|
|
->getSingleScalarResult(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getNonNpcFleetList(): iterable |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->getEntityManager() |
|
91
|
|
|
->createQuery( |
|
92
|
|
|
sprintf( |
|
93
|
|
|
'SELECT f FROM %s f WHERE f.user_id > :firstUserId', |
|
94
|
|
|
Fleet::class |
|
95
|
|
|
) |
|
96
|
|
|
) |
|
97
|
|
|
->setParameter('firstUserId', UserEnum::USER_FIRST_ID) |
|
98
|
|
|
->getResult(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function truncateAllFleets(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->getEntityManager()->createQuery( |
|
104
|
|
|
sprintf( |
|
105
|
|
|
'DELETE FROM %s f', |
|
106
|
|
|
Fleet::class |
|
107
|
|
|
) |
|
108
|
|
|
)->execute(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|