<?php
namespace app\controller;

use app\model\XyZskInspection;
use think\facade\Request;
use think\facade\Log;
use Exception;
use app\model\XyZskDisease;
use app\model\XyZskMedicine;

class Neo4j extends CommonTwoController
{

    // 获取列表数据(Admin|Grid)
	public function index()
	{
		$start   = Request::param('start')   ?? 0;
		$perPage = Request::param('perPage') ?? 10;
		$label   = Request::param('label')   ?? 'SickNess';
		$tag     = Request::param('tag')     ?? 'zy';
        $name    = Request::param('name')    ?? '';
        if($name) {
            $list = $this->getNodes($tag , 'n' , $label , $start , $perPage , 'name' , $name);
        } else {
            $list = $this->getNodes($tag , 'n' , $label , $start , $perPage);
        }
		return json($list);
	}


    public function add()
    {
//        $page     = Request::param('page') ?? 1;
//        $pagesize = 10;
//        $limit    = ($page - 1) * $pagesize;
//        $medicines = XyZskMedicine::select()->toArray();
//
//        $medicines = array_column($medicines , 'name');


//        foreach($medicines as $medicine)
//        {
//            $query = "match (n:MedicineProduction)  where n.name= " . "'" . $medicine . "'" . "return n";
//            $data  = $this->getAboutInfo($query);
//            //return $this->_json_succ($data[0]['n']);
//
//            $n = $data[0]['n'] ?? '';
//
//            $m = XyZskMedicine::where('name', $medicine)->find();
//            $m->pinyin = $n['pinyin'] ?? '';
//            $m->medicineNature = $n['medicineNature'] ?? '';
//            $m->oldUse = $n['oldUse']?? '';
//            $m->chilldUse = $n['chilldUse']?? '';
//            $m->medicinePregnant = $n['medicinePregnant']?? '';
//            $m->usageDosage = $n['usageDosage']?? '';
//            $m->notes = $n['notes'] ?? '';
//            $m->periodValidity = $n['periodValidity'] ?? '';
//            $m->indication = $n['indication']?? '';
//
//            $m->storage = $n['storage']     ?? '';
//            $m->character = $n['character'] ?? '';
//            $m->contraindication = $n['contraindication']?? '';
//            $m->pharmacologyToxicology = $n['pharmacologyToxicology']?? '';
//            $m->specification = $n['specification']?? '';
//
//            $m->approvalNumber = $n['approvalNumber']?? '';
//            $m->productionEnterprise = $n['productionEnterprise']?? '';
//            $m->relateSick = $n['relateSick']?? '';
//
//            $m->untowardEffect = $n['untowardEffect']?? '';
//            $m->majorConstituent = $n['majorConstituent']?? '';
//            $m->pname = $n['pname']?? '';
//            $m->medicineInteractions = $n['medicineInteractions']?? '';
//
//            $m->save();
//        }
//        exit;


  //      $start   = Request::param('start')   ?? 0;
//        $perPage = Request::param('perPage') ?? 2000;
//        $label   = Request::param('label')   ?? 'Disease';
//        $data    = $this->getNodes('' , 'n' , $label , $start , $perPage);
//        $list    = $data['list'];
//
//
//        foreach($list as $value)
//        {
//            $check  = XyZskDisease::where('name' , $value['name'])->find();
//            if(!$check) {
//                $result = XyZskDisease::create($value);
//            }
//        }
//        return $this->_json_succ($result);








//        $page     = Request::param('page') ?? 10;
//        $pagesize = 1000;
//        $limit    = ($page - 1) * $pagesize;
//
//        $data = XyZskDisease::limit($limit , $pagesize)->select()->toArray();

//        foreach($data as $key => $value) {
//            $query1 = "match (n:Disease)-[r:LinkInspection]-(m:Thing) where n.name= "."'".$value['name']."'"." return m";
//            $data1  = $this->getAboutInfo($query1);
//            $data2  = array_column($data1, 'm');
//
//            $XyZskInspection = new XyZskInspection();
//            foreach($data2 as $val)
//            {
//               $XyZskInspection->disease_name = $value['name'];
//               $XyZskInspection->name = $val['name'] ?? '';
//               $XyZskInspection->overview = $val['overview'] ?? '';
//               $XyZskInspection->principle = $val['principle'] ?? '';
//               $XyZskInspection->reagent = $val['reagent'] ?? '';
//               $XyZskInspection->operation = $val['operation'] ?? '';
//               $XyZskInspection->clinicalSignificance = $val['clinicalSignificance'] ?? '';
//               $XyZskInspection->normalValue = $val['normalValue'] ?? '';
//               $XyZskInspection->annotation = $val['annotation'] ?? '';
//               $res = $XyZskInspection->save();
//            }
//        }


//        foreach($data as $key => $value) {
//            $query1   = "match (n:Disease)-[r:LinkMedicineDisease]-(m:Thing) where n.name= "."'".$value['name']."'"." return m";
//            $data1  = $this->getAboutInfo($query1);
//            $data2  = array_column($data1, 'm');
//
//            $XyZskMedicine = new XyZskMedicine();
//            foreach($data2 as $val)
//            {
//                $XyZskMedicine->disease_name         = $value['name'];
//                $XyZskMedicine->name                 = $val['name'] ?? '';
//                $XyZskMedicine->pinyi                = $val['piny'] ?? '';
//                $XyZskMedicine->medicineNature       = $val['medicineNature'] ?? '';
//                $XyZskMedicine->oldUse               = $val['oldUse'] ?? '';
//                $XyZskMedicine->chilldUse            = $val['operation'] ?? '';
//                $XyZskMedicine->usageDosage          = $val['usageDosage'] ?? '';
//                $XyZskMedicine->notes                = $val['notes'] ?? '';
//                $XyZskMedicine->periodValidity       = $val['periodValidity'] ?? '';
//
//                $XyZskMedicine->indication           = $val['indication'] ?? '';
//                $XyZskMedicine->storage              = $val['storage'] ?? '';
//                $XyZskMedicine->character            = $val['character'] ?? '';
//                $XyZskMedicine->contraindication     = $val['contraindication'] ?? '';
//
//                $XyZskMedicine->pharmacologyToxicology     = $val['pharmacologyToxicology'] ?? '';
//                $XyZskMedicine->specification              = $val['specification']          ?? '';
//                $XyZskMedicine->approvalNumber             = $val['approvalNumber']         ?? '';
//                $XyZskMedicine->productionEnterprise       = $val['productionEnterprise']   ?? '';
//                $XyZskMedicine->relateSick                 = $val['relateSick']             ?? '';
//                $XyZskMedicine->untowardEffect             = $val['untowardEffect']   ?? '';
//                $XyZskMedicine->majorConstituent           = $val['majorConstituent'] ?? '';
//
//                $res = $XyZskMedicine->save();
//            }
//        }
//
//        return $this->_json_succ($res);
    }


