<?php

namespace App\Services;

use App\Constants\DoctorTitleCode;
use App\Constants\HttpEnumCode;
use App\Model\DoctorExpertise;
use App\Model\DoctorIdenFail;
use App\Model\Hospital;
use App\Model\HospitalDepartmentCustom;
use App\Model\User;
use App\Model\UserDoctor;
use App\Model\UserDoctorInfo;
use App\Utils\Mask;
use App\Utils\PcreMatch;
use Extend\VerifyDun\IdCard;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\DbConnection\Db;

/**
 * 医生认证
 */
class DoctorAuthService extends BaseService
{
    /**
     * 获取实名认证信息
     * @return array
     */
    public function getAuthReal(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        // 获取医生数据
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor = UserDoctor::getOne($params);
        if (empty($doctor)) {
            return fail(HttpEnumCode::HTTP_ERROR, "未知医生");
        }

        // 实名认证状态（0:未认证 1:认证通过 2:认证失败）
        if ($doctor['idcard_status'] == 0) {
            return success();
        }

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

        $fields = [
            'doctor_info_id',
            'doctor_id',
            'card_type',
            'card_name',
            'card_name_mask',
            'card_num',
            'card_num_mask',
        ];
        $user_doctor_info = UserDoctorInfo::getOne($params, $fields);
        if (empty($user_doctor_info)) {
            return success();
        }

        return success($user_doctor_info->toArray());
    }

    /**
     * 新增实名认证
     * @return array
     * @throws GuzzleException
     */
    public function addAuthReal(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $card_name = $this->request->input('card_name');
        $card_num = $this->request->input('card_num');

        // 身份证验证
        $card_num = PcreMatch::pregIdCard($card_num);
        if (empty($card_num)) {
            return fail();
        }

        // 身份证最后一位x转换为大写
        $card_num = PcreMatch::pregIdCardX($card_num);
        if (empty($card_num)) {
            return fail();
        }

        // 身份证掩码
        $card_num_mask = Mask::maskIdCard($card_num);

        // 姓名掩码
        $card_name_mask = Mask::maskNameStr($card_name);

        // 获取医生数据
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor = UserDoctor::getOne($params);
        if (empty($doctor)) {
            return fail(HttpEnumCode::HTTP_ERROR, "未知医生");
        }

        // 实名认证状态（0:未认证 1:认证通过 2:认证失败）
        if ($doctor['idcard_status'] == 1) {
            return fail(HttpEnumCode::HTTP_ERROR, "已通过实名认证，无法修改");
        }

        // 检测身份证号是否已被实名认证
        $params = array();
        $params['card_num'] = $card_num;
        $res = UserDoctorInfo::getExists($params);
        if ($res){
            return fail(HttpEnumCode::HTTP_ERROR,"身份证已被使用");
        }

        // 网易易盾认证
        // 实人认证-生产环境开启
        $IdCard = new IdCard();

        $params = array();
        $params['name'] = $card_name;
        $params['cardNo'] = $card_num;
        $res = $IdCard->checkIdCard($params);
        if (!empty($res)) {
            return fail(HttpEnumCode::HTTP_ERROR, $res);
        }

        Db::beginTransaction();
        try {
            // 获取医生详情数据
            $params = array();
            $params['doctor_id'] = $user_info['client_user_id'];
            $user_doctor_info = UserDoctorInfo::getOne($params);
            if (empty($user_doctor_info)) {
                // 新增医生详情
                $data = array();
                $data['user_id'] = $user_info['user_id'];
                $data['doctor_id'] = $user_info['client_user_id'];
                $data['card_type'] = 1;
                $data['card_name'] = $card_name;
                $data['card_name_mask'] = $card_name_mask;
                $data['card_num'] = $card_num;
                $data['card_num_mask'] = $card_num_mask;

                $user_doctor_info = UserDoctorInfo::addUserDoctorInfo($data);
                if (empty($user_doctor_info)) {
                    Db::rollBack();
                    return fail(HttpEnumCode::SERVER_ERROR);
                }
            } else {
                if (!empty($user_doctor_info['card_name']) || !empty($user_doctor_info['card_num'])) {
                    Db::rollBack();
                    return fail(HttpEnumCode::HTTP_ERROR, "不可修改");
                }

                // 已存在详情表数据，进行添加
                $data = array();
                $data['card_type'] = 1;
                $data['card_name'] = $card_name;
                $data['card_name_mask'] = $card_name_mask;
                $data['card_num'] = $card_num;
                $data['card_num_mask'] = $card_num_mask;
                $data['updated_at'] = date('Y-m-d H:i:s', time());

                $params = array();
                $params['doctor_info_id'] = $user_doctor_info['doctor_info_id'];
                $res = UserDoctorInfo::editUserDoctorInfo($params, $data);
                if (!$res) {
                    Db::rollBack();
                    return fail(HttpEnumCode::SERVER_ERROR);
                }
            }

            // 医生年龄
            $age = getIdCardAge($card_num);

            // 性别
            $sex = getIdCardSex($card_num);

            // 修改医生实名认证状态
            $data = array();
            $data['idcard_status'] = 1;

            $params = array();
            $params['doctor_id'] = $user_info['client_user_id'];
            $res = UserDoctor::editUserDoctor($params, $data);
            if (!$res) {
                Db::rollBack();
                return fail(HttpEnumCode::SERVER_ERROR);
            }

            $data = array();
            $data['age'] = $age;
            $data['sex'] = $sex;
            $params = array();
            $params['user_id'] = $user_info['user_id'];
            $res = User::editUser($params, $data);
            if (!$res) {
                Db::rollBack();
                return fail(HttpEnumCode::SERVER_ERROR);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollBack();
            return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage());
        }

        return success();
    }

