cxfoot/modules/api/models/CoachForm.php
2023-10-24 14:54:18 +08:00

112 lines
3.3 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2020-12-2
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\models;
use function AlibabaCloud\Client\value;
use app\components\FlashStorage;
use app\components\SiteHelper;
use app\models\Coach;
use app\models\DeviceUniqueBindUser;
use app\models\Store;
use app\models\User;
use app\components\auth\AToken;
use app\components\EncryptHelper;
use app\modules\api\components\ApiHelper;
use app\modules\api\components\GetDistance;
use yii\data\Pagination;
class CoachForm extends ApiModel
{
public $limit;//条数
public $page; //页数
public $pageCount; // 总页数
public $store_id;
public $coach_id;
public $cx_mch_id;
public $user_id;
public $name;
public $user_type;
public function rules()
{
return [
[['limit', 'page'], 'integer'],
[['page'], 'default', 'value' => 1],
];
}
/**
* 学员列表
*/
public function search()
{
if ($this->user_type != User::TYPE_COACH) {
return $this->apiReturnError('账号错误');
}
$where = ['c.status' => 1, 'c.is_delete' => 0,'c.user_id'=>$this->user_id];
$coachData = Coach::find()->alias('c')
->innerJoin(['s' => Store::tableName()], 'c.store_id=s.id')
->innerJoin(['u' => User::tableName()], 'c.user_id=u.id')
->select('u.id,s.name as store_name,c.coach_photo,u.real_name')
->where($where)
->andWhere([
'c.is_delete' => 0,
'c.status' => 1
])->asArray()->one();
if(empty($coachData)){
return $this->apiReturnError('非教练');
}
$coachData['coach_photo'] = SiteHelper::getFullUrl($coachData['coach_photo']);
$uids = DeviceUniqueBindUser::find()->where(['c_id' => $this->user_id])->select('DISTINCT(uid)')->asArray()->all();
$uidArr = array_column($uids, 'uid');
if (empty($this->limit)) {
$this->limit = 10;
}
if (empty($this->page)) {
$this->page = 1;
}
$query = User::find()
->where(['in', 'id', $uidArr])->andFilterWhere(['OR', ['LIKE', 'real_name', $this->name]])->select('id as user_id,real_name,nickname,avatar_url');
$count_query = clone $query;
$count = $count_query->count();
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['id' => SORT_DESC])->asArray()->all();
foreach ($list as $key=>$val){
if(empty($val['real_name'])){
$list[$key]['real_name'] = $val["nickname"];
}
$list[$key]['avatar_url'] = SiteHelper::getFullUrl($val['avatar_url']);
}
$this->pageCount = $pagination->pageCount;
$end_flag = $this->page >= $pagination->pageCount ? true : false;
$coachData['user_data'] = empty($list) ? [] : $list;
return [
'code' => 0,
'msg' => 'ok',
'data' => $coachData,
'count' => $count,
'page_size' => $this->limit,
'page_count' => $this->pageCount,
'page_no' => $this->page,
'end_flag' => $end_flag
];
}
}