PaperService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Services\Exam;
  3. use App\Contracts\Repositories\Exam\PaperRepository;
  4. use App\Repositories\Criteria\Exam\PaperCriteria;
  5. use App\Repositories\Eloquent\Exam\PaperRepositoryEloquent;
  6. use App\Repositories\Enums\ResponseCodeEnum;
  7. use App\Repositories\Models\Exam\Bank;
  8. use App\Repositories\Models\Exam\Paper;
  9. use App\Repositories\Presenters\Exam\PaperPresenter;
  10. use Illuminate\Http\Request;
  11. class PaperService
  12. {
  13. /**
  14. * @var PaperRepositoryEloquent
  15. */
  16. private $paperRepository;
  17. /**
  18. * PaperService constructor.
  19. *
  20. * @param PaperRepositoryEloquent $paperRepositoryEloquent
  21. */
  22. public function __construct(PaperRepositoryEloquent $paperRepositoryEloquent)
  23. {
  24. $this->paperRepository = $paperRepositoryEloquent;
  25. }
  26. /**
  27. * @param Request $request
  28. *
  29. * @return mixed
  30. * @throws \Prettus\Repository\Exceptions\RepositoryException
  31. */
  32. public function handleList(Request $request)
  33. {
  34. $this->paperRepository->pushCriteria(new PaperCriteria($request));
  35. $this->paperRepository->setPresenter(PaperPresenter::class);
  36. return $this->paperRepository->searchPapersByPage();
  37. }
  38. /**
  39. * @param $id
  40. *
  41. * @return \Illuminate\Database\Eloquent\Model
  42. */
  43. public function handleProfile($id)
  44. {
  45. $this->paperRepository->setPresenter(PaperPresenter::class);
  46. return $this->paperRepository->searchPaperBy($id);
  47. }
  48. /**
  49. * @param array $data
  50. *
  51. * @return mixed
  52. * @throws \Prettus\Validator\Exceptions\ValidatorException
  53. */
  54. public function handleStore($data)
  55. {
  56. self::defaultPoint($data);
  57. $paper = $this->paperRepository->create($data);
  58. // if (array_key_exists('banks', $data)) {
  59. // $paper->banks()->sync($data['banks']);
  60. // }
  61. return $paper;
  62. }
  63. /**
  64. * @param array $data
  65. *
  66. * @return mixed
  67. * @throws \Prettus\Validator\Exceptions\ValidatorException
  68. */
  69. public function handleUpdate($data)
  70. {
  71. self::defaultPoint($data);
  72. $paper = $this->paperRepository->update($data, $data['id']);
  73. // if (array_key_exists('banks', $data)) {
  74. // $paper->banks()->sync($data['banks']);
  75. // }
  76. return $paper;
  77. }
  78. public static function defaultPoint(&$data)
  79. {
  80. if (!array_key_exists('banks', $data)) {
  81. return false;
  82. }
  83. if (!is_array($data['banks'])) {
  84. return false;
  85. }
  86. $banks = $data['banks'];
  87. $banks = Bank::query()->whereIn('id', $banks)->get();
  88. $nums = count($banks);
  89. $da = [];
  90. if ($nums) {
  91. $p = bcdiv(100, $nums, 2);
  92. foreach ($banks as $bank) {
  93. $bankArr = $bank->toArray();
  94. $bankArr['point'] = $p;
  95. $da[] = $bankArr;
  96. }
  97. $data['banks'] = $da;
  98. }
  99. return true;
  100. }
  101. /**
  102. * @param Request $request
  103. *
  104. * @return mixed
  105. * @throws \Prettus\Validator\Exceptions\ValidatorException
  106. */
  107. public function handleDelete($id)
  108. {
  109. return $this->paperRepository->delete($id);
  110. }
  111. public function handleUpdateGrades($data)
  112. {
  113. $model = Paper::query()->where('id', $data['id'])->first();
  114. if (!$model) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该记录');
  115. $model->models()->sync($data['grades']);
  116. return true;
  117. }
  118. public function handleUpdateBanks($data)
  119. {
  120. $model = Paper::query()->where('id', $data['id'])->first();
  121. if (!$model) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该记录');
  122. self::defaultPoint($data);
  123. $model->banks = $data['banks'];
  124. $model->save();
  125. return true;
  126. }
  127. }