<?php

namespace App\Services;

use App\Constants\DoctorTitleCode;
use App\Constants\HttpEnumCode;
use App\Model\DoctorExpertise;
use App\Model\DoctorInquiryConfig as DoctorInquiryConfigModel;
use App\Model\Hospital;
use App\Model\OrderEvaluation;
use App\Model\OrderInquiry;
use App\Model\PatientFollow;
use App\Model\PatientHistoryInquiry as PatientHistoryInquiryModel;
use App\Model\SystemInquiryConfig;
use App\Model\SystemInquiryTime;
use App\Model\UserDoctor;
use App\Model\UserDoctor as UserDoctorModel;

class PatientDoctorService extends BaseService
{
    /**
     * 获取问诊医生列表
     * 专家问诊-公益问诊共用
     * @return array
     */
    public function getInquiryDoctorList(): array
    {
        $expertise_id = $this->request->input('expertise_id');
        $province_id = $this->request->input('province_id');
        $city_id = $this->request->input('city_id');
        $sort_order = $this->request->input('sort_order',1);
        $keyword = $this->request->input('keyword',"");
        $is_search_welfare_reception = $this->request->input('is_search_welfare_reception',0);
        $page = $this->request->input('page',1);
        $per_page = $this->request->input('per_page',10);

        // 组合条件
        $hospital_params = array();// 医院搜索
        $doctor_params = array();// 医生搜索
        $doctor_expertise_params = array();// 医生专长搜索

        // 省市区
        if (!empty($province_id)) {
            if (empty($city_id)) {
                // 省份存在时需和城市在一块
                return fail(HttpEnumCode::CLIENT_HTTP_ERROR);
            }
            $hospital_params[] = ['province_id', '=', $province_id];
            $hospital_params[] = ['city_id', '=', $city_id];
        }

        // 医生专长
        if (!empty($expertise_id)) {
            $doctor_expertise_params['expertise_id'] = $expertise_id;
        }

        // 固定医生查询条件
        $doctor_params['status'] = 1; // 状态（0:禁用 1:正常 2:删除）

        $doctor_params["iden_auth_status"] = 1;// 身份认证状态（0:未认证 1:认证通过 2:审核中 3:认证失败）

        $doctor_params["is_online"] = 1;// 是否在线（0:不在线 1:在线）

        $doctor_params["is_img_expert_reception"] = 1;// 是否参加专家图文接诊（0:否 1:是）

        if (!empty($is_search_welfare_reception)){
            $doctor_params["is_img_welfare_reception"] = $is_search_welfare_reception;// 是否参加公益图文问诊（0:否 1:是）
        }

        $fields = [
            "doctor_id",
            "user_id",
            "user_name",
            "multi_point_status",
            "is_bind_bank",
            "is_recommend",
            "avatar",
            "doctor_title",
            "department_custom_id",
            "department_custom_name",
            "hospital_id",
            "served_patients_num",
            "praise_rate",
            "avg_response_time",
            "number_of_fans",
            "is_online",
            "is_img_expert_reception",
            "is_img_welfare_reception",
            "is_platform_deep_cooperation",
            "be_good_at",
        ];

        $user_doctors = UserDoctor::getInquiryDoctorPage($keyword, $hospital_params, $doctor_params, $doctor_expertise_params, $sort_order, $fields,$page,$per_page);

        // 处理数据
        if (!empty($user_doctors['data'])) {
            foreach ($user_doctors['data'] as &$user_doctor) {
                $user_doctor['doctor_title_name'] = empty($user_doctor['doctor_title']) ? "" : DoctorTitleCode::getMessage($user_doctor['doctor_title']);

                // 医生专长
                if (!empty($user_doctor['DoctorExpertise'])) {
                    foreach ($user_doctor['DoctorExpertise'] as &$data) {
                        if (!empty($data['DiseaseClassExpertise'])) {
                            $data['expertise_name'] = $data['DiseaseClassExpertise']['expertise_name'];
                        }
                        unset($data['DiseaseClassExpertise']);
                    }
                }

                // 问诊价格
                $user_doctor['price'] = 0;
                $user_doctor['free_clinic_price'] = 0;
                if (!empty($user_doctor['DoctorInquiryConfig'])) {
                    foreach ($user_doctor['DoctorInquiryConfig'] as $doctor_inquiry_config) {
                        if ($doctor_inquiry_config['inquiry_mode'] == 1) {
                            if ($doctor_inquiry_config['inquiry_type'] == 1) {
                                // 专家
                                $user_doctor['price'] = $doctor_inquiry_config['inquiry_price'] ?? 0;
                            }
                            if ($doctor_inquiry_config['inquiry_type'] == 3) {
                                // 公益
                                $user_doctor['free_clinic_price'] = $doctor_inquiry_config['inquiry_price'];
                            }
                        }
                    }
                    unset($user_doctor['DoctorInquiryConfig']);
                }

                // 好评率-超过5个已结束的订单后展示
                $user_doctor['praise_rate'] = ceil($user_doctor['praise_rate'] * 0.05 * 100) / 100;
                // 响应时间-超过5个已结束的订单后展示
                $user_doctor['avg_response_time'] = ceil($user_doctor['avg_response_time'] * 0.05 * 100) / 100 * 60;

                // 获取医生订单数
                $params = array();
                $params['doctor_id'] = $user_doctor['doctor_id'];
                $params['inquiry_status'] = 6; // 已结束
                $inquiry_order_count = OrderInquiry::getCount($params);
                if (empty($inquiry_order_count) || $inquiry_order_count == 0) {
                    $user_doctor['is_display_score'] = false;
                } else {
                    $user_doctor['is_display_score'] = true;
                }

                // 头像
                $user_doctor['avatar'] = addAliyunOssWebsite($user_doctor['avatar']);
            }
        }
        return success($user_doctors);
    }