    /**
     * 获取身份认证信息
     * @return array
     */
    public function getAuthIden(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $source = $this->request->input('source'); // 来源（1:身份认证 2:简介）

        // 获取医生数据
        $fields = [
            'doctor_id',
            'user_id',
            'idcard_status',
            'iden_auth_status',
            'avatar',
            'hospital_id',
            'department_custom_id',
            'department_custom_name',
            'department_custom_mobile',
            'doctor_title',
            'brief_introduction',
            'be_good_at',
        ];

        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor = UserDoctor::getOne($params, $fields);
        if (empty($doctor)) {
            return fail(HttpEnumCode::HTTP_ERROR, "未知医生");
        }

        // 实名认证状态（0:未认证 1:认证通过 2:认证失败）
        if ($doctor['idcard_status'] != 1) {
            return fail(HttpEnumCode::HTTP_ERROR, "请先实名认证");
        }

        $result = $doctor->toArray();

        // 头像
        if (!empty($doctor['avatar'])) {
            $result['avatar'] = addAliyunOssWebsite($doctor['avatar']);
        }

        // 职称转换
        $result['doctor_title_name'] = empty($doctor['doctor_title']) ? "" : DoctorTitleCode::getMessage($doctor['doctor_title']);

        if ($source == 1){
            // 获取医生详情数据
            $result['license_cert'] = [];
            $result['qualification_cert'] = [];
            $result['work_cert'] = [];

            $params = array();
            $params['doctor_id'] = $user_info['client_user_id'];
            $user_doctor_info = UserDoctorInfo::getOne($params);
            if (!empty($user_doctor_info)) {
                // 医师执业证
                if (!empty($user_doctor_info['license_cert'])) {
                    $license_cert = explode(',', $user_doctor_info['license_cert']);
                    foreach ($license_cert as &$item) {
                        $item = addAliyunOssWebsite($item);
                    }
                    $result['license_cert'] = $license_cert;
                }

                // 医师职称证
                if (!empty($user_doctor_info['qualification_cert'])) {
                    $qualification_cert = explode(',', $user_doctor_info['qualification_cert']);
                    foreach ($qualification_cert as &$item) {
                        $item = addAliyunOssWebsite($item);
                    }
                    $result['qualification_cert'] = $qualification_cert;
                }

                // 医师资格证
                if (!empty($user_doctor_info['work_cert'])) {
                    $work_cert = explode(',', $user_doctor_info['work_cert']);
                    foreach ($work_cert as &$item) {
                        $item = addAliyunOssWebsite($item);
                    }
                    $result['work_cert'] = $work_cert;
                }
            }
        }

        // 获取医生医院
        $result['hospital'] = [];

        $fields = [
            'hospital_id',
            'hospital_name',
            'hospital_level_name',
            'province_id',
            'province',
            'city_id',
            'city',
            'county_id',
            'county',
        ];

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

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

        // 获取医生身份认证失败原因
        $iden_auth_fail_reson = $this->getIdenAuthFailReason($doctor['iden_auth_status'], $doctor['doctor_id']);

        $result['avatar_reason'] = $iden_auth_fail_reson['avatar_reason'] ?? "";
        $result['department_custom_mobile_reason'] = $iden_auth_fail_reson['department_custom_mobile_reason'] ?? "";
        $result['brief_introduction_reason'] = $iden_auth_fail_reson['brief_introduction_reason'] ?? "";
        $result['be_good_at_reason'] = $iden_auth_fail_reson['be_good_at_reason'] ?? "";
        if ($source == 1){
            $result['license_cert_reason'] = $iden_auth_fail_reson['license_cert_reason'] ?? "";
            $result['qualification_cert_reason'] = $iden_auth_fail_reson['qualification_cert_reason'] ?? "";
            $result['work_cert_reason'] = $iden_auth_fail_reson['work_cert_reason'] ?? "";
        }
        $result['department_custom_name_reason'] = $iden_auth_fail_reson['department_custom_name_reason'] ?? "";

        return success($result);
    }

