| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- // +----------------------------------------------------------------------
- // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: thinkphp <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\api\model;
- use app\api\service\User as UserService;
- use app\common\model\SourceShool as SourceShoolModel;
- /**
- * 生源学校模型类
- * Class SourceShool
- * @package app\api\model
- */
- class SourceShool extends SourceShoolModel
- {
- protected $globalScope = [''];
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- ];
- /**
- * 获取列表
- * @param array $param 查询条件
- * @param int $listRows 分页数量
- * @return mixed|\think\model\Collection|\think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList(array $param = [], int $listRows = 15)
- {
- // 整理查询参数
- $params = array_merge($param, []);
- // 获取商品列表
- $list = parent::getList($params, $listRows);
- if ($list->isEmpty()) {
- return $list;
- }
- // 隐藏冗余的字段
- $list->hidden(array_merge($this->hidden, ['province_id']));
- // 整理列表数据并返回
- return $this->setListDataFromApi($list);
- }
- /**
- * 设置展示的数据 api模块
- * @param $info
- * @return mixed
- */
- private function setListDataFromApi($info)
- {
- $userInfo = UserService::getCurrentLoginUser(true);
- return $this->setListData($info, function ($data) use ($userInfo) {
- // 整理数据 api模块
- $this->setDataFromApi($data, $userInfo);
- });
- }
- /**
- * 整理数据 api模块
- * @param $info
- * @return mixed
- */
- private function setDataFromApi($info, User $userInfo)
- {
- return $this->setData($info, function ($data) use ($userInfo) {
- // 解锁统计
- $lockedNum = rand(0, $data['students_num']);
- $data['locked_num'] = $lockedNum;
- $data['unlock_num'] = max(0, $data['students_num'] - $lockedNum);
- // 区域
- $data['city_name'] = Region::getNameById($data['city_id']);
- $data['region_name'] = Region::getNameById($data['region_id']);
- $data->hidden(array_merge($this->hidden,['province_id']));
- });
- }
- /**
- * 获取选项列表
- * @param array $param 查询条件
- * @param int $listRows 分页数量
- * @return mixed|\think\model\Collection|\think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getOptionList(array $param = [], string $field='')
- {
- return $this->where(function ($query) use ($param){
- $keyword = isset($param['keyword'])? trim($param['keyword']) : '';
- if($keyword){
- $query->where(function($query) use($keyword){
- $query->where('source_shools_name','like',"%{$keyword}%")
- ->whereOr('address','like',"%{$keyword}%");
- });
- }
- $userId = isset($param['user_id'])? intval($param['user_id']) : 0;
- if($userId>0){
- $query->whereNotIn('source_shools_id', SourceShoolApply::getSchoolsByUser($userId));
- }
- })
- ->field($field?:'source_shools_id as id,source_shools_name as school_name,students_num')
- ->order('students_num desc,id desc')
- ->select()
- ->each(function($item, $k) use ($param){
- $userId = isset($param['user_id'])? intval($param['user_id']) : 0;
- $item['is_apply'] = $userId? SourceShoolApply::checkApplyByUser($userId, $item['id']) : 0;
- });
- }
- /**
- * 获取学校信息
- * @param $where
- * @param array $with
- * @return static|array|false|null
- */
- public static function detail($where, array $with = [])
- {
- $filter = [];
- if (is_array($where)) {
- $filter = array_merge($filter, $where);
- } else {
- $filter['user_id'] = (int)$where;
- }
- return static::get($filter, $with);
- }
- /**
- * 获取学校字段信息
- * @param $schoolId
- * @param string $field
- * @return mixed
- */
- public static function getSchoolField($schoolId, $field='source_shools_name')
- {
- return static::where(['source_shools_id'=>$schoolId])->value($field);
- }
- }
|