SourceShool.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <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('source_shools_name','like',"%{$keyword}%")
  94. ->whereOr('address','like',"%{$keyword}%");
  95. }
  96. })
  97. ->field($field?:'source_shools_id as id,source_shools_name as school_name,students_num')
  98. ->order('students_num desc,source_shools_id desc')
  99. ->select();
  100. }
  101. /**
  102. * 获取学校信息
  103. * @param $where
  104. * @param array $with
  105. * @return static|array|false|null
  106. */
  107. public static function detail($where, array $with = [])
  108. {
  109. $filter = [];
  110. if (is_array($where)) {
  111. $filter = array_merge($filter, $where);
  112. } else {
  113. $filter['user_id'] = (int)$where;
  114. }
  115. return static::get($filter, $with);
  116. }
  117. }