480 lines
19 KiB
PHP
480 lines
19 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Model;
|
||
|
||
|
||
use Hyperf\Database\Model\Collection;
|
||
use Hyperf\Database\Model\Relations\BelongsTo;
|
||
use Hyperf\Database\Model\Relations\HasMany;
|
||
use Hyperf\Database\Model\Relations\HasOne;
|
||
use Hyperf\Database\Model\Relations\HasOneThrough;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\Snowflake\Concern\Snowflake;
|
||
use Hyperf\Utils\Arr;
|
||
|
||
/**
|
||
* @property int $doctor_id 主键
|
||
* @property int $user_id 用户id
|
||
* @property string $user_name 用户名称
|
||
* @property string $open_id 微信open_id
|
||
* @property string $union_id 微信开放平台唯一标识
|
||
* @property string $wx_session_key 微信会话密钥
|
||
* @property int $status 状态(0:禁用 1:正常 2:删除)
|
||
* @property int $idcard_status 实名认证状态(0:未认证 1:认证通过 2:认证失败)
|
||
* @property int $iden_auth_status 身份认证状态(0:未认证 1:认证通过 2:审核中 3:认证失败)
|
||
* @property string $iden_auth_time 审核时间
|
||
* @property string $iden_auth_fail_reason 身份认证失败原因
|
||
* @property int $multi_point_status 医生多点执业认证状态(0:未认证 1:认证通过 2:审核中 3:认证失败)
|
||
* @property string $multi_point_time 审核时间
|
||
* @property string $multi_point_fail_reason 多点执业认证失败原因
|
||
* @property int $introduction_status 个人简介审核状态(0:未审核 1:审核通过 2:审核中 3:审核失败)
|
||
* @property string $introduction_time 审核时间
|
||
* @property int $is_bind_bank 是否已绑定结算银行卡(0:否 1:是)
|
||
* @property int $is_recommend 是否首页推荐(0:否 1:是)
|
||
* @property string $avatar 头像
|
||
* @property int $doctor_title 医生职称(1:主任医师 2:主任中医师 3:副主任医师 4:副主任中医师 5:主治医师 6:住院医师)
|
||
* @property int $department_custom_id 科室id-自定义
|
||
* @property string $department_custom_name 科室名称(如未自己输入,填入标准科室名称)
|
||
* @property string $department_custom_mobile 科室电话
|
||
* @property int $hospital_id 所属医院id
|
||
* @property int $served_patients_num 服务患者数量(订单结束时统计)
|
||
* @property string $praise_rate 好评率(百分制。订单平均评价中超过4-5分的订单总数 / 总订单数 * 5)
|
||
* @property string $avg_response_time 平均响应时间(分钟制)
|
||
* @property int $number_of_fans 被关注数量
|
||
* @property int $is_img_expert_reception 是否参加专家图文接诊(0:否 1:是)
|
||
* @property int $is_img_welfare_reception 是否参加公益图文问诊(0:否 1:是)
|
||
* @property int $is_img_quick_reception 是否参加快速图文接诊(0:否 1:是)
|
||
* @property int $is_platform_deep_cooperation 是否平台深度合作医生(0:否 1:是)
|
||
* @property int $is_enterprise_deep_cooperation 是否企业深度合作医生(0:否 1:是)
|
||
* @property int $is_sys_diagno_cooperation 是否先思达合作医生(0:否 1:是)
|
||
* @property int $is_welfare_cooperation 是否公益问诊合作医生(可把公益问诊设为0元)
|
||
* @property string $qr_code 分享二维码
|
||
* @property string $be_good_at 擅长
|
||
* @property string $brief_introduction 医生简介
|
||
* @property \Carbon\Carbon $created_at 创建时间
|
||
* @property \Carbon\Carbon $updated_at 修改时间
|
||
* @property-read Hospital|null $Hospital
|
||
* @property-read User|null $User
|
||
* @property-read \Hyperf\Database\Model\Collection|DoctorInquiryConfig[]|null $DoctorInquiryConfig
|
||
* @property-read \Hyperf\Database\Model\Collection|DoctorExpertise[]|null $DoctorExpertise
|
||
* @property-read \Hyperf\Database\Model\Collection|OrderInquiry[]|null $OrderInquiry
|
||
*/
|
||
class UserDoctor extends Model
|
||
{
|
||
use Snowflake;
|
||
|
||
/**
|
||
* The table associated with the model.
|
||
*/
|
||
protected ?string $table = 'user_doctor';
|
||
|
||
/**
|
||
* The attributes that are mass assignable.
|
||
*/
|
||
protected array $fillable = ['doctor_id', 'user_id', 'user_name', 'open_id', 'union_id', 'wx_session_key', 'status', 'idcard_status', 'iden_auth_status', 'iden_auth_time', 'iden_auth_fail_reason', 'multi_point_status', 'multi_point_time', 'multi_point_fail_reason', 'introduction_status', 'introduction_time', 'is_bind_bank', 'is_recommend', 'avatar', 'doctor_title', 'department_custom_id', 'department_custom_name', 'department_custom_mobile', 'hospital_id', 'served_patients_num', 'praise_rate', 'avg_response_time', 'number_of_fans', 'is_img_expert_reception', 'is_img_welfare_reception', 'is_img_quick_reception', 'is_platform_deep_cooperation', 'is_enterprise_deep_cooperation', 'is_sys_diagno_cooperation', 'is_welfare_cooperation', 'qr_code', 'be_good_at', 'brief_introduction', 'created_at', 'updated_at'];
|
||
|
||
protected string $primaryKey = "doctor_id";
|
||
|
||
/**
|
||
* 关联医院表
|
||
*/
|
||
public function Hospital(): HasOne
|
||
{
|
||
return $this->hasOne(Hospital::class, 'hospital_id', 'hospital_id');
|
||
}
|
||
|
||
/**
|
||
* 关联用户表
|
||
*/
|
||
public function User(): HasOne
|
||
{
|
||
return $this->hasOne(User::class, 'user_id', 'user_id');
|
||
}
|
||
|
||
/**
|
||
* 关联问诊配置表(一对多)
|
||
* @return HasMany
|
||
*/
|
||
public function DoctorInquiryConfig(): HasMany
|
||
{
|
||
return $this->hasMany(DoctorInquiryConfig::class, "doctor_id", "doctor_id");
|
||
}
|
||
|
||
/**
|
||
* 关联医生专长表
|
||
* @return HasMany
|
||
*/
|
||
public function DoctorExpertise(): HasMany
|
||
{
|
||
return $this->hasMany(DoctorExpertise::class, "doctor_id", "doctor_id");
|
||
}
|
||
|
||
/**
|
||
* 关联订单-问诊表表
|
||
* @return HasMany
|
||
*/
|
||
public function OrderInquiry(): HasMany
|
||
{
|
||
return $this->hasMany(OrderInquiry::class, "doctor_id", "doctor_id");
|
||
}
|
||
|
||
/**
|
||
* 获取医生信息-单条
|
||
* @param array $params
|
||
* @param array $fields
|
||
* @return object|null
|
||
*/
|
||
public static function getOne(array $params, array $fields = ['*']): object|null
|
||
{
|
||
return self::where($params)->first($fields);
|
||
}
|
||
|
||
/**
|
||
* 获取数据-多
|
||
* @param array $params
|
||
* @param array $fields
|
||
* @return Collection|array
|
||
*/
|
||
public static function getList(array $params = [], array $fields = ['*']): Collection|array
|
||
{
|
||
return self::where($params)->get($fields);
|
||
}
|
||
|
||
/**
|
||
* 新增医生-批量
|
||
* @param array $data 新增数据
|
||
* @return \Hyperf\Database\Model\Model|UserDoctor
|
||
*/
|
||
public static function addUserDoctor(array $data): \Hyperf\Database\Model\Model|UserDoctor
|
||
{
|
||
return self::create($data);
|
||
}
|
||
|
||
/**
|
||
* 修改医生-批量
|
||
* @param array $params
|
||
* @param array $data
|
||
* @return int
|
||
*/
|
||
public static function editUserDoctor(array $params = [], array $data = []): int
|
||
{
|
||
return self::where($params)->update($data);
|
||
}
|
||
|
||
/**
|
||
* 首页推荐医生
|
||
* 推荐规则:
|
||
* 1:平台深度合作医生
|
||
* 2:接诊量由高到低的医生
|
||
* 3:好评率由高到低的医生
|
||
* 5:在线
|
||
* 6:5个
|
||
* @param int $limit 数量
|
||
* @param array $fields
|
||
* @return array|Collection|\Hyperf\Utils\Collection
|
||
*/
|
||
public static function getIndexRecommendDoctorLimit(int $limit = 5, array $fields = ['*']): array|Collection|\Hyperf\Utils\Collection
|
||
{
|
||
$params = array();
|
||
// 状态(0:禁用 1:正常 2:删除)
|
||
$params["status"] = 1;
|
||
|
||
// 身份认证状态(0:未认证 1:认证通过 2:审核中 3:认证失败)
|
||
$params["iden_auth_status"] = 1;
|
||
|
||
// // 是否在线(0:不在线 1:在线)
|
||
// $params["is_online"] = 1;
|
||
|
||
// // 是否参加专家图文接诊(0:否 1:是)
|
||
// $params["is_img_expert_reception"] = 1;
|
||
|
||
$datas = self::with([
|
||
'Hospital:hospital_id,hospital_name,hospital_level_name',
|
||
"DoctorInquiryConfig",
|
||
"User"
|
||
])
|
||
->where($params)
|
||
->orderBy("is_recommend", "desc")
|
||
->orderBy("is_platform_deep_cooperation", "desc")
|
||
->orderBy("served_patients_num", "desc")
|
||
->orderBy("praise_rate", "desc")
|
||
->limit($limit)
|
||
->get($fields);
|
||
|
||
|
||
return $datas;
|
||
}
|
||
|
||
/**
|
||
* 获取问诊医生列表
|
||
* 专家问诊-公益问诊共用
|
||
* @param string $keyword
|
||
* @param array $hospital_params 医院搜索条件
|
||
* @param array $doctor_params 医生搜索条件
|
||
* @param array $doctor_expertise_params
|
||
* @param array $inquiry_type
|
||
* @param array $inquiry_mode
|
||
* @param string|int $is_first_online 是否优先在线(1:是)
|
||
* @param string|int $sort_order
|
||
* @param array $fields
|
||
* @param int|null $page
|
||
* @param int|null $per_page
|
||
* @return array
|
||
*/
|
||
public static function getInquiryDoctorPage(string $keyword = "", array $hospital_params = [], array $doctor_params = [], array $doctor_expertise_params = [],array $inquiry_type = [],array $inquiry_mode = [],string|int $is_first_online = 0, string|int $sort_order = 1, array $fields = ["*"], int $page = null, ?int $per_page = 10): array
|
||
{
|
||
$query = self::with([
|
||
"Hospital:hospital_id,hospital_name,hospital_status,hospital_level_name,province_id,city_id",
|
||
"DoctorExpertise",
|
||
"DoctorInquiryConfig",
|
||
"User:user_id,is_online"
|
||
])
|
||
->where($doctor_params)
|
||
->when($keyword, function ($query, $keyword) {
|
||
$query->where(function ($query) use ($keyword) {
|
||
$query->orwhere("user_name", 'like', '%' . $keyword . '%');
|
||
$query->orwhere("department_custom_name", 'like', '%' . $keyword . '%');
|
||
$query->orWhereHas('Hospital', function ($query) use ($keyword) {
|
||
$query->where('hospital_name', 'like', '%' . $keyword . '%');
|
||
});
|
||
});
|
||
})
|
||
->whereHas('Hospital', function ($query) use ($hospital_params) {
|
||
$query->where($hospital_params);
|
||
})
|
||
->whereHas('DoctorExpertise', function ($query) use ($doctor_expertise_params) {
|
||
$query->where($doctor_expertise_params);
|
||
});
|
||
// ->whereHas('DoctorInquiryConfig', function ($query) use ($doctor_inquiry_config_params) {
|
||
// $params = array();
|
||
// $params['is_enable'] = 1;
|
||
//
|
||
// if (!empty($doctor_inquiry_config_params)){
|
||
// if (!empty($doctor_inquiry_config_params['inquiry_mode'])){
|
||
// $params['inquiry_mode'] = $doctor_inquiry_config_params['inquiry_mode'];
|
||
// }
|
||
//
|
||
// if (!empty($doctor_inquiry_config_params['inquiry_type'])){
|
||
// $params['inquiry_type'] = $doctor_inquiry_config_params['inquiry_type'];
|
||
// }
|
||
// }
|
||
//
|
||
// $query->where($params);
|
||
// });
|
||
// ->whereHas('DoctorInquiryConfig', function ($query) use ($doctor_inquiry_config_params) {
|
||
// $params = array();
|
||
// $params['is_enable'] = 1;
|
||
//
|
||
// if (!empty($doctor_inquiry_config_params)){
|
||
// if (!empty($doctor_inquiry_config_params['inquiry_mode'])){
|
||
// $inquiry_mode = explode(',',$doctor_inquiry_config_params['inquiry_mode']);
|
||
// $query->whereIn('inquiry_mode', $inquiry_mode);
|
||
// }
|
||
//
|
||
// if (!empty($doctor_inquiry_config_params['inquiry_type'])){
|
||
// $inquiry_type = explode(',',$doctor_inquiry_config_params['inquiry_type']);
|
||
// $query->whereIn('inquiry_type', $inquiry_type);
|
||
// }
|
||
// }
|
||
//
|
||
// $query->where($params);
|
||
// });
|
||
|
||
if ($is_first_online == 1){
|
||
$query->join('user as u', function ($query) {
|
||
$query->on('user_doctor.user_id', '=', 'u.user_id');
|
||
})
|
||
->select("user_doctor.*")
|
||
->orderBy('u.is_online', 'desc');
|
||
}
|
||
|
||
// 问诊服务搜索
|
||
// select doctor_id, min(price) price_min from price group by doctor_id
|
||
$raw = "inquiry_price as min_inquiry_price";
|
||
if (!empty($sort_order) && in_array($sort_order,[1,3,4])){
|
||
if ($sort_order == 1){
|
||
// 综合-价格从低到高
|
||
$raw = "MIN(inquiry_price) as min_inquiry_price";
|
||
} elseif ($sort_order == 3){
|
||
// 价格从低到高
|
||
$raw = "MIN(inquiry_price) as min_inquiry_price";
|
||
} elseif ($sort_order == 4){
|
||
// 价格从高到低
|
||
$raw = "MAX(inquiry_price) as min_inquiry_price";
|
||
}
|
||
}
|
||
|
||
$latestPosts = Db::table('doctor_inquiry_config')
|
||
->select('doctor_inquiry_config.doctor_id', Db::raw($raw))
|
||
->where('is_enable', 1);
|
||
if (!empty($inquiry_type)){
|
||
$latestPosts = $latestPosts->whereIn('inquiry_type', $inquiry_type)
|
||
->whereIn('inquiry_mode',$inquiry_mode);
|
||
}
|
||
|
||
$latestPosts = $latestPosts->groupBy(["doctor_inquiry_config.doctor_id"]);
|
||
|
||
$query = $query
|
||
->joinSub($latestPosts, 'doctor_inquiry_config', function($join) {
|
||
$join->on('user_doctor.doctor_id', '=', 'doctor_inquiry_config.doctor_id');
|
||
});
|
||
|
||
if (!empty($sort_order)){
|
||
if ($sort_order == 1) {
|
||
// 综合-价格从低到高
|
||
$query->orderBy('is_recommend', 'desc');// 是否首页推荐(0:否 1:是)
|
||
$query->orderByRaw('avg_response_time = 0 ASC');
|
||
$query->orderBy('avg_response_time');
|
||
$query->orderBy('served_patients_num', 'desc');// 服务数从多到少
|
||
$query->orderBy(Db::raw("convert(substr(gdxz_user_doctor.user_name,1,1) using `GBK`)"), 'asc');// 名称排名
|
||
$query->orderBy('doctor_inquiry_config.min_inquiry_price', 'asc');
|
||
} elseif ($sort_order == 2) {
|
||
// 响应时间快
|
||
$query->orderByRaw('avg_response_time = 0 ASC');
|
||
$query->orderBy('avg_response_time');
|
||
$query->orderBy(Db::raw("convert(substr(gdxz_user_doctor.user_name,1,1) using `GBK`)"), 'asc');// 名称排名
|
||
} elseif ($sort_order == 3) {
|
||
// 价格从低到高
|
||
$query->orderBy('doctor_inquiry_config.min_inquiry_price', 'asc');
|
||
$query->orderByRaw('avg_response_time = 0 ASC');
|
||
$query->orderBy('avg_response_time');
|
||
$query->orderBy(Db::raw("convert(substr(gdxz_user_doctor.user_name,1,1) using `GBK`)"), 'asc');// 名称排名
|
||
} elseif ($sort_order == 4) {
|
||
// 价格从高到低
|
||
$query->orderBy('doctor_inquiry_config.min_inquiry_price', 'desc');
|
||
$query->orderByRaw('avg_response_time = 0 ASC');
|
||
$query->orderBy('avg_response_time');
|
||
$query->orderBy(Db::raw("convert(substr(gdxz_user_doctor.user_name,1,1) using `GBK`)"), 'asc');// 名称排名
|
||
} elseif ($sort_order == 5) {
|
||
// 服务数从多到少
|
||
$query->orderBy('served_patients_num', 'desc');
|
||
$query->orderBy(Db::raw("convert(substr(gdxz_user_doctor.user_name,1,1) using `GBK`)"), 'asc');// 名称排名
|
||
}
|
||
}
|
||
|
||
$result = $query->paginate($per_page, $fields, "page", $page);
|
||
|
||
$data = array();
|
||
$data['current_page'] = $result->currentPage();// 当前页码
|
||
$data['total'] = $result->total();//数据总数
|
||
$data['data'] = $result->items();//数据
|
||
$data['per_page'] = $result->perPage();//每页个数
|
||
$data['last_page'] = $result->lastPage();//最后一页
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 获取是否存在
|
||
* @param array $params
|
||
* @return bool
|
||
*/
|
||
public static function getExists(array $params): bool
|
||
{
|
||
return self::where($params)->exists();
|
||
}
|
||
|
||
/**
|
||
* 获取医生数据
|
||
* 关联医院表
|
||
* @param array $params
|
||
* @param array $fields
|
||
* @return object|null
|
||
*/
|
||
public static function getWithHospitalOne(array $params,array $fields = ['*']): object|null
|
||
{
|
||
return self::with([
|
||
"Hospital:hospital_id,hospital_name,hospital_level_name"
|
||
])
|
||
->where($params)
|
||
->first($fields);
|
||
}
|
||
|
||
/**
|
||
* 获取数据单
|
||
* @param array $params
|
||
* @param array $not_in_params
|
||
* @param array $fields
|
||
* @return object|null
|
||
*/
|
||
public static function getUserDoctorNotInOne(array $params = [], array $not_in_params = [],array $fields = ['*']): object|null
|
||
{
|
||
return self::where($params)->whereNotIn('doctor_id',$not_in_params)->first($fields);
|
||
}
|
||
|
||
/**
|
||
* 获取数据-多
|
||
* @param array $params
|
||
* @param array $not_in_params
|
||
* @param array $fields
|
||
* @return Collection|array|\Hyperf\Utils\Collection
|
||
*/
|
||
public static function getUserDoctorNotInList(array $params = [], array $not_in_params = [],array $fields = ['*']): Collection|array|\Hyperf\Utils\Collection
|
||
{
|
||
return self::where($params)->whereNotIn('doctor_id',$not_in_params)->get($fields);
|
||
}
|
||
|
||
/**
|
||
* 自增
|
||
* @param array $params
|
||
* @param string $field
|
||
* @param float $numeral
|
||
* @return int
|
||
*/
|
||
public static function inc(array $params,string $field,float $numeral = 1): int
|
||
{
|
||
return self::where($params)->increment($field,$numeral);
|
||
}
|
||
|
||
/**
|
||
* 自减
|
||
* @param array $params
|
||
* @param string $field
|
||
* @param float $numeral
|
||
* @return int
|
||
*/
|
||
public static function dec(array $params,string $field,float $numeral = 1): int
|
||
{
|
||
return self::where($params)->decrement($field,$numeral);
|
||
}
|
||
|
||
/**
|
||
* 获取先思达合作医生列表
|
||
* @param array $params
|
||
* @param array $hospital_params
|
||
* @param array $fields
|
||
* @return array|Collection
|
||
*/
|
||
public static function getDiagnoCoopDoctorList(array $params,array $hospital_params,array $fields = ['*']): array|Collection
|
||
{
|
||
return self::with([
|
||
"Hospital:hospital_id,hospital_name,hospital_level_name"
|
||
])
|
||
->whereHas('Hospital', function ($query) use ($hospital_params) {
|
||
$query->where($hospital_params);
|
||
})
|
||
->where($params)
|
||
->get($fields);
|
||
}
|
||
|
||
/**
|
||
* 获取医生数据-医生配置
|
||
* @param array $params
|
||
* @param array $not_in_params
|
||
* @param array $doctor_inquiry_config_params
|
||
* @param array $fields
|
||
* @return Collection|array|\Hyperf\Utils\Collection
|
||
*/
|
||
public static function getListWithConfig(array $params = [], array $not_in_params = [],array $doctor_inquiry_config_params = [],array $fields = ['*']): Collection|array|\Hyperf\Utils\Collection
|
||
{
|
||
$doctor_ids = Db::table('doctor_inquiry_config')->where($doctor_inquiry_config_params)->select("doctor_id");
|
||
|
||
return self::where($params)
|
||
->whereNotIn('doctor_id',$not_in_params)
|
||
->whereIn("doctor_id",$doctor_ids)
|
||
->get($fields);
|
||
}
|
||
}
|