    public function updateMedicine()
    {
        $page     = Request::param('page') ?? 10;
        $pagesize = 1000;
        $limit    = ($page - 1) * $pagesize;
        $medicines = XyZskMedicine::limit($limit , $pagesize)->select()->toArray();
        foreach($medicines as $medicine) {
            $query   = "match (n:MedicineProduction)  where n.name= "."'".$medicine['name']."'"." return n";
            $data    = $this->getAboutInfo($query);
            $n       = $data['n'];
            return $this->_json_succ($n);
        }
    }


    // 创建节点
    public function createLabel()
    {
        $data = Request::all();
        if(empty($data)) {
            return $this->_json_error('请求有误！');
        }

        $label  = $data['label'];
        unset($data['label']);

        try {
            $result = $this->createNode('n' , $label , $data);
        }catch(Exception $e){
            return $this->_json_error($e->getMessage());
        }
        return $this->_json_succ($result);
    }



	// 获取单个数据详情
	public function singleNode()
	{
		$id   = Request::param('id')   ?? 401;
		$zsk  = Request::param('zsk')  ?? 'zy';
		$data = $this->getNodeById($zsk , $id);
		return json($data);
	}

	// 获取id
	public function getLableById()
	{
		$id     = Request::param('id');
        $zsk    = Request::param('zsk')  ?? 'zy';
		$labels = $this->getNodeById($zsk , $id , 1);
		return json($labels);
	}


	// 数据更新
	public function setNodeById()
	{
		$query = Request::param('query');
		$tag   = Request::param('tag') ?? 'zy';
        Log::info('setnode' , ['tag' => $tag , 'query' => $query]);
		return json($this->setNode($query , $tag));
	}
}