    /**
     * 获取问诊医生详情
     * @return array
     */
    public function getInquiryDoctorInfo(): array
    {
        $doctor_id = $this->request->route('doctor_id');

        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $result = array();
        $result['hospital'] = [];
        $result['days'] = 0;
        $result['doctor_inquiry_config'] = [];

        $fields = [
            "doctor_id",
            "user_id",
            "user_name",
            "iden_auth_status",
            "multi_point_status",
            "avatar",
            "doctor_title",
            "department_custom_id",
            "department_custom_name",
            "hospital_id",
            "served_patients_num",
            "praise_rate",
            "avg_response_time",
            "number_of_fans",
            "is_online",
            "is_img_expert_reception",
            "is_img_welfare_reception",
            "is_platform_deep_cooperation",
            "be_good_at",
            "brief_introduction",
        ];

        $params = array();
        $params['doctor_id'] = $doctor_id;
        $params['status'] = 1; // 状态（0:禁用 1:正常 2:删除）
        $params["iden_auth_status"] = 1;// 身份认证状态（0:未认证 1:认证通过 2:审核中 3:认证失败）
        $user_doctor = UserDoctor::getOne($params, $fields);
        if (empty($user_doctor)) {
            return fail(HttpEnumCode::HTTP_SUCCESS, "医生错误");
        }

        $result = $user_doctor->toArray();

        $result['doctor_title_name'] = empty($user_doctor['doctor_title']) ? "" : DoctorTitleCode::getMessage($user_doctor['doctor_title']);

        $result['hospital'] = [];
        $result['days'] = 0;
        $result['doctor_inquiry_config'] = 0;

        // 获取医生医院数据
        $fields = [
            'hospital_id',
            'hospital_name',
            'hospital_level_name',
        ];

        $params = array();
        $params['hospital_id'] = $user_doctor['hospital_id'];
        $hospital = Hospital::getOne($params, $fields);
        if (!empty($hospital)) {
            $result['hospital'] = $hospital;
        }

        // 获取服务天数
        $params = array();
        $params['patient_id'] = $user_info['client_user_id'];
        $params['doctor_id'] = $doctor_id;
        $params['history_status'] = 1;
        $patient_history_doctor = PatientHistoryInquiryModel::getOne($params);
        // 按照天来计算，当日为1，前一天为2 未服务过为0
        if (!empty($patient_history_doctor['created_at'])) {
            $result['days'] = ceil((strtotime(date('Y-m-d', strtotime('+1 day'))) - strtotime($patient_history_doctor['created_at'])) / 86400);
        }

        // 获取关注状态
        $params = array();
        $params['patient_id'] = $user_info['client_user_id'];
        $params['doctor_id'] = $doctor_id;
        $result['follow'] = PatientFollow::getExists($params);

        // 获取问诊价格
        // 专家-公益
        $params = array();
        $params[] = ['doctor_id', '=', $doctor_id];
        $params[] = ['inquiry_mode', '=', 1];// 接诊方式（1:图文 2:视频 3:语音 4:电话 5:会员）

        $inquiry_type_in_params = array();
        if ($user_doctor['is_img_expert_reception'] == 1){
            $inquiry_type_in_params[] = 1;
        }
        if ($user_doctor['is_img_welfare_reception'] == 1){
            $inquiry_type_in_params[] = 3;
        }

        $doctor_inquiry_config = DoctorInquiryConfigModel::getInquiryConfigList($params,['*'],$inquiry_type_in_params);
        if (!empty($doctor_inquiry_config)) {
            foreach ($doctor_inquiry_config as &$value) {
                // 获取医生当日的全部订单
                $params = array();
                $params[] = ['doctor_id', '=', $doctor_id];
                $params[] = ['inquiry_type', '=', $value['inquiry_type']];
                $params[] = ['inquiry_mode', '=', 1];
                $params[] = ['inquiry_refund_status', '=', 0];
                $value['order_inquiry_count'] = OrderInquiry::getCount($params);

                // 获取系统问诊配置
                $fields = [
                    'system_inquiry_config_id',
                    'times_number',
                    'duration',
                ];

                $params = array();
                $params['inquiry_type'] = $value['inquiry_type'];
                $params['inquiry_mode'] = $value['inquiry_mode'];
                $system_inquiry_config = SystemInquiryConfig::getOne($params,$fields);
                if (empty($system_inquiry_config)){
                    return fail();
                }

                $value['times_number'] = $system_inquiry_config['times_number'];
                $value['duration'] = $system_inquiry_config['duration'];

                unset($system_inquiry_config);
            }
            $result['doctor_inquiry_config'] = $doctor_inquiry_config;
        }

        // 获取医生已选择专长
        $UserDoctorService = new UserDoctorService();
        $result['doctor_expertise'] = $UserDoctorService->getDoctorSelectedExpertise($user_doctor['doctor_id']);

        // 好评率-超过5个已结束的订单后展示
        $result['praise_rate'] = ceil($user_doctor['praise_rate'] * 0.05 * 100) / 100;

        // 响应时间-超过5个已结束的订单后展示
        $result['avg_response_time'] = ceil($user_doctor['avg_response_time'] * 0.05 * 100) / 100 * 60;

        $params = array();
        $params['doctor_id'] = $user_doctor['doctor_id'];
        $params['inquiry_status'] = 6; // 已结束
        $inquiry_order_count = OrderInquiry::getCount($params);
        if (empty($inquiry_order_count) || $inquiry_order_count == 0) {
            $result['is_display_score'] = false;
        } else {
            $result['is_display_score'] = true;
        }

        // 头像
        $result['avatar'] = addAliyunOssWebsite($user_doctor['avatar']);

        return success($result);
    }

