Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

112 lines
2.5KB

  1. <?php
  2. namespace common\logic;
  3. use common\components\ActiveRecordCommon;
  4. use common\logic\Distribution\Distribution\Service\DistributionDefinition;
  5. use yii\data\ActiveDataProvider;
  6. use yii\db\ActiveQuery;
  7. abstract class AbstractRepositoryQuery extends AbstractService implements RepositoryQueryInterface
  8. {
  9. protected ActiveQuery $query;
  10. public function loadDefinition(string $serviceClass): void
  11. {
  12. $this->definition = $this->loadService($serviceClass);
  13. }
  14. public function getDefinition()
  15. {
  16. return $this->definition;
  17. }
  18. public function baseQuery(): ActiveQuery
  19. {
  20. $class = $this->definition->getEntityFqcn();
  21. return $class::find();
  22. }
  23. public function createQuery(): self
  24. {
  25. $this->query = $this->baseQuery();
  26. return $this;
  27. }
  28. public function __call(string $name, $params): self
  29. {
  30. call_user_func_array([$this->query, $name], $params);
  31. return $this;
  32. }
  33. public function query()
  34. {
  35. return $this->query;
  36. }
  37. public function count()
  38. {
  39. $class = $this->definition->getEntityFqcn();
  40. $class::groupByPrimaryKey($class, $this->query);
  41. return $this->query->count();
  42. }
  43. public function find()
  44. {
  45. return $this->query->all();
  46. }
  47. public function findOne()
  48. {
  49. return $this->query->one();
  50. }
  51. public function filterById(int $id): self
  52. {
  53. $class = $this->definition->getEntityFqcn();
  54. $this->query->andWhere([$class::tableName().'.id' => $id]);
  55. return $this;
  56. }
  57. public function filterByCondition(string $condition = ''): self
  58. {
  59. if($condition && strlen($condition) > 0) {
  60. $this->andWhere($condition);
  61. }
  62. return $this;
  63. }
  64. public function filterIsStatusOnline()
  65. {
  66. $this->andWhere(['status' => StatusInterface::STATUS_ONLINE]);
  67. return $this;
  68. }
  69. public function filterIsStatusOnlineAndOffline()
  70. {
  71. $this->andWhere('status >= :status')->addParams(['status' => StatusInterface::STATUS_OFFLINE]);
  72. return $this;
  73. }
  74. public function filterIsStatusDeleted()
  75. {
  76. $this->andWhere(['status' => StatusInterface::STATUS_DELETED]);
  77. return $this;
  78. }
  79. public function getDataProvider(int $pageSize): ActiveDataProvider
  80. {
  81. return new ActiveDataProvider([
  82. 'query' => $this->query,
  83. 'sort' => false,
  84. 'pagination' => [
  85. 'pageSize' => $pageSize,
  86. ],
  87. ]);
  88. }
  89. }