|
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\Commodity\CommodityTypeEnum; |
|
10
|
|
|
use Stu\Module\PlayerSetting\Lib\UserEnum; |
|
11
|
|
|
use Stu\Orm\Entity\Researched; |
|
12
|
|
|
use Stu\Orm\Entity\ResearchedInterface; |
|
13
|
|
|
use Stu\Orm\Entity\UserInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @extends EntityRepository<Researched> |
|
17
|
|
|
*/ |
|
18
|
|
|
final class ResearchedRepository extends EntityRepository implements ResearchedRepositoryInterface |
|
19
|
|
|
{ |
|
20
|
|
|
public function hasUserFinishedResearch(UserInterface $user, array $researchIds): bool |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->getEntityManager() |
|
23
|
|
|
->createQuery( |
|
24
|
|
|
sprintf( |
|
25
|
|
|
'SELECT COUNT(t.id) FROM %s t WHERE t.research_id IN (:researchIds) AND t.user_id = :userId AND t.finished > 0', |
|
26
|
|
|
Researched::class, |
|
27
|
|
|
) |
|
28
|
|
|
) |
|
29
|
|
|
->setParameters(['userId' => $user, 'researchIds' => $researchIds]) |
|
30
|
|
|
->getSingleScalarResult() > 0; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getListByUser(int $userId): array |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->getEntityManager() |
|
|
|
|
|
|
36
|
|
|
->createQuery( |
|
37
|
|
|
sprintf( |
|
38
|
|
|
'SELECT t FROM %s t WHERE t.user_id = :userId AND (t.finished > 0 OR t.aktiv > 0)', |
|
39
|
|
|
Researched::class, |
|
40
|
|
|
) |
|
41
|
|
|
) |
|
42
|
|
|
->setParameter('userId', $userId) |
|
43
|
|
|
->getResult(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getFinishedListByUser(int $userId): array |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->getEntityManager() |
|
|
|
|
|
|
49
|
|
|
->createQuery( |
|
50
|
|
|
sprintf( |
|
51
|
|
|
'SELECT t FROM %s t WHERE t.user_id = :userId AND t.finished > 0', |
|
52
|
|
|
Researched::class, |
|
53
|
|
|
) |
|
54
|
|
|
) |
|
55
|
|
|
->setParameter('userId', $userId) |
|
56
|
|
|
->getResult(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getCurrentResearch(UserInterface $user): ?ResearchedInterface |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getEntityManager() |
|
|
|
|
|
|
62
|
|
|
->createQuery( |
|
63
|
|
|
sprintf( |
|
64
|
|
|
'SELECT t FROM %s t WHERE t.user = :user AND t.aktiv > 0', |
|
65
|
|
|
Researched::class |
|
66
|
|
|
) |
|
67
|
|
|
) |
|
68
|
|
|
->setParameter('user', $user) |
|
69
|
|
|
->getOneOrNullResult(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getFor(int $researchId, int $userId): ?ResearchedInterface |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->findOneBy([ |
|
75
|
|
|
'research_id' => $researchId, |
|
76
|
|
|
'user_id' => $userId, |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function save(ResearchedInterface $researched): void |
|
81
|
|
|
{ |
|
82
|
|
|
$em = $this->getEntityManager(); |
|
83
|
|
|
|
|
84
|
|
|
$em->persist($researched); |
|
85
|
|
|
$em->flush(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function delete(ResearchedInterface $researched): void |
|
89
|
|
|
{ |
|
90
|
|
|
$em = $this->getEntityManager(); |
|
91
|
|
|
|
|
92
|
|
|
$em->remove($researched); |
|
93
|
|
|
$em->flush(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function prototype(): ResearchedInterface |
|
97
|
|
|
{ |
|
98
|
|
|
return new Researched(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function truncateForUser(int $userId): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->getEntityManager() |
|
104
|
|
|
->createQuery( |
|
105
|
|
|
sprintf( |
|
106
|
|
|
'DELETE FROM %s t WHERE t.user_id = :userId', |
|
107
|
|
|
Researched::class |
|
108
|
|
|
) |
|
109
|
|
|
) |
|
110
|
|
|
->setParameter('userId', $userId) |
|
111
|
|
|
->execute(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getResearchedPoints(): array |
|
115
|
|
|
{ |
|
116
|
|
|
$rsm = new ResultSetMapping(); |
|
117
|
|
|
$rsm->addScalarResult('user_id', 'user_id', 'integer'); |
|
118
|
|
|
$rsm->addScalarResult('points', 'points', 'integer'); |
|
119
|
|
|
|
|
120
|
|
|
return $this |
|
121
|
|
|
->getEntityManager() |
|
122
|
|
|
->createNativeQuery( |
|
123
|
|
|
'SELECT u.id as user_id, |
|
124
|
|
|
(SELECT coalesce(sum(r.points), 0) |
|
125
|
|
|
FROM stu_researched red JOIN stu_research r ON red.research_id = r.id |
|
126
|
|
|
WHERE red.user_id = u.id AND r.commodity_id = :lvl1) |
|
127
|
|
|
+ (SELECT coalesce(sum(r.points), 0) |
|
128
|
|
|
FROM stu_researched red JOIN stu_research r ON red.research_id = r.id |
|
129
|
|
|
WHERE red.user_id = u.id AND r.commodity_id = :lvl2) *2 |
|
130
|
|
|
+ (SELECT coalesce(sum(r.points), 0) |
|
131
|
|
|
FROM stu_researched red JOIN stu_research r ON red.research_id = r.id |
|
132
|
|
|
WHERE red.user_id = u.id AND r.commodity_id = :lvl3) *3 |
|
133
|
|
|
+ (SELECT coalesce(sum(r.points), 0) |
|
134
|
|
|
FROM stu_researched red JOIN stu_research r ON red.research_id = r.id |
|
135
|
|
|
WHERE red.user_id = u.id AND r.commodity_id IN (:lvl4)) *4 AS points |
|
136
|
|
|
FROM stu_user u |
|
137
|
|
|
WHERE u.id >= :firstUserId |
|
138
|
|
|
ORDER BY points DESC', |
|
139
|
|
|
$rsm |
|
140
|
|
|
) |
|
141
|
|
|
->setParameters([ |
|
142
|
|
|
'firstUserId' => UserEnum::USER_FIRST_ID, |
|
143
|
|
|
'lvl1' => CommodityTypeEnum::COMMODITY_RESEARCH_LVL1, |
|
144
|
|
|
'lvl2' => CommodityTypeEnum::COMMODITY_RESEARCH_LVL2, |
|
145
|
|
|
'lvl3' => CommodityTypeEnum::COMMODITY_RESEARCH_LVL3, |
|
146
|
|
|
'lvl4' => CommodityTypeEnum::COMMODITY_RESEARCH_LVL4 |
|
147
|
|
|
]) |
|
148
|
|
|
->getArrayResult(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|