您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

89 行
2.0KB

  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 baseQuery(): ActiveQuery
  15. {
  16. $class = $this->definition->getEntityFqcn();
  17. return $class::find();
  18. }
  19. public function createQuery(): self
  20. {
  21. $this->query = $this->baseQuery();
  22. return $this;
  23. }
  24. public function __call(string $name, $params): self
  25. {
  26. call_user_func_array([$this->query, $name], $params);
  27. return $this;
  28. }
  29. public function query()
  30. {
  31. return $this->query;
  32. }
  33. public function count()
  34. {
  35. $class = $this->definition->getEntityFqcn();
  36. $class::groupByPrimaryKey($class, $this->query);
  37. return $this->query->count();
  38. }
  39. public function find()
  40. {
  41. return $this->query->all();
  42. }
  43. public function findOne()
  44. {
  45. return $this->query->one();
  46. }
  47. public function filterById(int $id): self
  48. {
  49. $class = $this->definition->getEntityFqcn();
  50. $this->query->andWhere([$class::tableName().'.id' => $id]);
  51. return $this;
  52. }
  53. public function filterByCondition(string $condition = ''): self
  54. {
  55. if($condition && strlen($condition) > 0) {
  56. $this->andWhere($condition);
  57. }
  58. return $this;
  59. }
  60. public function getDataProvider(int $pageSize): ActiveDataProvider
  61. {
  62. return new ActiveDataProvider([
  63. 'query' => $this->query,
  64. 'sort' => false,
  65. 'pagination' => [
  66. 'pageSize' => $pageSize,
  67. ],
  68. ]);
  69. }
  70. }