1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use App\Repositories\Criteria\BestSolution; |
6
|
|
|
use App\Repositories\Criteria\Distinct; |
7
|
|
|
use App\Repositories\Criteria\GroupBy; |
8
|
|
|
use App\Repositories\Criteria\OrderBy; |
9
|
|
|
use App\Repositories\Criteria\RawSelect; |
10
|
|
|
use App\Repositories\Criteria\Where; |
11
|
|
|
use App\Repositories\SolutionRepository; |
12
|
|
|
use App\Status; |
13
|
|
|
|
14
|
|
|
class ProblemService |
15
|
|
|
{ |
16
|
|
|
public function numberOfAcceptedUser($problemId) |
17
|
|
|
{ |
18
|
|
|
/** @var SolutionRepository $repository */ |
19
|
|
|
$repository = app(SolutionRepository::class); |
20
|
|
|
$repository->pushCriteria(new Where('problem_id', $problemId)); |
21
|
|
|
$repository->pushCriteria(new Where('result', Status::ACCEPT)); |
22
|
|
|
$repository->pushCriteria(new Distinct('user_id')); |
23
|
|
|
|
24
|
|
|
return $repository->count(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function numberOfSubmitUser($problemId) |
28
|
|
|
{ |
29
|
|
|
/** @var SolutionRepository $repository */ |
30
|
|
|
$repository = app(SolutionRepository::class); |
31
|
|
|
$repository->pushCriteria(new Where('problem_id', $problemId)); |
32
|
|
|
$repository->pushCriteria(new Distinct('user_id')); |
33
|
|
|
|
34
|
|
|
return $repository->count(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $problemId |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getResultCount($problemId) |
43
|
|
|
{ |
44
|
|
|
/** @var SolutionRepository $repository */ |
45
|
|
|
$repository = app(SolutionRepository::class); |
46
|
|
|
$repository->pushCriteria(new Where('problem_id', $problemId)); |
47
|
|
|
$repository->pushCriteria(new RawSelect('count(*) as user_count, result')); |
48
|
|
|
$repository->pushCriteria(new GroupBy('result')); |
49
|
|
|
|
50
|
|
|
app('db')->connection()->statement('SET sql_mode = \'\''); |
51
|
|
|
|
52
|
|
|
return $repository->all(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function bestSolutions($problemId, $perPage = 50) |
56
|
|
|
{ |
57
|
|
|
/** @var SolutionRepository $repository */ |
58
|
|
|
$repository = app(SolutionRepository::class); |
59
|
|
|
|
60
|
|
|
$repository->pushCriteria(new BestSolution()); |
61
|
|
|
$repository->pushCriteria(new Where('problem_id', $problemId)); |
62
|
|
|
$repository->pushCriteria(new Where('result', Status::ACCEPT)); |
63
|
|
|
$repository->pushCriteria(new OrderBy('time_cost', 'asc')); |
64
|
|
|
$repository->pushCriteria(new OrderBy('score', 'asc')); |
65
|
|
|
$repository->pushCriteria(new OrderBy('id', 'asc')); |
66
|
|
|
$repository->pushCriteria(new GroupBy('user_id')); |
67
|
|
|
|
68
|
|
|
return $repository->paginate($perPage); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|