<?php

namespace App\Services;

use App\Constants\DoctorTitleCode;
use App\Constants\HttpEnumCode;
use App\Model\Banner as BannerModel;
use App\Model\DoctorInquiryConfig as DoctorInquiryConfigModel;
use App\Model\Module as ModuleModel;
use App\Model\PatientHistoryInquiry as PatientHistoryInquiryModel;
use App\Model\PharmacistAuditStatistic;
use App\Model\User;
use App\Model\UserDoctor as UserDoctorModel;
use App\Model\UserPharmacist;

/**
 * 首页模块
 */
class IndexService extends BaseService
{
    /**
     * 医生端-首页
     * @return array
     */
    public function getDoctorIndex(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        // 获取医生数据
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];

        $fields = [
            "doctor_id",
            "user_id",
            "user_name",
            "open_id",
            "status",
            "idcard_status",
            "iden_auth_status",
            "iden_auth_fail_reason",
            "multi_point_status",
            "is_bind_bank",
            "praise_rate",
            "avg_response_time",
            "number_of_fans",
            "avatar",
            "is_online",
            "is_img_expert_reception",
            "is_img_welfare_reception",
        ];

        $doctor = UserDoctorModel::getOne($params, $fields);
        if (empty($doctor)) {
            return fail(HttpEnumCode::USER_STATUS_ERROR);
        }

        if ($doctor['status'] == 0){
            return fail(HttpEnumCode::USER_STATUS_DISABLE);
        }

        if ($doctor['status'] != 1){
            return fail(HttpEnumCode::USER_STATUS_ERROR);
        }

        $InquiryService = new InquiryService();
        $OrderPrescriptionService = new OrderPrescriptionService();

        // 获取未接诊患者个数
        $not_accepted_inquiry_num = $InquiryService->getDoctorNotAcceptedInquiryNum($doctor['doctor_id']);

        // 获取接诊中患者个数
        $accepting_inquiry_num = $InquiryService->getDoctorAcceptingInquiryNum($doctor['doctor_id']);

        // 获取被驳回处方数据
        $reject_prescription_number = $OrderPrescriptionService->getDoctorExistsAuditFail($doctor['doctor_id']);

        // 获取医生问诊价格
        $params = array();
        $params['doctor_id'] = $doctor['doctor_id'];
        $params['inquiry_type'] = 1;// 接诊类型（1:专家问诊 2:快速问诊 3:公益问诊）
        $params['inquiry_mode'] = 1;// 接诊方式（1:图文 2:视频 3:语音 4:电话 5:会员）
        $doctor_inquiry_config = DoctorInquiryConfigModel::getOne($params);

        // 获取banner
        $params = array();
        $params["app_type"] = 1;
        $params["client_type"] = 2;
        $params["banner_place"] = 1;
        $params["banner_status"] = 1;
        $banner = BannerModel::getList($params);
        if (!empty($banner)){
            foreach ($banner as &$item){
                $item['banner_path'] = addAliyunOssWebsite($item['banner_path']);
            }
        }