    /**
     * 医生详情简介-详情中的简介
     * @return array
     */
    public function getDoctorProfile(): array
    {
        $doctor_id = $this->request->route('doctor_id');

        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $fields = [
            "doctor_id",
            "user_id",
            "user_name",
            "status",
            "iden_auth_status",
            "avatar",
            "doctor_title",
            "department_custom_name",
            "hospital_id",
            "be_good_at",
            "brief_introduction",
        ];

        $params = array();
        $params['doctor_id'] = $doctor_id;
        $params['status'] = 1; // 状态（0:禁用 1:正常 2:删除）
        $params["iden_auth_status"] = 1;// 身份认证状态（0:未认证 1:认证通过 2:审核中 3:认证失败）
        $user_doctor = UserDoctor::getOne($params, $fields);
        if (empty($user_doctor)) {
            return fail(HttpEnumCode::CLIENT_HTTP_ERROR);
        }

        $user_doctor = $user_doctor->toArray();

        $user_doctor['doctor_title_name'] = empty($user_doctor['doctor_title']) ? "" : DoctorTitleCode::getMessage($user_doctor['doctor_title']);

        // 获取医生医院数据
        $fields = [
            'hospital_id',
            'hospital_name',
            'hospital_level_name',
        ];

        $params = array();
        $params['hospital_id'] = $user_doctor['hospital_id'];
        $hospital = Hospital::getOne($params, $fields);
        if (!empty($hospital)) {
            $user_doctor['hospital'] = $hospital;
        }

        // 获取医生已选择专长
        $UserDoctorService = new UserDoctorService();
        $user_doctor['doctor_expertise'] = $UserDoctorService->getDoctorSelectedExpertise($doctor_id);

        return success($user_doctor);
    }