    /**
     * 新增身份认证信息
     * @return array
     */
    public function addAuthIden(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $request_params = $this->request->all();

        // 获取医生数据
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor = UserDoctor::getOne($params);
        if (empty($doctor)) {
            return fail(HttpEnumCode::HTTP_ERROR, "未知医生");
        }

        // 实名认证状态（0:未认证 1:认证通过 2:认证失败）
        if ($doctor['idcard_status'] != 1) {
            return fail(HttpEnumCode::HTTP_ERROR, "请先实名认证");
        }

        // 审核中
        if ($doctor['iden_auth_status'] == 2) {
            return fail(HttpEnumCode::HTTP_ERROR, "审核中，暂不允许修改");
        }

        // 组合存储数据
        $doctor_data = array();// 医生
        $doctor_info_data = array();// 医生详情

        // 头像
        $request_params['avatar'] = PcreMatch::pregRemoveOssWebsite($request_params['avatar']);
        if ($doctor['avatar'] != $request_params['avatar']) {
            $doctor_data['avatar'] = $request_params['avatar'];
        }

        // 职称
        if ($doctor['doctor_title'] != $request_params['doctor_title']) {
            $doctor_data['doctor_title'] = $request_params['doctor_title'];
        }

        // 医院
        if ($doctor['hospital_id'] != $request_params['hospital_id']) {
            $params = array();
            $params['hospital_id'] = $request_params['hospital_id'];
            $hospital = Hospital::getOne($params);
            if (empty($hospital)) {
                return fail(HttpEnumCode::HTTP_ERROR, "医院选择错误");
            }

            $doctor_data['hospital_id'] = $request_params['hospital_id'];
        }

        // 科室
        if ($doctor['department_custom_id'] != $request_params['department_custom_id']) {
            $params = array();
            $params['department_custom_id'] = $request_params['department_custom_id'];
            $hospital_department_custom = HospitalDepartmentCustom::getOne($params);
            if (empty($hospital_department_custom)) {
                return fail(HttpEnumCode::HTTP_ERROR, "科室选择错误");
            }

            $doctor_data['department_custom_id'] = $request_params['department_custom_id'];
            $doctor_data['department_custom_name'] = $hospital_department_custom['department_custom_name'];

            if (!empty($request_params['department_custom_name'])) {
                $doctor_data['department_custom_name'] = $request_params['department_custom_name'];
            }
        }

        // 科室电话
        if ($doctor['department_custom_mobile'] != $request_params['department_custom_mobile']) {
            $doctor_data['department_custom_mobile'] = $request_params['department_custom_mobile'];
        }

        // 医生简介
        if ($doctor['brief_introduction'] != $request_params['brief_introduction']) {
            $doctor_data['brief_introduction'] = $request_params['brief_introduction'];
        }

        // 擅长领域
        if ($doctor['be_good_at'] != $request_params['be_good_at']) {
            $doctor_data['be_good_at'] = $request_params['be_good_at'];
        }

        // 身份认证
        if ($request_params['source'] == 1){
            // 获取医生详情数据
            $params = array();
            $params['doctor_id'] = $user_info['client_user_id'];
            $user_doctor_info = UserDoctorInfo::getOne($params);
            if (empty($user_doctor_info)) {
                return fail(HttpEnumCode::SERVER_ERROR);
            }

            // 执业证-上传后无法修改
            if (empty($user_doctor_info['license_cert'])){
                $license_cert = implode(',', $request_params['license_cert']);
                $license_cert = PcreMatch::pregRemoveOssWebsite($license_cert);
                $doctor_info_data['license_cert'] = $license_cert;
            }

            // 医师资格证-上传后无法修改
            if (empty($user_doctor_info['qualification_cert'])){
                $qualification_cert = implode(',', $request_params['qualification_cert']);
                $qualification_cert = PcreMatch::pregRemoveOssWebsite($qualification_cert);
                $doctor_info_data['qualification_cert'] = $qualification_cert;
            }


            // 医师工作证-上传后无法修改
            if (empty($user_doctor_info['work_cert'])){
                $work_cert = implode(',', $request_params['work_cert']);
                $work_cert = PcreMatch::pregRemoveOssWebsite($work_cert);
                $doctor_info_data['work_cert'] = $work_cert;
            }
        }


        //已选择专长列表
        $expertise_ids = [];

        // 获取医生专长
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor_expertise = DoctorExpertise::getList($params);
        if (!empty($doctor_expertise)) {
            $expertise_ids = array_column($doctor_expertise->toArray(), 'expertise_id');
        }

        // 对比已选择专长
        // 对比专长是否删除
        $is_delete_expertise = array_diff($request_params['doctor_expertise'], $expertise_ids);

        if (empty($is_delete_expertise)) {
            // 对比专长是否新增
            $is_add_expertise = array_diff($expertise_ids, $request_params['doctor_expertise']);
        }

        // 专长是否改动：false（未改动） true（已改动）
        $expertise_is_change = false;
        if (!empty($is_delete_expertise) || !empty($is_add_expertise)) {
            $expertise_is_change = true;
        }

        // 审核失败，重新提交
        if ($doctor['iden_auth_status'] == 3){
            // 获取认证失败字段
            $params = array();
            $params['doctor_id'] = $user_info['client_user_id'];
            $doctor_iden_fails = DoctorIdenFail::getList($params);
            if (!empty($doctor_iden_fails)){
                foreach ($doctor_iden_fails as $doctor_iden_fail){
                    // 科室电话
                    if ($doctor_iden_fail['field_name'] == "department_custom_mobile") {
                        if ($doctor['department_custom_mobile'] == $request_params['department_custom_mobile']){
                            return fail(HttpEnumCode::HTTP_ERROR,"科室电话" . $doctor_iden_fail['fail_reason']);
                        }
                    }

                    // 医生简介
                    if ($doctor_iden_fail['field_name'] == "brief_introduction") {
                        if ($doctor['brief_introduction'] == $request_params['brief_introduction']){
                            return fail(HttpEnumCode::HTTP_ERROR,"医生简介" . $doctor_iden_fail['fail_reason']);
                        }
                    }

                    // 擅长领域
                    if ($doctor_iden_fail['field_name'] == "be_good_at") {
                        if ($doctor['be_good_at'] == $request_params['be_good_at']){
                            return fail(HttpEnumCode::HTTP_ERROR,"擅长领域" . $doctor_iden_fail['fail_reason']);
                        }
                    }
                }
            }

        }

        Db::beginTransaction();
        try {
            // 专长有改动
            if ($expertise_is_change) {
                // 删除原专长
                $params = array();
                $params['doctor_id'] = $user_info['client_user_id'];
                DoctorExpertise::deleteDoctorExpertise($params);

                // 新增专长
                foreach ($request_params['doctor_expertise'] as $item) {
                    $data = array();
                    $data['doctor_id'] = $user_info['client_user_id'];
                    $data['expertise_id'] = $item;

                    $doctor_expertise = DoctorExpertise::addDoctorExpertise($data);
                    if (empty($doctor_expertise)) {
                        Db::rollBack();
                        return fail(HttpEnumCode::SERVER_ERROR);
                    }
                }
            }

            // 医生数据
            if (!empty($doctor_data)) {
                $params = array();
                $params['doctor_id'] = $user_info['client_user_id'];

                $doctor_data['updated_at'] = date('Y-m-d H:i:s', time());
                $res = UserDoctor::editUserDoctor($params, $doctor_data);
                if (!$res) {
                    Db::rollBack();
                    return fail(HttpEnumCode::SERVER_ERROR);
                }
            }

            // 医生详情数据
            if (!empty($doctor_info_data)) {
                // 修改医生详情数据
                $params = array();
                $params['doctor_id'] = $user_info['client_user_id'];

                $doctor_info_data['updated_at'] = date('Y-m-d H:i:s', time());
                $res = UserDoctorInfo::editUserDoctorInfo($params, $doctor_info_data);
                if (!$res) {
                    Db::rollBack();
                    return fail(HttpEnumCode::SERVER_ERROR);
                }
            }

            // 修改审核状态
            if (!empty($doctor_data) || !empty($doctor_info_data) || $expertise_is_change) {
                $params = array();
                $params['doctor_id'] = $user_info['client_user_id'];

                $data = array();
                $data['iden_auth_status'] = 2;// 身份认证状态-审核中
                $data['iden_auth_fail_reason'] = "";// 身份认证失败原因
                $data['updated_at'] = date('Y-m-d H:i:s', time());
                $res = UserDoctor::editUserDoctor($params, $data);
                if (!$res) {
                    Db::rollBack();
                    return fail(HttpEnumCode::SERVER_ERROR);
                }
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollBack();
            return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage());
        }

        return success();
    }

