<?php

namespace App\Utils;

use App\Model\Area;
use App\Model\Hospital;
use App\Model\TbHospitalMy;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Snowflake\IdGeneratorInterface;
use Psr\Container\ContainerInterface;

class Data
{
    #[Inject]
    protected ContainerInterface $container;

    // 迁移医院数据
    public function hospital()
    {
        $generator = $this->container->get(IdGeneratorInterface::class);

        $params = array();
        $params['prov_name'] = "贵州省";
        $hospital = TbHospitalMy::getList($params);
        foreach ($hospital as $key => $value) {
            $params = array();
            $params['hospital_name'] = $value['name'];
            $params['province'] = $value['prov_name'];
            $params['city'] = $value['city_name'];
            $res = Hospital::getOne($params);
            if(!empty($res)){
                dump("重复-跳过-".$value['name']);
                $params = array();
                $params['id'] = $value['id'];
                $res = Db::table('tb_hospital_my')->where($params)->delete();
                if (!$res) {
                    return "删除错误";
                }
                continue;
            }

            $data = array();
            $data['hospital_id'] = $generator->generate();;
            $data['hospital_name'] = $value['name'];
            $data['hospital_status'] = 1;

            if ($value['level'] == 0){
                $data['hospital_level_name'] = "未知";
            }elseif ($value['level'] == 1){
                $data['hospital_level_name'] = "三甲";
            }elseif ($value['level'] == 2){
                $data['hospital_level_name'] = "三级";
            }elseif ($value['level'] == 3){
                $data['hospital_level_name'] = "二级";
            }elseif ($value['level'] == 4){
                $data['hospital_level_name'] = "其他";
            }else{
                continue;
            }

            $data['post_code'] = $value['postcode'];
            $data['tele_phone'] = $value['office_phone'];
            $data['lat'] = $value['lat'];
            $data['lng'] = $value['lng'];
            $data['desc'] = $value['info'];
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            // 省
            $params = array();
            $params['area_name'] = $value['prov_name'];
            $province = Area::getOne($params);
            if(empty($province)){
                dump("省份-未知-".$value['prov_name']);
                continue;
            }else{
                $data['province_id'] = $province['area_id'];
                $data['province'] = $province['area_name'];
            }

            // 市
            $params = array();
            $params['area_name'] = $value['city_name'];
            $params['parent_id'] = $province['area_id'];
            $city = Area::getOne($params);
            if(empty($city)){
                dump("市区-未知-".$value['city_name']);
                continue;
            }else{
                $data['city_id'] = $city['area_id'];
                $data['city'] = $city['area_name'];
            }

            $params = array();
            $params['area_name'] = $value['county_name'];
            $params['parent_id'] = $city['area_id'];
            $county = Area::getOne($params);
            if(empty($county)){
                dump("区县-未知-".$value['county_name']);
                continue;
            }else{
                $data['county_id'] = $county['area_id'];
                $data['county'] = $county['area_name'];
            }

            $data['address'] = $value['address'];

            $res = Hospital::addHospital($data);
            if (empty($res)) {
                dump("添加-失败-跳过");
                continue;
            }

            $params = array();
            $params['id'] = $value['id'];
            $res = Db::table('tb_hospital_my')->where($params)->delete();
            if (!$res) {
                return "删除错误";
            }
        }

        return success();
    }
}