SourceShool.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use think\model\Collection;
  15. use think\Paginator;
  16. /**
  17. * 生源学校模型类
  18. * Class SourceShool
  19. * @package app\common\model
  20. */
  21. class SourceShool extends BaseModel
  22. {
  23. protected $globalScope = [''];
  24. // 定义表名
  25. protected $name = 'source_shools';
  26. // 定义主键
  27. protected $pk = 'source_shools_id';
  28. /**
  29. * 获取列表
  30. * @param array $param 查询条件
  31. * @param int $listRows 分页数量
  32. * @return mixed
  33. * @throws \think\db\exception\DbException
  34. */
  35. public function getList(array $param = [], int $listRows = 15)
  36. {
  37. // 筛选条件
  38. $query = $this->getQueryFilter($param);
  39. // 排序条件
  40. $sort = $this->setQuerySort($param);
  41. // 执行查询
  42. $list = $query->alias($this->name)
  43. ->order($sort)
  44. ->paginate($listRows);
  45. // 整理列表数据并返回
  46. return $list;
  47. }
  48. /**
  49. * 设置商品展示的数据
  50. * @param Collection|Paginator $list 商品列表
  51. * @param callable|null $callback 回调函数
  52. * @return mixed
  53. */
  54. protected function setListData($list, callable $callback = null)
  55. {
  56. if ($list->isEmpty()) return $list;
  57. // 遍历商品列表整理数据
  58. foreach ($list as &$item) {
  59. $data = $this->setData($item, $callback);
  60. }
  61. return $list;
  62. }
  63. /**
  64. * 整理数据
  65. * @param Collection|static $info
  66. * @param callable|null $callback
  67. * @return mixed
  68. */
  69. protected function setData($info, callable $callback = null)
  70. {
  71. // 回调函数
  72. is_callable($callback) && call_user_func($callback, $info);
  73. return $info->hidden(array_merge($this->hidden,['province_id']));
  74. }
  75. /**
  76. * 检索查询条件
  77. * @param array $params
  78. * @return \think\db\BaseQuery
  79. */
  80. private function getQueryFilter(array $params)
  81. {
  82. $filter = [];
  83. // 实例化新查询对象
  84. $query = $this->getNewQuery();
  85. // 实例化新查询对象
  86. return $query->where($filter)->where(function($query) use ($params){
  87. // 关键词
  88. if(!empty($params['keyword'])){
  89. $query->where('source_shools_name','like', "%{$params['keyword']}%");
  90. }
  91. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  92. $type = isset($params['type'])? $params['type'] : 0;
  93. if($type>0 && $userId>0){
  94. // 已解锁过的学校
  95. $query->whereIn('source_shools_id', UnlockOrder::getSchoolsByUser($userId));
  96. }else if($userId>0){
  97. // 已申请通过的学校
  98. $query->whereIn('source_shools_id', SourceShoolApply::getSchoolsByUser($userId));
  99. }
  100. });
  101. }
  102. /**
  103. * 检索排序条件
  104. * @param array $param
  105. * @return array|string[]
  106. */
  107. private function setQuerySort(array $param = [])
  108. {
  109. $params = $this->setQueryDefaultValue($param, [
  110. 'sortType' => 'all', // 排序类型
  111. 'students_num' => false, // 热门排序 (true高到低 false低到高)
  112. ]);
  113. // 排序规则
  114. $sort = [];
  115. if ($params['sortType'] === 'all') {
  116. $sort = ['students_num' => 'desc'];
  117. }
  118. return array_merge($sort, [$this->getPk() => 'desc']);
  119. }
  120. }