BaseModel.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/10/19 22:42
  5. */
  6. namespace app\api\model;
  7. use app\common\until\Until;
  8. use think\db\Query;
  9. use think\Model;
  10. class BaseModel extends Model {
  11. const NORMAL = 1;
  12. private $where = [];
  13. private $page = 1;
  14. private $pageSize = 10;
  15. private $sort = false;
  16. private $sortWhere = '';
  17. /**
  18. * @param Model $objectModel
  19. * @return array
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\DbException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. */
  24. public function getPageList(BaseModel $objectModel) {
  25. $count = $objectModel::where($this->getWhere())->count();
  26. if ($count) {
  27. $order = [];
  28. if ($this->isSort()) {
  29. $order['sort'] = 'desc';
  30. }
  31. if (empty($this->sortWhere)){
  32. $order['id'] = 'desc';
  33. }else {
  34. $order[$this->getSortWhere()] = 'desc';
  35. }
  36. $list = $objectModel::where($this->getWhere())->order($order)->page($this->getPage(), $this->getPageSize())->select();
  37. $list = Until::modelToArray($list);
  38. }
  39. return [
  40. 'count' => $count,
  41. 'pageCount' => $count ? ceil($count / $this->getPageSize()) : 0,
  42. 'page' => $this->getPage(),
  43. 'pageSize' => $this->getPageSize(),
  44. 'list' => $count ? $list : []
  45. ];
  46. }
  47. public function joinModelPageList( $countModel, $selectModel) {
  48. $count = $countModel
  49. ->where($this->where)
  50. ->count('*');
  51. if ($count > 0){
  52. $list = $selectModel
  53. ->where($this->where)
  54. ->page($this->getPage(), $this->getPageSize())
  55. ->select();
  56. $list = Until::modelToArray($list);
  57. }
  58. return [
  59. 'count' => $count,
  60. 'pageCount' => $count ? ceil($count / $this->getPageSize()) : 0,
  61. 'page' => $this->getPage(),
  62. 'pageSize' => $this->getPageSize(),
  63. 'list' => $count ? $list : []
  64. ];
  65. }
  66. /**
  67. * @return array
  68. */
  69. public function getWhere() {
  70. return $this->where;
  71. }
  72. /**
  73. * @param array $where
  74. */
  75. public function setWhere($where) {
  76. $this->where = $where;
  77. }
  78. /**
  79. * @return int
  80. */
  81. public function getPage(): int {
  82. return $this->page;
  83. }
  84. /**
  85. * @param int $page
  86. */
  87. public function setPage( $page) {
  88. $this->page = (int)$page;
  89. }
  90. /**
  91. * @return int
  92. */
  93. public function getPageSize(): int {
  94. return $this->pageSize;
  95. }
  96. /**
  97. * @param int $pageSize
  98. */
  99. public function setPageSize( $pageSize) {
  100. $this->pageSize = (int)$pageSize;
  101. }
  102. /**
  103. * @return bool
  104. */
  105. public function isSort(): bool {
  106. return $this->sort;
  107. }
  108. /**
  109. * @param bool $sort
  110. */
  111. public function setSort(bool $sort) {
  112. $this->sort = $sort;
  113. }
  114. /**
  115. * @return string
  116. */
  117. public function getSortWhere(): string {
  118. return $this->sortWhere;
  119. }
  120. /**
  121. * @param string $sortWhere
  122. */
  123. public function setSortWhere(string $sortWhere) {
  124. $this->sortWhere = $sortWhere;
  125. }
  126. }