Files
anmo/server/app/api/lists/CoachLists.php
2025-08-19 14:16:51 +08:00

107 lines
3.8 KiB
PHP
Executable File

<?php
namespace app\api\lists;
use app\common\enum\coach\CoachEnum;
use app\common\logic\CityLogic;
use app\common\logic\CoachLogic;
use app\common\model\coach\Coach;
use app\common\model\coach\CoachGoodsIndex;
use app\common\model\coach\Collect;
use app\common\model\goods\GoodsComment;
use app\common\service\ConfigService;
/**
* 技师列表类
* Class CoachLists
* @package app\api\lists
*/
class CoachLists extends BaseApiDataLists
{
public $longitude = '';
public $latitude = '';
public $where = '';
public function setWhere()
{
$this->longitude = $this->params['longitude'] ?? '';
$this->latitude = $this->params['latitude'] ?? '';
$keyword = $this->params['keyword'] ?? '';
$skillId = $this->params['skill_id'] ?? '';
$goodsId = $this->params['goods_id'] ?? '';
$shopId = $this->params['shop_id'] ?? '';
$cityLists = CityLogic::getNearbyCity($this->longitude,$this->latitude);
$cityId = $cityLists[0]['city_id'] ?? '';
$where[] = ['city_id','=',$cityId];
$where[] = ['server_status','=',1];
$where[] = ['work_status','=',1];
$where[] = ['audit_status','=',CoachEnum::AUDIT_STATUS_PASS];
if($keyword){
$where[] = ['name','like','%'.$keyword.'%'];
}
if($skillId){
$where[] = ['skill_id','=',$skillId];
}
if($goodsId){
$coachId = CoachGoodsIndex::where(['goods_id'=>$goodsId])->column('coach_id');
empty($coachId) && $coachId = [0];
$where[] = ['id','in',implode(',',$coachId)];
}
if($shopId){
$where[] = ['shop_id','=',$shopId];
}
$this->where = $where;
}
/**
* @notes 技师列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author cjhao
* @date 2024/9/4 16:32
*/
public function lists(): array
{
$coachServerScope = ConfigService::get('server_setting', 'coach_server_scope');
$this->setWhere();
$field = 'id,work_status,work_photo,shop_id,name,round(st_distance_sphere(point('.$this->longitude.','.$this->latitude.'),
point(longitude, latitude))/1000,2) as distance,order_num,good_comment';
$coachLists = Coach::where($this->where)
->append(['distance_desc'])
->order('distance asc')
->limit($this->limitOffset, $this->limitLength)
->having('distance < '.$coachServerScope)
->field($field)
->select()
->toArray();
$userId = $this->userId;
$collect = '';
if($userId){
$collect = Collect::where(['user_id'=>$userId,'type'=>1])->value('relation_id');
}
$coachIds = array_column($coachLists,'id');
$collectLists = Collect::where(['type'=>1,'relation_id'=>$coachIds])
->group('relation_id')
->column('count(id) as num','relation_id');
$commentLists = GoodsComment::where(['coach_id'=>$coachIds])
->group('coach_id')
->column('count(id) as num','coach_id');
foreach ($coachLists as $key => $coach)
{
$coachLists[$key]['first_appoint'] = CoachLogic::getLatelyLeisureTime($coach['id']);
$coachLists[$key]['is_collect'] = $collect ? 1 : 0;
$coachLists[$key]['comment_num'] = $commentLists[$coach['id']] ?? 0;
$coachLists[$key]['good_comment'] = $coach['good_comment'].'%';
$coachLists[$key]['collect_num'] = $collectLists[$coach['id']] ?? 0;
}
return $coachLists;
}
public function count(): int
{
return Coach::where($this->where)->count();
}
}