    /**
     * 获取多点执业认证信息
     * @return array
     */
    public function getAuthMulti(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        // 获取医生数据
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor = UserDoctor::getOne($params);
        if (empty($doctor)) {
            return fail(HttpEnumCode::HTTP_ERROR, "未知医生");
        }

        // 获取医生详情数据
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $user_doctor_info = UserDoctorInfo::getOne($params);
        if (empty($user_doctor_info)) {
            return success();
        }

        $result = array();
        $result['id_card_front'] = addAliyunOssWebsite($user_doctor_info['id_card_front']);
        $result['id_card_back'] = addAliyunOssWebsite($user_doctor_info['id_card_back']);
        $result['sign_image'] = addAliyunOssWebsite($user_doctor_info['sign_image']);
        $result['multi_point_status'] = $doctor['multi_point_status']; // 医生多点执业认证状态（0:未认证 1:认证通过 2:审核中 3:认证失败）
        $result['multi_point_fail_reason'] = $doctor['multi_point_fail_reason']; // 多点执业认证失败原因

        return success($result);
    }

    /**
     * 新增多点执业认证信息
     * @return array
     */
    public function addAuthMulti(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $id_card_front = $this->request->input('id_card_front');
        $id_card_back = $this->request->input('id_card_back');
        $sign_image = $this->request->input('sign_image');

        // 获取医生数据
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $doctor = UserDoctor::getOne($params);
        if (empty($doctor)) {
            return fail(HttpEnumCode::HTTP_ERROR, "未知医生");
        }

        if ($doctor['idcard_status'] != 1) {
            return fail(HttpEnumCode::HTTP_ERROR, "请先进行实名认证");
        }

        if ($doctor['iden_auth_status'] != 1) {
            return fail(HttpEnumCode::HTTP_ERROR, "请先进行身份认证");
        }

        // 审核中
        if ($doctor['multi_point_status'] == 2) {
            return fail(HttpEnumCode::HTTP_ERROR, "审核中，暂不允许修改");
        }

        // 获取医生详情数据
        $params = array();
        $params['doctor_id'] = $user_info['client_user_id'];
        $user_doctor_info = UserDoctorInfo::getOne($params);
        if (empty($user_doctor_info)) {
            return fail(HttpEnumCode::SERVER_ERROR);
        }

        $doctor_info_data = array();

        // 身份证正面
        $id_card_front = PcreMatch::pregRemoveOssWebsite($id_card_front);
        if ($id_card_front != $user_doctor_info['id_card_front']) {
            $doctor_info_data['id_card_front'] = $id_card_front;
        }

        // 身份证反面
        $id_card_back = PcreMatch::pregRemoveOssWebsite($id_card_back);
        if ($id_card_back != $user_doctor_info['id_card_back']) {
            $doctor_info_data['id_card_back'] = $id_card_back;
        }

        // 签名
        $sign_image = PcreMatch::pregRemoveOssWebsite($sign_image);
        if ($sign_image != $user_doctor_info['sign_image']) {
            $doctor_info_data['sign_image'] = $sign_image;
        }

        if (empty($doctor_info_data)) {
            return success();
        }

        Db::beginTransaction();
        try {
            $params = array();
            $params['doctor_id'] = $user_info['client_user_id'];

            $res = UserDoctorInfo::editUserDoctorInfo($params, $doctor_info_data);
            if (!$res) {
                Db::rollBack();
                return fail(HttpEnumCode::SERVER_ERROR);
            }

            // 修改审核状态
            $params = array();
            $params['doctor_id'] = $user_info['client_user_id'];

            $data = array();
            $data['multi_point_status'] = 2;// 医生多点执业认证状态（0:未认证 1:认证通过 2:审核中 3:认证失败）
            $data['multi_point_fail_reason'] = "";// 多点执业认证失败原因
            $data['updated_at'] = date('Y-m-d H:i:s', time());
            $res = UserDoctor::editUserDoctor($params, $data);
            if (!$res) {
                Db::rollBack();
                return fail(HttpEnumCode::SERVER_ERROR);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollBack();
            return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage());
        }

        return success();
    }

