SourceShool.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\api\model;
  13. use app\api\service\User as UserService;
  14. use app\common\model\SourceShool as SourceShoolModel;
  15. /**
  16. * 生源学校模型类
  17. * Class SourceShool
  18. * @package app\api\model
  19. */
  20. class SourceShool extends SourceShoolModel
  21. {
  22. protected $globalScope = [''];
  23. /**
  24. * 隐藏字段
  25. * @var array
  26. */
  27. protected $hidden = [
  28. ];
  29. /**
  30. * 获取列表
  31. * @param array $param 查询条件
  32. * @param int $listRows 分页数量
  33. * @return mixed|\think\model\Collection|\think\Paginator
  34. * @throws \think\db\exception\DbException
  35. */
  36. public function getList(array $param = [], int $listRows = 15)
  37. {
  38. // 整理查询参数
  39. $params = array_merge($param, []);
  40. // 获取商品列表
  41. $list = parent::getList($params, $listRows);
  42. if ($list->isEmpty()) {
  43. return $list;
  44. }
  45. // 隐藏冗余的字段
  46. $list->hidden(array_merge($this->hidden, ['province_id']));
  47. // 整理列表数据并返回
  48. return $this->setListDataFromApi($list);
  49. }
  50. /**
  51. * 设置展示的数据 api模块
  52. * @param $info
  53. * @return mixed
  54. */
  55. private function setListDataFromApi($info)
  56. {
  57. $userInfo = UserService::getCurrentLoginUser(true);
  58. return $this->setListData($info, function ($data) use ($userInfo) {
  59. // 整理数据 api模块
  60. $this->setDataFromApi($data, $userInfo);
  61. });
  62. }
  63. /**
  64. * 整理数据 api模块
  65. * @param $info
  66. * @return mixed
  67. */
  68. private function setDataFromApi($info, User $userInfo)
  69. {
  70. return $this->setData($info, function ($data) use ($userInfo) {
  71. // 解锁统计
  72. $lockedNum = rand(0, $data['students_num']);
  73. $data['locked_num'] = $lockedNum;
  74. $data['unlock_num'] = max(0, $data['students_num'] - $lockedNum);
  75. // 区域
  76. $data['city_name'] = Region::getNameById($data['city_id']);
  77. $data['region_name'] = Region::getNameById($data['region_id']);
  78. $data->hidden(array_merge($this->hidden,['province_id']));
  79. });
  80. }
  81. /**
  82. * 获取选项列表
  83. * @param array $param 查询条件
  84. * @param int $listRows 分页数量
  85. * @return mixed|\think\model\Collection|\think\Paginator
  86. * @throws \think\db\exception\DbException
  87. */
  88. public function getOptionList(array $param = [], string $field='')
  89. {
  90. return $this->where(function ($query) use ($param){
  91. $keyword = isset($param['keyword'])? trim($param['keyword']) : '';
  92. if($keyword){
  93. $query->where(function($query) use($keyword){
  94. $query->where('source_shools_name','like',"%{$keyword}%")
  95. ->whereOr('address','like',"%{$keyword}%");
  96. });
  97. }
  98. $userId = isset($param['user_id'])? intval($param['user_id']) : 0;
  99. if($userId>0){
  100. $query->whereNotIn('source_shools_id', SourceShoolApply::getSchoolsByUser($userId));
  101. }
  102. })
  103. ->field($field?:'source_shools_id as id,source_shools_name as school_name,students_num')
  104. ->order('students_num desc,id desc')
  105. ->select()
  106. ->each(function($item, $k) use ($param){
  107. $userId = isset($param['user_id'])? intval($param['user_id']) : 0;
  108. $item['is_apply'] = $userId? SourceShoolApply::checkApplyByUser($userId, $item['id']) : 0;
  109. });
  110. }
  111. /**
  112. * 获取学校信息
  113. * @param $where
  114. * @param array $with
  115. * @return static|array|false|null
  116. */
  117. public static function detail($where, array $with = [])
  118. {
  119. $filter = [];
  120. if (is_array($where)) {
  121. $filter = array_merge($filter, $where);
  122. } else {
  123. $filter['user_id'] = (int)$where;
  124. }
  125. return static::get($filter, $with);
  126. }
  127. /**
  128. * 获取学校字段信息
  129. * @param $schoolId
  130. * @param string $field
  131. * @return mixed
  132. */
  133. public static function getSchoolField($schoolId, $field='source_shools_name')
  134. {
  135. return static::where(['source_shools_id'=>$schoolId])->value($field);
  136. }
  137. }