        $info = array();
        $info['doctor_id'] = $doctor['doctor_id'];
        $info['user_name'] = $doctor['user_name'];
        $info['status'] = $doctor['status'];
        $info['idcard_status'] = $doctor['idcard_status'];// 实名认证状态（0:未认证 1:认证通过 2:认证失败）
        $info['iden_auth_status'] = $doctor['iden_auth_status'];// 身份认证状态（0:未认证 1:认证通过 2:审核中 3:认证失败）
        $info['multi_point_status'] = $doctor['multi_point_status'];// 医生多点执业认证状态（0:未认证 1:认证通过 2:审核中 3:认证失败）
        $info['is_bind_bank'] = $doctor['is_bind_bank'];// 是否已绑定结算银行卡（0:否 1:是）
        $info['avatar'] = addAliyunOssWebsite($doctor['avatar']);
        $info['is_img_expert_reception'] = $doctor['is_img_expert_reception'];// 是否参加专家图文接诊（0:否 1:是）
        $info['is_img_welfare_reception'] = $doctor['is_img_welfare_reception'];// 是否参加公益图文问诊（0:否 1:是）
        $info['praise_rate'] = $doctor['praise_rate'];// 好评率（百分制。回复质量占4、服务态度占3、回复速度占3。每周计算一次）
        $info['avg_response_time'] = $doctor['avg_response_time'];// 平均响应时间（分钟制）
        $info['number_of_fans'] = $doctor['number_of_fans'];// 被关注数量
        $info['inquiry_price'] = $doctor_inquiry_config['inquiry_price'] ?? "";// 在线问诊价格
        $info['not_accepted_inquiry_num'] = $not_accepted_inquiry_num ?? 0;// 获取未接诊患者个数
        $info['accepting_inquiry_num'] = $accepting_inquiry_num ?? 0;// 获取接诊中患者个数
        $info['reject_prescription_number'] = $reject_prescription_number ?? 0;// 获取被驳回处方数据

        $data = array();
        $data['banner'] = $banner ?? [];// banner
        $data['info'] = $info;// 医生数据

        return success($data);
    }

    /**
     * 患者端-首页
     * @return array
     */
    public function getPatientIndex(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        // 获取banner
        $params = array();
        $params["app_type"] = 1;
        $params["client_type"] = 1;
        $params["banner_place"] = 1;
        $params["banner_status"] = 1;
        $banner = BannerModel::getList($params);
        if (!empty($banner)){
            foreach ($banner as &$item){
                $item['banner_path'] = addAliyunOssWebsite($item['banner_path']);
            }
        }

        // 获取模块
        $params = array();
        $params["app_type"] = 1;
        $params["client_type"] = 1;
        $params["module_place"] = 1;
        $params["module_status"] = 1;
        $module = ModuleModel::getList($params);

        $PatientDoctorService = new PatientDoctorService();

        // 我的医生
        $my_doctor = [];
        if (!empty($user_info)) {
            $my_doctor = $PatientDoctorService->getIndexPatientDoctorLimit($user_info['client_user_id']);
        }

        // 推荐医生
        $recommend_doctor = $PatientDoctorService->getIndexRecommendDoctorLimit();

        $result = array();
        $result["banner"] = $banner ?? [];// banner
        $result["recommend_doctor"] = $recommend_doctor['data'] ?? []; // 推荐医生
        $result["my_doctor"] = $my_doctor ?? []; // 我的医生
        $result["module"] = $module ?? []; // 首页模块
        return success($result);
    }

    /**
     * 药师端-首页
     * @return array
     */
    public function getPharmacistIndex(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        // 获取药师数据
        $params = array();
        $params['pharmacist_id'] = $user_info['client_user_id'];

        $fields = [
            "pharmacist_id",
            "user_id",
            "user_name",
            "status",
            "avatar",
            "is_online",
        ];

        $user_pharmacist = UserPharmacist::getOne($params,$fields);
        if (empty($user_pharmacist)) {
            return fail(HttpEnumCode::USER_STATUS_ERROR);
        }

        if ($user_pharmacist['status'] == 0){
            return fail(HttpEnumCode::USER_STATUS_DISABLE);
        }

        if ($user_pharmacist['status'] != 1){
            return fail(HttpEnumCode::USER_STATUS_ERROR);
        }

        $user_pharmacist['avatar'] = addAliyunOssWebsite($user_pharmacist['avatar']);

        // 获取药师审方数量
        $params = array();
        $params['pharmacist_id'] = $user_pharmacist['pharmacist_id'];
        $params['statistics_date'] = date('Y-m-d',time());
        $audit_number = PharmacistAuditStatistic::getCount($params);
        if (empty($audit_number)){
            $audit_number = 0;
        }

        // 组合返回数据
        $data = array();
        $data['pharmacist'] = $user_pharmacist;
        $data['audit_number'] = $audit_number;

        return success($data);
    }



}