    /**
     * 获取医生身份认证失败原因
     * @param int|string $iden_auth_status
     * @param string|int $doctor_id
     * @return array
     */
    public function getIdenAuthFailReason(int|string $iden_auth_status, string|int $doctor_id): array
    {
        $result = array();

        // 身份认证状态（0:未认证 1:认证通过 2:审核中 3:认证失败）
        if ($iden_auth_status == 3) {
            // 获取认证失败字段
            $params = array();
            $params['doctor_id'] = $doctor_id;
            $doctor_iden_fails = DoctorIdenFail::getList($params);

            if (!empty($doctor_iden_fails)) {
                foreach ($doctor_iden_fails as $doctor_iden_fail) {
                    // 头像
                    if ($doctor_iden_fail['field_name'] == "avatar") {
                        $result['avatar_reason'] = $doctor_iden_fail['fail_reason'];
                    }

                    // 科室电话
                    if ($doctor_iden_fail['field_name'] == "department_custom_mobile") {
                        $result['department_custom_mobile_reason'] = $doctor_iden_fail['fail_reason'];
                    }

                    // 科室名称
                    if ($doctor_iden_fail['field_name'] == "department_custom_name") {
                        $result['department_custom_name_reason'] = $doctor_iden_fail['fail_reason'];
                    }

                    // 医生简介
                    if ($doctor_iden_fail['field_name'] == "brief_introduction") {
                        $result['brief_introduction_reason'] = $doctor_iden_fail['fail_reason'];
                    }

                    // 擅长
                    if ($doctor_iden_fail['field_name'] == "be_good_at") {
                        $result['be_good_at_reason'] = $doctor_iden_fail['fail_reason'];
                    }

                    // 医师执业证
                    if ($doctor_iden_fail['field_name'] == "license_cert") {
                        $result['license_cert_reason'] = $doctor_iden_fail['fail_reason'];
                    }

                    // 医师资格证
                    if ($doctor_iden_fail['field_name'] == "qualification_cert") {
                        $result['qualification_cert_reason'] = $doctor_iden_fail['fail_reason'];
                    }

                    // 医师工作证
                    if ($doctor_iden_fail['field_name'] == "work_cert") {
                        $result['work_cert_reason'] = $doctor_iden_fail['fail_reason'];
                    }
                }
            }
        }

        return $result;
    }
}