<?php

namespace App\Services;

use App\Model\BasicBank;
use App\Model\BasicJob;
use App\Model\BasicNation;
use App\Model\DiseaseClass;
use App\Model\DiseaseClassExpertise;
use App\Model\DiseaseClassIcd;
use App\Model\Hospital;
use App\Model\HospitalDepartmentCustom;
use App\Model\OperationManual;
use App\Model\Product;
use Hyperf\Redis\Redis;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 基础数据服务类
 */
class BasicDataService extends BaseService
{
    /**
     * 获取医院数据
     * @return array
     */
    public function getHospital(): array
    {
        $hospital_name = $this->request->input('hospital_name');
        $province_id = $this->request->input('province_id');
        $city_id = $this->request->input('city_id');
        $county_id = $this->request->input('county_id');

        $params = array();
        $params[] = ['hospital_status', '=', 1];

        if (!empty($hospital_name)) {
            $params[] = ['hospital_name', 'like', '%' . $hospital_name . '%'];
        }

        if (!empty($province_id)) {
            $params[] = ['province_id', '=', $province_id];
        }

        if (!empty($city_id)) {
            $params[] = ['city_id', '=', $city_id];
        }

        if (!empty($county_id)) {
            $params[] = ['county_id', '=', $county_id];
        }

        $fields = [
            'hospital_id',
            'hospital_name',
        ];

        $hospital = Hospital::getList($params, $fields);
        if (empty($hospital)) {
            return success();
        }

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

    /**
     * 获取自定义科室数据
     * @return array
     */
    public function getCustomDepartment(): array
    {
        $params = array();
        $params['department_status'] = 1;

        $fields = [
            'department_custom_id',
            'department_custom_name',
        ];
        $hospital_department_custom = HospitalDepartmentCustom::getList($params, $fields);
        if (empty($hospital_department_custom)) {
            return success();
        }

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

    /**
     * 获取银行列表
     * @return array
     */
    public function getBank(): array
    {
        $basic_bank = BasicBank::getList([]);
        if (empty($basic_bank)) {
            return success();
        }

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

    /**
     * 获取操作手册列表
     * @return array
     */
    public function getOperationManual(): array
    {
        $fields = [
            'manual_id',
            'title',
        ];

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

        $operation_manual = OperationManual::getList($params);
        if (empty($operation_manual)) {
            return success();
        }

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

    /**
     * 获取操作手册详情
     * @return array
     */
    public function getOperationManualInfo(): array
    {
        $manual_id = $this->request->route('manual_id');

        $params = array();
        $params['manual_id'] = $manual_id;
        $params['status'] = 1;

        $operation_manual = OperationManual::getOne($params);
        if (empty($operation_manual)) {
            return fail();
        }

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

    /**
     * 搜索疾病分类
     * @return array
     */
    public function getDiseaseSearch(): array
    {
        $disease_class_name = $this->request->input('disease_class_name');

        $fields = [
            'disease_class_id',
            'disease_class_name',
        ];
        $params = array();
        $params[] = ["disease_class_status", 1];
        $params[] = ["disease_class_enable", 1];
        $params[] = ['disease_class_name', 'like', '%' . $disease_class_name . '%'];
        $disease_class = DiseaseClass::getList($params, $fields);
        if (empty($disease_class)){
            $params = array();
            $params[] = ["disease_class_status", 1];
            $params[] = ["disease_class_enable", 1];
            $disease_class = DiseaseClass::getList($params, $fields);
            if(empty($disease_class)){
                return success();
            }
        }


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

    /**
     * 获取常见疾病分类
     * @return array
     */
    public function getDiseaseHot(): array
    {
        $fields = [
            'disease_class_id',
            'disease_class_name',
        ];

        $params = array();
        $params[] = ["disease_class_status", 1];
        $params[] = ["disease_class_enable", 1];
        $params[] = ['is_hot', 1];
        $disease_class = DiseaseClass::getLimit($params, 10, $fields);

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

    /**
     * 专长列表
     * @return array
     */
    public function getDiseaseExpertiseList(): array
    {
        $params = array();
        $fields = [
            'expertise_id',
            'expertise_name',
        ];
        $disease_class_expertise = DiseaseClassExpertise::getOrderList($params, $fields);
        return success($disease_class_expertise);
    }

    /**
     * 搜索商品
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getProductSearch(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $product_keyword = $this->request->input('product_keyword',"");

        $fields = [
            'product_id',
            'product_platform_id',
            'product_name',
            'product_price',
            'product_cover_img',
            'product_spec',
            'manufacturer',
            'single_unit',
            'single_use',
            'packaging_unit',
            'frequency_use',
            'available_days',
        ];

        $params = array();

        $product = Product::getSearchKeywordList($params, $product_keyword,$fields);
        if (empty($product)) {
            return success();
        }

        // 获取购物车缓存数据
        $redis = $this->container->get(Redis::class);

        $redis_key = "shopping_cart" . $user_info['client_user_id'];
        $shopping_cart = $redis->get($redis_key);
        if (!empty($shopping_cart)){
            $shopping_cart = json_decode($shopping_cart,true);
        }

        foreach ($product as &$item) {
            $item['product_cover_img'] = addAliyunOssWebsite($item['product_cover_img']);

            $item['product_name'] = $item['product_name'] . ' ' . $item['product_spec'];
            $item['stock'] = 0;
            if (!empty($item['ProductPlatformAmount'])){
                $item['stock'] = $item['ProductPlatformAmount']['stock'];
            }

            unset($item['ProductPlatformAmount']);

            // 处理购物车商品数据
            $item['shopping_cart_num'] = 0;
            if (!empty($shopping_cart)){
                foreach ($shopping_cart as $value){
                    if ($item['product_id'] == $value['product_id']){
                        $item['shopping_cart_num'] = $value['shopping_cart_num'];
                    }
                }
            }
        }

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

    /**
     * 搜索平台疾病分类
     * @return array
     */
    public function getDiseaseIcdSearch(): array
    {
        $icd_keyword = $this->request->input('icd_keyword','');

        $fields = [
            'icd_id',
            'icd_name',
        ];
        $params = array();
        $params['icd_status'] = 1;// 状态（0:删除 1:正常）
        $params['icd_enable'] = 1;// 是否启用（0:否 1:是）
        $disease_class_icd = DiseaseClassIcd::getSearchKeywordLimit($params, $icd_keyword, $fields);
        if (empty($disease_class_icd)){
            return success();
        }

        return success($disease_class_icd->toArray());

    }

    /**
     * 获取民族数据
     * @return array
     */
    public function getNation(): array
    {
        $params = array();
        $basic_nation = BasicNation::getList($params);
        if (empty($basic_nation)){
            return success();
        }
        return success($basic_nation->toArray());
    }

    /**
     * 获取职业数据
     * @return array
     */
    public function getJob(): array
    {
        $params = array();
        $basic_job = BasicJob::getList($params);
        if (empty($basic_job)){
            return success();
        }
        return success($basic_job->toArray());
    }
}