    /**
     * 检测是否可以接诊
     * @return array
     */
    public function getDoctorInquiryCheck(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $inquiry_type = $this->request->input('inquiry_type');
        $inquiry_mode = $this->request->input('inquiry_mode');
        $doctor_id = $this->request->input('doctor_id','');

        $result = array();
        $result['data'] = [];
        $result['message'] = "可接诊";
        $result['status'] = 1;

        // 检测是否存在同类型未完成的问诊订单
        $PatientOrderService = new PatientOrderService();
        $order_inquiry_id = $PatientOrderService->getNotFinishedOrdeInquiry($inquiry_type,$user_info['client_user_id']);
        if (!empty($order_inquiry_id)){
            $result['status'] = 2;
            $result['data']['order_inquiry_id'] = $order_inquiry_id;
            $result['message'] = "存在未完成的问诊订单";
            return success($result);
        }

        if (!empty($doctor_id)){
            // 获取医生信息
            $params = array();
            $params['doctor_id'] = $doctor_id;

            $fields = [
                'doctor_id',
                'user_name',
                'iden_auth_status',
                'idcard_status',
                'multi_point_status',
                'avatar',
                'is_bind_bank',
            ];
            $user_doctor = UserDoctor::getOne($params, $fields);
            if (empty($user_doctor)) {
                return fail(HttpEnumCode::HTTP_ERROR, "非法医生");
            }

            // 检测医生身份认证
            if ($user_doctor['iden_auth_status'] != 1) {
                return fail();
            }

            if ($user_doctor['idcard_status'] != 1) {
                return fail();
            }

            // 检测当前医生是否和患者存在未完成问诊订单
            $InquiryService = new InquiryService();
            $order_inquiry = $InquiryService->checkPatientDoctorProgressInquiry($user_info['client_user_id'],$doctor_id);
            if (!empty($order_inquiry)){
                $result['status'] = 2;
                $result['data']['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
                $result['message'] = "您和当前医生存在问诊中订单，无法再次发起问诊";
                return success($result);
            }

            // 获取医生每日最大接诊数量
            $UserDoctorService = new UserDoctorService();
            $max_work_num_day = $UserDoctorService->getDoctorInquiryDayMaxNum($doctor_id,$inquiry_type,$inquiry_mode);

            // 获取医生未接诊订单数量(包含未接诊、未支付、已接诊)
            $order_inquiry_count = $UserDoctorService->getDoctorUnInquiryOrderNum($doctor_id,$inquiry_type,$inquiry_mode);

            // 计算剩余未接诊数量 = 最大数量-
            $access_inquiry_num = $max_work_num_day - $order_inquiry_count;
            if ($access_inquiry_num <= 0){
                $result['status'] = 3;
                $result['data'] = "";
                $result['message'] = "当前医生已无每日问诊次数，请选择其他医生";
                return success($result);
            }
        }

        if ($inquiry_type == 4){
            // 公益问诊-检测当前患者问诊次数（24小时内两次公益问诊）
            $params = array();
            $params['patient_id'] = $user_info['client_user_id'];
            $params['inquiry_type'] = $inquiry_type;
            $params['inquiry_mode'] = $inquiry_mode;

            $inquiry_status_params = [1,2,3,4];
            $inquiry_count = OrderInquiry::getInquiryStatusCount($params,$inquiry_status_params);
            if ($inquiry_count >= 2){
                $result['status'] = 3;
                $result['data'] = "";
                $result['message'] = "您还有未完成的公益问诊，完成之后才可以重新问诊";
                return success($result);
            }
        }

        // 检测快速、购药问诊是否由可分配医生
        if ($inquiry_type == 2 || $inquiry_type == 4){
            $UserDoctorService = new UserDoctorService();
            $doctor_id = $UserDoctorService->getInquiryAssignDoctor($inquiry_type,$user_info['client_user_id']);
            if (empty($doctor_id)){
                // 无合适医生
                $result['status'] = 3;
                $result['data'] = "";
                $result['message'] = "目前暂无可接诊医生";
                return success($result);
            }
        }

        return success($result);
    }

    /**
     * 获取我的问诊、关注医生列表
     * @return array|void
     */
    public function getDoctorList(){
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $my_doctor_type = $this->request->input('my_doctor_type'); // 医生类型（1：问诊 2：关注）
        $page = $this->request->input('page', 1);
        $per_page = $this->request->input('per_page', 10);

        if ($my_doctor_type == 1){
            // 问诊
            $params = array();
            $params['patient_id'] = $user_info['client_user_id'];
            $params['history_status'] = 1;
            $result = PatientHistoryInquiryModel::getPage($params,['*'],$page,$per_page);
        }else{
            // 关注
            $params = array();
            $params['patient_id'] = $user_info['client_user_id'];
            $result = PatientFollow::getPage($params,['*'],$page,$per_page);
        }

        // 处理数据
        if (!empty($result['data'])) {
            foreach ($result['data'] as $item) {
                $data = array();

                // 医生专长
                if (!empty($item['DoctorExpertise'])) {
                    foreach ($item['DoctorExpertise'] as &$doctor_expertise) {
                        if (!empty($doctor_expertise['DiseaseClassExpertise'])) {
                            $doctor_expertise['expertise_name'] = $doctor_expertise['DiseaseClassExpertise']['expertise_name'];
                        }
                        unset($doctor_expertise['DiseaseClassExpertise']);
                    }
                }
                $data['doctor_expertise'] = $item['DoctorExpertise'];

                // 医生数据
                $data['user_doctor'] = $item['UserDoctor'];

                // 头像
                $data['user_doctor']['avatar'] = addAliyunOssWebsite($data['user_doctor']['avatar']);

                // 职称
                $data['user_doctor']['doctor_title_name'] = empty($data['user_doctor']['doctor_title']) ? "" : DoctorTitleCode::getMessage($data['user_doctor']['doctor_title']);

                $result['data'] = $data;
            }
        }

        return success($result);

    }

    /**
     * 获取首页推荐医生
     * 限制条数
     * @return array
     */
    public function getIndexRecommendDoctorLimit(): array
    {
        $results = array();

        // 推荐医生
        $fields = [
            "doctor_id",
            "user_id",
            "user_name",
            "status",
            "multi_point_status",
            "avatar",
            "doctor_title",
            "department_custom_name",
            "hospital_id",
            "is_online",
            "is_img_expert_reception",
            "is_img_welfare_reception",
            "be_good_at",
        ];

        $recommend_doctors = UserDoctorModel::getIndexRecommendDoctorLimit(5, $fields);
        if (empty($recommend_doctors)) {
            return success();
        }

        foreach ($recommend_doctors as $recommend_doctor) {
            $data = array();
            $data['doctor_id'] = $recommend_doctor['doctor_id'];
            $data['user_id'] = $recommend_doctor['user_id'];
            $data['user_name'] = $recommend_doctor['user_name'];
            $data['status'] = $recommend_doctor['status'];
            $data['multi_point_status'] = $recommend_doctor['multi_point_status']; // 医生多点执业认证状态（0:未认证 1:认证通过 2:审核中 3:认证失败）
            $data['avatar'] = addAliyunOssWebsite($recommend_doctor['avatar']);
            $data['doctor_title'] = empty($recommend_doctor['doctor_title']) ? "" : DoctorTitleCode::getMessage($recommend_doctor['doctor_title']);
            $data['department_custom_name'] = $recommend_doctor['department_custom_name'];
            $data['is_online'] = $recommend_doctor['is_online'];
            $data['is_img_welfare_reception'] = $recommend_doctor['is_img_welfare_reception'];
            $data['be_good_at'] = $recommend_doctor['be_good_at'];
            $data['hospital_name'] = $recommend_doctor['Hospital']['hospital_name'] ?? "";
            $data['hospital_level_name'] = $recommend_doctor['Hospital']['hospital_level_name'] ?? "";

            // 处理接诊价格
            $data['price'] = 0;
            $data['free_clinic_price'] = 0;
            foreach ($recommend_doctor['DoctorInquiryConfig'] as $doctor_inquiry_config) {
                if ($doctor_inquiry_config['inquiry_mode'] == 1){
                    if ($doctor_inquiry_config['inquiry_type'] == 1) {
                        // 专家问诊
                        $data['price'] = $doctor_inquiry_config['inquiry_price'];
                    }

                    if ($doctor_inquiry_config['inquiry_type'] == 3) {
                        // 公益
                        $data['free_clinic_price'] = $doctor_inquiry_config['inquiry_price'];
                    }
                }
            }

            $results[] = $data;

            unset($data);
        }
        return success($results);
    }

    /**
     * 获取医生评价
     * @return array
     */
    public function getDoctorEvaluationList(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $doctor_id = $this->request->route('doctor_id');
        $page = $this->request->input('page', 1);
        $per_page = $this->request->input('per_page', 10);

        $params = array();
        $params['doctor_id'] = $doctor_id;

        $order_evaluation = OrderEvaluation::getPage($params, ['*'],$page,$per_page);
        if (!empty($order_evaluation['data'])) {
            foreach ($order_evaluation['data'] as &$item) {
                $item['reply_quality'] = floor($item['reply_quality'] * 0.05);
                $item['service_attitude'] = floor($item['service_attitude'] * 0.05);
                $item['reply_progress'] = floor($item['reply_progress'] * 0.05);
                $item['avg_score'] = floor($item['avg_score'] * 0.05);
            }
        }
        return success($order_evaluation);
    }

    /**
     * 新增关注医生
     * @return array
     */
    public function addDoctorFollow(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $doctor_id = $this->request->route('doctor_id');

        $params = array();
        $params['patient_id'] = $user_info['client_user_id'];
        $params['doctor_id'] = $doctor_id;
        $res = PatientFollow::getExists($params);
        if ($res){
            // 已关注，重复请求
            return success();
        }

        $data = array();
        $data['patient_id'] = $user_info['client_user_id'];
        $data['doctor_id'] = $doctor_id;
        $patient_follow = PatientFollow::addPatientFollow($data);
        if (empty($patient_follow)){
            return fail(HttpEnumCode::SERVER_ERROR);
        }

        return success();
    }

    /**
     * 取消关注医生
     * @return array
     */
    public function deleteDoctorFollow(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $doctor_id = $this->request->route('doctor_id');

        $params = array();
        $params['patient_id'] = $user_info['client_user_id'];
        $params['doctor_id'] = $doctor_id;
        $res = PatientFollow::getExists($params);
        if (!$res){
            // 未关注，执行取消关注
            return success();
        }

        $params = array();
        $params['patient_id'] = $user_info['client_user_id'];
        $params['doctor_id'] = $doctor_id;
        PatientFollow::deletePatientFollow($params);

        return success();
    }

    /**
     * 获取首页服务过患者的医生
     * 限制条数
     * @param string $patient_id
     * @return array
     */
    public function getIndexPatientDoctorLimit(string $patient_id): array
    {
        $results = array();

        $params = array();
        $params['patient_id'] = $patient_id;
        $params['history_status'] = 1;
        $patient_history_doctors = PatientHistoryInquiryModel::getIndexHistoryDoctorList($params);
        if (!empty($patient_history_doctors)) {
            foreach ($patient_history_doctors as $patient_history_doctor) {
                if (empty($patient_history_doctor['userDoctor'])) {
                    continue;
                }

                if (count($results) > 5){
                    // 超出5个
                    continue;
                }

                // 检测是否重复，如果重复，删除最早的一个数据
                foreach ($results as $key => $result){
                    if ($patient_history_doctor['doctor_id'] == $result['doctor_id']){
                        unset($results[$key]);
                    }
                }

                // 组合数据
                $data = array();
                $data['doctor_id'] = $patient_history_doctor['userDoctor']['doctor_id'];
                $data['user_name'] = $patient_history_doctor['userDoctor']['user_name'];
                $data['avatar'] = addAliyunOssWebsite($patient_history_doctor['userDoctor']['avatar']);
                $data['hospital_name'] = "";
                if (!empty($patient_history_doctor['userDoctor']['Hospital'])) {
                    $data['hospital_name'] = $patient_history_doctor['userDoctor']['Hospital']['hospital_name'];
                }

                // 按照天来计算，当日为1，前一天为2 未服务过为0
                if (empty($patient_history_doctor['created_at'])) {
                    $data['days'] = 0;
                } else {
                    $data['days'] = ceil((strtotime(date('Y-m-d', strtotime('+1 day'))) - strtotime($patient_history_doctor['created_at'])) / 86400);
                }

                $results[] = $data;

                unset($data);
            }
        }

        $results = array_merge($results);
        return $results;
    }
}