<?php

namespace App\Services;

use App\Constants\HttpEnumCode;
use App\Model\BasicJob;
use App\Model\BasicNation;
use App\Model\DiseaseClass;
use App\Model\OrderProduct;
use App\Model\PatientFamily as PatientFamilyModel;
use App\Model\PatientFamilyHealth;
use App\Model\PatientFamilyPersonal as PatientFamilyPersonalModel;
use App\Utils\Mask;
use Extend\VerifyDun\IdCard;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\DbConnection\Db;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

/**
 * 家庭成员
 */
class PatientFamilyService extends BaseService
{
    /**
     * 获取家庭成员列表
     * @return array
     */
    public function getFamilyList(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $params = array();
        $params['patient_id'] = $user_info['client_user_id'];
        $params['status'] = 1;

        $field = [
            'family_id',
            'patient_id',
            'relation',
            'is_default',
            'card_name',
            'card_name_mask',
            'sex',
            'age',
        ];
        $patient_familys = PatientFamilyModel::getList($params, $field);

        return empty($patient_familys) ? success() : success($patient_familys->toArray());
    }

    /**
     * 新增家庭成员
     * @return array
     * @throws GuzzleException
     */
    public function addFamily(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

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

        // 获取全部家庭成员
        $params = array();
        $params['patient_id'] = $user_info['client_user_id'];
        $params['status'] = 1;
        $patient_familys = PatientFamilyModel::getList($params);
        if (!empty($patient_familys)) {
            foreach ($patient_familys as $patient_family) {
                // 检测该家庭成员是否已存在
                if ($patient_family['type'] == $request_params['type'] && $patient_family['id_number'] == $request_params['id_number']) {
                    return fail(HttpEnumCode::HTTP_ERROR, "证件号码重复");
                }

                // 检测姓名是否重复
                if ($patient_family['card_name'] == $request_params['card_name']) {
                    return fail(HttpEnumCode::HTTP_ERROR, "姓名重复");
                }

                // 检测是否默认字段是否重复
                if (isset($request_params['is_default'])) {
                    if ($request_params['is_default'] == 1 && $patient_family['is_default'] == 1) {
                        // 记录原默认id
                        $is_default_id = $patient_family['family_id'];
                    }
                }
            }
        }

        // 检测省市区
        if (!empty($request_params['county_id']) ){
            if (empty($request_params['city_id']) || empty($request_params['province_id'])){
                // 区县存在时需和城市、省份在一块
                return fail(HttpEnumCode::CLIENT_HTTP_ERROR);
            }
        }

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

        $areaService = new AreaService();

        if (!empty($request_params['province_id'])){
            $req = $areaService->checkAreaById($request_params['province_id'],$request_params['city_id'],$request_params['county_id']);
            if(empty($req)){
                return fail(HttpEnumCode::HTTP_ERROR,"地区选择错误");
            }

            $area = $areaService->getAreaById($request_params['province_id'],$request_params['city_id'],$request_params['county_id']);
        }

        if ($request_params['type'] == 1) {
            // 解析年龄、性别字段
            $age = getIdCardAge($this->request->input('id_number'));

            $sex = getIdCardSex($this->request->input('id_number'));
        }

        // 民族
        if (!empty($request_params['nation_id'])){
            $params = array();
            $params['nation_id'] = $request_params['nation_id'];
            $nation = BasicNation::getOne($params);
            if (empty($nation)){
                return fail(HttpEnumCode::HTTP_ERROR,"民族选择错误");
            }
        }

        // 职业
        if (!empty($request_params['job_id'])){
            $params = array();
            $params['job_id'] = $request_params['job_id'];
            $job = BasicJob::getOne($params);
            if (empty($job)){
                return fail(HttpEnumCode::HTTP_ERROR,"职位选择错误");
            }
        }

        // 实人认证-生产环境开启
//        $IdCard = new IdCard();
//
//        $params =array();
//        $params['name'] = $request_params['card_name'];
//        $params['cardNo'] = $request_params['id_number'];
//        $res = $IdCard->checkIdCard($params);
//        if (!empty($res)){
//            return fail(HttpEnumCode::HTTP_ERROR,$res);
//        }

        Db::beginTransaction();

        try {
            // 修改是否默认
            if (isset($is_default_id)) {
                $params = array();
                $params['family_id'] = $is_default_id;

                $data = array();
                $data['is_default'] = 0;
                $data['updated_at'] = date('Y-m-d H:i:s', time());
                $res = PatientFamilyModel::edit($params, $data);
                if (!$res) {
                    Db::rollBack();
                    return fail(HttpEnumCode::SERVER_ERROR);
                }
            }

            // 新增 患者家庭成员信息表-基本信息（patient_family）
            $data = array();
            $data['patient_id'] = $user_info['client_user_id'];
            $data['relation'] = $request_params['relation'];
            $data['status'] = 1;
            $data['is_default'] = empty($request_params['is_default']) ? 0 : 1;
            $data['card_name'] = $request_params['card_name'];
            $data['card_name_mask'] = Mask::maskNameStr($request_params['card_name']);
            $data['mobile'] = $request_params['mobile'];
            $data['mobile_mask'] = Mask::maskPhoneStr($request_params['mobile']);
            $data['type'] = $request_params['type'];
            $data['id_number'] = $request_params['id_number'];
            $data['id_number_mask'] = Mask::maskIdCard($request_params['id_number']);
            $data['sex'] = $sex ?? 0;
            $data['age'] = empty($age) ? null : $age;

            if (isset($area)) {
                $data['province_id'] = $request_params['province_id'];
                $data['province'] = $area['province']['area_name'] ?? "";

                $data['city_id'] = $request_params['city_id'];
                $data['city'] = $area['city']['area_name'] ?? "";

                $data['county_id'] = $request_params['county_id'];
                $data['county'] = $area['county']['area_name'] ?? "";
            }

            if (!empty($request_params['height'])){
                $data['height'] = $request_params['height'];
            }

            if (!empty($request_params['weight'])){
                $data['weight'] = $request_params['weight'];
            }

            $data['marital_status'] = $request_params['marital_status'] ?? 0;
            if (!empty($request_params['nation_id'])){
                $data['nation_id'] = $request_params['nation_id'];
                $data['nation_name'] = $nation['nation_name'];
            }

            if (!empty($request_params['job_id'])){
                $data['job_id'] = $request_params['job_id'];
                $data['job_name'] = $job['job_name'];
            }

            $patient_family = PatientFamilyModel::addPatientFamily($data);
            if (empty($patient_family)) {
                Db::rollBack();
                return fail(HttpEnumCode::SERVER_ERROR);
            }

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

        $result = array();
        $result['family_id'] = $patient_family['family_id'];
        return success($result);
    }

    /**
     * 删除家庭成员
     * @return array
     */
    public function deleteFamily(): array
    {
        $family_id = $this->request->route('family_id');

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

        // 查看是否存在
        $params = array();
        $params['family_id'] = $family_id;
        $params['patient_id'] = $user_info['client_user_id'];
        $params['status'] = 1;
        $patient_family = PatientFamilyModel::getOne($params);
        if (empty($patient_family)) {
            return fail();
        }

        Db::beginTransaction();

        try {
            // 删除家庭成员
            $params = array();
            $params['family_id'] = $family_id;

            $data = array();
            $data['status'] = 2; // 状态（1:正常 2:删除）
            PatientFamilyModel::edit($params, $data);

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

    /**
     * 获取家庭成员详情
     * @return array
     */
    public function getFamily(): array
    {
        $family_id = $this->request->route('family_id');

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

        // 查看是否存在
        $params = array();
        $params['family_id'] = $family_id;
        $params['patient_id'] = $user_info['client_user_id'];
        $params['status'] = 1;

        $patient_family = PatientFamilyModel::getOne($params);
        if (empty($patient_family)){
            return success();
        }

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

    /**
     * 修改家庭成员
     * @return array
     * @throws GuzzleException
     */
    public function editFamily(): array
    {
        $family_id = $this->request->route('family_id');

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

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

        // 查看是否存在
        $params = array();
        $params['family_id'] = $family_id;
        $params['patient_id'] = $user_info['client_user_id'];
        $params['status'] = 1;
        $patient_family = PatientFamilyModel::getOne($params);
        if (empty($patient_family)) {
            return fail(HttpEnumCode::HTTP_ERROR, "成员不存在");
        }

        // 获取全部家庭成员
        $params = array();
        $params[] = ["patient_id", '=', $user_info['client_user_id']];
        $params[] = ["status", '=', 1];
        $params[] = ["family_id", '!=', $family_id];
        $patient_familys = PatientFamilyModel::getList($params);
        if (!empty($patient_familys)) {
            foreach ($patient_familys as $key => $value) {
                if ($value['family_id'] == $family_id){
                    continue;
                }
                // 检测姓名是否重复
                if ($patient_family['card_name'] != $request_params['card_name']) {
                    if ($request_params['card_name'] == $value['card_name']) {
                        // 姓名改变 检测是否与其他家庭成员姓名重复
                        return fail(HttpEnumCode::HTTP_ERROR, "姓名重复");
                    }
                }

                // 检测证件号是否重复
                if ($value['type'] == $request_params['type'] && $request_params['id_number'] == $value['id_number']) {
                    return fail(HttpEnumCode::HTTP_ERROR, "证件号码重复");
                }

//                // 检测患者关系-本人类型是否重复
//                if ($request_params['relation'] == 1 && $value['relation'] == 1) {
//                    return fail(HttpEnumCode::HTTP_ERROR, "患者类型重复");
//                }

                // 检测是否默认字段是否重复
                if (isset($request_params['is_default'])) {
                    if ($request_params['is_default'] == 1 && $value['is_default'] == 1) {
                        // 记录原默认id
                        $is_default_id = $value['family_id'];
                    }
                }
            }
        }

        // 检测省市区
        if (isset($request_params['province_id']) && isset($request_params['city_id']) && isset($request_params['county_id'])){
            if ($request_params['province_id'] != $patient_family['province_id'] || $request_params['city_id'] != $patient_family['city_id'] || $request_params['county_id'] != $patient_family['county_id']){
                $areaService = new AreaService();

                $req = $areaService->checkAreaById($request_params['province_id'],$request_params['city_id'],$request_params['county_id']);
                if(empty($req)){
                    return fail(HttpEnumCode::HTTP_ERROR,"地区选择错误");
                }

                $area = $areaService->getAreaById($request_params['province_id'],$request_params['city_id'],$request_params['county_id']);
            }
        }

        // 检测证件号是否修改
        if ($patient_family['id_number'] != $request_params['id_number'] || $patient_family['card_name'] != $request_params['card_name']){
            // 解析年龄、性别字段
            $age = getIdCardAge($this->request->input('id_number'));

            $sex = getIdCardSex($this->request->input('id_number'));

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

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

        // 民族
        if (!empty($request_params['nation_id'])){
            if ($patient_family['nation_id'] != $request_params['nation_id']){
                $params = array();
                $params['nation_id'] = $request_params['nation_id'];
                $nation = BasicNation::getOne($params);
                if (empty($nation)){
                    return fail(HttpEnumCode::HTTP_ERROR,"民族选择错误");
                }
            }
        }


        // 职业
        if (!empty($request_params['job_id'])){
            if ($patient_family['job_id'] != $request_params['job_id']){
                $params = array();
                $params['job_id'] = $request_params['job_id'];
                $job = BasicJob::getOne($params);
                if (empty($job)){
                    return fail(HttpEnumCode::HTTP_ERROR,"职位选择错误");
                }
            }
        }

        Db::beginTransaction();

        try {
            // 修改是否默认
            if (isset($is_default_id)) {
                $params = array();
                $params['family_id'] = $is_default_id;

                $data = array();
                $data['is_default'] = 0;
                $data['updated_at'] = date('Y-m-d H:i:s', time());
                PatientFamilyModel::edit($params, $data);
            }

            // 修改 患者家庭成员信息表-基本信息（patient_family）
            $params = array();
            $params['family_id'] = $family_id;

            $data = array();
            if (isset($request_params['relation'])) {
                $data['relation'] = $request_params['relation'];
            }

            if (isset($request_params['is_default'])) {
                if ($request_params['is_default'] != ""){
                    $data['is_default'] = $request_params['is_default'];
                }
            }

            if ($patient_family['card_name'] != $request_params['card_name'] || $patient_family['id_number'] != $request_params['id_number']) {
                $data['card_name'] = $request_params['card_name'];
                $data['card_name_mask'] = Mask::maskNameStr($request_params['card_name']);

                $data['id_number'] = $request_params['id_number'];
                $data['id_number_mask'] = Mask::maskIdCard($request_params['id_number']);
            }

            if(isset($request_params['mobile'])){
                if ($patient_family['mobile'] != $request_params['mobile']) {
                    $data['mobile'] = $request_params['mobile'];
                    $data['mobile_mask'] = Mask::maskPhoneStr($request_params['mobile']);
                }
            }

            $data['type'] = $request_params['type'];
            if (isset($sex)) {
                $data['sex'] = $sex ?? 0;
            }
            if (isset($age)) {
                $data['age'] = empty($age) ? "" : $age;
            }

            if (isset($area)) {
                // 如有改动，变量确定存在
                if ($request_params['province_id'] != $patient_family['province_id']){
                    $data['province_id'] = $request_params['province_id'];
                    $data['province'] = $area['province']['area_name'] ?? "";
                }

                if ($request_params['city_id'] != $patient_family['city_id']){
                    $data['city_id'] = $request_params['city_id'];
                    $data['city'] = $area['city']['area_name'] ?? "";
                }

                if ($request_params['county_id'] != $patient_family['county_id']){
                    $data['county_id'] = $request_params['county_id'];
                    $data['county'] = $area['county']['area_name'] ?? "";
                }
            }

            if(isset($request_params['height'])){
                if ($request_params['height'] != $patient_family['height']){
                    $data['height'] = $request_params['height'];
                }
            }

            if(isset($request_params['weight'])){
                if ($request_params['weight'] != $patient_family['weight']){
                    $data['weight'] = $request_params['weight'];
                }
            }

            if(isset($request_params['marital_status'])){
                if ($request_params['marital_status'] != $patient_family['marital_status']){
                    $data['marital_status'] = $request_params['marital_status'];
                }
            }

            if(isset($request_params['nation_id'])){
                if ($request_params['nation_id'] != $patient_family['nation_id']){
                    $data['nation_id'] = $request_params['nation_id'];
                    $data['nation_name'] = $nation['nation_name'];
                }
            }

            if(isset($request_params['job_id'])){
                if ($request_params['job_id'] != $patient_family['job_id']){
                    $data['job_id'] = $request_params['job_id'];
                    $data['job_name'] = $job['job_name'];
                }
            }

            if (!empty($data)){
                $data['updated_at'] = date('Y-m-d H:i:s', time());
                PatientFamilyModel::edit($params, $data);
            }

            Db::commit();

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

        return success();
    }

    /**
     * 获取家庭成员详情-个人情况
     * @return array
     */
    public function getFamilyPersonal(): array
    {
        $family_id = $this->request->route('family_id');

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

        $params = array();
        $params['family_id'] = $family_id;
        $params['patient_id'] = $user_info['client_user_id'];
        $patient_family_personal = PatientFamilyPersonalModel::getOne($params);

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

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

    /**
     * 修改家庭成员-个人情况
     * @return array
     */
    public function editFamilyPersonal(): array
    {
        $family_personal_id = $this->request->route('family_personal_id');
        $user_info = $this->request->getAttribute("userInfo");
        $request_params = $this->request->all();

        // 获取家庭成员-个人情况数据
        $params = array();
        $params['family_personal_id'] = $family_personal_id;
        $params['patient_id'] = $user_info['client_user_id'];
        $patient_family_personal = PatientFamilyPersonalModel::getOne($params);
        if (empty($patient_family_personal)) {
            return fail();
        }

        // 修改
        $data = array();
        if (isset($request_params['is_allergy_history'])){
            if ($request_params['is_allergy_history'] != $patient_family_personal['is_allergy_history']){
                // 是否存在过敏史（0:否 1:是）
                $data['is_allergy_history'] = $request_params['is_allergy_history'];
                $data['allergy_history'] = $request_params['allergy_history'];
            }
        }

        if (isset($request_params['is_family_history'])){
            if ($request_params['is_family_history'] != $patient_family_personal['is_family_history']){
                // 是否存在家族病史（0:否 1:是）
                $data['is_family_history'] = $request_params['is_family_history'];
                $data['family_history'] = $request_params['family_history'];
            }
        }

        if (isset($request_params['is_pregnant'])){
            if ($request_params['is_pregnant'] != $patient_family_personal['is_pregnant']){
                // 是否备孕、妊娠、哺乳期（0:否 1:是）
                $data['is_pregnant'] = $request_params['is_pregnant'];
                $data['pregnant'] = $request_params['pregnant'];
            }
        }

        if (isset($request_params['is_operation'])){
            if ($request_params['is_operation'] != $patient_family_personal['is_operation']){
                // 是否存在手术（0:否 1:是）
                $data['is_operation'] = $request_params['is_operation'];
                $data['operation'] = $request_params['operation'];
            }
        }

        if (isset($request_params['drink_wine_status'])){
            if ($request_params['drink_wine_status'] != $patient_family_personal['drink_wine_status']){
                // 饮酒状态（1:从不 2:偶尔 3:经常 4:每天 5:已戒酒）
                $data['drink_wine_status'] = $request_params['drink_wine_status'];
            }
        }

        if (isset($request_params['smoke_status'])){
            if ($request_params['smoke_status'] != $patient_family_personal['smoke_status']){
                // 吸烟状态（1:从不 2:偶尔 3:经常 4:每天 5:已戒烟）
                $data['smoke_status'] = $request_params['smoke_status'];
            }
        }

        if (isset($request_params['chemical_compound_status'])){
            if ($request_params['chemical_compound_status'] != $patient_family_personal['chemical_compound_status']){
                // 化合物状态（1:从不 2:偶尔 3:经常 4:每天）
                $data['chemical_compound_status'] = $request_params['chemical_compound_status'];
                $data['chemical_compound_describe'] = $request_params['chemical_compound_describe'];
            }
        }

        if (!empty($data)){
            $data['updated_at'] = date('Y-m-d H:i:s', time());
            $params = array();
            $params['family_personal_id'] = $patient_family_personal['family_personal_id'];

            PatientFamilyPersonalModel::edit($params, $data);
        }

        return success();
    }

    /**
     * 新增家庭成员-个人情况
     * @return array
     */
    public function addFamilyPersonal(): array
    {
        $user_info = $this->request->getAttribute("userInfo");
        $request_params = $this->request->all();

        $params = array();
        $params['family_id'] = $request_params['family_id'];
        $params['patient_id'] = $user_info['client_user_id'];
        $params['status'] = 1;
        $patient_family = PatientFamilyModel::getOne($params);
        if (empty($patient_family)) {
            return fail();
        }

        // 获取家庭成员-个人情况
        $params = array();
        $params['family_id'] = $request_params['family_id'];
        $params['patient_id'] = $user_info['client_user_id'];
        $patient_family_personal = PatientFamilyPersonalModel::getOne($params);
        if (!empty($patient_family_personal)) {
            return fail();
        }

        $data = array();
        $data['family_id'] = $request_params['family_id'];
        $data['patient_id'] = $user_info['client_user_id'];
        if (isset($request_params['is_allergy_history'])){
            $data['is_allergy_history'] = $request_params['is_allergy_history'];
            $data['allergy_history'] = $request_params['allergy_history'] ?? "";
        }

        if (isset($request_params['is_family_history'])){
            $data['is_family_history'] = $request_params['is_family_history'];
            $data['family_history'] = $request_params['family_history'] ?? "";
        }

        if (isset($request_params['is_pregnant'])){
            $data['is_pregnant'] = $request_params['is_pregnant'];
            $data['pregnant'] = $request_params['pregnant'] ?? "";
        }

        if (isset($request_params['is_operation'])){
            $data['is_operation'] = $request_params['is_operation'];
            $data['operation'] = $request_params['operation'] ?? "";
        }

        if (isset($request_params['drink_wine_status'])){
            $data['drink_wine_status'] = $request_params['drink_wine_status'];
            $data['smoke_status'] = $request_params['smoke_status'] ?? "";
        }

        if (isset($request_params['chemical_compound_status'])){
            $data['chemical_compound_status'] = $request_params['chemical_compound_status'];
            $data['chemical_compound_describe'] = $request_params['chemical_compound_describe'];
        }

        $patient_family_personal = PatientFamilyPersonalModel::addPatientFamilyPersonal($data);
        if (empty($patient_family_personal)){
            return fail();
        }

        return success();
    }

    /**
     * 获取家庭成员-健康状况
     * @return array
     */
    public function getFamilyHealth(): array
    {
        $family_id = $this->request->route('family_id');

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

        $params = array();
        $params['family_id'] = $family_id;
        $params['patient_id'] = $user_info['client_user_id'];
        $patient_family_health = PatientFamilyHealth::getOne($params);
        if (empty($patient_family_health)){
            return success();
        }

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

    /**
     * 修改家庭成员-健康情况
     * @return array
     */
    public function editFamilyHealth(): array
    {
        $family_health_id = $this->request->route('family_health_id');
        $user_info = $this->request->getAttribute("userInfo");
        $request_params = $this->request->all();

        // 获取家庭成员-个人情况数据
        $params = array();
        $params['family_health_id'] = $family_health_id;
        $params['patient_id'] = $user_info['client_user_id'];
        $patient_family_health = PatientFamilyHealth::getOne($params);
        if (empty($patient_family_health)) {
            return fail();
        }

        $data = array();
        if (isset($request_params['disease_class_id'])){
            if ($request_params['disease_class_id'] != $patient_family_health['disease_class_id']){
                // 疾病分类id-系统
                $data['disease_class_id'] = $request_params['disease_class_id'];

                // 检测疾病是否正确
                $params = array();
                $params['disease_class_id'] = $request_params['disease_class_id'];
                $disease_class = DiseaseClass::getOne($params);
                if (empty($disease_class)){
                    return fail(HttpEnumCode::HTTP_ERROR,"疾病数据错误");
                }

                $data['disease_class_name'] = $disease_class['disease_class_name'];
            }
        }

        if (isset($request_params['diagnosis_date'])){
            if ($request_params['diagnosis_date'] != $patient_family_health['diagnosis_date']){
                $data['diagnosis_date'] = $request_params['diagnosis_date'];
            }
        }

        if (isset($request_params['diagnosis_hospital'])){
            if ($request_params['diagnosis_hospital'] != $patient_family_health['diagnosis_hospital']){
                $data['diagnosis_hospital'] = $request_params['diagnosis_hospital'];
            }
        }

        if (isset($request_params['drugs_name'])){
            if ($request_params['drugs_name'] != $patient_family_health['drugs_name']){
                $data['drugs_name'] = $request_params['drugs_name'];
            }
        }

        if (!empty($data)){
            $data['updated_at'] = date('Y-m-d H:i:s', time());

            $params = array();
            $params['family_health_id'] = $patient_family_health['family_health_id'];

            PatientFamilyHealth::edit($params,$data);
        }

        return success();
    }

    /**
     * 新增家庭成员-健康情况
     * @return array
     */
    public function addFamilyHealth(): array
    {
        $user_info = $this->request->getAttribute("userInfo");
        $request_params = $this->request->all();

        $params = array();
        $params['family_id'] = $request_params['family_id'];
        $params['patient_id'] = $user_info['client_user_id'];
        $params['status'] = 1;
        $patient_family = PatientFamilyModel::getOne($params);
        if (empty($patient_family)) {
            return fail();
        }

        // 获取家庭成员-个人情况数据
        $params = array();
        $params['family_id'] = $patient_family['family_id'];
        $params['patient_id'] = $user_info['client_user_id'];
        $patient_family_health = PatientFamilyHealth::getOne($params);
        if (!empty($patient_family_health)) {
            return fail();
        }

        $data = array();
        $data['family_id'] = $request_params['family_id'];
        $data['patient_id'] = $user_info['client_user_id'];

        if (!empty($request_params['disease_class_id'])){
            // 疾病分类id-系统
            $data['disease_class_id'] = $request_params['disease_class_id'];

            // 检测疾病是否正确
            $params = array();
            $params['disease_class_id'] = $request_params['disease_class_id'];
            $disease_class = DiseaseClass::getOne($params);
            if (empty($disease_class)){
                return fail(HttpEnumCode::HTTP_ERROR,"疾病数据错误");
            }

            $data['disease_class_name'] = $disease_class['disease_class_name'];
        }

        $data['diagnosis_date'] = $request_params['diagnosis_date'];
        $data['diagnosis_hospital'] = $request_params['diagnosis_hospital'];
        $data['drugs_name'] = $request_params['drugs_name'];
        $patient_family_health = PatientFamilyHealth::addPatientFamilyHealth($data);
        if (empty($patient_family_health)){
            return fail();
        }

        return success();
    }

    /**
     * 获取家庭成员用药记录列表
     * @return array
     */
    public function getFamilyProductRecord(): array
    {
        $user_info = $this->request->getAttribute("userInfo");

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

        $params = array();
        $params['patient_id'] = $user_info['client_user_id'];
        if ($family_id != 0){
            $params['family_id'] = $user_info['family_id'];
        }

        $params['order_product_status'] = 4;// 订单状态（1:待支付 2:待发货 3:已发货 4:已签收 5:已取消）
        $params['pay_status'] = 2;// 支付状态（1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款）

        $fields = [
            'order_product_id',
            'order_inquiry_id',
            'order_prescription_id',
            'family_id',
            'created_at',
        ];

        $result = OrderProduct::getProductRecordPage($params,$fields,$page,$per_page);
        if (!empty($result['data'])){
            foreach ($result['data'] as &$item){
                if (!empty($item['OrderProductItem'])){
                    foreach ($item['OrderProductItem'] as &$order_product_item){
                        $order_product_item['product_cover_img'] = addAliyunOssWebsite($order_product_item['product_cover_img']);
                    }
                }
            }
        }
        return success($result);
    }

}